视图:视图其实就是一组表的映射。
赋予用户创建视图的权限:grant create view to scott;
创建视图:create view v_customer as select account, realname,address from tbl_customer;
列的重命名: create or replace view v_cust as select account,realname||gender as aa from tbl_customer;
create or replace view v_customer_two(account,fulladdress) as
select account,address||','||zipcode from tbl_customer;
在视图中执行更新,删除,插入操作:
update v_cust_order set totalp=totalp+1 where account='guest';
delete from v_category where cateid=4;
insert into v_category(cateid,catename,remark) values(4,'fashion','about fashion...');
commit;
在视图中修改数据时的几个问题:
1、对于多表视图不能使用DELETE语句。
2、除非底层表的所有非空列都已经在视图中出现,否则你不能使用INSERT语句。
3、如果你在视图中使用了DISTINCT语句,那么就不能插入或更新这个视图中的记录。
4、不能更新视图中的虚拟列(它是用计算字段得到的)。
视图的使用场景:
1、在单位换算中使用视图
视图在你提供给用户的数据与数据库中的真实数据不同时相当有用。
例子:AMOUNT存储的是美元,加拿大的用户不想频繁地进行美元与加拿大元之间的转换工作。那么你可以创建如下视图:
CREATE VIEW CANADIAN_BILLS(NAME,CAN)AMOUNT) AS
SELECT NAME,AMOUNT/1.10
FROM BILLS;