更新为:MySQL 5.7
版本
概述
DDL(Data Definition Language,数据定义语言)语句,主要作用为创建、删除、修改、库或表结构,对数据库或表的结构操作。
关键字
DROP语句
DROP
语句主要用来对库表等做删除操作;
1 2 3 4 5 6 7 8 9 10 11 12
| DROP DATABASE DROP EVENT DROP FUNCTION DROP FUNCTION UDF DROP INDEX DROP PROCEDURE DROP SERVER DROP TABLE DROP TABLESPACE DROP TRIGGER DROP USER DROP VIEW
|
删库
参考:DROP DATABASE Syntax
语法格式
1
| DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
|
Tips:
IF EXISTS
:用于防止当数据库不存在时发生错误;
- 删除数据库中的所有表格并同时删除数据库;
- 如果要使用 DROP DATABASE,需要获得数据库 DROP 权限;
删表
参考:DROP TABLE Syntax
语法格式
1
| DROP TABLE [IF EXISTS] tbl_name [, tbl_name] ...
|
Tips:
- 可以同时删除多个表;
- 会删除表结构和表数据,需谨慎操作;
IF EXISTS
:用于防止当表不存在时发生错误;
CREATE语句
CREATE
命令主要用来建表、库、索引等:
1 2 3 4 5 6 7 8 9 10 11 12 13
| CREATE DATABASE CREATE EVENT CREATE FUNCTION CREATE FUNCTION UDF CREATE INDEX CREATE LOGFILE GROUP CREATE PROCEDURE CREATE SERVER CREATE TABLE CREATE TABLESPACE CREATE TRIGGER CREATE USER CREATE VIEW
|
当然也可以通过SHOW
命令来查看已有库表等对象的创建命令:
1 2 3 4 5 6 7
| SHOW SHOW CREATE DATABASE SHOW CREATE EVENT SHOW CREATE FUNCTION SHOW CREATE PROCEDURE SHOW CREATE TABLE SHOW CREATE USER
|
建库
语法格式
1 2 3 4 5 6
| CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [create_specification] ...
create_specification: [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name
|
tips: []
为可选框,[]
里面的内容为选填语句。
example
1
| CREATE DATABASE `school` CHARACTER SET 'utf8mb4';
|
- tips:
CHARACTER SET
可以用CHARSET
来代替。
- tips:建库时务必设置字符集。
字符集
字符集是一套符合和编码,校验规则(collation)是在字符集内用于比较字符的一套规则,即字符集的排序规则。MySQL可以使用对种字符集和检验规则来组织字符。
可以看到MySQL支持有41种字符集,目前常用的为utf8
和utf8mb4
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| mysql> SHOW CHARACTER SET; + | Charset | Description | Default collation | Maxlen | + | big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 | | dec8 | DEC West European | dec8_swedish_ci | 1 | | cp850 | DOS West European | cp850_general_ci | 1 | | hp8 | HP West European | hp8_english_ci | 1 | | koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 | | latin1 | cp1252 West European | latin1_swedish_ci | 1 | | latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 | | swe7 | 7bit Swedish | swe7_swedish_ci | 1 | | ascii | US ASCII | ascii_general_ci | 1 | | ujis | EUC-JP Japanese | ujis_japanese_ci | 3 | | sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 | | hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 | | tis620 | TIS620 Thai | tis620_thai_ci | 1 | | euckr | EUC-KR Korean | euckr_korean_ci | 2 | | koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 | | gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 | | greek | ISO 8859-7 Greek | greek_general_ci | 1 | | cp1250 | Windows Central European | cp1250_general_ci | 1 | | gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 | | latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 | | armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 | | utf8 | UTF-8 Unicode | utf8_general_ci | 3 | | ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 | | cp866 | DOS Russian | cp866_general_ci | 1 | | keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 | | macce | Mac Central European | macce_general_ci | 1 | | macroman | Mac West European | macroman_general_ci | 1 | | cp852 | DOS Central European | cp852_general_ci | 1 | | latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 | | utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 | | cp1251 | Windows Cyrillic | cp1251_general_ci | 1 | | utf16 | UTF-16 Unicode | utf16_general_ci | 4 | | utf16le | UTF-16LE Unicode | utf16le_general_ci | 4 | | cp1256 | Windows Arabic | cp1256_general_ci | 1 | | cp1257 | Windows Baltic | cp1257_general_ci | 1 | | utf32 | UTF-32 Unicode | utf32_general_ci | 4 | | binary | Binary pseudo charset | binary | 1 | | geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 | | cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 | | eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 | | gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 | + 41 rows in set (0.00 sec)
|
Tips:MySQL中的utf8
编码只支持最大3字节每字符。真正正在使用的UTF-8编码是应该能支持4字节每个字符。而MySQL的开发者没有修复这个bug。他们在2010年增加了一个变通的方法:一个新的字符集utf8mb4
。现在很多指南推荐用户使用utf8
其实都错了。
建表
参考:CREATE TABLE Syntax
语法格式
创建一张新表:
1 2 3 4
| CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name (create_definition,...) [table_options] [partition_options]
|
example
1 2 3 4 5 6 7 8 9 10 11
| CREATE TABLE `school`.`student` ( `id` bigint(0) UNSIGNED NOT NULL AUTO_INCREMENT, `name` varchar(255) NULL COMMENT '姓名', `age` int(10) NULL COMMENT '年龄', `gender` varchar(255) NULL COMMENT '性别', `city` varchar(255) NULL COMMENT '城市', `create_time` datetime(0) NULL COMMENT '创建时间', `modify_time` datetime(0) NOT NULL DEFAULT current_timestamp COMMENT '更新时间', PRIMARY KEY (`id`), INDEX `index_name`(`name`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COMMENT = '学生表';
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| # 创建新表的同时将`SELECT`的查询结果数据插入到新表中,但索引和主外键信息都不会同步过来:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)] [table_options] [partition_options] [IGNORE | REPLACE] [AS] query_expression
# 基于另外一个表的定义复制一个新的空表,空表上的字段属性和索引和原表相同:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name { LIKE old_tbl_name | (LIKE old_tbl_name) }
create_definition: col_name column_definition | {INDEX|KEY} [index_name] [index_type] (key_part,...) [index_option] ... | {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (key_part,...) [index_option] ... | [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (key_part,...) [index_option] ... | [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (key_part,...) [index_option] ... | [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (col_name,...) reference_definition | CHECK (expr)
column_definition: data_type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT] [UNIQUE [KEY]] [[PRIMARY] KEY] [COMMENT 'string'] [COLLATE collation_name] [COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}] [STORAGE {DISK|MEMORY}] [reference_definition] | data_type [COLLATE collation_name] [GENERATED ALWAYS] AS (expr) [VIRTUAL | STORED] [NOT NULL | NULL] [UNIQUE [KEY]] [[PRIMARY] KEY] [COMMENT 'string'] [reference_definition]
data_type: (see Chapter 11, Data Types)
key_part: col_name [(length)] [ASC | DESC]
index_type: USING {BTREE | HASH}
index_option: KEY_BLOCK_SIZE [=] value | index_type | WITH PARSER parser_name | COMMENT 'string'
reference_definition: REFERENCES tbl_name (key_part,...) [MATCH FULL | MATCH PARTIAL | MATCH SIMPLE] [ON DELETE reference_option] [ON UPDATE reference_option]
reference_option: RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT
table_options: table_option [[,] table_option] ...
table_option: AUTO_INCREMENT [=] value | AVG_ROW_LENGTH [=] value | [DEFAULT] CHARACTER SET [=] charset_name | CHECKSUM [=] {0 | 1} | [DEFAULT] COLLATE [=] collation_name | COMMENT [=] 'string' | COMPRESSION [=] {'ZLIB'|'LZ4'|'NONE'} | CONNECTION [=] 'connect_string' | {DATA|INDEX} DIRECTORY [=] 'absolute path to directory' | DELAY_KEY_WRITE [=] {0 | 1} | ENCRYPTION [=] {'Y' | 'N'} | ENGINE [=] engine_name | INSERT_METHOD [=] { NO | FIRST | LAST } | KEY_BLOCK_SIZE [=] value | MAX_ROWS [=] value | MIN_ROWS [=] value | PACK_KEYS [=] {0 | 1 | DEFAULT} | PASSWORD [=] 'string' | ROW_FORMAT [=] {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT} | STATS_AUTO_RECALC [=] {DEFAULT|0|1} | STATS_PERSISTENT [=] {DEFAULT|0|1} | STATS_SAMPLE_PAGES [=] value | TABLESPACE tablespace_name [STORAGE {DISK|MEMORY}] | UNION [=] (tbl_name[,tbl_name]...)
partition_options: PARTITION BY { [LINEAR] HASH(expr) | [LINEAR] KEY [ALGORITHM={1|2}] (column_list) | RANGE{(expr) | COLUMNS(column_list)} | LIST{(expr) | COLUMNS(column_list)} } [PARTITIONS num] [SUBPARTITION BY { [LINEAR] HASH(expr) | [LINEAR] KEY [ALGORITHM={1|2}] (column_list) } [SUBPARTITIONS num] ] [(partition_definition [, partition_definition] ...)]
partition_definition: PARTITION partition_name [VALUES {LESS THAN {(expr | value_list) | MAXVALUE} | IN (value_list)}] [[STORAGE] ENGINE [=] engine_name] [COMMENT [=] 'string' ] [DATA DIRECTORY [=] 'data_dir'] [INDEX DIRECTORY [=] 'index_dir'] [MAX_ROWS [=] max_number_of_rows] [MIN_ROWS [=] min_number_of_rows] [TABLESPACE [=] tablespace_name] [(subpartition_definition [, subpartition_definition] ...)]
subpartition_definition: SUBPARTITION logical_name [[STORAGE] ENGINE [=] engine_name] [COMMENT [=] 'string' ] [DATA DIRECTORY [=] 'data_dir'] [INDEX DIRECTORY [=] 'index_dir'] [MAX_ROWS [=] max_number_of_rows] [MIN_ROWS [=] min_number_of_rows] [TABLESPACE [=] tablespace_name]
query_expression: SELECT ... (Some valid select or union statement)
|
表名
tbl_name
:表名可以指定为db_name.tbl_name
,以在特定数据库中创建表。不管是否存在默认数据库,这都是可行的,前提是数据库是存在的。如果使用引号标识符,请分别引用数据库名称和表名。例如,写`mydb`.`mytbl`, 而不是 `mydb.mytbl`。
IF NOT EXISTS
:如果存在表,则防止发生错误。但是,无法验证现有表的结构是否与CREATETABLE语句所指示的结构相同。
Temporary Tables
:临时表仅在当前会话中可见,并在会话关闭时自动删除。
表复制和克隆
LIKE
:使用CREATE TABLE ... LIKE
根据另一个表的定义创建一个空表,包括在原始表中定义的任何列属性和索引:
1
| CREATE TABLE new_tbl LIKE orig_tbl;
|
[AS] query_expression
:若要从另一个表创建表,请在CREATE TABLE
语句末尾添加SELECT
语句:
1
| CREATE TABLE new_tbl AS SELECT * FROM orig_tbl;
|
IGNORE
和REPLACE
:忽略和替换选项指示如何使用SELECT语句复制表时处理重复键值的行。
数据类型
参考:MySQL 数据类型
MySQL支持多种类型,大致可以分为三类:数值、日期/时间和字符串(字符)类型。
数值类型
MySQL支持所有标准SQL数值数据类型。
这些类型包括严格数值数据类型(INTEGER
、SMALLINT
、DECIMAL
和NUMERIC
),以及近似数值数据类型(FLOAT
、REAL
和DOUBLE PRECISION
)。
关键字INT
是INTEGER
的同义词,关键字DEC
是DECIMAL
的同义词。
BIT
数据类型保存位字段值,并且支持MyISAM
、MEMORY
、InnoDB
和BDB
表。
作为SQL标准的扩展,MySQL也支持整数类型TINYINT
、MEDIUMINT
和BIGINT
。下面的表显示了需要的每个整数类型的存储和范围。
tips:自增ID若可预测该表数据量将非常大,可改用BIGINT
UNSIGNED
类型。
日期和时间类型
表示时间值的日期和时间类型为DATETIME
、DATE
、TIMESTAMP
、TIME
和YEAR
。
tips:DEFAULT current_timestamp
:在插入记录时未指定具体时间数据则将该时间戳字段值设置为当前时间。
字符串类型
字符串类型指CHAR
、VARCHAR
、BINARY
、VARBINARY
、BLOB
、TEXT
、ENUM
和SET
。
索引
索引的目的在于提高查询效率,本质是通过不断地缩小想要获取数据的范围来筛选出最终想要的结果,同时把随机的事件变成顺序的事件,也就是说,有了这种索引机制,我们可以总是用同一种查找方式来锁定数据。
索引分单列索引和组合索引。单列索引,即一个索引只包含单个列,一个表可以有多个单列索引,但这不是组合索引。组合索引,即一个索引包含多个列。
索引类型
索引类型大致分为:
- 普通索引(NORMAL):加速查找
- 主键索引(PRIMARY KEY):加速查找+约束(不为空且唯一)
- 唯一索引(UNIQUE):加速查找+约束(唯一)
- 空间索引(SPATIAL):几乎用不到。
- 全文索引(FULLTEXT):用于搜索很长一篇文章的时候,效果最好。
Tips:唯一即无法插入重复的值,插入会报错。
索引方法
索引方法分为:
- BTREE:InnoDB默认
- HASH:InnoDB存储引擎支持的HASH索引是自适应的,InnoDB存储引擎会根据表的使用情况自动为表生成HASH索引,不能认为干预是否在一张表中生成HASH索引。
ALTER语句
ALTER
通常用来修改库表结构等操作。
修改数据库
参考:ALTER DATABASE Syntax
语法格式
1
| ALTER {DATABASE | SCHEMA} [db_name] [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name
|
Tips:
- 该操作需要获得数据库
ALTER
权限;
CHARACTER SET
子句用于更改默认的数据库字符集,其中COLLATE
为排序规则;
修改表
参考:ALTER TABLE Syntax
ALTER TABLE
语句主要用来改变原有表的结构,例如增加或删减列、创建或取消索引、更改原有列类型、重新命名列或表等。
语法格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| ALTER TABLE tbl_name [alter_specification [, alter_specification] ...] [partition_options]
alter_specification: table_options | ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name] | ADD [COLUMN] (col_name column_definition,...) | ADD {INDEX|KEY} [index_name] [index_type] (key_part,...) [index_option] ... | ADD {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (key_part,...) [index_option] ... | ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (key_part,...) [index_option] ... | ADD [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (key_part,...) [index_option] ... | ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (col_name,...) reference_definition | ADD CHECK (expr) | ALGORITHM [=] {DEFAULT|INPLACE|COPY} | ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT} | CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name] | [DEFAULT] CHARACTER SET [=] charset_name [COLLATE [=] collation_name] | CONVERT TO CHARACTER SET charset_name [COLLATE collation_name] | {DISABLE|ENABLE} KEYS | {DISCARD|IMPORT} TABLESPACE | DROP [COLUMN] col_name | DROP {INDEX|KEY} index_name | DROP PRIMARY KEY | DROP FOREIGN KEY fk_symbol | FORCE | LOCK [=] {DEFAULT|NONE|SHARED|EXCLUSIVE} | MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name] | ORDER BY col_name [, col_name] ... | RENAME {INDEX|KEY} old_index_name TO new_index_name | RENAME [TO|AS] new_tbl_name | {WITHOUT|WITH} VALIDATION
partition_options: partition_option [partition_option] ...
partition_option: ADD PARTITION (partition_definition) | DROP PARTITION partition_names | DISCARD PARTITION {partition_names | ALL} TABLESPACE | IMPORT PARTITION {partition_names | ALL} TABLESPACE | TRUNCATE PARTITION {partition_names | ALL} | COALESCE PARTITION number | REORGANIZE PARTITION partition_names INTO (partition_definitions) | EXCHANGE PARTITION partition_name WITH TABLE tbl_name [{WITH|WITHOUT} VALIDATION] | ANALYZE PARTITION {partition_names | ALL} | CHECK PARTITION {partition_names | ALL} | OPTIMIZE PARTITION {partition_names | ALL} | REBUILD PARTITION {partition_names | ALL} | REPAIR PARTITION {partition_names | ALL} | REMOVE PARTITIONING | UPGRADE PARTITIONING
key_part: col_name [(length)] [ASC | DESC]
index_type: USING {BTREE | HASH}
index_option: KEY_BLOCK_SIZE [=] value | index_type | WITH PARSER parser_name | COMMENT 'string'
table_options: table_option [[,] table_option] ...
table_option: AUTO_INCREMENT [=] value | AVG_ROW_LENGTH [=] value | [DEFAULT] CHARACTER SET [=] charset_name | CHECKSUM [=] {0 | 1} | [DEFAULT] COLLATE [=] collation_name | COMMENT [=] 'string' | COMPRESSION [=] {'ZLIB'|'LZ4'|'NONE'} | CONNECTION [=] 'connect_string' | {DATA|INDEX} DIRECTORY [=] 'absolute path to directory' | DELAY_KEY_WRITE [=] {0 | 1} | ENCRYPTION [=] {'Y' | 'N'} | ENGINE [=] engine_name | INSERT_METHOD [=] { NO | FIRST | LAST } | KEY_BLOCK_SIZE [=] value | MAX_ROWS [=] value | MIN_ROWS [=] value | PACK_KEYS [=] {0 | 1 | DEFAULT} | PASSWORD [=] 'string' | ROW_FORMAT [=] {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT} | STATS_AUTO_RECALC [=] {DEFAULT|0|1} | STATS_PERSISTENT [=] {DEFAULT|0|1} | STATS_SAMPLE_PAGES [=] value | TABLESPACE tablespace_name [STORAGE {DISK|MEMORY}] | UNION [=] (tbl_name[,tbl_name]...)
partition_options: (see CREATE TABLE options)
|
修改表名
语法格式如下:
1
| ALTER TABLE tbl_name RENAME [TO|AS] new_tbl_name
|
添加字段
语法格式如下:
1
| ALTER TABLE tbl_name ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name]
|
Tips:
column_definition
为列定义,即字段的数据类型;
- FIRST 为可选参数,其作用是将新添加的字段设置为表的第一个字段;
- AFTER 为可选参数,其作用是将新添加的字段添加到指定的已存在的字段名的后面;
修改字段名称
语法格式如下:
1
| ALTER TABLE tbl_name CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name]
|
修改字段数据类型
语法格式如下:
1
| ALTER TABLE tbl_name MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name]
|
删除字段
语法格式如下:
1
| ALTER TABLE tbl_name DROP [COLUMN] col_name
|