Home  >  Article  >  Database  >  How to create a table in mysql

How to create a table in mysql

下次还敢
下次还敢Original
2024-04-14 19:21:34613browse

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

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:

  • ##table_name is the name of the table to be created.
  • column_name is the name of the column in the table.
  • data_type is the data type of the column (for example: INT, VARCHAR, DATE).
  • constraints are constraints on the column (for example: NOT NULL, UNIQUE, FOREIGN KEY).

Example:

Create a table named

students, 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:

  • AUTO_INCREMENT Indicates that the id column is an auto-incrementing primary key and will be automatically incremented each time a new row is inserted.
  • PRIMARY KEY Specifies the id column as the primary key, which uniquely identifies each row in the table.
  • NOT NULL The constraint specifies that the name column is not allowed to be NULL.
You can view the definition of the created table by using the

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!

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