The basic syntax of the create table statement in MySQL is

Source: Internet
Author: User

The basic syntax of the create table statement in MySQL is:
Create [TEMPORARY] TABLE [if not exists] tbl_name [(create_definition,...)]
[Table_options] [select_statement]
TEMPORARY: This keyword indicates that the table created with create table is a TEMPORARY table, which will automatically disappear after the current session ends. Temporary tables are mainly used in stored procedures. For MySQL that does not currently support stored procedures, this keyword is generally not used.
If not exists: in fact, the create table operation is performed only when the table does NOT exist. This option can be used to prevent the table from being created again.
Tbl_name: name of the table you want to create. The table name must comply with the identifier rules. Generally, only letters, numbers, and underscores are used in the table name. For example, titles, our_sales, and my_user1 should be regarded as standardized table names.
Create_definition: This is the key part of the create table statement. This section defines the attributes of each column in the table.
The basic statements for create_definition are:
Col_name type [not null | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
[Primary key] [reference_definition]
Or primary key (index_col_name ,...)
Or KEY [index_name] (index_col_name ,...)
Or INDEX [index_name] (index_col_name ,...)
Or UNIQUE [INDEX] [index_name] (index_col_name ,...)
Or [CONSTRAINT symbol] foreign key index_name (index_col_name ,...)
[Reference_definition]
Or CHECK (expr)
Col_name: name of the column in the table. It must comply with the identifier rules and be unique in the table.
Type: the data type of the column. Some data types need to specify the length n and enclose them in parentheses. For details about the data types currently provided by MySQL, see MySQL advanced _ column type.
Not null | NULL: Specifies whether the column can be NULL. If neither NULL nor not null is specified, the column is considered NULL.
DEFAULT default_value: Specify the DEFAULT value for the column. If no default value is specified for the column, MySQL automatically assigns one. If the column can take NULL as the value, the default value is NULL. If the column is declared as not null, the default value depends on the column type: 1. For numeric types without the AUTO_INCREMENT attribute declared, the default value is 0. For an AUTO_INCREMENT column, the default value is the next value in the sequence. 2. For date and time types except TIMESTAMP, the default value is the appropriate "zero" value of this type. For the first TIMESTAMP column in the table, the default value is the current date and time. 3. For string types except ENUM, the default value is null. For ENUM, the default value is the first enumerated value.
AUTO_INCREMENT: set this column to have the auto-increment attribute. This attribute can be set only for integer columns. When you insert a NULL value or 0 to an AUTO_INCREMENT column, the column is set to value + 1, where value is the maximum value of this column in the previous table. The AUTO_INCREMENT sequence starts from 1. Each table can have only one AUTO_INCREMENT column, and it must be indexed.

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.