mysql - 无法通过 mysql 中具有函数的 View 创建表

标签 mysql select view insert

我创建了两个表

CREATE TABLE `prova` (
`id`  int NOT NULL AUTO_INCREMENT ,
`text`  varchar(255) NOT NULL ,
PRIMARY KEY (`id`)
)
;

CREATE TABLE `prova2` (
`id2`  int NOT NULL AUTO_INCREMENT ,
`text2`  varchar(255) NOT NULL ,
PRIMARY KEY (`id2`)
)
;

insert into prova (text) values ('ffffff');

仅当变量 @test 的值设置为 0 时,函数才对表一进行选择并在表二中插入一行:

CREATE FUNCTION `get_prova`()
 RETURNS int(11)
BEGIN

declare id_prova int ;
declare test int ;


    set @test = 1;
    set @id_prova = (select id from prova limit 1);

    if (@test = 0) THEN
        insert into prova2 (text2) values ('dddd');
    end if;

return @id_prova;

END;

然后,我创建一个调用此函数的 View :

create view temp_prova as 
select  id,
            text, 
            get_prova() as prova
from prova

我想创建包含 View 结果的表 3:

CREATE TABLE zzz_prova  SELECT * FROM temp_prova;

但是当我尝试创建表 zzz_prova 时出现此错误:

[SQL]CREATE TABLE zzz_prova SELECT * FROM temp_prova; [Err] 1746 - Can't update table 'prova2' while 'zzz_prova' is being created.

为什么会出现这个错误? 谢谢

最佳答案

您运行的是什么版本的 MySQL?

Changes in MySQL 5.6.2 (2011-04-11)

  • Incompatible Change; Replication: It is no longer possible to issue a CREATE TABLE ... SELECT statement which changes any tables other than the table being created. Any such statement is not executed and instead fails with an error.

    One consequence of this change is that FOR UPDATE may no longer be used at all with the SELECT portion of a CREATE TABLE ... SELECT.

    This means that, prior to upgrading from a previous release, you should rewrite any CREATE TABLE ... SELECT statements that cause changes in other tables so that the statements no longer do so.

    This change also has implications for statement-based replication between a MySQL 5.6 (or later slave) and a master running a previous version of MySQL. In such a case, if a CREATE TABLE ... SELECT statement on the master that causes changes in other tables succeeds on the master, the statement nonetheless fails on the slave, causing replication to stop. To keep this from happening, you should either use row-based replication, or rewrite the offending statement before running it on the master. (Bug #11749792, Bug #11745361, Bug #39804, Bug #55876)

    References: See also Bug #47899.

更新

MySQL 5.5:

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.5.47    |
+-----------+
1 row in set (0.00 sec)

mysql> DROP FUNCTION IF EXISTS `f`;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS `t1`;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS `t2`;
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER |

mysql> CREATE FUNCTION `f`()
    -> RETURNS INT
    -> BEGIN
    ->     INSERT INTO `t2` VALUES (1);
    ->     RETURN 1;
    -> END|
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

mysql> CREATE TABLE `t2`(`c1` INT);
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE `t1` SELECT `f`() `c1`;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> SELECT `c1` FROM `t1`;
+------+
| c1   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> SELECT `c1` FROM `t2`;
+------+
| c1   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

MySQL 5.6:

mysql> SELECT VERSION();
+-----------------+
| VERSION()       |
+-----------------+
| 5.6.25          |
+-----------------+
1 row in set (0.00 sec)

mysql> DROP FUNCTION IF EXISTS `f`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> DROP TABLE IF EXISTS `t1`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> DROP TABLE IF EXISTS `t2`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> DELIMITER |

mysql> CREATE FUNCTION `f`()
    -> RETURNS INT
    -> BEGIN
    ->     INSERT INTO `t2` VALUES (1);
    ->     RETURN 1;
    -> END|
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

mysql> CREATE TABLE `t2`(`c1` INT);
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE `t1` SELECT `f`() `c1`;
ERROR 1746 (HY000): Can't update table 't2' while 't1' is being created.

关于mysql - 无法通过 mysql 中具有函数的 View 创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35033959/

相关文章:

mysql - 不想要在 MySQL 中减法的负结果

php - 从mysql上的两个表中选择

java - Android 中的拖放 View

Spring Boot MVC,没有返回我的观点

database - VIEW 与 SQL 语句的性能

php - 使用多个 php 函数查询单个(mysql)数据库的正确方法

MySQL - 使用左连接获取未分配资源的复杂查询

mysql - 具有条件限制和顺序的cakephp查询

jQuery 克隆链接选择

select - jq select 过滤器的意外操作