【发布时间】:2021-05-20 08:48:05
【问题描述】:
脚本创建 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 中创建所需的表,而不是仅仅编写创建表的命令。
谁能帮帮我!
标志
【问题讨论】:
-
CREATE TABLE if does not exists - 多余的单词,会导致错误。
-
这能回答你的问题吗? Is it possible to execute a string in MySQL?
标签: mysql create-table