插入两个表的视图?
Insert into view of two tables?
我工作了很多w。 MySQL,但没有意见。我想做的在概念上很简单。有两个 table,创建一个组合它们的视图,并且 insert/update/delete 到像 table 的视图。类似于:
示例:
CREATE TABLE IF NOT EXISTS `baseuser` (
`id` int(11) NOT NULL,
`uname` varchar(55) NOT NULL,
`pwd` varchar(55) NOT NULL
) ENGINE=InnoDB ;
CREATE TABLE IF NOT EXISTS `facultydet` (
`id` int(11) NOT NULL,
`baseuser_id` int(11) NOT NULL,
`department` varchar(55) DEFAULT NULL
) ENGINE=InnoDB;
CREATE ALGORITHM = MERGE
VIEW `facultyuser`
AS SELECT `baseuser`.`id` AS `id`, `uname`, `pwd`, `department`
FROM `baseuser`, `facultydet`
WHERE `facultydet`.`baseuser_id` = `baseuser`.`id`;
上面创建了视图 'facultyuser',我可以更新它,但我不能从 'facultyuser' 视图中插入或删除。有没有办法直接用这样的视图来做到这一点?我在 VIEW 和 TABLE defs 上尝试了不同的变体,查看了 mysql 文档,但显然我遗漏了一些东西。任何建议表示赞赏。
谢谢,
克里斯
直接来自文档 18.4.3 Updatable and Insertable Views:
With respect to insertability (being updatable with INSERT
statements), an updatable view is insertable if it also satisfies
these additional requirements for the view columns:
There must be no duplicate view column names.
The view must contain all columns in the base table that do not have a
default value.
The view columns must be simple column references. They must not be
expressions or composite expressions
此外,您不能在联合视图中修改多个 table,因此您基本上不适合这种方法。
It is sometimes possible for a multiple-table view to be updatable,
assuming that it can be processed with the MERGE algorithm. For this
to work, the view must use an inner join (not an outer join or a
UNION). Also, only a single table in the view definition can be
updated
我工作了很多w。 MySQL,但没有意见。我想做的在概念上很简单。有两个 table,创建一个组合它们的视图,并且 insert/update/delete 到像 table 的视图。类似于:
示例:
CREATE TABLE IF NOT EXISTS `baseuser` (
`id` int(11) NOT NULL,
`uname` varchar(55) NOT NULL,
`pwd` varchar(55) NOT NULL
) ENGINE=InnoDB ;
CREATE TABLE IF NOT EXISTS `facultydet` (
`id` int(11) NOT NULL,
`baseuser_id` int(11) NOT NULL,
`department` varchar(55) DEFAULT NULL
) ENGINE=InnoDB;
CREATE ALGORITHM = MERGE
VIEW `facultyuser`
AS SELECT `baseuser`.`id` AS `id`, `uname`, `pwd`, `department`
FROM `baseuser`, `facultydet`
WHERE `facultydet`.`baseuser_id` = `baseuser`.`id`;
上面创建了视图 'facultyuser',我可以更新它,但我不能从 'facultyuser' 视图中插入或删除。有没有办法直接用这样的视图来做到这一点?我在 VIEW 和 TABLE defs 上尝试了不同的变体,查看了 mysql 文档,但显然我遗漏了一些东西。任何建议表示赞赏。
谢谢,
克里斯
直接来自文档 18.4.3 Updatable and Insertable Views:
With respect to insertability (being updatable with INSERT statements), an updatable view is insertable if it also satisfies these additional requirements for the view columns:
There must be no duplicate view column names.
The view must contain all columns in the base table that do not have a default value.
The view columns must be simple column references. They must not be expressions or composite expressions
此外,您不能在联合视图中修改多个 table,因此您基本上不适合这种方法。
It is sometimes possible for a multiple-table view to be updatable, assuming that it can be processed with the MERGE algorithm. For this to work, the view must use an inner join (not an outer join or a UNION). Also, only a single table in the view definition can be updated