Home > Database > Mysql Tutorial > body text

How to create data table command in mysql

下次还敢
Release: 2024-04-14 18:15:51
Original
1227 people have browsed it

In MySQL, you can use the CREATE TABLE statement to create a data table, specifying the table name, column name, data type and constraints. Specific steps include: 1. Connect to the MySQL database; 2. Use the CREATE TABLE statement to create a data table.

How to create data table command in mysql

MySQL Create Data Table Command

In MySQL, you can use the CREATE TABLE statement to create a data table. The syntax of this statement is as follows:

<code>CREATE TABLE table_name (
  column_name1 data_type [constraints],
  column_name2 data_type [constraints],
  ...
);</code>
Copy after login

Parameter description:

  • table_name: Data table name.
  • column_name: Data table column name.
  • data_type: Data type, such as INT, VARCHAR, DECIMAL, etc.
  • constraints: Column constraints, such as NOT NULL, UNIQUE, PRIMARY KEY, etc.

Example:

The following statement creates a data table named users, which contains id, name and email Columns:

<code>CREATE TABLE users (
  id INT NOT NULL AUTO_INCREMENT,
  name VARCHAR(255) NOT NULL,
  email VARCHAR(255) UNIQUE
);</code>
Copy after login

Description:

  • ##NOT NULL Constraint Indicates that the column cannot be a NULL value.
  • AUTO_INCREMENT The constraint indicates that the id column will automatically generate a unique value.
  • UNIQUE The constraint means that the values ​​in the email column must be unique.

Detailed field type:

Data typeDescriptionINTINTEGERVARCHAR(length)Variable length stringDECIMAL(length, decimal places)Decimal with decimal placesDATEDateTIMETimeDATETIMEDate and timeBOOLEANBoolean value (
TRUE or FALSE)
##Constraint type:

Constraint Type##NOT NULLColumn cannot be UNIQUEThe value in the column must be uniquePRIMARY KEY The value in the column uniquely identifies the rowFOREIGN KEYForeign key, which relates this table to another tableSteps to create a data table:
Description
NULL

Connect to the MySQL database.

    Use the
  1. CREATE TABLE
  2. statement to create a data table.
  3. Specify table name, column name, data type and constraints.

The above is the detailed content of How to create data table command in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template