mysql-数据表-DDL语句

--- 表名,列名,列属性,表属性
--- 列属性
PRIMARY KEY 主键约束,表中只能有一个,非空且唯一
NOT NULL 	非空约束,不允许有空值
UNIQUE KEY 	唯一建约束, 不允许有空值
DEFAULT 	一般配合NOT NULL 一起使用
UNSIGNED 	无符号,配合数字列使用,表示非负数
COMMENT 	注释

写一个标准的,符合规范的建表语句

create table stu (
	id int primary key  not null auto_increment comment "学号",
	sname varchar(255) not null comment "姓名",
	age tinyint unsigned not null default 0 comment "年龄",
	gender enum('M','F','n') not null default 'n' comment '性别',
	intime datetime not null default NOW() comment '入学时间'
) ENGINE INNODB CHARSET utf8mb4;

具体的规范如下

1, 表名小写字母,不能数字开头;
2,不能使用保留字符,要使用和业务相关的表明;
3,选择合适的数据类型和长度;
比如varchar 长度不超过256;
4,每个列设置not null + default, 对于数据使用0补充,对于字符使用有效字符串补充;
5,每个列设置注释;
6,表必须设置存储引擎和字符集; 
字符集建议utf8mb4,存储引擎需要Innodb
7,主键列尽量是无关列数字列,最好是自增长;
8,enum 不要保存数字,只能保存字符串类型。

修改

--- 在stu表中添加qq列
alter table stu add qq varchar(20) not null unique comment 'QQ号';
--- 在sname 后添加微信列
alter table stu add wechat varchar(64) 	not null unique comment '微信号'after sname;
--- 在id列前加一个新列num
after table stu add num int not null unique  comment '身份证' first;
--- 删除列
alter table stu drop num;
--- 修改sname 数据类型的属性
alter table stu modify sname varchar(64) not null comment '姓名';
--- 修改gender 修改为 sg 数据类型改为char 类型
alter table stu  change gender sex char(4) not null comment '性别'

你可能感兴趣的:(mysql-数据表-DDL语句)