Home > Database > Mysql Tutorial > How to update view in mysql

How to update view in mysql

PHPz
Release: 2023-05-26 11:52:56
forward
2564 people have browsed it

1. Some views are not updatable because updates to these views cannot be uniquely and meaningfully converted to the corresponding basic tables.

2. Generally speaking, rows and rows subset views can be updated. In addition to column subset views, it is theoretically possible to update some views.

Example

-- 创建视图 ldq_t1
CREATE VIEW ldq_t1 AS
SELECT
  *
FROM
  t3
WHERE id1 > 10 WITH CHECK OPTION ;
-- 查询ldq_t1中的所有结果
SELECT * FROM ldq_t1;
 
-- 创建视图 ldq_t2
CREATE VIEW ldq_t2 AS
SELECT
  *
FROM
  ldq_t1
WHERE id1 < 30 WITH LOCAL CHECK OPTION ;
 
-- 创建视图 ldq_t3
CREATE VIEW ldq_t3 AS
SELECT
  *
FROM
  ldq_t1
WHERE id1 < 30  WITH  CHECK OPTION ;
 
-- 更新视图ldq_t2(只有ldq_t2中存在的数据都可以更新)
SELECT * FROM ldq_t2; -- 查看ldq_t2当前记录
UPDATE ldq_t2 SET id1=5 WHERE id2=22;  -- 可以执行成功
UPDATE ldq_t2 SET id1=35 WHERE id2=22;  -- 将会报错CHECK OPTION failed(因为执行该语句之后,id2=22记录将从ldq_t2消失)
UPDATE ldq_t2 SET id1=28 WHERE id2=22;  -- 可以执行成功
 
 
-- 更新ldq_t3
SELECT * FROM ldq_t3;
UPDATE ldq_t3 SET id1=5 WHERE id2=22;  -- 将会报错CHECK OPTION failed(因为数据更新之后,必须还要保证其仍然在ldq_t3和ldq_t1之中,该语句执行后id2=22记录将从ldq_t1消失)
UPDATE ldq_t3 SET id1=15 WHERE id2=22; -- 能够执行成功
UPDATE ldq_t3 SET id1=35 WHERE id2=22; -- 将会报错CHECK OPTION failed(因为执行该语句之后,id2=22记录将从ldq_t3消失)
DELETE FROM  ldq_t3 WHERE id2=22;  -- 执行成功
Copy after login

The above is the detailed content of How to update view in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template