MySQL multi CREATE TABLE syntax help? - mysql

I'm trying to write a MySQL script that creates several tables. I have:
CREATE TABLE `DataBase1`.`tbl_this`(
...
);
CREATE TABLE `DataBase1`.`tbl_that`(
...
);
... (14 more) ...
BUT, only the first CREATE TABLE statement is executed. I get no syntax errors. Erm, am I missing the MSSQL equivalent of GO ? What am I doing wrong here; how do I get this baby to run all the tables?

How are you executing this script?
If you are trying to run it programmatically, you should know that the MySQL API only executes one statement at a time by default. You can't string them together with semicolons and expect it to run all the statements.
You can execute each CREATE TABLE statement individually in a loop, or else you can run a script by feeding it as input to the mysql command-line client.
It's not as easy as it would seem to write a general-purpose script runner class in your application, because the full script syntax include many corner cases.
See examples of the corner cases in my answer to Loading .sql files from within PHP.

The create table syntax looks fine. Probably the tool you use to execute your SQL just executes the first statement.

try this:
use database_name;
create table a..;
create table b..;
create table c..;

Are the tables referencing (e.g. primary keys and the like) one another? Tables are created serially, so if your second table is referencing a table that is not yet created, it will fail.

How do you execute your script ?
If you do it from command line it should be something like this:
mysql -u[username] -p[password] --database DataBase1 < scriptname.sql

Related

Mysql drop table/create table sequence gives strange error

This situation makes no sense.
I have the following sequence of SQL operations in my php code:
DROP TABLE IF EXISTS tablename;
CREATE TABLE tablename;
Of course the php code does not look like that, but those are the commands being executed.
Every once in a while on the CREATE statement, the system returns "table already exists".
I would not think this could happen, unless it is some kind of delay in the dropping. The table is Innodb and I read that there could be processes using the table. However, the tablename has embedded within it a session_id for the user, because this table is somewhat transient and is dedicated to the specific user only--no other user can be using the table, and not even any other script can be using it. It is a "user-specific, script-specific" table. However, it is possible that the user could execute this script, go away to a different script, then come back to this script.
The describe code is in a routine that decides whether it can re-use the table, or whether it has to be recreated. If it has to be recreated, then the two lines execute.
Any ideas what is causing this error condition?
EDIT:
The problem with "actual code" is that sometimes it just leads to more questions that diverge from the actual point. Neverthess, here is a copy from the actual script:
$query1 = "DROP TABLE IF EXISTS {$_SESSION['tmpContact']}";
SQL($query1);
$memory_table = "CREATE TABLE {$_SESSION['tmpContact']}";
The SQL() function executes the command and has error handling.
Plan A: Check for errors after the DROP. There may be a clue there.
Plan B: CREATE TEMPORARY TABLE ... -- That will be local to the connection, so [presumably] you won't need the DROP.
$a = mysql_query("SELECT TABLE");
if($a != ''){}else{}
try mixing the php with the sql.

Exit MySQL script if database exists

I have a MySQL script that I want to use only if the database doesn't exist to inject some initial demo data for development. If it does exist I just want to break out of the script. The script starts like
CREATE DATABASE IF NOT EXISTS `demo-database`;
USE `demo-database`;
Is there a way to exit here or above the create database if the database exists so that it wont run through all the table setups and inserts?
Try this. Use INFORMATION_SCHEMA.SCHEMATA to check the existence of Database
If not exists (SELECT 1
FROM INFORMATION_SCHEMA.SCHEMATA
WHERE SCHEMA_NAME = 'demo-database')
CREATE DATABASE `demo-database`
.....
What I believe now is that the construct with an if/else/endif can only be used in a stored program (see also https://dev.mysql.com/doc/refman/8.0/en/if.html)
I do not have a workaround other than creating a stored procedure that contains the code. In my situation I also have code I would to execute only on a test environment. I am creating only the stored procedure on test then as well.

MySQL SHOW CREATE TABLE parameters?

I am using the SHOW CREATE TABLE command to get a create code for my table. However, I noticed that the create command is missing a few features.
For instance, it doesn't have an IF NOT EXISTS parameter. It also lists the AUTO_INCREMENT=# position at the end; which isn't really needed since its an auto increment.
Is there any way to add parameters to the query to fix these issues?
I think your more interested in mysql dump that does include more information.
However, specifically for the "if not exists", you will have to replace that using a script or grep.
References:
Create table if not exists from mysqldump
ahhh...simple...no, you can't. Check docs

Generate table DDL via query on MySQL and SQL Server

Is there an easy way to extract table DDL information, via a query, using either Ms or My SQL server? (preferably both?)
For example, using MySQL Administrator / Navicat for MySql, there is a "DDL" function, which generates the "create table foo (....)" script.
Is there any way to get this information from a query itself, such as:
Select DDL from foo where table_name='bar';
Any have the "Create table bar (.....)" returned to me?
If not - any suggestions?
it's mysql-specific, but SHOW CREATE TABLE <table-name> gives you the DDL for a table.
You have to create that yourself.
You can query INFORMATION_SCHEMA.COLUMNS for the column name and data type.
You can't get the CREATE Table text in a cross platform way, but you can get enough information to build it yourself from the INFORMATION_SCHEMA views.

Need MySQL 4 to ignore ALTER TABLE errors

I have a MySQL script which is executed automatically under certain conditions. That script executes an ALTER TABLE command, because that column is needed in the database, but it may or may not have it...
Is it possible to make MySQL 4 execute the ALTER TABLE statement if the column doesn't exist or ignore the duplicate column error for this single command and allow the script execution to continue?
ALTER [IGNORE] TABLE will only ignore certain errors, like duplicate key errors when adding a new UNIQUE index, or SQL mode errors.
http://dev.mysql.com/doc/refman/4.1/en/alter-table.html
More details about the "script" you are using would help to answer the question. In python for example, the error would raise an exception which could then be caught and dealt with or ignored.
[EDIT] From the comment below, seems like you're looking for the mysql -f command line option.
You can first check the table schema before you attempt an addition of the column? However , I strongly suspect the design where you need to add columns on the fly. Something is not quite right. Can you explain the requirement in a little detail. I'm sure there are other cleaner way around this.