当前位置:Gxlcms > 数据库问题 > mysql - 约束

mysql - 约束

时间:2021-07-01 10:21:17 帮助过:5人阅读

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 "默认值";

  

mysql - 约束

标签:类型   多个   创建表   结合   prim   table   highlight   一个   primary   

人气教程排行