建一個視圖能夠訪問usermajor表和study_record表中兩個字段sql
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
新建一個對視圖插數據而後往表中分別插數據的觸發器
fetch
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);spa
對物化視圖的修改間接對相關表進行了修改;code