mysql 建表语句示例_MySQL Create Table语句和示例

mysql 建表语句示例

In this article, I am going to explain the MySQL CREATE TABLE statement with examples. The following syntax contains basic statements to create a table in MySQL.

在本文中,我将通过示例解释MySQL CREATE TABLE语句。 以下语法包含在MySQL中创建表的基本语句。

CREATE TABLE ‘schemaname’.’tablename’(
  column_1 datatype (length) NOT NULL | DEFAULT | UNIQUE, 
  ...,
  Primary key, 
Foreign key 
) ENGINE=storage_engine;

The Create table command has the following aspects. It is described under the sections:

创建表命令具有以下方面。 以下各节对此进行了描述:

  1. Table Name

    表名
  2. Column data type and attributes

    列数据类型和属性
  3. Primary key and foreign keys

    主键和外键

表名称:tblname (Table Name: tblname)

The table name must be specified as <DBName>.<TableName> to create a table in a specific database. This command assumes that the database name specified in the create table command does exist. If you do not specify the database name, then it returns the following error.

表名称必须指定为<DBName>。<TableName>才能在特定数据库中创建表。 该命令假定在create table命令中指定的数据库名称确实存在。 如果未指定数据库名称,则它将返回以下错误。

ERROR 1046 (3D000): No database selected

错误1046(3D000):未选择数据库

See the following image:

见下图:

MySQL create table example: Database not selected error

列数据类型和属性 (Column data types and Attributes)

The list of the columns must be followed by its data type and table constraint. The column name must be separated by the comma (,). You must specify the column name in the following format:

列的列表后必须跟随其数据类型和表约束。 列名必须用逗号(,)分隔。 您必须以以下格式指定列名称:

“Column_name” data_type(length) [table_constraint] [table_option]

“列名”数据类型(长度) [table_constraint] [table_option]

data_type:

data_type

It represents the data type of the column. MySQL has the following three main categories of the data type.

它代表列的数据类型。 MySQL具有以下三种主要的数据类型类别。

  1. Numeric Datatypes

    数值数据类型
  2. Text data type

    文字数据类型
  3. Date and Time data types

    日期和时间数据类型

Below is the list of the numeric data type.

下面是数字数据类型的列表。

Data type Name

Normal Range

Unsigned Range

TINYINT()

-128 to 127 UNSIGNED.

0 to 255

SMALLINT()

-32768 to 32767

0 to 65535

MEDIUMINT()

-8388608 to 8388607 UNSIGNED.

0 to 16777215

INT( )

-2147483648 to 2147483647

0 to 4294967295

BIGINT( )

-9223372036854775808 to 9223372036854775807

0 to 18446744073709551615

数据类型名称

普通范围

无符号范围

TINYINT()

-128至127未签名。

0至255

SMALLINT()

-32768至32767

0至65535

MEDIUMINT()

-8388608至8388607未签名。

0至16777215

INT()

-2147483648至2147483647

0至4294967295

BIGINT()

-9223372036854775808至9223372036854775807

0至18446744073709551615

Below is the list of Text data types.

下面是Text数据类型的列表。

Data type name

Type

Range

CHAR( )

fixed string

255 characters

VARCHAR( )

Variable string

255 characters

TINYTEXT

string

255 characters

TEXT

string

65535 characters

MEDIUMTEXT

string

16777215 characters

LONGTEXT

string

4294967295 characters

数据类型名称

类型

范围

字符()

固定弦

255个字符

VARCHAR()

可变字符串

255个字符

细语

255个字符

文本

65535个字符

中文字

16777215个字符

长文本

4294967295个字符

Below is the list of date and time data types

以下是日期和时间数据类型的列表

Data type Name

Format

DATE

YYYY-MM-DD

DATETIME

YYYY-MM-DD HH:MM:SS

TIMESTAMP

YYYYMMDDHHMMSS

TIME

HH:MM:SS

数据类型名称

格式

日期

YYYY-MM-DD

约会时间

YYYY-MM-DD HH:MM:SS

时间戳

YYYYMMDDHHMMSS

时间

HH:MM:SS

表约束 (Table constraints)

You can use any of the following table constraints.

您可以使用以下任何表约束。

  1. NOT NULL: Ensures that the value of the column must not be null NOT NULL:确保列的值不能为null
  2. CHECK: Before inserting data in the table, it evaluates the condition specified in the CHECK constraint. If the condition fails, then the insert statement fails CHECK:在将数据插入表中之前,它将评估CHECK约束中指定的条件。 如果条件失败,则插入语句失败
  3. DEFAULT: Default values of the column. If you do not specify the value of the column in the insert statement, the query inserts the value specified in the DEFAULT constraint 默认值:列的默认值。 如果未在insert语句中指定列的值,则查询将插入在DEFAULT约束中指定的值

主键和外键 (Primary and Foreign keys)

Once columns are defined, you can create primary key and foreign keys using the following keywords

一旦定义了列,就可以使用以下关键字创建主键和外键

  1. PRIMARY KEY: It’s a unique index and must be defined as NOT NULL. A table can have only one primary key. The PRIMARY KEY is placed first in the 主键:这是唯一索引,必须定义为NOT NULL。 一个表只能有一个主键。 将PRIMARY KEY放在create table statement create table语句中的第一位
  2. FOREIGN KEY: MySQL supports the foreign keys. A table can have more than one foreign key that references the primary key of different tables FOREIGN KEY: MySQL支持外键。 一个表可以有多个外键,这些外键引用不同表的主键

