MySQL SHOW CREATE TABLE parameters? - mysql

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

Related

What does generated by server on drop tables mean [duplicate]

Under the structure tab, when EXPORTing a database using phpmyadmin there is a check box labeled:
Add DROP TABLE / VIEW / PROCEDURE / FUNCTION
What does this do?
When creating a table, view, procedure, or function, it will add a DROP statement before it. The result of this is that even if the item exists, it will still be created.
For example: If you have a table called users and you run the export script without the DROP checkbox, it will attempt to create the users table again but will fail since it already exists. If you check it, it will drop the table before it is created (if it exists) to ensure the creation will always be successful.
Of course this can be dangerous if you have data in the table that you don't want to lose.
For example: If you have a table called users and you run the export script without the DROP checkbox, it will attempt to create the users table again but will fail since it already exists. If you check it, it will drop the table before it is created (if it exists) to ensure the creation will always be successful.
I was confused as to what this statement meant exactly, so I did additional research on the topic and wanted to leave an elaborated explanation here for future reference.
The create and drop actions in the above quote are simply instructions for when you import the file you have already exported. I was initially under the impression that these actions were happening as I was exporting. This is not the case. It's simply instructions for when you import your exported file.

MySQL workbench creates forward script dropping of tables in wrong sequence, violating foreign key constraints

The subject line actually says it all.
I have a schema, now when I create a forward script it generates the tables and I added the option to do a DROP TABLE IF EXISTS in front of every create table SQL. The creation part is fine. But if you run the script twice you notice that the drop sequence is the wrong order.
I think the concept of aligning the drop sequence with the create sequence is just conceptually arguable. I think you might be able to create schemas where you won't be able to create the tables in the same sequence as you would drop them.
Anyhow, I can't find any option to change the order or do anything. Has anyone an idea how I can change the drop sequence manually ?
I am sorry to be not able to share any SQL, however I think the problem is really generic. And you want solve it by writing different SQL. So it should be possible to answer based on discussion, not on code.
That's Workbench version 6.3.6. So almost latest. (Currently 6.3.7)
You must drop table in reverse order of creating them.
When creating:
Create table a...;
Create table b...;
Create table c...;
When dropping:
Drop table c;
Drop table b;
Drop table a;

Conditionally add a column to a mysql table

I have a project where I want to be able to manage several instances of the same database on several people's localhosts. I want each developer to be able to reset their DB back to the canonical origin no matter what state they get their DB into. To this end I maintain a standard database file. It holds the schema using CREATE TABLE table_name IF NOT EXISTS {
However, I want to be able to add to the starting table structures as needed as this project moves along. To do this, I would love to be able to do something like the following ALTER TABLE table_name ADD COLUMN IF NOT EXISTS column_name but that does not seem to exist. I did notice a stored procedure floating around the internet that solves this, but I wanted to know if something simpler is able to achieve the goal I have in mind. Thank you for the time and help.
It won't be possible to do it with plain SQL. Stored procedure should work the best: read information_schema and check if the column is present. If not - execute the alter statement.
One option is to execute your ALTER statement without checking anything:
ALTER TABLE table_name ADD column_name VARCHAR(40);
(change VARCHAR(40) to whatever you need)
If the column didn't already exist, then the statement creates it.
If the column already existed, the statement does nothing and returns an error. Just ignore the error and continue.

Retrieve CREATE TABLE code of an already existing table?

Is there a way to do this?
In case the DBMS command history got cleaned or, in my case, when many ALTER TABLE were used in the course of time.
I'm using MySQL.
Yes, it is as simple as
SHOW CREATE TABLE yourtable;
This will include all the subsequent ALTER TABLE statements. You cannot retrieve the table's original state.
Here is the relevant documentation

MySQL multi CREATE TABLE syntax help?

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