關(guān)于視圖數(shù)據(jù)修改的結(jié)果
基本認(rèn)識(shí)
視圖:是一個(gè)或多個(gè)表依照某個(gè)條件組合而成的部分選中字段的結(jié)果集(一般情況下只是用來(lái)進(jìn)行select操作)
下面就單表視圖進(jìn)行測(cè)試:
準(zhǔn)備階段 :
1.學(xué)生表和分?jǐn)?shù)表
CREATE TABLE TbStudent (
stuid INTEGER NOT NULL,
name VARCHAR (20) NOT NULL,
sex bit DEFAULT 1,
birth datetime ,
tel CHAR (11),
addr VARCHAR (255),
PRIMARY KEY (stuid)
);
create table TbSC
(
scid integer primary key auto_increment,
sid integer,
scdate datetime,
score float
);
//學(xué)生信息數(shù)據(jù)插入
INSERT INTO `tbstudent` VALUES ('1', 'zhangsan', '0', null, '135', 'addr1');
INSERT INTO `tbstudent` VALUES ('2', 'lisi', '1', null, '136', 'addr2');
// 學(xué)生成績(jī)信息插入
INSERT INTO `tbsc` VALUES ('1', '1', null, '83');
INSERT INTO `tbsc` VALUES ('2', '2', null, '84');
INSERT INTO `tbsc` VALUES ('3', '1', null, '16');
2.視圖
create view tb_view as select name ,tel from tbstudent
- 新增外鍵(刪除之前的外鍵約束影響:alter table TbSC drop foreign key tbsc_ibfk_1)
alter table TbSC add foreign key (sid) references TbStudent (stuid)
4. 測(cè)試
4.1 測(cè)試修改關(guān)聯(lián)主鍵捧弃,設(shè)置級(jí)聯(lián)是可以修改的
update tb_view set name ='zhangsan' where tel='123'
update tb_stu_score set stuid='1' where name = 'you'
結(jié)論1:視圖中為純粹單表數(shù)據(jù)字段時(shí)赠叼,對(duì)視圖修改會(huì)對(duì)應(yīng)修改表中數(shù)據(jù)
結(jié)論2:級(jí)聯(lián)更新是可以的
4.2 測(cè)試函數(shù)數(shù)據(jù),不能修改
create view tb_stu_score1 as select stuid ,name ,sum(score) from tbstudent A,tbsc B where A.stuid = B.sid GROUP BY A.stuid
update tb_stu_score1 set name='you2' where stuid=3
報(bào)錯(cuò):The target table tb_stu_score1 of the UPDATE is not updatable
結(jié)論3:級(jí)當(dāng)視圖中存在使用函數(shù)得出的數(shù)據(jù)時(shí)违霞,則不能修改