A, MySQL create a data table
After the database is created, the next step is to create a data table in the database. The so-called creation of data tables, referring to the establishment of a new table in the database has been created.
Process is the process of creating a data table of a predetermined attribute data columns, and implements the data integrity (including physical integrity, domain integrity and referential integrity) process constraints.
The basic syntax
In MySQL, you can use the CREATE TABLE create table statement. The syntax is:
CREATE TABLE <table name> ([Table definitions Option]) [Table Option] [Partition Options];
Among them, [表定义选项]
the format is:
<1 column name> <type 1> [, ...] <column name n> <type n>
CREATE TABLE command more grammar, which is mainly to create definitions (create-definition), table options (table-options) and partitioning options (partition-options) composed by the table.
The main grammar and how to use the CREATE TABLE statement is as follows:
- CREATE TABLE: used to create a table given name, must have the permission of the CREATE TABLE.
- <Table name>: Specifies the name of the table to be created, after the CREATE TABLE given, must comply with the identifier naming rules. Table name is specified in order to create tables in a specific database for db_name.tbl_name,. Whether or not there is a current database, you can create in this way. When you create a table in the current database, you can be omitted db-name. If you use a distinguished name in quotation marks, it should have the database and table names are quoted. For example, 'mydb'. 'Mytbl' is legal, but 'mydb.mytbl' illegal.
- <Table Definition Options>: Create a table is defined by the column name (col_name), column definitions (column_definition) and possibly null explanation, integrity constraints or the composition of the index table.
- By default, the table is created in the current database. If the table already exists, there is no current database or database does not exist, an error occurs.
Tip: Use CREATE TABLE to create a table, you must specify the following information:
- Name of the table to be created is not case sensitive, you can not use SQL language keywords, such as DROP, ALTER, INSERT and so on.
- Data table each column (field) names and data types, if you create multiple columns, separated by commas.
Create a data table in the specified database
Data table belongs to a database, before creating the data table, use the statement "USE <database>" specifies the operation in which the database, if the database is not selected, it will be thrown No database selected mistake.
Or use the command SELECT DATABASE (); view the database is currently located:
show tables see the current database already created a database table
CREATE TABLE <table name> to create a data table
describe <table name> See Data Table Structure
Wherein the meaning of each field is as follows:
- Null: indicate whether the column can store NULL values.
- Key: Indicates whether the column is indexed. PRI denotes the column part of the table is the primary key, UNI represents the column index is part UNIQUE, MUL represents a given value in a column with multiple occurrences allowed.
- Default: Indicates whether the column has a default value, if any, value is.
- Extra: indicates additional information associated with a given column can be obtained, such as AUTO_INCREMENT like.
Or show create table <table name> See specific configuration
Tip: Use the CREATE TABLE statement can not only SHOW view detailed statement when you create a table, and you can view the storage engine and character encoding. If you do not add "\ G" parameters, the displayed results may be very confusing, then add "\ G" parameter, you can show results more intuitive, easier to read.