外鍵約束、視圖:單表+多表

外鍵約束

有三種約束模式: ? ? ? ??

district:嚴(yán)格模式(默認(rèn)的)—— 都是針對(duì)父表的 ????????

cascade:級(jí)聯(lián)模式 ????????

set null:置空模式

視圖:單表+多表

create view my_v1as select *? from my_student;

create view my_v2as select *? from my_class;

create view my_v3as select *? from my_studentas sleft join my_classas con s.c_id=c.id;-- 錯(cuò)誤:id重復(fù)

-- 多表視圖

create view my_v3as select s.*,c.name as c_name,c.room from my_studentas sjoin my_classas con s.c_id=c.id;

show tables;

-- 查看視圖創(chuàng)建語句

show create view my_v3\G

-- 視圖的使用

select *? from my_v1;

select *? from my_v2;

select *? from my_v3;

-- 修改視圖

alter view my_v1as select id,name,age,sex,height,c_idfrom my_student;

-- 刪除視圖

create view my_v4as select * from my_student;

drop view my_v4;

-- 多表視圖插入數(shù)據(jù) : 不能插入數(shù)據(jù)

insert into my_v3values (null,'bc20200008','張三豐','男',150,180,3,'python1910','a204');

-- 將學(xué)生表的學(xué)號(hào)字段設(shè)置成不能為空

alter table my_studentmodify number char(10)not null unique ;

-- 單表視圖插入數(shù)據(jù): 視圖不包括所有不允許為空的字段

insert into my_v1values (null,'bc20200008','張三豐',150,'男',180,3);

-- 單表視圖插入數(shù)據(jù)

insert into my_v2values (2,'python1903','a204');

-- 多表視圖刪除數(shù)據(jù): 不能刪除數(shù)據(jù)

delete from mydb.my_v3where id=1;

-- 單表視圖刪除數(shù)據(jù)

delete from my_v2where id=4;

-- 多表視圖更新數(shù)據(jù)

update my_v3set c_id=4 where id=6;

-- 視圖: age 字段限制更新

create view my_v4as select * from my_studentwhere age>30 with check option;

-- 表示視圖的數(shù)據(jù)來源都是年齡大于30歲的,是由where age>30 決定的

-- with check option決定通過視力更新的時(shí)候,不能將已經(jīng)得到的數(shù)據(jù)age>30的改成<30的

-- 將視圖可以查看到的數(shù)據(jù)改成年齡小于30

update my_v4set age=29 where id=5;

-- 可以修改數(shù)據(jù)讓視圖可以查到: 可以改,但是沒有效果

update my_v4set age=32 where id=3;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。