The basic syntax for the CREATE TABLE statement in MySQL is the _php base

Source: Internet
Author: User
The basic syntax for 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 new table created with the CREATE TABLE is a temporary table that will automatically disappear after the current session ends. Temporary tables are mainly used in stored procedures, and for MySQL, which does not currently support stored procedures, this keyword is generally not available.
IF not EXISTS: In fact, add a judgment before the table is being built to perform the CREATE TABLE operation only if the table does not currently exist. Use this option to avoid errors where the table already exists and cannot be created again.
Tbl_name: The table name of the table you want to create. The table name must conform to the rules for identifiers. The usual practice is to use only letters, numbers, and underscores in the table name. For example, titles, Our_sales, My_user1 and so on should be regarded as the table name of the comparison specification.
Create_definition: This is where the key part of the CREATE TABLE statement is. The properties of each column in the table are specifically defined in this section.
The basic statements of 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: The name of the column in the table. Must conform to the rules for identifiers and be unique in the table.
Type: The data type of the column. Some data types need to indicate length n, enclosed in parentheses. MySQL currently provides data types as detailed in the MySQL Advanced _ column type.
Not NULL | Null: Specifies whether the column is allowed to be empty. If NULL is not specified and NOT NULL is specified, the column is considered null-specified.
Default Default_value: Specifies the defaults for the column. If you do not specify a default value for the column, MySQL automatically assigns one. If the column can be null as a value, the default value is null. If the column is declared to not NULL, the default value depends on the column type: 1, for a numeric type that has no auto_increment attribute declared, the default value is 0. For a auto_increment column, the default value is the next value in the order. 2. For a date and time type other than timestamp, the default value is the appropriate "0" value for that type. For the first timestamp column in the table, the default value is the current date and time. 3. For the string type except Enum, the default is an empty string. For an enum, the default value is the first enumeration value.
Auto_increment: Set this column to have a self-added property, only an integer column can set this property. When you insert a null value or 0 to a auto_increment column, the columns are set to Value+1, where value is the maximum value of the column in the previous table. The auto_increment order begins with 1. Each table can have only one auto_increment column, and it must be indexed.
Related Article

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.