Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL脚本问题_Mysql_Create Table_Script - Fatal编程技术网

MySQL脚本问题

MySQL脚本问题,mysql,create-table,script,Mysql,Create Table,Script,脚本创建sql命令,以便在目标数据库中不存在表的情况下,将表从一个数据库复制到另一个数据库。该查询检查该表是否不存在于sakila1中,并且它是否存在于sakila中,然后应创建该表 在mysql上运行时,命令提示下面的查询 SELECT concat('CREATE TABLE if does not exists sakila1.', TABLE_NAME, ' like sakila.', TABLE_NAME, ';') FROM information_schema.`TABLES`

脚本创建sql命令,以便在目标数据库中不存在表的情况下,将表从一个数据库复制到另一个数据库。该查询检查该表是否不存在于sakila1中,并且它是否存在于sakila中,然后应创建该表

在mysql上运行时,命令提示下面的查询

SELECT concat('CREATE TABLE if does not exists sakila1.', TABLE_NAME, ' like sakila.', TABLE_NAME, ';') 
FROM information_schema.`TABLES` 
WHERE TABLE_SCHEMA = 'sakila'
它只编写以下sql命令

CREATE TABLE if does not exists sakila1.actor like sakila.actor;                      |

| CREATE TABLE if does not exists sakila1.actor_info like sakila.actor_info;                 |

| CREATE TABLE if does not exists sakila1.address like sakila.address;                    |

| CREATE TABLE if does not exists sakila1.category like sakila.category;                   |

| CREATE TABLE if does not exists sakila1.city like sakila.city;                       |

| CREATE TABLE if does not exists sakila1.country like sakila.country;                    |

| CREATE TABLE if does not exists sakila1.customer like sakila.customer;                   |

| CREATE TABLE if does not exists sakila1.customer_list like sakila.customer_list;              |

| CREATE TABLE if does not exists sakila1.film like sakila.film;                       |

| CREATE TABLE if does not exists sakila1.film_actor like sakila.film_actor;                 |

| CREATE TABLE if does not exists sakila1.film_category like sakila.film_category;              |

| CREATE TABLE if does not exists sakila1.film_list like sakila.film_list;                  |

| CREATE TABLE if does not exists sakila1.film_text like sakila.film_text;                  |

| CREATE TABLE if does not exists sakila1.inventory like sakila.inventory;                  |

| CREATE TABLE if does not exists sakila1.language like sakila.language;                   |

| CREATE TABLE if does not exists sakila1.nicer_but_slower_film_list like sakila.nicer_but_slower_film_list; |

| CREATE TABLE if does not exists sakila1.payment like sakila.payment;                    |

| CREATE TABLE if does not exists sakila1.rental like sakila.rental;                     |

| CREATE TABLE if does not exists sakila1.sales_by_film_category like sakila.sales_by_film_category;     |

| CREATE TABLE if does not exists sakila1.sales_by_store like sakila.sales_by_store;             |

| CREATE TABLE if does not exists sakila1.staff like sakila.staff;                      |

| CREATE TABLE if does not exists sakila1.staff_list like sakila.staff_list;                 |

| CREATE TABLE if does not exists sakila1.store like sakila.store;                      |

+------------------------------------------------------------------------------------------------------------+

23 rows in set (0.00 sec)
SQL命令不会执行,而只是显示为SQL命令

我需要在sakila1中通过运行上述查询来创建所需的表,而不仅仅是编写创建表的命令

谁能帮帮我


Flag

CREATE TABLE if not exists-多余的单词将导致错误。这是否回答了您的问题?
CREATE FUNCTION fn_table_exists(dbName VARCHAR(255), tableName VARCHAR(255))
  RETURNS BOOLEAN
  BEGIN
    DECLARE totalTablesCount INT DEFAULT (
      SELECT COUNT(*)
      FROM information_schema.TABLES
      WHERE (TABLE_SCHEMA COLLATE utf8_general_ci = dbName COLLATE utf8_general_ci)
        AND (TABLE_NAME COLLATE utf8_general_ci = tableName COLLATE utf8_general_ci)
    );
    RETURN IF(
      totalTablesCount > 0,
      TRUE,
      FALSE
    );
END
;


Set @query:=''
SELECT @query:=@query+concat('IF fn_table_exists(sakila1,',TABLE_NAME,')=FALSE THEN CREATE TABLE sakila1.', TABLE_NAME, ';') FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'sakila'
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;