最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]MySQL查看数据库中表的结构和数据

MySQL crifan 3074浏览 0评论

MySQL show tables – How do I list the tables in a MySQL database? | MySQL list tables command | alvinalexander.com

MySQL FAQ: How do I show the fields or schema of a database table? | alvinalexander.com

MySQL :: MySQL 5.7 Reference Manual :: 13.7.5.5 SHOW COLUMNS Syntax

[useradmin@AZWECHAT02 ~]$ mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 28009641
Server version: 5.5.44-MariaDB MariaDB Server
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
MariaDB [(none)]> show databases;
+————————-+
| Database                |
+————————-+
| information_schema      |
| dev_qoros               |
| guanzhi                 |
| guanzhi_removeDuplicate |
| mysql                   |
| performance_schema      |
| qwechat                 |
+————————-+
7 rows in set (0.01 sec)
MariaDB [(none)]> use guanzhi_removeDuplicate
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [guanzhi_removeDuplicate]> show tables;
+———————————–+
| Tables_in_guanzhi_removeDuplicate |
+———————————–+
| cars                              |
| results                           |
| selection                         |
| staffs                            |
+———————————–+
4 rows in set (0.00 sec)
MariaDB [guanzhi_removeDuplicate]> show cars;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘cars’ at line 1
MariaDB [guanzhi_removeDuplicate]> show table cars
    -> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘cars’ at line 1
MariaDB [guanzhi_removeDuplicate]> show table cars;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘cars’ at line 1
MariaDB [guanzhi_removeDuplicate]> show cars;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘cars’ at line 1
MariaDB [guanzhi_removeDuplicate]> show * from cars;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘* from cars’ at line 1
MariaDB [guanzhi_removeDuplicate]> show * FROM cars;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘* FROM cars’ at line 1
MariaDB [guanzhi_removeDuplicate]> decr cars;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘decr cars’ at line 1
MariaDB [guanzhi_removeDuplicate]> desc cars;
+——————-+————–+——+—–+———+—————-+
| Field             | Type         | Null | Key | Default | Extra          |
+——————-+————–+——+—–+———+—————-+
| id                | int(11)      | NO   | PRI | NULL    | auto_increment |
| sn                | varchar(10)  | NO   |     | NULL    |                |
| frame_number      | varchar(30)  | NO   |     | NULL    |                |
| car_type          | varchar(20)  | NO   |     | NULL    |                |
| type_year         | varchar(20)  | NO   |     | NULL    |                |
| market_name       | varchar(50)  | NO   |     | NULL    |                |
| out_color         | varchar(20)  | NO   |     | NULL    |                |
| inner_car         | varchar(20)  | NO   |     | NULL    |                |
| mileage           | int(10)      | NO   |     | NULL    |                |
| manufacture       | varchar(20)  | NO   |     | NULL    |                |
| effluent_standard | varchar(10)  | NO   |     | NULL    |                |
| mrsp              | varchar(20)  | NO   |     | NULL    |                |
| starting_price    | varchar(10)  | NO   |     | NULL    |                |
| remarks           | varchar(200) | NO   |     | NULL    |                |
+——————-+————–+——+—–+———+—————-+
14 rows in set (0.00 sec)
MariaDB [guanzhi_removeDuplicate]>
MariaDB [guanzhi_removeDuplicate]> desc selection;
+————+————-+——+—–+———+—————-+
| Field      | Type        | Null | Key | Default | Extra          |
+————+————-+——+—–+———+—————-+
| id         | int(10)     | NO   | PRI | NULL    | auto_increment |
| staff_id   | int(10)     | NO   |     | NULL    |                |
| car_sn     | varchar(10) | NO   |     | NULL    |                |
| bid_price  | int(10)     | NO   |     | NULL    |                |
| created_at | int(10)     | NO   |     | NULL    |                |
| sequence   | int(10)     | NO   |     | NULL    |                |
+————+————-+——+—–+———+—————-+
6 rows in set (0.00 sec)
MariaDB [guanzhi_removeDuplicate]> SHOW COLUMNS from selection;
+————+————-+——+—–+———+—————-+
| Field      | Type        | Null | Key | Default | Extra          |
+————+————-+——+—–+———+—————-+
| id         | int(10)     | NO   | PRI | NULL    | auto_increment |
| staff_id   | int(10)     | NO   |     | NULL    |                |
| car_sn     | varchar(10) | NO   |     | NULL    |                |
| bid_price  | int(10)     | NO   |     | NULL    |                |
| created_at | int(10)     | NO   |     | NULL    |                |
| sequence   | int(10)     | NO   |     | NULL    |                |
+————+————-+——+—–+———+—————-+
6 rows in set (0.00 sec)

最后终于用:

desc tableName;

可以查看到表的结构了。

等价于:

SHOW COLUMNS from tableName;

但是还需要看到数据:

mysql show table data

