是否可以将数据插入到 MySQL 视图中?

发布于 2024-09-25 22:53:52 字数 63 浏览 4 评论 0原文

我制作了一个包含 4 个表的 MySQL 视图。是否可以将数据插入视图并让MySQL自动将数据传递到正确的表中?

I made a MySQL view with 4 tables. Is it possible to insert data into the view and have MySQL automatically pass the data into the right table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷子口的你 2024-10-02 22:53:52

如果您使用内部联接,并且您的视图包含基表中的所有列,那么您的视图可能是可更新的。但是,对于多表可更新视图,如果插入到单个表中,INSERT 就可以工作。您可以将插入操作拆分为多个 INSERT 语句。

您可能需要查看以下文章以获取有关该主题的更多信息:

考虑以下示例:

CREATE TABLE table_a (id int, value int);
CREATE TABLE table_b (id int, ta_id int, value int);

INSERT INTO table_a VALUES (1, 10);
INSERT INTO table_a VALUES (2, 20);
INSERT INTO table_a VALUES (3, 30);

INSERT INTO table_b VALUES (1, 1, 100);
INSERT INTO table_b VALUES (2, 1, 200);
INSERT INTO table_b VALUES (3, 2, 300);
INSERT INTO table_b VALUES (4, 2, 400);

现在让我们创建一个视图:

CREATE VIEW v AS 
    SELECT      a.id a_id, b.id b_id, b.ta_id, a.value v1, b.value v2
    FROM        table_a a
    INNER JOIN  table_b b ON (b.ta_id = a.id);

SELECT * FROM v;
+------+------+-------+------+------+
| a_id | b_id | ta_id | v1   | v2   |
+------+------+-------+------+------+
|    1 |    1 |     1 |   10 |  100 |
|    1 |    2 |     1 |   10 |  200 |
|    2 |    3 |     2 |   20 |  300 |
|    2 |    4 |     2 |   20 |  400 |
+------+------+-------+------+------+
4 rows in set (0.00 sec)

以下INSERT失败:

INSERT INTO v (a_id, b_id, ta_id, v1, v2) VALUES (3, 5, 3, 30, 500);
-- ERROR 1393 (HY000): Can not modify more than one base table through a join view 

但我们可以将其拆分为两个操作:

INSERT INTO v (a_id, v1) VALUES (3, 30);
-- Query OK, 1 row affected (0.00 sec)
INSERT INTO v (b_id, ta_id, v2) VALUES (5, 3, 500);
-- Query OK, 1 row affected (0.00 sec)

结果:

SELECT * FROM v;
+------+------+-------+------+------+
| a_id | b_id | ta_id | v1   | v2   |
+------+------+-------+------+------+
|    1 |    1 |     1 |   10 |  100 |
|    1 |    2 |     1 |   10 |  200 |
|    2 |    3 |     2 |   20 |  300 |
|    2 |    4 |     2 |   20 |  400 |
|    3 |    5 |     3 |   30 |  500 |
+------+------+-------+------+------+
6 rows in set (0.00 sec)

If you are using inner joins, and your view contains all the columns in the base tables, then your view might be updatable. However, for a multiple-table updatable view, INSERT can work if it inserts into a single table. You could split your insert operation into multiple INSERT statements.

You may want to check out the following article for more information on the topic:

Consider the following example:

CREATE TABLE table_a (id int, value int);
CREATE TABLE table_b (id int, ta_id int, value int);

INSERT INTO table_a VALUES (1, 10);
INSERT INTO table_a VALUES (2, 20);
INSERT INTO table_a VALUES (3, 30);

INSERT INTO table_b VALUES (1, 1, 100);
INSERT INTO table_b VALUES (2, 1, 200);
INSERT INTO table_b VALUES (3, 2, 300);
INSERT INTO table_b VALUES (4, 2, 400);

Now let's create a view:

CREATE VIEW v AS 
    SELECT      a.id a_id, b.id b_id, b.ta_id, a.value v1, b.value v2
    FROM        table_a a
    INNER JOIN  table_b b ON (b.ta_id = a.id);

SELECT * FROM v;
+------+------+-------+------+------+
| a_id | b_id | ta_id | v1   | v2   |
+------+------+-------+------+------+
|    1 |    1 |     1 |   10 |  100 |
|    1 |    2 |     1 |   10 |  200 |
|    2 |    3 |     2 |   20 |  300 |
|    2 |    4 |     2 |   20 |  400 |
+------+------+-------+------+------+
4 rows in set (0.00 sec)

The following INSERT fails:

INSERT INTO v (a_id, b_id, ta_id, v1, v2) VALUES (3, 5, 3, 30, 500);
-- ERROR 1393 (HY000): Can not modify more than one base table through a join view 

But we can split it into two operations:

INSERT INTO v (a_id, v1) VALUES (3, 30);
-- Query OK, 1 row affected (0.00 sec)
INSERT INTO v (b_id, ta_id, v2) VALUES (5, 3, 500);
-- Query OK, 1 row affected (0.00 sec)

Result:

SELECT * FROM v;
+------+------+-------+------+------+
| a_id | b_id | ta_id | v1   | v2   |
+------+------+-------+------+------+
|    1 |    1 |     1 |   10 |  100 |
|    1 |    2 |     1 |   10 |  200 |
|    2 |    3 |     2 |   20 |  300 |
|    2 |    4 |     2 |   20 |  400 |
|    3 |    5 |     3 |   30 |  500 |
+------+------+-------+------+------+
6 rows in set (0.00 sec)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文