MySQL--表操作(约束条件foreign key关联表 多对1,多对多,1对1)
1、表的组织结构复杂不清晰
2、浪费空间
3、扩展性极差
a、分表 + foreign key: 有硬性限制(关联表中的关联字段内容必须来自于被关联表),但后续修改删除麻烦(不能直接修改,删除要先删除关联对象中的相应元素再删除被关联对象中的相应元素)
#foreign key (MUL): 可以理解成外部有一个硬性限制
b、分表 + foreign key + on update cascade on delete cascade: 有硬性限制,对被关联表进行修改删除,关联表相应元素跟着改变;强耦合
c、分表: 靠逻辑上的关系维护,解开耦合
i、a True & b False 多对一
ii、a False & b True 多对一
实现多对一: 在emp表中新增一个dep_id字段,该字段指向dep表的id字段
约束1:在创建表时,先建被关联的表dep,才能建关联表emp
create table dep(
id int primary key auto_increment,
dep_name char(10),
dep_comment char(60)
);
id int primary key auto_increment,
name char(16),
gender enum('male','female') not null default 'male',
dep_id int,
foreign key(dep_id) references dep(id)
);
insert into dep(dep_name,dep_comment) values
('sb教学部','sb辅导学生学习,教授python课程'),
('外交部','老男孩上海校区驻张江形象大使'),
('nb技术部','nb技术能力有限部门');
('alex','male',1),
('egon','male',2),
('lxx','male',1),
('wxx','male',1),
('wenzhou','female',3);
a、单单只加foreign key:有硬性限制,但后续修改删除麻烦(不能直接修改,删除要先删除关联对象中的相应元素再删除被关联对象中的相应元素)
b、foreign key(dep_id) references dep(id) + on update cascade on delete cascade:(当前表的外键关联另一个表的主键)
1)先删除关联表emp,再删除被关联表dep,准备重建
mysql> drop table emp;
mysql> drop table dep;
create table dep(
id int primary key auto_increment,
dep_name char(10),
dep_comment char(60)
);
id int primary key auto_increment,
name char(16),
gender enum('male','female') not null default 'male',
dep_id int,
foreign key(dep_id) references dep(id)
on update cascade
on delete cascade #一条语句
);
('sb教学部','sb辅导学生学习,教授python课程'),
('外交部','老男孩上海校区驻张江形象大使'),
('nb技术部','nb技术能力有限部门');
('alex','male',1),
('egon','male',2),
('lxx','male',1),
('wxx','male',1),
('wenzhou','female',3);
mysql> select * from dep;
+----+------------------+------------------------------------------------------------------------------------------+
| id | dep_name | dep_comment |
+----+------------------+------------------------------------------------------------------------------------------+
| 1 | sb教学部 | sb辅导学生学习,教授python课程 |
| 2 | 外交部 | 老男孩上海校区驻张江形象大使 |
| 3 | nb技术部 | nb技术能力有限部门 |
+----+------------------+------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)
+----+------------------+--------+--------+
| id | name | gender | dep_id |
+----+------------------+--------+--------+
| 1 | alex | male | 1 |
| 2 | egon | male | 2 |
| 3 | lxx | male | 1 |
| 4 | wxx | male | 1 |
| 5 | wenzhou | female | 3 |
+----+------------------+--------+--------+
5 rows in set (0.00 sec)
Query OK, 1 row affected (0.02 sec)
+----+------------------+------------------------------------------------------------------------------------------+
| id | dep_name | dep_comment |
+----+------------------+------------------------------------------------------------------------------------------+
| 2 | 外交部 | 老男孩上海校区驻张江形象大使 |
| 3 | nb技术部 | nb技术能力有限部门 |
+----+------------------+------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
+----+------------------+--------+--------+
| id | name | gender | dep_id |
+----+------------------+--------+--------+
| 2 | egon | male | 2 |
| 5 | wenzhou | female | 3 |
+----+------------------+--------+--------+
2 rows in set (0.00 sec)
mysql> select * from emp;
+----+------------------+--------+--------+
| id | name | gender | dep_id |
+----+------------------+--------+--------+
| 2 | egon | male | 2 |
| 5 | wenzhou | female | 3 |
+----+------------------+--------+--------+
2 rows in set (0.00 sec)
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
+-----+------------------+------------------------------------------------------------------------------------------+
| id | dep_name | dep_comment |
+-----+------------------+------------------------------------------------------------------------------------------+
| 3 | nb技术部 | nb技术能力有限部门 |
| 200 | 外交部 | 老男孩上海校区驻张江形象大使 |
+-----+------------------+------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
+----+------------------+--------+--------+
| id | name | gender | dep_id |
+----+------------------+--------+--------+
| 2 | egon | male | 200 |
| 5 | wenzhou | female | 3 |
+----+------------------+--------+--------+
2 rows in set (0.00 sec)
1、delete from tb1;
上面的这条命令确实可以将表里的所有记录都删掉,但不会将id重置为0
该条命令不是用来清空表的,delete是用来删除表中某一些符合条件的记录 (delete from tb1 where id > 10;)
如果要清空表,使用truncate tb1;
作用:将整张表重置
iii、a True & b True 多对多
实现多对多:建立第三张表,该表中有一个字段fk左表的id,还有一个字段是fk右表的id
id int primary key auto_increment,
name char(16)
);
id int primary key auto_increment,
bname char(16),
price int
);
('egon'),
('alex'),
('wxx')
;
('python从入门到入土',200),
('葵花宝典切割到精通',800),
('九阴真经',500),
('九阳神功',100)
;
id int primary key auto_increment,
author_id int,
book_id int,
foreign key(author_id) references author(id)
on update cascade
on delete cascade,
foreign key(book_id) references book(id)
on update cascade
on delete cascade
);
(1,3),
(1,4),
(2,2),
(2,4),
(3,1),
(3,2),
(3,3),
(3,4);
egon写了什么书?
a、在author表中找到egon对应的id(author_id)
b、根据author_id 在关系表中找到对应的book_id
c、根据book_id 再book表中找到对应的book_name
iv、a False & b False 一对一
实现一对一:在emp表中新增一个dep_id字段,该字段指向dep表的id字段, foreign key + unique; 在多对一的基础上限制关联表中的相应字段必须唯一
id int primary key auto_increment,
name char(20) not null,
qq char(10) not null,
phone char(16) not null
);
id int primary key auto_increment,
class_name char(20) not null,
customer_id int unique, #该字段一定要是唯一的
foreign key(customer_id) references customer(id) #外键的字段一定要保证unique
on delete cascade
on update cascade
);
('李飞机','31811231',13811341220),
('王大炮','123123123',15213146809),
('守榴弹','283818181',1867141331),
('吴坦克','283818181',1851143312),
('赢火箭','888818181',1861243314),
('战地雷','112312312',18811431230)
;
('脱产3班',3),
('周末19期',4),
('周末19期',5)
;
1. 修改表名
ALTER TABLE 表名 RENAME 新表名;
#mysql中库名、表名对大小写不敏感
ALTER TABLE 表名ADD 字段名 数据类型 [完整性约束条件…], ADD 字段名 数据类型 [完整性约束条件…];
3. 删除字段
ALTER TABLE 表名 DROP 字段名;
ALTER TABLE 表名 MODIFY 字段名 数据类型 [完整性约束条件…];
#modify不能改字段名
ALTER TABLE 表名 CHANGE 旧字段名 新字段名 新数据类型 [完整性约束条件…];
#change可以改字段名
#修改表通常改的是字段,而不修改数据类型(前后冲突则报错)
a、复制表结构+记录 (key不会复制: 主键、外键和索引)
mysql> create table new_service select * from service;
mysql> select * from service where 1=2; //条件为假,查不到任何记录
Empty set (0.00 sec)
mysql> create table new1_service select * from service where 1=2;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
DROP TABLE 表名;
mysql> select * from emp;
+----+------------------+--------+--------+
| id | emp_name | gender | dep_id |
+----+------------------+--------+--------+
| 2 | egon | male | 200 |
| 5 | wenzhou | female | 3 |
+----+------------------+--------+--------+
2 rows in set (0.00 sec)
where id > 1 and name like "%on%"
group by dep_id
having 分组后的过滤条件
order by 排序依据
limit n;
MySQL--表操作(约束条件foreign key关联表 多对1,多对多,1对1)的更多相关文章
- [MySQL数据库之表的约束条件:primary key、auto_increment、not null与default、unique、foreign key:表与表之间建立关联]
[MySQL数据库之表的约束条件:primary key.auto_increment.not null与default.unique.foreign key:表与表之间建立关联] 表的约束条件 约束 ...
- MySQL完整性约束foreign key与表操作。
一 MySQL中表的完整性约束: 我们首先知道约束条件跟类型的宽度一样,都是可选的,也就是说,我们在创建表的时候可以不指定,但是为了创建的表更加的完整,我们一般会加一些约束条件,name下面我们讲一 ...
- SQL Server 2008 R2——TRUNCATE TABLE 无法截断表 该表正由 FOREIGN KEY 约束引用
=================================版权声明================================= 版权声明:原创文章 禁止转载 请通过右侧公告中的“联系邮 ...
- MSSQL导入数据时,出现“无法截断表 因为表正由Foreign key引用”错误
* 错误 0xc002f210: 准备 SQL 任务: 执行查询“TRUNCATE TABLE [dsc100552_db].[dbo].[ALV_SalesBigClass] ”失败,错误如下:“无 ...
- 报错:无法截断表 '某表',因为该表正由 FOREIGN KEY 约束引用
某表的某个字段作为另一个表的FOREIGN KEY,在truncate另外一个表后,再truncate某表,就报如上的错. 解决方法: → 删除另外一个表的外键 IF OBJECT_ID(N'[dbo ...
- 8-[表操作]--foreign key、表与表的关系
1. foreign key (1)快速理解foreign key 员工信息表有三个字段:工号 姓名 部门 公司有3个部门,但是有1个亿的员工,那意味着部门这个字段需要重复存储,部门名字越长,越浪费 ...
- 【MySQL】Create table 以及 foreign key 删表顺序考究。
1.以下是直接从数据库导出的建表语句. 1 -- ---------------------------- 2 -- Table structure for files 3 -- ---------- ...
- mysql 库操作、存储引擎、表操作
阅读目录 库操作 存储引擎 什么是存储引擎 mysql支持的存储引擎 如何使用存储引擎 表操作 创建表 查看表结构 修改表ALTER TABLE 复制表 删除表 数据类型 表完整性约束 回到顶部 一. ...
- 数据库基本表创建 完整性约束 foreign Key
理解以下几张表的内容,根据实际情况设计属性名.数据类型.及各种完整性约束(primary key.foreign key.not null.unique.check),用数据定义语言实现,然后设计实验 ...
随机推荐
- ava.io.InputStream & java.io.FileInputStream
java.io.InputStream & java.io.FileInputStream java.io.InputStream,这个抽象类是表示字节输入流的超类,这个抽象类的共性的方法有: ...
- int、bool和str
int bit_length 返回以二进制表示的最短长度 print(int.bit_length(10)) 结果 4 Process finished with exit code 0 int() ...
- SpringBoot系列: 理解 Spring 的依赖注入(一)
==============================Spring 的依赖注入==============================对于 Spring 程序, Spring 框架为我们提供 ...
- Groovy 设计模式 -- Strategy 模式
策略模式 https://en.wikipedia.org/wiki/Strategy_pattern In computer programming, the strategy pattern (a ...
- luogu 1268 树的重量
打眼一看就是最小生成树嘛,但经过板子wa掉的经历后得知,,emmmm,原来是, 构造! (虽然不知是什么但觉得听起来很厉害的样子...手动微笑) n=2的情况 自然就是g(1,2) n=3的情况,由于 ...
- 【python小练】0001
第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? # coding ...
- matplotlib-形状
需要 import matplotlib.patches as mp import numpy as np import matplotlib.pyplot as plt import matpl ...
- Coursera Deep Learning 3 Structuring Machine Learning Projects, ML Strategy
Why ML stategy 怎么提高预测准确度?有了stategy就知道从哪些地方入手,而不至于找错方向做无用功. Satisficing and Optimizing metric 上图中,run ...
- 第20月第4天 pycharm utf-8
1.运行python %run a.py 运行 https://blog.csdn.net/little_bobo/article/details/78982412 2.UnicodeDecodeEr ...
- PKUSC2018游记
由于菜鸡DreamlessDreams还需要准备中考的原因....这篇游记拖到今天才发. Day0:出发+报道 ...