創建多表及多表的關係
問: 分類表和商品表之間是不是有關係? 如果有關係,在數據庫中如何表示這種關係?
create table category(
cid int primary key auto_increment,
cname varchar(10),
cdesc varchar(31)
);
insert into category values(null,'手機數碼','電子產品,黑馬生產');
insert into category values(null,'鞋靴箱包','江南皮鞋廠傾情打造');
insert into category values(null,'香菸酒水','黃鶴樓,茅臺,二鍋頭');
insert into category values(null,'酸奶餅乾','娃哈哈,蒙牛酸酸乳');
insert into category values(null,'饞嘴零食','瓜子花生,八寶粥,辣條');
select * from category;
select cname,cdesc from category;
--所有商品
1.商品ID
2.商品名稱
3.商品的價格
4.生產日期
5.商品分類ID
商品和商品分類 : 所屬關係
create table product(
pid int primary key auto_increment,
pname varchar(10),
price double,
pdate timestamp,
cno int
);
insert into product values(null,'小米mix4',998,null,1);
insert into product values(null,'錘子',2888,null,1);
insert into product values(null,'阿迪王',99,null,2);
insert into product values(null,'老村長',88,null,3);
insert into product values(null,'勁酒',35,null,3);
insert into product values(null,'小熊餅乾',1,null,4);
insert into product values(null,'衛龍辣條',1,null,5);
insert into product values(null,'旺旺大餅',1,null,5);
- 多表之間的關係如何來維護
外鍵約束: foreign key
-給product中的cno 添加一個外鍵約束
alter table product add foreign key(cno) references category(cid);
-從分類表中,刪除分類爲5信息
delete from category where cid =5; //刪除失敗,
首先得去product表, 刪除所有分類ID5 商品
delete from product where cno=5;
- 建數據庫原則: 通常情況下,一個項目/應用建一個數據庫
- 多表之間的建表原則
- 一對多 : 商品和分類
建表原則: 在多的一方添加一個外鍵,指向一的一方的主鍵
- 多對多: 老師和學生, 學生和課程
建表原則: 建立一張中間表,將多對多的關係,拆分成一對多的關係,中間表至少要有兩個外鍵,分別指向原來的那兩張表。
- 一對一: 班級和班長, 公民和身份證, 國家和國旗
建表原則:
- 將一對一的情況,當作是一對多情況處理,在任意一張表添加一個外鍵,並且這個外鍵要唯一,指向另外一張表
- 直接將兩張表合併成一張表
- 將兩張表的主鍵建立起連接,讓兩張表裏面主鍵相等
- 實際用途: 用的不是很多. (拆表操作 )
- 相親網站:
- 個人信息 : 姓名,性別,年齡,身高,體重,三圍,興趣愛好,(年收入, 特長,學歷, 職業, 擇偶目標,要求)
- 拆表操作 : 將個人的常用信息和不常用信息,減少表的臃腫,
網上商城表案例分析:用戶購物流程
- 商品分類表(分類ID,分類名稱,分類描述)
create table category(
cid int primary key auto_increment,
cname varchar(15),
cdesc varchar(100)
);
insert into category values(null,'手機數碼','電子產品,黑馬生產');
insert into category values(null,'鞋靴箱包','江南皮鞋廠傾情打造');
insert into category values(null,'香菸酒水','黃鶴樓,茅臺,二鍋頭');
insert into category values(null,'酸奶餅乾','娃哈哈,蒙牛酸酸乳');
insert into category values(null,'饞嘴零食','瓜子花生,八寶粥,辣條');
- 商品表 (商品ID, 商品名稱,商品價格,外鍵cno)
create table product(
pid int primary key auto_increment,
pname varchar(10),
price double,
cno int,
foreign key(cno) references category(cid)
);
insert into product values(null,'小米mix4',998,1);
insert into product values(null,'錘子',2888,1);
insert into product values(null,'阿迪王',99,2);
insert into product values(null,'老村長',88,3);
insert into product values(null,'勁酒',35,3);
insert into product values(null,'小熊餅乾',1,4);
insert into product values(null,'衛龍辣條',1,5);
insert into product values(null,'旺旺大餅',1,5);
- 用戶表
create table user(
uid int primary key auto_increment,
username varchar(31),
password varchar(31),
phone varchar(11)
);
insert into user values(1,'zhangsan','123','13811118888');
- 訂單表 (訂單編號,總價,訂單時間 ,地址,外鍵用戶的ID)
create table orders(
oid int primary key auto_increment,
sum int not null,
otime timestamp,
address varchar(100),
uno int,
foreign key(uno) references user(uid)
);
insert into orders values(1,200,null,'學校',1);
insert into orders values(2,250,null,'家裏',1);
- 訂單項: 中間表(訂單ID,商品ID,商品數量,訂單項總價)
create table orderitem(
ono int,
pno int,
foreign key(ono) references orders(oid),
foreign key(pno) references product(pid),
ocount int,
subsum double
);
--給1號訂單添加商品 200塊錢的商品
insert into orderitem values(1,7,100,100);
insert into orderitem values(1,8,101,100);
--給2號訂單添加商品 250塊錢的商品 ()
insert into orderitem values(2,5,1,35);
insert into orderitem values(2,3,3,99);
- 內連接查詢
-隱式內連接
select * from product p,category c where p.cno=c.cid;
-顯式內連接
select * from product p inner join category c on p.cno=c.cid;
-區別:
隱式內連接:在查詢出結果的基礎上去做where 條件過濾
顯式內連接:帶着條件去查詢結果,執行效率高。
- 連接查詢
左外連接:會將左表中的所有數據都查詢出來,如果右表中沒有對應的數據,用NULL代替。
右外連接:會將右表中的所有數據查詢出來如果左表沒有對應數據的話 - 分頁查詢
每頁數據數據3,起始索引從0 ,第1頁: 0,第2頁: 3。
起始索引: index 代表顯示第幾頁 頁數從1開始,每頁顯示3條數據
startIndex = (index-1)*3
- 第一個參數是索引
第二個參數顯示的個數
select * from product limit 0,3;
select * from product limit 3,3;
- 子查詢
sql的嵌套:查詢語句裏面嵌套查詢語句