MySQL多表创建关联及操作
外键
现在有两张表“分类表”和“商品表”,为了表明商品属于哪个 类别,通常情况下,我们将在商品上添加一列,用来存放分类的cid信息,此列成为外键。
此时,分类表 category 称作主表,cid 成为主键。商品表 products 成为从表,category_id 成为外键。
通过主表和从表的外键描述主外键的关系,呈现的就是一对多的关系。
外键特点
1. 从表外键的值是对主键的引用。
2. 从表外键类型必须与主表主键类型一致。
声明外键约束
语法:
alter table 从表 add[constraint] [外键名称] foreign key (从表外键在字段名)references 主表(主表的主键);
[外键名称] 用于删除外键约束的,一般建议 “ _fk ”结尾。
alter table 从表 drop foreign key 外键名称
实际操作
创建数据库
创建分类表
.分类表
create table category(
cid varchar() primary key,
cname varchar()
);
创建商品表
.商品表
create table product(
pid varchar() primary key,
pname varchar(),
price double,
category_id varchar()
);
设置编码格式
set name gbk;
添加点数据
insert into category(cid,cname) values('c001','家电');
insert into category(cid,cname) values('c002','服饰');
insert into category(cid,cname) values('c003','化妆品'); insert into product(pid,pname,price,category_id) values('p001','联想','','c001');
insert into product(pid,pname,price,category_id) values('p002','海尔','','c001');
insert into product(pid,pname,price,category_id) values('p003','雷神','','c001'); insert into product(pid,pname,price,category_id) values('p004','JACK JONES','','c002');
insert into product(pid,pname,price,category_id) values('p005','真维斯','','c002');
insert into product(pid,pname,price,category_id) values('p006','花花公子','','c002');
insert into product(pid,pname,price,category_id) values('p007','劲霸','','c002'); insert into product(pid,pname,price,category_id) values('p008','香奈儿','','c003');
insert into product(pid,pname,price,category_id) values('p009','相宜本草','','c003');
最后一列 category_id 列在创建表的时候自己定义的,如果没有,则可以使用一下命令添加一列。
alter table product add category_id
链接两张表
alter table product add foreign key(category_id) references category(cid);
两张表已经关联,主表有外键约束。不能轻易删除主表数据,因为从表有他的记录。
要想解除关系,先删除从表对他有关系的数据删除,再删除主表数据。
使用外键的目的
保证数据的完整性。
注意事项
从表外键不能添加主表中不存在的记录。
主表不能删除从表中已经引用的记录。
表与表之间的关系
表语表之间的关系,说的就是表与表数据之间的关系。
一对多的关系
常见的实例:客户和订单,分类和商品,部门和员工。
一对多建表原则:在从表(多方)创建一个字段,字段作为外键指向主表的主键。
alter table 从表 add [constraint][外键名称] foreign key(从表外键在字段名) references 主表(主表的主键);
多对多的关系
常见的实例:学生和课程,商品和订单,演员和角色。
多对多关系建立表原则:需要创建第三张表,中间表中至少两个字段,这两个字段分别作为外键指向各自一方的主键。
建立关系
alter table stu_course add foreign key(sno) references stu(sid);
alter table stu_course add foreign key(sno) references course(cid);
一对一关系(了解)
在实际开发中应用不多,因为一对一可以创建成一张表。
两种建表原则:
外键唯一:主表的主键和从简的外键唯一,形成主外键关系,外键唯一 unique。
外键是主键:主表的主键和从表的主键,形成主外键关系。
多对多连接
实现如下表结构:
创建订单表
create table orders(
oid varchar() primary key,
totalprice double
);
创建经单项的表(中间表)
create table orderitem(
oid varchar(),
pid varchar()
);
关联两张表
alter table orderitem add constraint orderitem_fk foreign key(oid) references orders(oid);
alter table orderitem add constraint orderitem1_fk foreign key(pid) references product(pid);
多表查询操作
1. 交叉连接查询(基本不会用到 - 得到的是两个表的乘积)
select * from A,B
select * from category,product;
内容是乱的!第一张表3条数据,第二张表9条数据,一共 3*9 = 27 条数据。
2. 内连接查询(使用关键字 inner join -- inner可以省略)
隐式内连接
select * from A,B where 条件
select * from category c,product p where c.cid = p.category_id;
显式内连接
select * from A inner join B on 条件
select * from category inner join product on cid=category_id;
select * from category join product on cid=category_id;
3. 外连接查询(使用关键字 outer join --outer可以省略)
左外连接:left outer join
select * from A left outer join B on 条件
select * from category left join product on cid=category_id;
右外连接 right outer join
select * from A right outer join B on 条件;
select * from category right join product on cid=category_id;
左右连接的区别:
左连接:左边的数据全部查出来。category数据全部出来。
右连接:右边的数据全部查出来。product数据全部出来。
子查询
将一条 select 语句查询的结果作为另一条 select 语法的一部分(查询条件,查询结果,表等)。
例如:查询化妆品分类中上架商品详情。
查询化妆品分类
select cid from category where cname='化妆品';
利用子查询实现需求
select * from product where category_id=(select cid from category where cname="化妆品");
MySQL多表创建关联及操作的更多相关文章
- 数据库迁移(创建关联等操作) Target database is not up to date报错
使用Mysql-sqlalchemy执行数据库迁移 来更新数据库: 队长试探性的在网上找了几种方案 依然没有解决报错问题: 后来看了https://www.aliyun.com/jiaocheng/4 ...
- Mysql 数据库 表中列的操作
[1]Mysql数据库中表的列操作 Mysql中关于表中列的操作集语句: -- [1]增加一列 ) DEFAULT NULL COMMENT '目的码区号'; -- [2]增加一列,在dnis_are ...
- MySQL 数据表创建及管理
use stuinfo; -- 指定当前数据库 CREATE table if not exists student1( -- 创建数据表student1 sNo ) not NULL, sName ...
- mysql 单表下的字段操作_查询
查询的规律 查询语句限定条件越多,查询范围越小: 1.整个表 Select * From 库名.表名 2.整个表的某字段内 Select id From 库名.表名 3.整个表某字段的范围内 Sele ...
- Mysql建表+创建索引
创建表时可以直接创建索引,这种方式最简单.方便.其基本形式如下: CREATE TABLE 表名( 属性名 数据类型[完整性约束条件], 属性名 数据类型[完整性约束条件], ...... 属性名 数 ...
- mysql 单表下的字段操作
如下只介绍单表的添加.更新.删除.查询表结构操作,查询数据操作范围太大用单独的篇幅来讲解: 查看表结构 desc test_tb; Insert 插入数据 插入 = 添加 为表中指定的字段插入数据 C ...
- PYTHON MYSQL 的表创建和插入
import mysql.connector cnx = mysql.connector.connect(user='xx',password='xx++.',host='139.107.11.166 ...
- 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表
创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表 创建数据模型类(POCO类) 在Models文件夹下添 ...
- python开发mysql:单表查询&多表查询
一 单表查询,以下是表内容 一 having 过滤 1.1 having和where select * from emp where id > 15; 解析过程;from > where ...
随机推荐
- 天兔修改登录页的title
1.将 /opt/lampp/htdocs/lepus/application/views/login.php 文件中 第6行 <title><?php echo $this-> ...
- C++构造函数和重载函数运算符如何区分
构造函数和重载函数运算符如何区分: class Distance { private: int feet; int inches; public: Distance(){ feet = ; inche ...
- laravel如何向视图传递值
1.定义路由 Route::get('demo','DemoController@demo'); 2.定义控制器(内with();方法就是定义传递的值 key=>value)=>" ...
- 虚拟磁盘VHD文件压缩方法
问题描述 因工作需要在Mac上跑了一个VirtualBox虚拟win7,使用对win系统友好的vhd格式作为虚拟硬盘.经过一段时间使用发现vhd占用空间远大于虚拟磁盘使用量,想办法减减肥才行. 步骤整 ...
- JavaScript DOM–节点操作
节点 节点至少拥有nodeType(节点类型).nodeName(节点名称)和nodeValue(节点值)这三个基本属性. 元素节点 nodeType 为1 属性节点 nodeType 为2 文本 ...
- Ubuntu 安装交叉编译器出错问题
安装教程网上有很多,可参考:Ubuntu14.04(64位)下gcc-linaro-arm-linux-gnueabihf交叉编译环境搭建 但是我的问题一直是路径搭好了,就是找不到文件:反复查找,花了 ...
- 1.spring异常:Caused by: java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springfr ...
- Redis5-集群搭建实验
集群规划: nodeA:192.168.29.22(22-master,23-slave) nodeB:192.168.29.23(23-master,24-slave) nodeC:192.168. ...
- ORA-01935: missing user or role name
问题描述 ORA-01935: missing user or role name ORA-01935:缺少用户或角色名
- POJ3258 River Hopscotch(二分最大化最小值)
题目链接:http://poj.org/problem?id=3258 题意:给n个石头,起点和终点也是两个石头,去掉这石头中的m个,使得石头间距的最小值最大. 思路:二分石头间的最短距离,每次贪心地 ...