Home > Database > Mysql Tutorial > body text

Create a data table with MySQL (MYSQL data table operation tutorial 1)

巴扎黑
Release: 2017-04-27 18:44:45
Original
1657 people have browsed it

Before operating the MySQL data table, you need to select the database first before you can operate the data table in the specified database, such as creating a data table, modifying the table structure, renaming the data table, or deleting the data table, etc. Otherwise, you cannot operate the data table. The data table is operated. Okay, let’s introduce the specific operations on the data table.

Create data table

Creating a data table is very similar to creating a database. The syntax format is:

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] Data table name

[(create_definition,...)][table_options][select_statement];


Explanation on the create data table statement:

TEMPORARY: If you use this keyword, it means creating a temporary table

IF NOT EXISTS: This keyword is used to avoid errors reported by MySQL when the table does not exist

create_definition: The column attribute part of the table. MySQL requires that when creating a representation, the table must contain at least one column

table_options: some characteristic parameters of the table

select_statement: the familiar part of the SELECT statement, which can be used to quickly create a table

The column attribute create_definition part is introduced below. The specific format of each column definition is as follows:

col_name type[NOT NULL | NULL][DEFAULT default_value][AUTO_INCREMENT][PRIMARY KEY][reference_definition]


Explanation on column attribute create_definition:

col_name: field name

type: field type

NOT NULL | NULL: Indicates whether the column is allowed to have null values. The system generally allows null values ​​by default, so when null values ​​are not allowed, NOT NULL

## must be used #DEFAULT default_value: Indicates the default value

AUTO_INCREMENT: Indicates whether it is automatic numbering. Each table can only have one AUTO_INCREMENT column and must be indexed.

PRIMARY KEY: Indicates whether it is the primary key. A table can only have one PRIMARY KEY. If there is no PRIMARY KEY in the table, and some applications require PRIMARY KEY, MySQL will return the first UNIQUE key without any NULL columns as the PRIMARY KEY.

reference_definition: Add comments to fields

The above are some basic knowledge of creating a data table. It seems very complicated, but in practical applications, you can use the most basic format to create a data table. The specific format is as follows:

create table table_name(column name 1 attribute, column name 2 attribute....);

The above is the detailed content of Create a data table with MySQL (MYSQL data table operation tutorial 1). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template