在咱們使用數據庫進行查詢或者建表時,常常須要查看表結構,下面以employees數據庫中的departments表爲例進行表結構查詢:mysql
departments表:(2列9行)
+---------+--------------------+ | dept_no | dept_name | +---------+--------------------+ | d009 | Customer Service | | d005 | Development | | d002 | Finance | | d003 | Human Resources | | d001 | Marketing | | d004 | Production | | d006 | Quality Management | | d008 | Research | | d007 | Sales | +---------+--------------------+
方法 1:DESC departments; 方法 2:DESCRIBE departments; 方法 3:SHOW COLUMNS FROM departments;
以上三種方法的查詢結果相同:sql
mysql> DESC departments; +-----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+-------+ | dept_no | char(4) | NO | PRI | NULL | | | dept_name | varchar(40) | NO | UNI | NULL | | +-----------+-------------+------+-----+---------+-------+
方法 4: 借用MySQL自身的information_schema數據庫,輸入以下指令:數據庫
-- 方法4 SELECT * FROM information_schema.COLUMNS WHERE table_schema = 'employees' AND table_name = 'departments'; -- 方法4簡化版(須要處於 information_schema數據庫內) SELECT * FROM COLUMNS WHERE table_name = 'departments';
查詢結果以下:spa
mysql> SELECT * -> FROM information_schema.COLUMNS -> WHERE table_schema = 'employees' AND table_name = 'departments'\G *************************** 1. row *************************** TABLE_CATALOG: def TABLE_SCHEMA: employees TABLE_NAME: departments COLUMN_NAME: dept_no ORDINAL_POSITION: 1 COLUMN_DEFAULT: NULL IS_NULLABLE: NO DATA_TYPE: char CHARACTER_MAXIMUM_LENGTH: 4 CHARACTER_OCTET_LENGTH: 12 NUMERIC_PRECISION: NULL NUMERIC_SCALE: NULL DATETIME_PRECISION: NULL CHARACTER_SET_NAME: utf8 COLLATION_NAME: utf8_general_ci COLUMN_TYPE: char(4) COLUMN_KEY: PRI EXTRA: PRIVILEGES: select,insert,update,references COLUMN_COMMENT: GENERATION_EXPRESSION: *************************** 2. row *************************** TABLE_CATALOG: def TABLE_SCHEMA: employees TABLE_NAME: departments COLUMN_NAME: dept_name ORDINAL_POSITION: 2 COLUMN_DEFAULT: NULL IS_NULLABLE: NO DATA_TYPE: varchar CHARACTER_MAXIMUM_LENGTH: 40 CHARACTER_OCTET_LENGTH: 120 NUMERIC_PRECISION: NULL NUMERIC_SCALE: NULL DATETIME_PRECISION: NULL CHARACTER_SET_NAME: utf8 COLLATION_NAME: utf8_general_ci COLUMN_TYPE: varchar(40) COLUMN_KEY: UNI EXTRA: PRIVILEGES: select,insert,update,references COLUMN_COMMENT: GENERATION_EXPRESSION:
方法 5: 表信息查詢,輸入以下指令:code
SHOW TABLE STATUS LIKE '%departments%';
查詢結果:orm
*************************** 1. row *************************** Name: departments Engine: InnoDB Version: 10 Row_format: Dynamic Rows: 9 Avg_row_length: 1820 Data_length: 16384 Max_data_length: 0 Index_length: 16384 Data_free: 0 Auto_increment: NULL Create_time: 2018-12-05 20:37:56 Update_time: NULL Check_time: NULL Collation: utf8_general_ci Checksum: NULL Create_options: Comment: 1 row in set (0.00 sec)
方法 6:建表信息查詢 ,輸入以下指令:blog
SHOW CREATE TABLE departments \G
查詢結果:ci
mysql> show create table departments\G *************************** 1. row *************************** Table: departments Create Table: CREATE TABLE `departments` ( `dept_no` char(4) NOT NULL, `dept_name` varchar(40) NOT NULL, PRIMARY KEY (`dept_no`), UNIQUE KEY `dept_name` (`dept_name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec)