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),用数据定义语言实现,然后设计实验 ...
随机推荐
- C# Math.Round实现中国式四舍五入
decimal sum = 11111.334; sum = , MidpointRounding.AwayFromZero); sum:11111.33decimal sum = 11111.34 ...
- 29. SpringBoot Redis 非注解
1. 引入依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...
- [译]Managing Vue.js State with Vuex
原文 准备 安装 Vuex, 是Vue官方出的package, 它不是Vue内置的.需要另外安装. npm install vuex --save 然后,需要在应用启动文件启用Vuex. main.j ...
- orcle查看表空间数据文件使用情况
-- 查看表空间数据文件使用情况select a.*, round(a.usedgb/a.maxgb*100) || '%' usedPer from (select t.TABLESPACE_NAM ...
- [C++]动态规划系列之币值最大化
/** * * @author Zen Johnny * @date 2018年3月31日 下午10:04:48 * */ package freeTest.dynamicProgramming; i ...
- 使用Excel过滤重复数据
如题 由于数据太多,没有办法人工过滤,所以借助Excel 我的数据是这样的 需要找出里面重复的数据 首先,选中需要筛选的数据,点击开始 --> 条件格式 --> 突出显示单元格规则 --& ...
- npm 的 --save 和 --save-dev 的区别
--save-dev 是作为开发依赖保存到 packsge.json 中的 devDependencies 中,即在开发环境中用到的依赖,如 webpack.babel 等用于开发打包的依赖,只是在执 ...
- tp5.0 SHOW COLUMNS FROM 生成数据表字段缓存
TP5.0 生成数据表字段缓存 =控制台执行以下命令= 1.生成指定数据库的所有表字段缓存 php think optimize:schema --db databaseName 2.生成指定数据表的 ...
- 20165325 2017-2018-2《Java程序设计》课程总结
20165325 2017-2018-2<Java程序设计>课程总结 一.每周作业链接汇总 1.预备作业一:我期待的师生关系 20165325 期望的师生关系 简要内容: 我心中的好老师 ...
- 如何解决错误0×80071AC3
前几天我在机房敲百例的时候,敲完了想把文件夹移动到我的U盘里去,结果出现了这种情况 上面说运行chkdsk并重试,我运行了一下,没什么反应,我就想既然不能移动文件夹 ...