MySQL创建表示例 (MySQL Create Table example)

If you want to create a table using MySQL Workbench, you must configure a new connection. To do that, open MySQL workbench, and on the Welcome screen, click on “MySQL connections.” See the following image:

如果要使用MySQL Workbench创建表,则必须配置一个新连接。 为此,请打开MySQL工作台,然后在“ 欢迎”屏幕上 ,单击“ MySQL连接。 ”,请参见下图:

MySQL create table example: Welcome screen

In “Setup New Connection” dialog box, provide the desired name of the connection, Hostname or IP Address of the MySQL database server, port, user name, and password and click on OK. See the following image:

在“ 设置新连接 ”对话框中,提供所需的连接名称,MySQL数据库服务器的主机名或IP地址端口用户名密码 ,然后单击“确定”。 见下图:

MySQL create table example: Configure new connection

Execute the following query to create a new table named “tblEmployees” in the “Employees” database.

执行以下查询以在“ 雇员 ”数据库中创建一个名为“ tblEmployees ”的新表。

CREATE TABLE `employees`.`tblemployee` (
  `Employee_ID` INT NOT NULL AUTO_INCREMENT,
  `Employee_Name` VARCHAR(45) NOT NULL,
  `Employee_Department_ID` INT NOT NULL,
  `Employee_Grade_ID` INT NOT NULL DEFAULT A,
  `Employee_Salary` INT NOT NULL,
  PRIMARY KEY (`Employee_ID`),
  INDEX `FK_Department_ID_idx` (`Employee_Department_ID` ASC) VISIBLE,
  CONSTRAINT `FK_Department_ID`
    FOREIGN KEY (`Employee_Department_ID`)
    REFERENCES ` employees`.`department` (`Department_ID`)
    ON DELETE RESTRICT
    ON UPDATE CASCADE);

Following are the details of the columns:

以下是各列的详细信息:

  1. employee_id column. It is a primary key of the table and has an auto-increment column; hence you do not have to specify the value for this column explicitly. When you insert the data in the table, MySQL generates a sequential integer for the employee_id列中。 它是表的主键,并具有一个自动增量列。 因此,您不必显式指定此列的值。 当您将数据插入表中时,MySQL会为employee_id column employee_id列生成一个顺序整数
  2. employee_name column. The data type of the column is employee_name列中。 该列的数据类型为varchar(), and the length is varchar() ,长度为45. You cannot insert a 45 。 您不能在employee_name列中插入NULL value in the employee_name column NULL
  3. employee_department_id column. The data type of this column is employee_department_id列中。 该列的数据类型为INTEGER. It’s a foreign key column that references the INTEGER 。 这是一个外键列,它引用了department_id column of the tbldepartment表的tbldepartment table. If any row is updated in the tbldepartment table, the values in department_id列。 如果有任何行在tbldepartment表更新tblemployee updates automatically ,tblemployee自动更新(ON UPDATE CASCADE), and the delete operation on the (ON UPDATE CASCADE),并在tbldepartment is restricted tbldepartment删除操作的值限制(ON DELETE RESTRICT) (ON DELETE RESTRICT)
  4. employee_garde column. The data type of this column is varchar and length is 2. The employee_garde列中。 该列的数据类型为varchar,长度为2。已在此列上创建了DEFAULT constraint has been created on this column. If we do not specify any value for this column, MySQL inserts the “DEFAULT约束。 如果我们没有为该列指定任何值,MySQL将插入“ A” as a default value A ”作为默认值
  5. employee_salary column. The data type of the column is INTEGER employee_salary列中。 列的数据类型为INTEGER

使用MySQL Workbench查看表定义 (View the table definition using MySQL Workbench)

To view the table from MySQL workbench, Expand Employees database from Left pan expand tables. Under Tables, you can see the table “tblEmployees” has been created. See the following image:

要从MySQL工作台查看表,请从左平移展开展开员工数据库。 在表下,您可以看到表“ tblEmployees ”已创建。 见下图:

MySQL create table example: View table in MySQL workbench

To view the table definition, execute the following command in the query editor window.

要查看表定义,请在查询编辑器窗口中执行以下命令。

Describe employees.tblemployee

Following is the output:

以下是输出:

MySQL create table example: View table definition in MySQL workbench

如何使用MySQL命令行查看表定义 (How to view the table definition using MySQL Command-line)

To view the table definition using the command-line tool, open the MySQL command-line tool, and enter the password to login to the MySQL database.

要使用命令行工具查看表定义,请打开MySQL命令行工具,然后输入密码以登录到MySQL数据库。

MySQL create table example: Open MySQL command-line

Select the employees database. Execute the following query

选择员工数据库。 执行以下查询

Use employees

Output:

输出:

MySQL create table example: connect to the employee database

View the table definition by executing the following command.

通过执行以下命令来查看表定义。

Describe employees.tblemployee

Following is the output:

以下是输出:

MySQL create table example:View table definition using query.

摘要 (Summary)

In this article, I have explained about the MySQL create table statement with examples. I have covered the create table syntax and how to view the definition of the table using MySQL workbench and MySQL command-line tool.

在本文中,我通过示例解释了有关MySQL create table语句的信息。 我已经介绍了创建表语法以及如何使用MySQL工作台和MySQL命令行工具查看表的定义。

翻译自: https://www.sqlshack.com/mysql-create-table-statement-with-examples/

mysql 建表语句示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值