Home  >  Article  >  Database  >  What is the syntax for creating tables in mysql database

What is the syntax for creating tables in mysql database

下次还敢
下次还敢Original
2024-04-22 19:57:58331browse

Use CREATE TABLE syntax to create a MySQL table. You need to specify the table name, column name, data type, whether to allow null values ​​and default values. Among them, the table name and column name are case-sensitive, the primary key column must be non-null, and the default value should comply with the data type constraints.

What is the syntax for creating tables in mysql database

MySQL table creation syntax

The syntax for creating a table in MySQL is as follows:

<code>CREATE TABLE table_name (
  column_name1 data_type1 [NOT NULL] [DEFAULT default_value1],
  column_name2 data_type2 [NOT NULL] [DEFAULT default_value2],
  ...
) [table_options];</code>

Detailed explanation of syntax:

  • CREATE TABLE: Specify the command to create a table.
  • table_name: The name of the table to be created.
  • column_nameX: The name of each column to be created.
  • data_typeX: The data type of each column.
  • NOT NULL: The specified column cannot have a NULL value.
  • DEFAULT default_valueX: Specifies the default value of the column. If no value is explicitly specified, this default value is used.
  • table_options: Can be used to specify table-level options, such as storage engine, character set, etc.

Example:

Create a table named "employees", which contains three columns: "id" (primary key), "name" ( String) and "salary" (number):

<code class="mysql">CREATE TABLE employees (
  id INT NOT NULL AUTO_INCREMENT,
  name VARCHAR(255) NOT NULL,
  salary DECIMAL(10, 2) NOT NULL
);</code>

Note:

  • Table names and column names are case-sensitive.
  • The primary key column must be specified as "NOT NULL".
  • The default value can only be a valid value of the column data type.
  • The "table_options" section can be added or omitted as needed.

The above is the detailed content of What is the syntax for creating tables in mysql database. 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