日期:2014-05-16 浏览次数:20423 次
视图的作用:
1、可以公开表中特定的行或列。
通过限制用户对实际表的 SELECT 操作权限,而仅仅赋予用户对相应视图的 SELECT 操作权限,
来达到限制用户只能读取特定行或列的目的。
2、简化复杂的 SQL 语句
3、可以限制可插入/ 更新的 范围。使用 WITH CHECK POINT
CREATE VIEW v_order( oid, odate, memo, uid, zip, address, name, pid, pname, price, quantity) AS SELECT ob.oid, ob.odate, ob.memo, u.uid, u.zip, u.address, u.name, p.pid, p.pname, p.price, od.quantity FROM ( ( ( order_basic AS ob INNER JOIN order_detail AS od ON ob.oid = od.oid ) INNER JOIN product AS p ON od.pid = p.pid ) INNER JOIN user AS u ON ob.uid = u.uid );
CREATE OR REPLACE VIEW v_order AS SELECT ob.oid AS oid, ob.odate AS odate, ob.memo AS memo, u.uid AS uid, u.zip AS zip, u.address AS address, u.name AS name, p.pid AS pid, p.pname AS pname, p.price AS price, od.quantity AS quantity FROM ( ( ( order_basic AS ob INNER JOIN order_detail AS od ON ob.oid = od.oid ) INNER JOIN product AS p ON od.pid = p.pid ) INNER JOIN user AS u ON ob.uid = u.uid );
DROP VIEW v_order;
SHOW TABLES; SHOW TABLES LIKE 'v\_%'; SHOW FIELDS FROM v_order;
SELECT * FROM v_order WHERE oid = 'D00001'\G UPDATE product SET pname = '灯罩' WHERE pid = 'P0002'; COMMIT;
使用视图,进行变更数据(INSERT / UPDATE / DELETE)时,以下情况,会导致操作失败:
1、视图中的列,含有统计函数
2、视图定义时,使用了 GROUP BY / HAVING 语句,DISTINCT 语句,UNION 语句
3、视图定义时,使用了子查询
4、跨越多个基础表,进行数据变更
创建视图时,使用 WITH CHECK OPTION 命令
CREATE VIEW v_product3000up AS SELECT * FROM product WHERE price >= 3000 WITH CHECK OPTION;此时,如果,对 视图 v_product3000up 插入的记录中 price < 3000, 那么会插入失败
它能简化复杂的 SELECT 语句,但是,不意味着简化了内部的处理过程。
慎重使用!