MySQL :: MySQL 5.7 Reference Manual :: 3.3.4 Retrieving Information from a Table

sql – Select data from "show tables" MySQL query – Stack Overflow

| 3043 |      226 | 88     |     78799 | 1453352647 |       88 |
| 3044 |      226 | 89     |     77788 | 1453352648 |       89 |
| 3045 |      226 | 90     |     68899 | 1453352648 |       90 |
| 3046 |      223 | 33     |     85001 | 1453352662 |       33 |
| 3067 |      269 | 64     |     35321 | 1453353548 |       64 |
| 3068 |      269 | 74     |     51021 | 1453353548 |       74 |
| 3069 |      269 | 77     |     41280 | 1453353548 |       77 |
+——+———-+——–+———–+————+———-+
3067 rows in set (0.01 sec)
MariaDB [guanzhi_removeDuplicate]> SELECT * from selection;

然后用:

SELECT * from tableName;

即可,看到输出的数据。

并且,想要只控制输出多少条记录

mysql select only output some rows

sql – MySQL: Select N rows, but with only unique values in one column – Stack Overflow

好像是LIMIT N

去试试:

MariaDB [guanzhi_removeDuplicate]> SELECT * from selection LIMIT 10;
+—-+———-+——–+———–+————+—————–+———-+
| id | staff_id | car_sn | bid_price | created_at | created_at_gmt8 | sequence |
+—-+———-+——–+———–+————+—————–+———-+
|  1 |      287 | 13     |     99800 | 1453253772 |               0 |       13 |
|  2 |      287 | 15     |    109119 | 1453253772 |               0 |       15 |
|  3 |      160 | 46     |     45000 | 1453253789 |               0 |       46 |
|  4 |      160 | 64     |     38888 | 1453253789 |               0 |       64 |
|  5 |      160 | 77     |     45000 | 1453253789 |               0 |       77 |
|  6 |      160 | 82     |     63000 | 1453253789 |               0 |       82 |
|  7 |      160 | 83     |     50010 | 1453253789 |               0 |       83 |
|  8 |      160 | 84     |     50000 | 1453253789 |               0 |       84 |
|  9 |      160 | 85     |     47818 | 1453253789 |               0 |       85 |
| 10 |      160 | 88     |     71800 | 1453253789 |               0 |       88 |
+—-+———-+——–+———–+————+—————–+———-+
10 rows in set (0.00 sec)
MariaDB [guanzhi_removeDuplicate]> SELECT * from selection LIMIT 20;
+—-+———-+——–+———–+————+—————–+———-+
| id | staff_id | car_sn | bid_price | created_at | created_at_gmt8 | sequence |
+—-+———-+——–+———–+————+—————–+———-+
|  1 |      287 | 13     |     99800 | 1453253772 |               0 |       13 |
|  2 |      287 | 15     |    109119 | 1453253772 |               0 |       15 |
|  3 |      160 | 46     |     45000 | 1453253789 |               0 |       46 |
|  4 |      160 | 64     |     38888 | 1453253789 |               0 |       64 |
|  5 |      160 | 77     |     45000 | 1453253789 |               0 |       77 |
|  6 |      160 | 82     |     63000 | 1453253789 |               0 |       82 |
|  7 |      160 | 83     |     50010 | 1453253789 |               0 |       83 |
|  8 |      160 | 84     |     50000 | 1453253789 |               0 |       84 |
|  9 |      160 | 85     |     47818 | 1453253789 |               0 |       85 |
| 10 |      160 | 88     |     71800 | 1453253789 |               0 |       88 |
| 11 |      160 | 89     |     67100 | 1453253789 |               0 |       89 |
| 12 |      160 | 90     |     58800 | 1453253789 |               0 |       90 |
| 13 |      246 | 14     |     95100 | 1453253891 |               0 |       14 |
| 14 |      246 | 15     |     95999 | 1453253891 |               0 |       15 |
| 15 |      246 | 28     |     86010 | 1453253891 |               0 |       28 |
| 16 |      246 | 31     |     81755 | 1453253891 |               0 |       31 |
| 17 |      246 | 33     |     80001 | 1453253891 |               0 |       33 |
| 18 |      246 | 39     |     83501 | 1453253891 |               0 |       39 |
| 19 |      246 | 41     |     83501 | 1453253891 |               0 |       41 |
| 20 |      246 | 44     |     75001 | 1453253891 |               0 |       44 |
+—-+———-+——–+———–+————+—————–+———-+
20 rows in set (0.00 sec)
MariaDB [guanzhi_removeDuplicate]>

果然是可以了:

[总结]

1.显示全部数据:

SELECT * from tableName;

2.只显示(前)N条数据:

SELECT * from TableName LIMIT N;

转载请注明:在路上 » [已解决]MySQL查看数据库中表的结构和数据

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.177 seconds, using 22.59MB memory