我在删除中没有看到任何歧义。DELETE FROM aView ;
应该删除(如果允许)视图中基础表中的所有行。
CREATE TABLE Student
( sid INT NOT NULL
, name VARCHAR(20) NOT NULL
, PRIMARY KEY (sid)
) ;
INSERT INTO Student
(sid, name)
VALUES
(1, 'Alex'),
(2, 'Bill'),
(3, 'Cate'),
(4, 'Dean'),
(5, 'Eve'),
(6, 'Alex'),
(7, 'Bill'),
(8, 'Cate') ;
CREATE VIEW NonUnique AS
SELECT *
FROM Student S1
WHERE EXISTS
( SELECT *
FROM Student S2
WHERE S1.sID <> S2.sID
AND S2.name = S1.name
) ;
DELETE FROM NonUnique ;
它在已DELETE
正确实现的 SQL 产品中运行良好。请参阅SQL-Server和Oracle的 SQL-Fiddles :
SELECT * FROM Student ;
结果:
sid | name
----------
4 | Dean
5 | Eve
MySQL提供错误:The target table NonUnique of the DELETE is not updatable
Postgres 给出:ERROR: cannot delete from view "nonunique":
如果你在 MySQL 中尝试这个:
DELETE s
FROM Student AS s
JOIN
NonUnique AS n
ON n.sid = s.sid ;
或这个:
DELETE
FROM Student
WHERE EXISTS
( SELECT *
FROM NonUnique
WHERE NonUnique.sid = Student.sid
) ;
错误是:The definition of table 'NonUnique' prevents operation DELETE on table 'Student'.
然而,在 Postgres 中,第二条语句成功并删除了正确的行。
只有当您设法将视图隐藏在派生表中时,MySQL才会成功:
DELETE s
FROM Student AS s
JOIN
( SELECT *
FROM NonUnique
) AS n
ON n.sid = s.sid ;