主要内容:
- 1.创建用户和授权
- 2.数据类型
- 3.完整性约束
1.创建用户和授权
(1)权限管理
MySql 的最高权限管理者是root用户,它拥有着最高的权限操作。包括select、update、delete、update、grant等操作。那么一般情况在公司之后DBA工程师会创建一个用户和密码,让你去连接数据库的操作,并给当前的用户设置某个操作的权限(或者所有权限)。那么这时就需要我们来简单了解一下:
- 如何创建用户和密码
- 给当前的用户授权
- 移除当前用户的权限
(2)创建新用户
-
进入到mysql数据库下
#进入到 mysql mysql> use mysql Database changed
-
对新用户进行增删改
#1.创建用户:指定ip:192.168.15.11的hehe用户登录 create user 'hehe'@'192.168.15.11' identified by '123'; # 指定ip:192.168.15.开头的hehe用户登录 create user 'hehe'@'192.118.1.%' identified by '123'; # 指定任何ip的hehe用户登录 create user 'hehe'@'%' identified by '123'; #2.删除用户 drop user '用户名'@'IP地址'; #3.修改用户 rename user '用户名'@'IP地址' to '新用户名'@'IP地址'; #4.修改密码 set password for '用户名'@'IP地址'=Password('新密码');
-
对当前用户授权管理
#1 查看权限 show grants for '用户'@'IP地址' #授权 hehe用户仅对db1.t1文件有查询、插入和更新的操作 grant select ,insert,update on db1.t1 to "hehe"@'%'; # 表示有所有的权限,除了grant这个命令,这个命令是root才有的。hehe用户对db1下的t1文件有任意操作 grant all privileges on db1.t1 to "hehe"@'%'; #hehe用户对db1数据库中的文件执行任何操作 grant all privileges on db1.* to "hehe"@'%'; #hehe用户对所有数据库中文件有任何操作 grant all privileges on *.* to "hehe"@'%'; #取消权限 # 取消hehe用户对db1的t1文件的任意操作 revoke all on db1.t1 from 'hehe'@"%"; # 取消来自远程服务器的hehe用户对数据库db1的所有表的所有权限 revoke all on db1.* from 'hehe'@"%"; #取消来自远程服务器的hehe用户所有数据库的所有的表的权限 revoke all privileges on *.* from 'hehe'@'%';
-
MySql 备份命令操作
# 备份:数据表结构+数据 mysqdump -u root db1 > db1.sql -p # 备份:数据表结构 mysqdump -u root -d db1 > db1.sql -p #导入现有的数据到某个数据库 #1.先创建一个新的数据库 create database db10; # 2.将已有的数据库文件导入到db10数据库中 mysqdump -u root -d db10 < db1.sql -p
2. 数据类型
mysql 常用数据类型
#1. 数字: 整型:tinyint int bigint 小数: float :在位数比较短的情况下不精准 double :在位数比较长的情况下不精准 0.000001230123123123 存成:0.000001230000 decimal:(如果用小数,则用推荐使用decimal) 精准 内部原理是以字符串形式去存 #2. 字符串: char(10):简单粗暴,浪费空间,存取速度快 root存成root000000 varchar:精准,节省空间,存取速度慢 sql优化:创建表时,定长的类型往前放,变长的往后放 比如性别 比如地址或描述信息 >255个字符,超了就把文件路径存放到数据库中。 比如图片,视频等找一个文件服务器,数据库中只存路径或url。 #3. 时间类型: 最常用:datetime #4. 枚举类型与集合类型 enum 和set
2.1 数值类型 & 浮点型 & 日期类型
(1)数值类型
整数类型:TINYINT SMALLINT MEDIUMINT INT BIGINT
作用:存储年龄,等级,id,各种号码等
约束的作用: 保证数据的完整性和一直性
-
tinyint [-128~127] 小整数
-
int 整数
-
bigint 极大整数
create table t1(id int(4) unsigned,name char(20));
验证1:有符号和无符号tinyint
============有符号tinyint============== # 创建数据库db4 create database db4 charset utf8; # 切换到当前db4数据库 mysql> use db4; # 创建t1 规定x字段为tinyint数据类型(默认是有符号的) mysql> create table t1(x tinyint); # 验证,插入-1这个数 mysql> insert into t1 values(-1); # 查询 表记录,查询成功(证明默认是有符号类型) mysql> select * from t1; +------+ | x | +------+ | -1 | +------+ #执行如下操作,会发现报错。因为有符号范围在(-128,127) mysql> insert into t1 values(-129),(128); ERROR 1264 (22003): Out of range value for column 'x' at row 1 ============无符号tinyint============== # 创建表时定义记录的字符为无符号类型(0,255) ,使用unsigned mysql> create table t2(x tinyint unsigned); # 报错,超出范围 mysql> insert into t2 values(-129); ERROR 1264 (22003): Out of range value for column 'x' at row 1 # 插入成功 mysql> insert into t2 values(255); Query OK, 1 row affected (0.00 sec)
验证2:int类型后面的存储是显示宽度,而不是存储宽度
mysql> create table t3(id int(1) unsigned); #插入255555记录也是可以的 mysql> insert into t3 values(255555); mysql> select * from t3; +--------+ | id | +--------+ | 255555 | +--------+ ps:以上操作还不能够验证,再来一张表验证用zerofill 用0填充 # zerofill 用0填充 mysql> create table t4(id int(5) unsigned zerofill); mysql> insert into t4 value(1); Query OK, 1 row affected (0.00 sec) #插入的记录是1,但是显示的宽度是00001 mysql> select * from t4; +-------+ | id | +-------+ | 00001 | +-------+ row in set (0.00 sec)
(2)浮点型
定点数类型: DEC等同于DECIMAL
作用:存储薪资、身高、体重、体质参数等
浮点类型:FLOAT DOUBLE
-
-
double 双精度 随着小数位数的增多.不准确,比float要准确
-
decimal 小数 精准的小数
-------------------------FLOAT------------------- FLOAT[(M,D)] [UNSIGNED] [ZEROFILL] #参数解释:单精度浮点数(非准确小数值),M是全长,D是小数点后个数。M最大值为255,D最大值为30 #有符号: -3.402823466E+38 to -1.175494351E-38, 1.175494351E-38 to 3.402823466E+38 #无符号: 1.175494351E-38 to 3.402823466E+38 #精确度: **** 随着小数的增多,精度变得不准确 **** -------------------------DOUBLE----------------------- DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL] #参数解释: 双精度浮点数(非准确小数值),M是全长,D是小数点后个数。M最大值为255,D最大值为30 #有符号: -1.7976931348623157E+308 to -2.2250738585072014E-308 2.2250738585072014E-308 to 1.7976931348623157E+308 #无符号: 2.2250738585072014E-308 to 1.7976931348623157E+308 #精确度: ****随着小数的增多,精度比float要高,但也会变得不准确 **** ====================================== --------------------DECIMAL------------------------ decimal[(m[,d])] [unsigned] [zerofill] #参数解释:准确的小数值,M是整数部分总个数(负号不算),D是小数点后个数。 M最大值为65,D最大值为30。 #精确度: **** 随着小数的增多,精度始终准确 **** 对于精确数值计算时需要用此类型 decaimal能够存储精确值的原因在于其内部按照字符串存储。
#1验证FLOAT类型建表: mysql> create table t5(x float(256,31)); ERROR 1425 (42000): Too big scale 31 specified for column 'x'. Maximum is 30. mysql> create table t5(x float(256,30)); ERROR 1439 (42000): Display width out of range for column 'x' (max = 255) mysql> create table t5(x float(255,30)); #建表成功 Query OK, 0 rows affected (0.03 sec) #2验证DOUBLE类型建表: mysql> create table t6(x double(255,30)); #建表成功 Query OK, 0 rows affected (0.03 sec) #3验证deimal类型建表: mysql> create table t7(x decimal(66,31)); ERROR 1425 (42000): Too big scale 31 specified for column 'x'. Maximum is 30. mysql> create table t7(x decimal(66,30)); ERROR 1426 (42000): Too big precision 66 specified for column 'x'. Maximum is 65. mysql> create table t7(x decimal(65,30)); #建表成功 Query OK, 0 rows affected (0.00 sec)
# 分别对三张表插入相应的记录 mysql> insert into t5 values(1.1111111111111111111111111111111);#小数点后31个1 Query OK, 1 row affected (0.01 sec) mysql> insert into t6 values(1.1111111111111111111111111111111); Query OK, 1 row affected (0.01 sec) mysql> insert into t7 values(1.1111111111111111111111111111111); Query OK, 1 row affected, 1 warning (0.00 sec) # 查询结果 mysql> select * from t5; #随着小数的增多,精度开始不准确 +----------------------------------+ | x | +----------------------------------+ | 1.111111164093017600000000000000 | +----------------------------------+ row in set (0.00 sec) mysql> select * from t6; #精度比float要准确点,但随着小数的增多,同样变得不准确 +----------------------------------+ | x | +----------------------------------+ | 1.111111111111111200000000000000 | +----------------------------------+ row in set (0.00 sec) mysql> select * from t7; #精度始终准确,d为30,于是只留了30位小数 +----------------------------------+ | x | +----------------------------------+ | 1.111111111111111111111111111111 | +----------------------------------+ row in set (0.00 sec)
(3)日期类型
date 年月日
time 时分秒
datetime 年月日 时分秒
now() sql语言中自带的内容函: 获取当前的时间(根据数据类型)
create table t10(born_year year,intClass datetime);
2.2 char类型 &varchar类型
2.3枚举和集合
3.完整性约束
3.1 not null 与 default
-
-
如果即设置了not null,又指定default,可以插入空值,会走default
(1)默认值可以为空
mysql> create table t1(id int);# id字段默认可以为空 Query OK, 0 rows affected (0.05 sec) mysql> desc t1; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ row in set (0.03 sec) mysql> insert into t1 values(); #给t1表插一个空的值 Query OK, 1 row affected (0.00 sec) #查询结果如下 mysql> select * from t1; +------+ | id | +------+ | NULL | +------+ row in set (0.00 sec) 默认值可以为空
(2)设置not null后 ,插入值不能为空
#给 num设置不能为空 mysql> create table t2(num int not null); Query OK, 0 rows affected (0.47 sec) mysql> desc t2; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | num | int(11) | NO | | NULL | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.01 sec) #此时报错,显示不能插空值 mysql> insert into t2 values(); ERROR 1364 (HY000): Field 'num' doesn't have a default value
(3) 如果即设置了not null,又指定default,可以插入空值,值为default,当插入值不是空值,结果是自己的值
#创建 t3表此时设置not null,并且default为 4 mysql> create table t3(num int not null default 4); Query OK, 0 rows affected (0.46 sec) mysql> desc t3; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | num | int(11) | NO | | 4 | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.01 sec) #当插入数据为空时,此时是默认值生效 mysql> insert into t3 values(); Query OK, 1 row affected (0.11 sec) #但是也可以插入值但是此时却是给的值而不是默认值 mysql> insert into t3 values(2); Query OK, 1 row affected (0.11 sec) mysql> select * from db2.t3; +-----+ | num | +-----+ | 4 | | 2 | +-----+ 2 rows in set (0.00 sec)
3.2
(1)单列唯一
# 创建t4表,给name设置单列唯一 mysql> create table t4(id int not null,name char(20) unique); Query OK, 0 rows affected (0.47 sec) mysql> desc t4; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | NO | | NULL | | | name | char(20) | YES | UNI | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.01 sec) mysql> insert into t4 values(1,'alex'); Query OK, 1 row affected (0.35 sec) mysql> insert into t4 values(1,'alex'); #有于给name设置了单列为一,因此插入相同的值会报错 ERROR 1062 (23000): Duplicate entry 'alex' for key 'name' #当将值改为与第一次不同时候,发现此时插入成功 mysql> insert into t4 values(1,'alex2'); Query OK, 1 row affected (0.34 sec) mysql> select * from db2.t4; +----+-------+ | id | name | +----+-------+ | 1 | alex | | 1 | alex2 | +----+-------+ 2 rows in set (0.00 sec)
两种书写方式:
create table t4( id int not null, name char(20) unique ); create table t4( id int not null, name char(20), unique(name) ); insert into t4(id,name) values(1,'alex'); insert into t4(id,name) values(1,'wusir');
(2)多列唯一:只要有一列是相同的就不能插入.
#书写
create table t5( id int, name char(20), unique(id), unique(name) );
#创建文件t5 ,此时将id 和name都设置 unique mysql> create table t5( -> id int, -> name char(20), -> unique(id), -> unique(name) -> ); Query OK, 0 rows affected (0.58 sec) #插入一组值 mysql> insert into t5 values(2,'alex'); ERROR 1062 (23000): Duplicate entry 'alex' for key 'name' #插入有相同内容的值时候报错 mysql> insert into t5 values(1,'alex2'); ERROR 1062 (23000): Duplicate entry '1' for key 'id' #当所有内容都不同时才能插入 mysql> insert into t5 values(2,'alex2'); Query OK, 1 row affected (0.10 sec) mysql> desc t5; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | UNI | NULL | | | name | char(20) | YES | UNI | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.01 sec) mysql> select * from db2.t5; +------+-------+ | id | name | +------+-------+ | 1 | alex | | 2 | alex2 | +------+-------+ 2 rows in set (0.00 sec)
(3)联合唯一:多列相同时不能插入
**适用场景 学生选课
#书写 create table t6( id int, name char(20), unique(id,name) );
#创建t6表 ,设置联合唯一 mysql> create table t6( -> id int, -> name char(20), -> unique(id,name) -> ); Query OK, 0 rows affected (0.47 sec) mysql> desc t6; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | MUL | NULL | | | name | char(20) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.01 sec) mysql> insert into t6 values(1,"alex"); Query OK, 1 row affected (0.35 sec) #当插入相同的两列记录会报错 mysql> insert into t6 values(1,"alex"); ERROR 1062 (23000): Duplicate entry '1-alex' for key 'id' mysql> insert into t6 values(2,"alex"); Query OK, 1 row affected (0.11 sec) #但是当两列记录中的一项内容不同就可以插入 mysql> insert into t6 values(1,"wusir"); Query OK, 1 row affected (0.10 sec) mysql> select *from t6; +------+-------+ | id | name | +------+-------+ | 1 | alex | | 1 | wusir | | 2 | alex | +------+-------+ 3 rows in set (0.01 sec)
3.3 主键primary key (not null + unique 的结果)
(1)单列主键 不能为空 ,并且是唯一
#书写 # primary key 索引(针对于大量数据) 查询速度要快 create table t7( id int primary key, name varchar(10) unique ); #相同结果的写法 create table t8( id int not null unique, name varchar(10) unique );
#创建t7表 此时为id设置主键,并且 name唯一 mysql> create table t7( -> id int primary key, -> name varchar(10) unique -> ); Query OK, 0 rows affected (0.51 sec) mysql> desc t7; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(10) | YES | UNI | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.01 sec) #当插入空值时候回报错 mysql> insert into t7 values(); ERROR 1364 (HY000): Field 'id' doesn't have a default value mysql> insert into t7(id,name) values(1,"wusir"); Query OK, 1 row affected (0.37 sec) #id唯一且不能为空 mysql> insert into t7(id,name) values(1,"wusir"); ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY' mysql> insert into t7(id,name) values(1,"wusir2"); ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY' mysql> insert into t7(id,name) values(2,"wusir2"); Query OK, 1 row affected (0.35 sec) mysql> select *from db2.t7; +----+--------+ | id | name | +----+--------+ | 1 | wusir | | 2 | wusir2 | +----+--------+ 2 rows in set (0.00 sec)
(2)联合主键
#书写方式 create table t9( id int, name varchar(10), primary key(id,name) );
#创建了t9表,此时可以设置空值 mysql> create table t9( -> id int, -> name varchar(10), -> primary key(id,name) -> ); Query OK, 0 rows affected (0.30 sec) mysql> desc t9; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | 0 | | | name | varchar(10) | NO | PRI | | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.01 sec) mysql> insert into t9 values(); Query OK, 1 row affected (0.11 sec) mysql> insert into t9(id name) values(1,"alex"); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'name) values(1,"alex")' at line 1 mysql> insert into t9(id, name) values(1,"alex"); Query OK, 1 row affected (0.10 sec) mysql> insert into t9(id, name) values(1,"alex2"); Query OK, 1 row affected (0.11 sec) mysql> select *from db2.t9; +----+-------+ | id | name | +----+-------+ | 0 | | | 1 | alex | | 1 | alex2 | +----+-------+ 3 rows in set (0.00 sec)
**学生选课可以为空
3.4.auto_increment (自增长)
#书写方式 create table student( id int primary key auto_increment, name varchar(20) not null, sex enum('male','female') default 'male', ip varchar(20) unique ); insert into student(name,sex,ip) values ('alex','female','127.0.0.5'),('wusir','male','173.45.32.1');
mysql> create table student( -> id int primary key auto_increment, -> name varchar(20) not null, -> sex enum('male','female') default 'male', -> ip varchar(20) unique -> ); Query OK, 0 rows affected (0.23 sec) mysql> insert into student(name,sex,ip) values ('alex','female','127.0.0.5'),('wusir','male','173.45.32.1'); Query OK, 2 rows affected (0.37 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> desc student; +-------+-----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | NO | | NULL | | | sex | enum('male','female') | YES | | male | | | ip | varchar(20) | YES | UNI | NULL | | +-------+-----------------------+------+-----+---------+----------------+ 4 rows in set (0.01 sec) mysql> select *from student; +----+-------+--------+-------------+ | id | name | sex | ip | +----+-------+--------+-------------+ | 1 | alex | female | 127.0.0.5 | | 2 | wusir | male | 173.45.32.1 | +----+-------+--------+-------------+ 2 rows in set (0.00 sec)
涉及到删除时:
#当删除第二条记录时候,会发现此时的结果任然保留id为2 时的序号 mysql> delete from student where id =2; Query OK, 1 row affected (0.49 sec) mysql> select *from student; +----+-------+--------+------------+ | id | name | sex | ip | +----+-------+--------+------------+ | 1 | alex | female | 127.0.0.5 | | 3 | alex1 | male | 127.1.0.12 | +----+-------+--------+------------+ 2 rows in set (0.00 sec) #在插入记录时候,会在后续排列 mysql> insert into student(name,sex,ip) values ('alex3','male','127.1.5.72'); Query OK, 1 row affected (0.11 sec) mysql> select *from student; +----+-------+--------+------------+ | id | name | sex | ip | +----+-------+--------+------------+ | 1 | alex | female | 127.0.0.5 | | 3 | alex1 | male | 127.1.0.12 | | 4 | alex3 | male | 127.1.5.72 | +----+-------+--------+------------+ 3 rows in set (0.00 sec) #但是可以指定id进行插入 mysql> insert into student(id,name,sex,ip) values (2,'wusir','male','127.3.5.9'); Query OK, 1 row affected (0.11 sec) mysql> mysql> Ctrl-C -- exit! select *from student; +----+-------+--------+------------+ | id | name | sex | ip | +----+-------+--------+------------+ | 1 | alex | female | 127.0.0.5 | | 2 | wusir | male | 127.3.5.9 | | 3 | alex1 | male | 127.1.0.12 | | 4 | alex3 | male | 127.1.5.72 | +----+-------+--------+------------+ 4 rows in set (0.00 sec) #清空记录 mysql> delete from student; Query OK, 4 rows affected (0.36 sec) mysql> delete from student; Query OK, 0 rows affected (0.00 sec) mysql> show tables; +---------------+ | Tables_in_db2 | +---------------+ | student | | t1 | | t2 | | t3 | | t4 | | t5 | | t6 | | t7 | | t9 | +---------------+ 9 rows in set (0.11 sec) #但是会保留自增长的序号,后续插入 mysql> insert into student(name,sex,ip) values ('alex3','male','127.1.5.72'); Query OK, 1 row affected (0.36 sec) mysql> select *from student; +----+-------+------+------------+ | id | name | sex | ip | +----+-------+------+------------+ | 5 | alex3 | male | 127.1.5.72 | +----+-------+------+------------+ 1 row in set (0.00 sec) #但是使用truncate时候,不会保留增长的序号 mysql> truncate table student; Query OK, 0 rows affected (0.21 sec) mysql> show tables; +---------------+ | Tables_in_db2 | +---------------+ | student | | t1 | | t2 | | t3 | | t4 | | t5 | | t6 | | t7 | | t9 | +---------------+ 9 rows in set (0.00 sec) mysql> select *from student; Empty set (0.00 sec) mysql> insert into student(name,sex,ip) values ('alex3','male','127.1.5.72'); Query OK, 1 row affected (0.35 sec) #再次插入时候从一开始 mysql> select *from student; +----+-------+------+------------+ | id | name | sex | ip | +----+-------+------+------------+ | 1 | alex3 | male | 127.1.5.72 | +----+-------+------+------------+ 1 row in set (0.00 sec)
delete from t1; #如果有自增id,新增的数据,仍然是以删除前的最后一样作为起始。
truncate table t1;数据量大,删除速度比上一条快,且直接从零开始。
3.5
# 先创建主表 create table dep( id int primary key auto_increment, name char(10) unique, dep_desc varchar(50) not null ); # 创建从表 create table emp( eid int primary key auto_increment, name char(10) not null, age int not null, dep_id int, school_id int, constraint fk_dep foreign key(dep_id) references dep(id) on delete cascade on update cascade, ); insert into dep(name,dep_desc) values('校长部','校长管理有限部门'),('公关部','公关管理有限部门'),('IT部门','IT技术有限部门'),('财务部','管钱很多部门'); insert into emp(name,age,dep_id) values ('alex',18,1), ('wusir',30,2), ('吴老板',20,3), ('马老板',18,4), ('邱老板',20,2), ('女神',16,3);
# 先创建主表 mysql> create table dep( -> id int primary key auto_increment, -> name char(10) unique, -> dep_desc varchar(50) not null -> ); Query OK, 0 rows affected (0.47 sec) #创建从表 mysql> create table emp( -> eid int primary key auto_increment, -> name char(10) not null, -> age int not null, -> dep_id int, -> school_id int, -> constraint fk_dep foreign key(dep_id) references dep(id) ); Query OK, 0 rows affected (0.55 sec) #主表插入数据 mysql> insert into dep(name,dep_desc) values('校长部','校长管理有限部门'),('公关部','公关管理有限部门'),('IT部门','IT技 术有限部门'),('财务部','管钱很多部门'); Query OK, 4 rows affected (0.11 sec) Records: 4 Duplicates: 0 Warnings: 0 #从表插入数据 mysql> insert into emp(name,age,dep_id) -> values -> ('alex',18,1), -> ('wusir',30,2), -> ('吴老板',20,3), -> ('马老板',18,4), -> ('邱老板',20,2), -> ('女神',16,3); Query OK, 6 rows affected (0.06 sec) Records: 6 Duplicates: 0 Warnings: 0 #将从表中对应主表的id为2 的项删除 mysql> delete from emp where dep_id =2; Query OK, 2 rows affected (0.37 sec) mysql> select *from emp; +-----+-----------+-----+--------+-----------+ | eid | name | age | dep_id | school_id | +-----+-----------+-----+--------+-----------+ | 1 | alex | 18 | 1 | NULL | | 3 | 吴老板 | 20 | 3 | NULL | | 4 | 马老板 | 18 | 4 | NULL | | 6 | 女神 | 16 | 3 | NULL | +-----+-----------+-----+--------+-----------+ 4 rows in set (0.00 sec) #此时可以将主表中id为2的部门删除 mysql> delete from dep where id=2; Query OK, 1 row affected (0.35 sec) mysql> select *from dep; +----+-----------+--------------------------+ | id | name | dep_desc | +----+-----------+--------------------------+ | 1 | 校长部 | 校长管理有限部门 | | 3 | IT部门 | IT技术有限部门 | | 4 | 财务部 | 管钱很多部门 | +----+-----------+--------------------------+ 3 rows in set (0.00 sec) #但是若果直接删除,会报错,因为此时该id对应的存在内容 mysql> delete from dep where id=3; ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`db2`.`emp`, CONSTRAINT `fk_dep` FOREIGN KEY (`dep_id`) REFERENCES `dep` (`id`))
所以要同步删除和同步更新
#当在创建从表时添加 on delete cascade on update cascade 同步更新同步删除 mysql> select *from dep; +----+-----------+--------------------------+ | id | name | dep_desc | +----+-----------+--------------------------+ | 1 | 校长部 | 校长管理有限部门 | | 2 | 公关部 | 公关管理有限部门 | | 3 | IT部门 | IT技术有限部门 | | 4 | 财务部 | 管钱很多部门 | +----+-----------+--------------------------+ 4 rows in set (0.00 sec) #直接删除部门可以,而对应的部门内的人也删除 mysql> delete from dep where id=2; Query OK, 1 row affected (0.35 sec) mysql> select *from dep; +----+-----------+--------------------------+ | id | name | dep_desc | +----+-----------+--------------------------+ | 1 | 校长部 | 校长管理有限部门 | | 3 | IT部门 | IT技术有限部门 | | 4 | 财务部 | 管钱很多部门 | +----+-----------+--------------------------+ 3 rows in set (0.00 sec) mysql> select *from emp; +-----+-----------+-----+--------+-----------+ | eid | name | age | dep_id | school_id | +-----+-----------+-----+--------+-----------+ | 1 | alex | 18 | 1 | NULL | | 3 | 吴老板 | 20 | 3 | NULL | | 4 | 马老板 | 18 | 4 | NULL | | 6 | 女神 | 16 | 3 | NULL | +-----+-----------+-----+--------+-----------+ 4 rows in set (0.00 sec)