MYQL Query CREATE TABLE statement show CREATE TABLE table_name

Source: Internet
Author: User

Technical Background:
When I first started to learn MySQL, sometimes lazy, I would use show create table name \g to copy table creation statements, but when running, always because "single quotes on table and column names", prompting for a syntax error that cannot be run.
List of issues:
1, why did it go wrong?
2, what is the solution?
solve the problem:
1, analyze the cause of the statement error in Show create TABLE copy
1.1 Reproduction process
1.1.1 Creates a test table, tests, and uses show create TABLE test to obtain the table's creation statement, the visible table name, and the column names wrapped in quotation marks.
Mysql> CREATE TABLE Test (
-ID int NOT NULL,
-primary key (ID)
);
Query OK, 0 rows Affected (0.00 sec)
Mysql> Show CREATE TABLE test \g
1. Row ***************************
Table:test
Create table:create Table' Test '(
' id 'Int (one) is not NULL,
PRIMARY KEY (' id ')
) Engine=myisam DEFAULT charset=latin1
1 row in Set (0.00 sec)
1.1.2 Drop the test table, then copy the creation statement, and after execution, the expected syntax error occurs.
mysql> drop table test;
Query OK, 0 rows Affected (0.00 sec)
mysql> Create table:create Table ' test ' (
' id ' int (one) is not NULL,
-PRIMARY KEY (' id ')
) Engine=myisam DEFAULT charset=latin1
;
error 1064 (42000): You have a ERROR in your SQL syntax; check the manual, corresponds to your MySQL server Versi on for the right syntax-use-near ': CREATE

1.2 principle
1.2.1 If you look closely at the ' Test ' and the ' test ' appearance of the single quotation mark, use the string ASCII () to verify (copy the statement ').
From the test results below,' The ASCII code is, and then, after looking at the code table, we know ' yes 'Heavy Accent", not a single quotation mark with ASCII 39.
Mysql> Select ASCII ("');
+------------+
| ASCII ("') |
+------------+
| 96 |
+------------+
View ASCII codes for single quotes
Mysql> Select ASCII ("'");
+------------+
| ASCII ("'") |
+------------+
| 39 |
+------------+
Description: Accent notes in the second row of the keyboard first key, when found, I said quite embarrassed.
2 Problem Solving
2.1 Using the session setting parameter set sql_quote_show_create=0;
2.1.1 Sql_quote_show_create, there are two values (1,0), the default is 1, indicating that the table name and column name will be "wrapped."
This server parameter can only be set at the session level and is not supported for global settings (MY.CNF settings are not supported).
After setting, it is visible that there is no accent below.
Mysql> set sql_quote_show_create=0;
Query OK, 0 rows Affected (0.00 sec)
Mysql> Show CREATE TABLE test \g
1. Row ***************************
Table:test
Create table:create Table Test (
ID Int (one) is not NULL,
PRIMARY KEY (ID)
) Engine=myisam DEFAULT charset=latin1
2.2 Using pager to process output
Mysql> Pager tr-d "
PAGER set to ' tr-d '
Mysql> Show create TABLE test;
+-------+------------------------------------------------------------------------------------------------------ ------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------ ------+
| Test | CREATE TABLE Test (
ID Int (one) is not NULL,
PRIMARY KEY (ID)
) Engine=myisam DEFAULT charset=latin1 |

3, the use of the command line: Lazy is one thing, if you write a shell, Python and other tools, may be based on show create table to handle some things, namely "command line Processing"
3.1 with ' SET sql_quote_show_create=0
-bash-3.2$ mysql-uroot-e ' SET sql_quote_show_create=0; Use test; Show create TABLE test ';
+-------+------------------------------------------------------------------------------------------------------ +
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------ +
| Test | CREATE TABLE Test (
ID Int (one) is not NULL,
PRIMARY KEY (ID)
) Engine=myisam DEFAULT charset=latin1 |
+-------+------------------------------------------------------------------------------------------------------ +
3.2
-bash-3.2$ mysql-e ' use test; Show CREATE TABLE Test \g ' | Tr-d ";
1. Row ***************************
Table:test
Create table:create Table Test (
ID Int (one) is not NULL,
PRIMARY KEY (ID)
) Engine=myisam DEFAULT charset=latin1
3.3 You can also use SED to solve
-bash-3.2$ mysql-e ' use test; Show CREATE TABLE Test \g ' | Sed-e ' s/'//g ';
1. Row ***************************
Table:test
Create table:create Table Test (
ID Int (one) is not NULL,
PRIMARY KEY (ID)
) Engine=myisam DEFAULT charset=latin1

MYQL Query CREATE TABLE statement show CREATE TABLE table_name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.