当 Mahesh 建议您不能更改视图中的数据时,他并不完全正确。所以在帕特里克看来
CREATE View vw_user_profile AS
Select A.user_id, B.profile_description
FROM tbl_user A left join tbl_profile B on A.user_id = b.user_id
我可以更新数据...例如,我可以执行其中任何一项...
Update vw_user_profile Set profile_description='Manager' where user_id=4
或
Update tbl_profile Set profile_description='Manager' where user_id=4
您不能插入到该视图,因为并非所有表中的所有字段都存在,并且我假设 PROFILE_ID 是主键并且不能为 NULL。
但是,您有时可以插入到视图中...
我使用 ... 在现有表上创建了一个视图
Create View Junk as SELECT * from [TableName]
那么
Insert into junk (Code,name) values
('glyn','Glyn Roberts'),
('Mary','Maryann Roberts')
和
DELETE from Junk Where ID>4
INSERT 和 DELETE 在这种情况下都有效
显然,您无法更新任何聚合或计算的字段,但任何只是直接视图的视图都应该是可更新的。
如果视图包含多个表,则不能插入或删除,但如果视图只是一个表的子集,则通常可以。