To create a table in MySQL, use the CREATE TABLE statement. The syntax is: CREATE TABLE table_name (column_name data_type [constraints], ...). Lists elements such as table_name, column_name, data_type, and constraints, and provides examples of auto-incrementing primary keys and NOT NULL constraints.
How to create a table in MySQL
In MySQL, use the CREATE TABLE
statement to create the table.
Basic syntax:
<code>CREATE TABLE table_name ( column_name data_type [constraints], ... );</code>
Where:
is the name of the table to be created.
is the name of the column in the table.
is the data type of the column (for example: INT, VARCHAR, DATE).
are constraints on the column (for example: NOT NULL, UNIQUE, FOREIGN KEY).
Example:
Create a table namedstudents, which contains
id (auto-increment primary key) ,
name (VARCHAR),
age (INT), and
grade (VARCHAR) columns:
<code class="sql">CREATE TABLE students ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, age INT, grade VARCHAR(10) );</code>
Note:
Indicates that the
id column is an auto-incrementing primary key and will be automatically incremented each time a new row is inserted.
Specifies the
id column as the primary key, which uniquely identifies each row in the table.
The constraint specifies that the
name column is not allowed to be NULL.
SHOW CREATE TABLE table_name statement.
The above is the detailed content of How to create a table in mysql. For more information, please follow other related articles on the PHP Chinese website!