MySQL约束条件

tech2022-07-11  176

sailan

1. not null 与 default2. unique3. primary key4. foreign key5. where

1. not null 与 default

设置插入数据时,是否可为空,not null 为不可空,default设置默认值 当插入数据时如果未主动设置,则自动添加默认值;

create table t1 (x int not null); mysql> desc t1; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | x | int(11) | NO | | NULL | | +-------+---------+------+-----+---------+-------+ create table t2 (x int not null default 111); # 设置默认值 mysql> desc t2; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | x | int(11) | NO | | 111 | | +-------+---------+------+-----+---------+-------+

2. unique

控制插入数据的唯一性; 单例唯一:

mysql> create table t3(name varchar(10) unique); # 指定单个数据的唯一性; mysql> desc t3; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | varchar(10) | YES | UNI | NULL | | +-------+-------------+------+-----+---------+-------+ mysql> insert t3 values("sailan"); Query OK, 1 row affected (0.00 sec) mysql> insert t3 values("sailan"); # 再插入同样的报错; ERROR 1062 (23000): Duplicate entry 'sailan' for key 'name'

联合为一:

mysql> create table t4(name varchar(10),id int,ip int,port int,unique(ip,port),unique(name)); # 单个UNI指定了多个数据唯一性,这就是联合,后续可指定单例唯一; mysql> desc t4; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | name | varchar(10) | YES | UNI | NULL | | | id | int(11) | YES | | NULL | | | ip | int(11) | YES | MUL | NULL | | | port | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+

not null 和unique一起使用会产生的化学反应=>会被识别成表的主键(primary key),主键下面有介绍ao;

create table t6(id int,name varchar(10) not null unique); mysql> desc t6; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(10) | NO | PRI | NULL | | +-------+-------------+------+-----+---------+-------+ create table t5(id int,name varchar(10) unique); mysql> desc t5; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(10) | YES | UNI | NULL | | +-------+-------------+------+-----+---------+-------+

3. primary key

站在约束角度去看主键可以限制字段不为空且唯一,主键primary key是innodb存储引擎组织数据的依据,innodb称之为索引组织表,一张表中必须有且只有一个主键,我不设置的话innodb底层会自动生成一个,但这属实无奈之举,我应每创建表格都定制主键,主键通常是ID字段。

特点:

主键的约束效果是not null+uniqueinnodb表有且只有一个主键,但是该主键可以是联合主键

单列主键:

create table t6( id int primary key auto_increment, # auto_increment 不传参自动增长 name varchar(5));

联合主键:

create table t7( id int, name varchar(5), primary key(id,name));

4. foreign key

用来关联两个表;

# 需先创建被关联表 #表类型必须是innodb存储引擎,且被关联的字段,即references指定的另外一个表的字段,必须保证唯一 create table dep( id int primary key auto_increment, name varchar(6), comment varchar(30) ); # 再创建关联表 create table emp( id int primary key auto_increment, name varchar(10), gender varchar(5), dep_id int, #dep_id外键,关联父表(dep主键id),实现同步更新,同步删除 foreign key(dep_id) references dep(id) on delete cascade on update cascade ); # 先往被关联表插入数据 insert into dep(id,name) values (1,'技术部'), (2,'人力资源部'), (3,'销售部'); # 再往被关联子表插入数据 insert into emp(name,gender,dep_id) values ('egon',"male",1), ('alex1',"male",2), ('alex2',"male",2), ('alex3',"male",2), ('李坦克',"male",3), ('刘飞机',"male",3), ('张火箭',"male",3), ('林子弹',"male",3), ('加特林',"male",3) ; #删父表dep,子表emp中对应的记录跟着删; #更新父表dep,子表emp中对应的记录跟着改;

快速找出两张表之间的关系 ;

分析步骤: #1、先站在左表的角度去找 是否左表的多条记录可以对应右表的一条记录,如果是,则证明左表的一个字段foreign key 右表一个字段(通常是id#2、再站在右表的角度去找 是否右表的多条记录可以对应左表的一条记录,如果是,则证明右表的一个字段foreign key 左表一个字段(通常是id#3、总结: #多对一: 如果只有步骤1成立,则是左表多对一右表 如果只有步骤2成立,则是右表多对一左表 #多对多 如果步骤12同时成立,则证明这两张表时一个双向的多对一,即多对多,需要定义一个这两张表的关系表来专门存放二者的关系 create table author( id int primary key auto_increment, name varchar(10) ); create table book( id int primary key auto_increment, name varchar(16) ); create table author2book( id int primary key auto_increment, author_id int, book_id int, foreign key(author_id) references author(id) on delete cascade on update cascade, foreign key(book_id) references book(id) on delete cascade on update cascade ); #一对一: 如果12都不成立,而是左表的一条记录唯一对应右表的一条记录,反之亦然。这种情况很简单,就是在左表foreign key右表的基础上,将左表的外键字段设置成unique即可 关联方式:foreign key+unique

5. where

where字句中可以使用:

1. 比较运算符:> < >= <= <> != 2. between 80 and 100 值在1020之间 3. in(80,90,100) 值是102030 4. like 'egon%' pattern可以是%或_, %表示任意多字符 _表示一个字符 5. 逻辑运算符:在多个条件直接可以使用逻辑运算符 and or not select * from emp where id >= 3 and id <= 5; # 大于三小于五 select * from emp where id between 3 and 5; # 三到五之间 select * from emp where id not between 3 and 5; # 小于三大于五 select * from emp where id=3 or id=5 or id=7; # 3或5或7 select * from emp where id in (3,5,7); # 3或5或7 select * from emp where id not in (3,5,7); # not3或5或7 # 关键字LIKE模糊匹配 select * from emp where name like 'jin%'; # 通配符’%’匹全部 select * from emp where name like 'jin___'; # 通配符’_’匹单个 select * from emp where name regexp 'n$'; # 正则
最新回复(0)