mysql约束
在mysql中对编辑的数据进行类型的限制,不满足约束条件的报错
1 2 3 4 5 6 7 8 9 | unsigned : 无符号 not null : 不为空 default : 默认值 unique : 唯一值,加入唯一索引 (索引相当于字典目录,索引的提出是为了加快速度,一味地乱加索引不会提高查询效率) primary key: 主键 auto_increment: 自增加一 ,必须设置了主键才能设置该参数 zerofill : 零填充 foreign key: 外键 |
约束在写sql时,放在数据类型的后面,如下,放在int的后面
字段名 类型 约束
unsigned 无符号
1 2 3 | create table t3(id int unsigned); insert into t3 values (-1); error insert into t3 values (4000000000); success |
设置无符号位约束,插入负值就报错
not null : 不为空
1 2 3 4 | create table t4(id int not null , name varchar (11)); insert into t4 values (1, "张宇" ); insert into t4 values ( null , "张宇" ); error insert into t4( name ) values ( "李四" ); error |
设置不为空约束,插入空就报错
NULL值是处于0和1之间的某个值,他也表示一个值,只不过这个值是NULL值,而不是0。
在进行计算的时候,1与NULL则结果为NULL。而0与NULL则结果为0。
1或NULL则结果为1,0或NULL则结果为NULL;可见NULL值是介于0和1之间的值。
另外非NULL既不是1也不是0,还是NULL
default : 默认值
1 2 3 | create table t5(id int not null , name varchar (11) default "沈思雨" ); insert into t5 values (1, null ); insert into t5(id) values (2); |
设置了默认值后,插入时填入值,就是设置的值,非全列插入时,不写该字段的值,就用默认值
1 2 | create table t5_2(id int not null default "1111" , name varchar (11) default "沈思雨" ); insert into t5_2 values (); # 在 values 里面不写值,默认使用默认值; |
unique: 唯一约束
加入唯一索引(索引的提出是为了加快速度,一味地乱加索引不会提高查询效率,索引是有一个文件来存索引)
唯一 可为null 标记成: UNI
1 2 3 4 5 | create table t6(id int unique , name char (10) default "赵万里" ); insert into t6(id) values (1); insert into t6(id) values (1); error insert into t6(id) values ( null ); insert into t6(id) values ( null ); # id变成了多个 null |
如果要删除null的字段,可以用 where 字段 is null 来删
唯一性约束,可以有多个null值,不违背唯一性约束
primary key: 主键
[ 唯一 + 不为null ] PRI 标记数据的唯一特征
一个表中,只能设置一个字段为一个主键,unique唯一约束可以设置多个
创建主键
1 2 3 4 | create table t7(id int primary key , name varchar (10) default "赵沈阳" ); insert into t7(id) values (1); insert into t7(id) values (1); error insert into t7(id) values ( null ); error |
设了主键,该字段不能重复,不能为空
unique + not null => PRI
1 | create table t8(id int unique not null , name varchar (10) default "赵沈阳" ); |
设置了唯一性约束,且不为null,功能就跟primary key一样了
如果没有设置primary key,设置了unique not null ,默认把unique +not null 设置的字段设为主键
primary key / unique + not null => 优先把primary key 作为主键;
1 | create table t9(id1 int unique not null , id2 int primary key ); |
同时设置了unique +not null 和 primary key 。优先把primary key 作为主键
一个表只能设置单个字段为一个主键;
1 | create table t10(id1 int primary key , id2 int primary key ); error |
auto_increment: 自增加一
一般配合 主键或者unique 使用
1 2 3 4 5 6 | create table t11(id int primary key auto_increment , name varchar (255) default "敬文栋" ); insert into t11 values (1, "张三" ); insert into t11 values ( null , "李四" ); insert into t11(id) values ( null ); # 使用默认值或者自增插入数据 insert into t11 values (); |
删除数据,这是删除所有数据
delete from t11;
删除数据 + 重置id
truncate table t11;
主键自增,可以用0,null,default占位
删除一条数据后,如果再添加不想主键从下一个开始,需要在添加之前,复位主键
删除数据后,执行下面的sql
如果是中途删除,先查看一下目前的auto_increment
1 2 3 4 5 6 7 8 9 10 11 12 13 | show create table student; | student | CREATE TABLE `student` ( `id` int NOT NULL AUTO_INCREMENT, ` name ` varchar (20) NOT NULL , `age` int NOT NULL , `birthday` date DEFAULT NULL , `is_del` tinyint DEFAULT '0' , `height` decimal (3,2) DEFAULT NULL , `cls_id` varchar (6) NOT NULL , PRIMARY KEY (`id`), KEY `fk_class` (`cls_id`), CONSTRAINT `fk_class` FOREIGN KEY (`cls_id`) REFERENCES `class` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8 | |
AUTO_INCREMENT=几 下次插入时就从几开始递增
ALTER TABLE (表名) AUTO_INCREMENT = 1;
zerofill : 零填充 (配合int使用,不够5位拿0来填充)
1 2 | create table t12(id int (5) zerofill); insert into t12 values (1234567); |
位数超了之后,按写入的数据直接插入
1 | insert into t12 values (12); |
位数不足,前面补0
到此这篇关于mysql中的各种约束条件的文章就介绍到这了,更多相关mysql约束条件内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!