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:
创建表命令具有以下方面。 以下各节对此进行了描述:
The table name must be specified as
表名称必须指定为
ERROR 1046 (3D000): No database selected
错误1046(3D000):未选择数据库
See the following image:
见下图:
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具有以下三种主要的数据类型类别。
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 |
You can use any of the following table constraints.
您可以使用以下任何表约束。
Once columns are defined, you can create primary key and foreign keys using the following keywords
一旦定义了列,就可以使用以下关键字创建主键和外键
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连接。 ”,请参见下图:
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地址 , 端口 , 用户名和密码 ,然后单击“确定”。 见下图:
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:
以下是各列的详细信息:
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 ”已创建。 见下图:
To view the table definition, execute the following command in the query editor window.
要查看表定义,请在查询编辑器窗口中执行以下命令。
Describe employees.tblemployee
Following is the output:
以下是输出:
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数据库。
Select the employees database. Execute the following query
选择员工数据库。 执行以下查询
Use employees
Output:
输出:
View the table definition by executing the following command.
通过执行以下命令来查看表定义。
Describe employees.tblemployee
Following is the output:
以下是输出:
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 建表语句示例