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:

见下图:

列数据类型和属性 (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连接。 ”,请参见下图:

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 RESTRICTON 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 ”已创建。 见下图:

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

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

Describe employees.tblemployee

Following is the output:

以下是输出:

如何使用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数据库。

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:

以下是输出:

摘要 (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 建表语句示例

mysql 建表语句示例_MySQL Create Table语句和示例相关推荐

  1. MYSQL建表语句错误:1103-Incorrect table name

    如题,使用MYSQL建表语句时发生错误 首先简述一下MySQL建表语句: 举例如下: CREATE table `iauth ` (`iid` varchar(32) NOT NULL COMMENT ...

  2. Hive建表语句详解--CREATE TABLE

    创建表的三种方法 Hive创建表的方式(默认路径/user/hive/warehouse,也可以location指定,主要针对external表) 1.使用create命令创建一个新表,带分区 CRE ...

  3. mysql建表语句 金额_mysql 建表语句

    最近项目在用mysql语句 指定非空,默认值为空字符串    NOT NULL  DEFAULT '' 建表 CREATE TABLE  IF NOT EXISTS `ims_test` ( `id` ...

  4. MySql建表语句迁移DB2方法总结(踩坑记录)

    一.前言 最近需要把mysql数据库中的表迁移到DB2数据库,表内的数据用kettle可以实现迁移,但是建表语句却怎么也搞不好. 百度半天,发现并没有什么好的方法或工具能把mysql建表语句转成DB2 ...

  5. mysql建表语句主键

    mysql数据库建表语句 createtableDMB_BDXX( IDbigintnotnull, MCVARCHAR(100)notnull, ZVARCHAR(100)notnull, ZXJB ...

  6. 三种常用的MySQL建表语句(转)

    MySQL建表语句是最基础的SQL语句之一,下面就为您介绍最常用的三种MySQL建表语句,如果您对MySQL建表语句方面感兴趣的话,不妨一看. 1.最简单的: CREATE TABLE t1(     ...

  7. 【Json】在线JSON转MySQL建表语句工具

    在线JSON转MySQL建表语句工具 在线JSON转MySQL建表语句工具 此工具可以将JSON对象转换成MySQL语句,支持复制和下载. 在开发过程中,使用此工具可以蛮方便的. 当然还有其他工具:( ...

  8. 在线JSON转MySQL建表语句工具

    在线JSON转MySQL建表语句工具 在线JSON转MySQL建表语句工具 JSON:(JavaScript Object Notation, JS对象简谱) 是一种轻量级的数据交换格式.它基于 EC ...

  9. mysql运维高级_Mysql DBA 高级运维学习之路-mysql建表语句及表知识

    1.创建表 1.1 建表的基本命令语法 create table( , --- ) 1.2 创建student表 (1)在linzhongniao库中创建student表 mysql> use ...

最新文章

  1. 数学各个研究方向简介
  2. Stumpwm的编译安装
  3. 【PC工具】简单好用的截屏gif录制小软件
  4. 解决 Cycript 信息显示不全的问题
  5. 500 OOPS: vsftpd: refusing to run with writable root inside chroot()
  6. 微信小程序在聊天中如何插入表情?
  7. 如何实现对合同进行智能化管理?
  8. loongson龙芯屏幕分辨率设置1280×1024
  9. linux中的设备管理
  10. 接了个私单,结果对方有部分尾款迟迟不付,还好有留了个后门
  11. 一位16年老员工的反思:什么才是真正的执行力?
  12. 我用Python量化了1000万次散户操作,然后反着来,胜率竟然高达...?! | 你可以永远相信散户!【量化投资邢不行啊】...
  13. you belong with me(你属于我)
  14. No qualifying bean of type ‘com.itheima.dao.BookDao1‘ available: expected single matching bean 问题解决
  15. 《Unity》FixedUpdate
  16. 软件实施工程师常问数据库问题
  17. 前端使用sockJs进行聊天通讯的功能
  18. English Learning - L2-13 英音地道语音语调 弱读技巧 2023.04.6 周四
  19. html+link作用,html link标签有什么作用?html link标签的定义及属性介绍
  20. ChatGPT 有哪些 “激动人心的时刻“?以及自己的一些思考

热门文章

  1. 清华大学 ucore-lab0 MacOS
  2. 网络编程学习2-套接字编程简介
  3. 02_Storm集群部署
  4. 认知心理学告诉你什么才是高效学习
  5. 设计模式之——单例模式(Singleton)的常见应用场景(转):
  6. 初学 快速幂 的理解
  7. eclipse中添加svn插件
  8. 常用的hooks入门
  9. JavaScript数据结构——字典(Dictionary)
  10. 【PHP学习】—PHP文件嵌套HTML(四)