库
create database 【if not exists】 库名 【character set 字符集名】;
alter database 库名 character set 字符集名;
drop database 【if exists】库名;
表
create table 【if not exists】 表名(
字段名 字段类型 【约束】,
字段名 字段类型 【约束】,
…
字段名 字段类型 【约束】
)
修改表
alter table 表名 add column 列表 类型 【first|after 字段名】;
alter table 表名 modify column 列名 新类型 【新约束】;
alter table 表名 change column 旧列名 新列名 类型;
alter table 表名 drop column 列名;
alter table 表名 rename 【to】 新表名
删除表
drop table 【if exists】 表名;
复制表
create table 表名 like 旧表;
复制表的结构+数据
create table 表名
select 查询列表 from 旧表【where 筛选】
数据类型
- 数值
整形
tinyint、smallint、mediumint 、int/integer、bigint
1 2 3 4 8
unsigned 无符号,只针对整形
可以不指定长度,也可以+zerofill 指定长度,不够在左边补0month
int(2) unsigned zerofill DEFAULT NULL
浮点
decimal(M,D) – 如果省略M默认10,D默认0
float(M,D)
double(M,D)
- 字符
char、varchar、binary、varbinary、enum、set、text、blob
char 固定长度,char(M),M省略则默认为1
varchar(M),M不能省略 - 日期
year
date
time
datetime 8
timestamp 4 (只能保存到2038年,受时区等影响,更能反映真实时间)
约束
not null
default
primary key
unique
check【mysql中不支持】
foreign key