MySQL multi CREATE TABLE syntax helper?

I am trying to write a MySQL script that creates multiple tables. I have:

CREATE TABLE `DataBase1`.`tbl_this`(
...
);
CREATE TABLE `DataBase1`.`tbl_that`(
...
);
... (14 more) ...

      

BUT, only the first statement is executed CREATE TABLE

. I am getting no syntax errors. Erm, is the MSSQL equivalent missing GO

? What am I doing wrong here? how can I get this child to run all tables?

+2


a source to share


5 answers


The create table syntax looks great. Probably the tool you are using to execute the SQL only executes the first statement.



+3


a source


How do you execute this script?

If you are trying to run it programmatically, you should be aware that the MySQL API only executes one statement at a time by default. You cannot group them together with a semicolon and expect it to run all instructions.

You can execute each statement CREATE TABLE

individually in a loop, otherwise you can run the script by supplying it as input to the command line client mysql

.



It is not as easy as it would seem to write a general purpose script runner class in your application, because the full script syntax includes many corner cases.

See the answer for loading .sql files from PHP for examples of corner cases .

+4


a source


try this:

use database_name;

create table a ..; create table b ..; create table c ..;

0


a source


Are the tables referencing (eg primary keys, etc.) to each other? Tables are created serially, so if your second table refers to a table that hasn't been created yet, it will fail.

0


a source


How do you execute your script? If you are doing it from the command line, it should be something like this:

mysql -u[username] -p[password] --database DataBase1 < scriptname.sql

      

0


a source







All Articles