建一个视图可以访问usermajor表和study_record表中两个字段
SELECT * FROM usermajor;
SELECT * FROM study_record
CREATE VIEW user_view AS
SELECT a.username as uname,b.course_id as course
FROM usermajor a,study_record b
WHERE a.userid=b.user_id
新建一个对视图插数据然后往表中分别插数据的触发器
CREATE TRIGGER users_insert ON user_view
INSTEAD OF INSERT
as
declare @name nvarchar(32),@course int;
declare ins_cursor cursor
for
select uname,course from inserted
open ins_cursor
fetch next from ins_cursor into @name,@course;
while(@@fetch_status = 0)
begin
--读取所有行,并插入
insert into usermajor(username) values (@name);
insert into study_record(course_id) values(@course);
fetch next from ins_cursor into @name,@course;
end
close ins_cursor
insert into user_view(uname,course) values('袁愈明',NULL);
insert into user_view(uname,course) values('王娜',NULL);
对物化视图的修改间接对相关表进行了修改;