mysql - 约束

not null                       不能存储null值

unique                         某列或者多个列,每列必须有唯一值,可以有多个唯一,多个列的唯一用逗号分隔

default                        默认字段名,可以起别名,规定没有赋值时的默认值

primary key                 相当于not null 和unique的结合,确保多个列的组合只能有一个

foreign key                  保证一个表中的数据匹配另外一个表中的值的参照完整性

  创建表时的写法:

create table 表名(
字段1 字段类型  primary key,
字段2 字段类型  not null,
字段3 字段类型  ,
字段4 字段类型  default "默认值",
字段5 字段类型  ,
字段6 字段类型 ,
.....

unique(字段3),(如果的难度命名或者多个列唯一constraint自定义名 unique(字段2,字段3,。。。))
primary key(字段1,字段2,。。。。) 
);

  

添加,修改,删除not null default

其实就是修改字段

alter table 表名 modify 字段名 字段类型 not null default "默认值";

  

猜你喜欢

转载自www.cnblogs.com/ivyharding/p/11565212.html