-- 创建表 -- create table 表名 ( -- 字段名 类型 约束 auto_increment, -- ... -- ); -- 删除表 -- drop table 表名; -- 修改表 -- 添加字段: -- alter table 表名 add 字段名 类型 约束 -- 删除字段: -- alter table 表名 drop 字段名 -- 修改字段类型: -- alter table 表名 modify 字段名 新字段类型 -- 修改字段名: -- alter table 表名 change 字段名 新字段名 类型 -- 修改表名: -- alter table 表名 rename as 新表名 -- MySQL常见字段类型 -- 数值型: -- int(长度), 表示整数类型的数据 -- float/double, 表示浮点数 -- 字符型: -- varchar(长度), 动态分配长度 -- char(长度), 分配固定长度 -- 日期型: -- date, 格式为 yyyy-MM-dd -- datetime, 格式为 yyyy-MM-dd HH:mm:ss, 占用8个字节 -- timestamp, 格式为 yyyy-MM-dd HH:mm:ss, 会自动进行时区的转换, 占用4个字节 -- time, 时间 -- year, 年份 -- 其他类型: -- text, 非二进制大对象(字符) -- blob, 二进制大对象(非字符) -- 约束: -- 主键约束: -- 在创建表时, 在字段后使用 primary key -- 在创建表的语句最后, 使用 constraint 约束名 primary key(主键字段) -- 在创建表后使用, alter table 表名 add constraint 约束名 primary key(主键字段) -- 非空约束: -- 在创建表时, 在字段后使用 not null -- 在创建表后使用, alter table 表名 modify 字段名 类型 not null -- MySQL中的非空约束, 是可以存空字符的 -- 检查约束: -- 在MySQL中是没有检查约束的, 但使用check关键字不会报错 -- 唯一约束: -- 在创建表时, 在字段后使用 unique -- 在创建表的语句最后, 使用 constraint 约束名 unique key(字段) -- 在创建表后使用, alter table 表名 add constraint 约束名 unique key(字段) -- 外键约束: -- 在创建表时, 在字段后使用 references 父表名(父表字段) -- 在创建表的语句最后, 使用 constraint 约束名 foreign key(子表字段) references 父表名(父表字段) -- 在创建表后使用, alter table 表名 add constraint 约束名 foreign key(子表字段) references 父表名(父表字段) -- 其他操作: -- 显示当前所有表 -- show tables -- 查看建表语句 -- show create table 表名 -- 注意: -- MySQL是没有序列的, 但是我们可以在创建表的时候直接指定主键是自增的 -- 使用关键字 auto_increment -- 在插入语句中, 可以使用 default
原文:https://www.cnblogs.com/mpci/p/12285823.html