开发者

How to create VIEWs so they alter too when base table is ALTERed?

Is there a way to link two tables, so when i alter base table, view will altered too? Something like that:

CREATE TABLE t (qty INT, price INT);

INSERT INTO t VALUES(3, 50);

CREATE VIEW v AS SELECT * FROM t;

SELECT * FROM v;
+------+-------+
| qty  | price |
+------+-------+
|    3 |    50 |
+------+-------+

ALTER TABLE t ADD COLUMN comm INT;

SELECT * FROM t;
+------+-------+------+
| qty  | price | comm |
+------+-------+------+
|    3 |    50 | NULL |
+------+-------+------+

SELECT *开发者_运维知识库 FROM v;
+------+-------+
| qty  | price |
+------+-------+
|    3 |    50 |
+------+-------+

Last two SELECT-s should be equal.

PS. I am aware that MySQL says:

The view definition is “frozen” at creation time, so changes to the underlying tables afterward do not affect the view definition.

And creating trigger is also not possible, because trigger events does not include ALTER TABLE


You need to recreate view when you alter table as stated in manual

The view definition is “frozen” at creation time, so changes to the underlying tables afterward do not affect the view definition. For example, if a view is defined as SELECT * on a table, new columns added to the table later do not become part of the view.

Either drop & recreate view or ALTER view too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