給product中的這個cno添加一個外鍵約束 alter table product add foreign key(cno) references category(cid);
再插入cid不匹配的數據會失敗
從分類表中刪除分類爲5的信息 delete from category where cid=4;//刪除失敗 首先得去product表中刪除分類id爲4的商品
一般狀況下,一個項目應用於建一個數據庫
一對多:商品和分類git
建表原則:在多的一方添加一個外鍵指向一的一方的主鍵
多對多:老師和學生,學生和課程數據庫
建表原則:多建一張中間表,將多對多的關係拆成一對多的關係,中間至少要有兩個外鍵,這兩個外鍵分別指向原來的那張表
一對一網站
建表原則: 將一對一的狀況,看成是1對多的狀況處理,在任意一張表添加一個外鍵,而且這個外鍵惟一,指向另外一張表 直接將兩張表合併成一張表 將兩張表主鍵創建起連接,讓兩張表裏面主鍵相等 實際用途:用的不是不少(拆表操做) 相親網站: 我的信息:姓名,性別,年齡,身高,體重,三維,興趣愛好(年收入、特長、學歷、職業、擇偶目標) 拆表操做:將我的信息的經常使用信息和不經常使用信息拆分出來,減小表的臃腫
用戶表(用戶的ID,用戶名,密碼,手機)ui
create table user( uid int primary key auto_increment, username varchar(31), password varchar(31), phone varchar(11) ); insert into user values(1,'Alsa','123','15822224444');
訂單表(訂單編號,總價,訂單時間,地址,外鍵用戶的ID)3d
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,'jingsu',1); insert into orders values(2,1176,null,'lianyungang',1);
商品表(商品ID,商品名稱,商品價格,外鍵cno)[先建商品分類]code
create table product( pid int primary key auto_increment, pname varchar(30), price double, cno int, foreign key(cno) references category(cid) ); insert into product values(null,'Wuliangye',998,4); insert into product values(null,'Cocktail',6,4); insert into product values(null,'Jenny Bakery',5,3); insert into product values(null,'Huawei Glory v10',2000,5); insert into product values(null,'Dress',168,1); insert into product values(null,'High-heeled shoes',400,2); insert into product values(null,'Sandals',100,2); insert into product values(null,'Stewed Chicken Point',42,3); insert into product values(null,'Mousse Cake',58,3); insert into product values(null,'Samsung S10',5000,5); insert into product values(null,'shirt',120,1);
訂單項:中間表(訂單ID,商品ID,商品數量,訂單項總價)blog
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,1,100); insert into orderitem values(1,9,1,58); insert into orderitem values(1,8,1,42); 給2號訂單添加1176元商品 insert into orderitem values(2,1,1,998); insert into orderitem values(2,3,2,10); insert into orderitem values(1,5,1,168);
商品分類表(分類ID,分類名稱,分類描述)索引
create table category( cid int primary key auto_increment, cname varchar(15), cdesc varchar(100) ); insert into category values(null,'clothes','Clothes that can not be trimmed are not good fabrics'); insert into category values(null,'shoes','Walking in all directions begins at the foot'); insert into category values(null,'food','Make a tasteful meal'); insert into category values(null,'Beverages','A little tightening makes life better'); insert into category values(null,'Digital','Look at the world without going out');
主鍵約束:默認是指向另外一張表的主鍵ci
外鍵都是指向另一張表的主鍵 主鍵一張表只能有一個
惟一約束:列裏面的內容,必須是惟一,不能重複出現重複,能夠爲空rem
惟一約束不能做爲其餘表的外鍵 能夠有多個惟一約束
笛卡爾積,查出來是兩張表的乘積結果沒有意義 select * from product,category;
過濾出有意義的數據 select * from product,category where cno=cid; select * from product as p,category as c where p.cno=c.cid; select * from product p,category c where p.cno=c.cid;
隱式內鏈接 select * from product p,category c where p.cno=c.cid; 顯式內鏈接(inner可省略) select * from product p inner join category c on p.cno=c.cid; 區別: 隱式內鏈接是在查詢出結果的基礎上去作的where條件過濾 顯示內鏈接是帶着條件去查詢結果,執行效率要高。
數據準備:在product中插入一個沒有對應cid的數據
insert into product values(null,'flowers',40,null);
select * from product p left outer join category c on p.cno=c.cid;
數據準備:在category中插入一行數據
insert into category values(100,'birds','Birds twitter and fragrance of flowers');
select * from product p right outer join category c on p.cno=c.cid;
每頁數據10條,起始索引1
select * from product limit 0,3; select * from product limit 3,3; 第一個參數是數據索引,第二個參數是顯示的個數 起始索引:index表明顯示第幾頁,頁數從1開始 每頁顯示3條數據 每頁開始的數據索引startindex=(index-1)*3
查詢出(商品名稱,商品分類名稱)信息
左鏈接(outer可省略) select p.pname,c.cname from product p left outer join category c on p.cno=c.cid; 子查詢 select pname,(select cname from category c where p.cno = c.cid) from product p;
查詢出分類名稱爲手機數碼的全部商品
select cid from category where cname='Beverages'; select * from product where cno = (select cid from category where cname='Beverages');