SHOW CREATE TABLE tbl_name
Shows the CREATE TABLE
statement
that creates the given table. To use this statement, you must
have some privilege for the table. This statement also works
with views.
mysql> SHOW CREATE TABLE t\G
*************************** 1. row ***************************
Table: t
Create Table: CREATE TABLE t (
id INT(11) default NULL auto_increment,
s char(60) default NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM
SHOW CREATE TABLE
quotes table
and column names according to the value of the
sql_quote_show_create
option.
See Section 5.1.5, “Session System Variables”.
User Comments
This is useful when you want to change a column name or definition with:
ALTER TABLE foobar CHANGE old_field_name new_field_name old_field_definition
You want to maintain the same column type, so having the original column definition at your fingers tips is handy.
This features seems to be from 4.0 release series onwards.
This 'ENGINE=' syntax is important; don't forget to look through your older code for similar 'TYPE=' designations. There are some well-know applications which are choking on 'TYPE=HEAP', for example, reporting a Syntax Error.
If people are not familiar with switching ENGINE types, this one can bite you!
You can also use create table if not exists [database].[table]
Replace [database] with the database of where the table should be created.
Replace [table] with the table name you want created.
example: (This will create a table called user in the test database)
create table if not exists test.user
(
UserID varchar(50)
, Password varchar(50)
) Engine=InnoDB;
This might go without saying for most folks, but just so you know, this statement does not necessarily return the same statement that was actually used to create the table in the first place. It returns a statement which if run at the current time will recreate the given table with its current structure (not including the data). So if you create a table, then alter it, then run the SHOW CREATE TABLE command, it will return a statement that acts as if the change represented by your ALTER command were incorporated into your initial CREATE TABLE command.
Add your own comment.