数据表完整创建
create table 表名(
字段1 数据类型 约束,
字段2 数据类型 约束
);
非空约束 not null
说明: not null 值不能为空值,就是null
not null
创建完表后, 添加非空约束
alter table 表名 modify 字段名 字段类型 not null;
删除字段的非空约束
alter table 表名 modify 字段 数据类型;
注意事项: 不加约束就是删除了
唯一约束 unique
说明: 唯一约束: unique. 值不能重复
unique
在创建完表后,添加唯一约束
alter table 表名 modify 字段 数据类型 unique;
注意事项: 在mysql中, 唯一约束限定的值可以有多个null, 也就是说null不在唯一约束的范畴
删除唯一约束
alter table 表名 drop index 唯一约束的字段;
主键约束 primary key
说明:非空且唯一,一张表只能有一个字段为主键, 主键就是表中记录唯一标识
primary key
创建完表后,添加主键
alter table 表名 modify id int primary key;
删除主键
alter table 表名 drop primary key;
自动增长 auto_increment
说明: 如果某一字段是数值类型,使用auto_increment, 可以完成值得自动增长
创建完表后添加自动增长
alter table 表名 modify 字段(数值类型) 数据类型 auto_increment;
删除自动增长
alter table 表名 modify 字段 数据类型;
自动增长搭配主键使用
create table 表名(
id int primary key auto_increment,
字段2 数据类型 约束
);