Home  >  Article  >  Backend Development  >  The basic syntax of the create table statement in MySQL is

The basic syntax of the create table statement in MySQL is

WBOY
WBOYOriginal
2016-07-29 08:36:13956browse

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 represents a new one created with create table The table is a temporary table and will disappear automatically after the current session ends. Temporary tables are mainly used in stored procedures. For MySQL, which currently does not support stored procedures, this keyword is generally not used.
IF NOT EXISTS: Actually, a judgment is added before creating the table. The create table operation is executed only when the table does not currently exist. Use this option to avoid errors in which the table already exists and cannot be created again.
tbl_name: The name of the table you want to create. The table name must comply with identifier rules. Common practice is to use only letters, numbers, and underscores in table names. For example, titles, our_sales, my_user1, etc. should be considered relatively standardized table names.
create_definition: This is the key part of the create table statement. In this section, the properties of each column in the table are specifically defined. The basic statement of
create_definition is:
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 comply with identifier rules and be unique within the table.
type: The data type of the column. Some data types require the length n to be specified and enclosed in parentheses. For details on the data types currently provided by MySQL, see MySQL Advanced_Column Types.
NOT NULL | NULL: Specify whether the column is allowed to be empty. If neither NULL nor NOT NULL is specified, the column is assumed to have NULL specified.
DEFAULT default_value: Specify a default value for the column. If no default value is specified for a column, MySQL automatically assigns one. If the column can take NULL as a value, the default value is NULL. If a column is declared NOT NULL, the default value depends on the column type: 1. For numeric types that do not declare the AUTO_INCREMENT attribute, 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 for the 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 is an empty string. For ENUM, the default value is the first enumeration value.
AUTO_INCREMENT: Set this column to have an auto-increment attribute. Only integer columns can set this attribute. When you insert a NULL value or 0 into an AUTO_INCREMENT column, the column is set to value + 1, where value is the maximum value for the column in this table. AUTO_INCREMENT order starts from 1. There can only be one AUTO_INCREMENT column per table, and it must be indexed.

The above has introduced the basic syntax of the create table statement in MySQL, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn