多表的關系介紹
- (1)專案中的多表
1:在實際的開發中,專案一定是有多張表組成的,這些表之間是有關系
2:表與表之間的關系分類:
一對一,一對多,多對多
(2)什么是一對一?
A表的一行對應B表的一行,反之也成立,此時,兩張表可以合并成一張表

(3)什么是一對多?
A表的一行對應B表的多行,反之不成立

(4)什么是多對多?
A表的一行對應B表的多行,B的一行對應A表的多行

多表的關系-一對多關系
(1)初始化資料
(2)一對多的創建流程

》創建主表(分類表)
》創建從表(商品表)
》給主表和從表之間添加外鍵約束
alter table 從表 add [constraint] [外鍵名稱] foreign key (從表外鍵欄位名) references 主表 (主表的主鍵);
》給主表添加資料(隨便添加)
》給從表添加資料(添加資料是必須依賴主表)
(3)一對多特點
添加資料: 主表:隨意添加,從表:受主表限制
洗掉資料: 主表:如果某一行的資料受到從表的依賴,則不能洗掉, 從表:可以隨意洗掉
(4)類比
省和市

create database day13_2
use day13_2
》創建主表(分類表)
create table category(
cid int primary key auto_increment,
cname varchar(20)
)
》創建從表(商品表)
create table product(
pid int primary key auto_increment,
pname varchar(20),
price double,
cid int -- 外鍵 表示屬于哪個分類
)
》給主表和從表之間添加外鍵約束
`alter table 從表 add [constraint] [外鍵名稱] foreign key (從表外鍵欄位名) references 主表 (主表的主鍵);`
alter table product add foreign key (cid) references category(cid)
》給主表添加資料(隨便添加)
insert into category value(null,'電子')
insert into category value(null,'服裝')
》給從表添加資料(添加資料是必須依賴主表)
insert into product value(null,'聯想',2000,1)
insert into product value(null,'華為',4000,1)
insert into product value(null,'真維斯',100,2)
多表的關系-多對多關系
- (1)使用excel分析
- (2)多對多創建流程
》》創建訂單表(主表) order
》》創建中間表(從表) 保存交叉線
》》給中間表建立外鍵約束(2次)
》》給訂單表添加資料(隨意)
》》給中間表添加資料(受限)


》》創建訂單表(主表) order
create table orders(
oid int primary key auto_increment,
money double
)
》》創建中間表(從表) 保存交叉線
create table orders_product(
oid int , -- 必須存在 外鍵
pid int -- 必須存在 外鍵
)
》》給中間表建立外鍵約束(2次)
alter table orders_product add foreign key (oid ) references orders(oid);
alter table orders_product add foreign key (pid ) references product(pid);
》》給訂單表添加資料(隨意)
insert into product value(null,'LV',30000,2)
》》給中間表添加資料(受限)
insert into orders_product value(3,6)
練習-一對多
(1)分析 省表與市表關系
(2)創建表:一對多的關系(省表和市表)
》》 創建主表(省表)
》》 創建從表(市表)
》》建立外鍵約束
》》給主表添加資料
》》給從表添加資料

# 練習1
》》 創建主表(省表)
create table province(
pid int primary key auto_increment,
pname varchar(20)
)
》》 創建從表(市表)
create table city(
cid int primary key auto_increment,
cname varchar(20),
pid_fk int
)
》》建立外鍵約束
alter table city add foreign key (pid_fk ) references province(pid);
》》給主表添加資料
》》給從表添加資料
練習-多對多-演員和角色
-
(1)分析 演員表與角色表關系

-
(2)創建表:多對多的關系(演員表與角色表)
》》 創建演員表(左側主表)
》》 創建角色表(右側主表)
》》創建中間表(從表)
》》建立外鍵約束(2次)
》》給演員表添加資料
》》給角色表添加資料
》》給中間表添加資料
》》 創建演員表(左側主表)
create table users(
uid int primary key auto_increment,
uname varchar(20)
)
》》 創建角色表(右側主表)
create table role(
rid int primary key auto_increment,
rname varchar(20)
)
》》 創建中間表(從表)
create table users_role(
rid int , -- 資料必須在role存在
uid int -- 資料必須在users存在
)
》》建立外鍵約束(2次)
alter table users_role add foreign key (rid ) references role(rid);
alter table users_role add foreign key (uid) references users(uid);
》》給演員表添加資料
》》給角色表添加資料
》》給中間表添加資料
練習-角色表和權限表
-
(1)分析 角色與權限 表關系

-
(2)創建表:多對多的關系(角色與權限)

》》 創建權限表
》》 第二個中間表
》》建立外鍵約束
》》給權限表添加資料
》》給中間表添加資料
》》 創建權限表
create table privilege(
pid int primary key auto_increment,
pname varchar(20)
)
》》 第二個中間表
create table privilege_role(
pid_fk int , -- 外鍵
rid_fk int -- 外鍵
)
》》建立外鍵約束
alter table privilege_role add foreign key (pid_fk) references privilege(pid);
alter table privilege_role add foreign key (rid_fk) references role(rid);
》》給權限表添加資料
》》給中間表添加資料
總結
- (1)分析 一對多,還是多對多
- (2)建表,加外鍵
- (3)不用所有題都做,只要選一道精做
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/118633.html
標籤:其他
上一篇:pymongo多條件篩選時間報錯‘module‘ object is not callable
下一篇:Oracle開發實戰學習
