前言

表就相当于文件夹中的excel文件,表中的每一条记录就是excel中的一条记录,字段就是表格的表头,记录就是表格中的每一行数据本文将详细介绍表相关操作、mysql存储引擎、字段数据类型及约束条件等知识。

存储引擎

计算机中存储的文件格式有很多种,针对不同的文件格式会有对应不同的存储方式和处理机制,比如存储pdf格式的文件和txt格式的文件存储机制就是不同的。

对应到MySQL中存储引擎就是不同的存储机制,在创建表时候可以指定存储引擎,可以通过下述SQL语句查看所有的存储引擎:

show engines;

mysql根据主表id查询子表_mysql

这里只介绍MySQL最常用的四种存储引擎:

Innodb : 是MySQL5.5版本及之后默认的存储引擎 存储数据更加的安全

myisam :是MySQL5.5版本之前默认的存储引擎 速度比Inodb更快,但是我们更加注重的是数据的安全

memory :内存引擎,数据全部存放在内存中,断电数据丢失

blackhole 无论存什么,都立刻消失(黑洞)

创建表

创建表的基本语法如下:

use database;  # 切换到需要创建表的库下
create table 表名(字段1 字段1数据类型(宽度) 约束条件, 字段2 字段2数据类型(宽度) 约束条件...)
复制代码

在同一张表中字段名不能重复,创建表时字段和字段类型是必须的,宽度一般情况下指的是对数据长度的限制,约束条件是在宽度的基础上为字段增加额外的约束,宽度和约束条件是可选的。

下述的SQL语句创建表的过程中,有些操作目前没有介绍,屏幕面前的小伙伴先看着,后续文章都会做详细介绍的。

-- 创建数据库
mysql> create database ex; 
Query OK, 1 row affected (0.00 sec)

-- 切换数据库
mysql> use ex;
Database changed

-- 在数据库内创建表,表名为info,字段分别是id name age sex phone
mysql> create table info(id int, name varchar(20), age int(3), sex enum('male','female'), phone bigint(11));
Query OK, 0 rows affected (0.01 sec)

-- 查看数据库中所有的表
mysql> show tables;
+--------------+
| Tables_in_ex |
+--------------+
| info         |
+--------------+
1 row in set (0.00 sec)

-- 查看info这张表的信息
mysql> desc info;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | YES  |     | NULL    |       |
| name  | varchar(20)           | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| phone | bigint(11)            | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
5 rows in set (0.09 sec)

-- 查询info这张表的所有数据
mysql> select * from info;
Empty set (0.00 sec)

表中插入数据

上述查看表中所有数据的操作中,得到的结果empty,这是因为我们虽然创建了表但是表中并没有数据的原因,因此我们需要向表中插入数据,可以一次性插入多条记录,向表中插入数据有两种方式:

第一种,按照字段和值的对应关系插入:

-- 基本语法
insert into 表名 (字段1,字段2...) values (字段1的值, 字段2的值...), (字段1的值, 字段2的值...);

-- 具体操作
mysql> insert into info(id, name, sex, phone) values(1, 'python', 'male', 110), (2, 'java', 'female', 119);  -- 插入两条数据
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

-- 查看表中所有数据,由于age没有插入对应数据,因此为null
mysql> select * from info;  
+------+--------+------+--------+-------+
| id   | name   | age  | sex    | phone |
+------+--------+------+--------+-------+
|    1 | python | NULL | male   |   110 |
|    2 | java   | NULL | female |   119 |
+------+--------+------+--------+-------+
2 rows in set (0.00 sec)

第二种,不指定字段值插入数据,必须按照创建表时的顺序增加数据,同样可以一次插入多条数据;

-- 语法
insert into 表名 values(字段1的值, 字段2的值...);

-- 具体操作
-- 如果没有按照创建表时字段的顺序和数量就会出现数据错乱和报错
mysql> insert into info values (3, 'php', 'male', 114);
ERROR 1136 (21S01): Column count does not match value count at row 1

-- 下述SQL语句就是正确的操作
mysql> insert into info values (3, 'php',10, 'male', 114),(4,'go',5,'male', 120);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from info;
+------+--------+------+--------+-------+
| id   | name   | age  | sex    | phone |
+------+--------+------+--------+-------+
|    1 | python | NULL | male   |   110 |
|    2 | java   | NULL | female |   119 |
|    3 | php    |   10 | male   |   114 |  
|    4 | go     |    5 | male   |   120 |
+------+--------+------+--------+-------+
4 rows in set (0.00 sec)

查看表结构

查看表结构有两种方式,分别是descshow create table。上述SQL语句中我们使用的就是desc 表名查看表结构,但是输出的信息还不够全面,为了得到更全面的表定义信息,有时候就需要查看创建表的SQL语句,使用show create table语法。除了可以看到表定义之外,还可以看到engine(存储引擎)和charset(字符集)等信息。

-- desc查看表结构,也可以使用describe,desc是descride的简写,效果相同
mysql> desc info;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | YES  |     | NULL    |       |
| name  | varchar(20)           | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| phone | bigint(11)            | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
5 rows in set (0.94 sec)

mysql> describe info;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | YES  |     | NULL    |       |
| name  | varchar(20)           | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| phone | bigint(11)            | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
5 rows in set (0.08 sec)

下面在使用show create table的方式查看一下表结构:

-- \G选项的含义是是的记录能够竖向排列,以便更好的显示内容较长的记录。
mysql> show create table info \G;  
*************************** 1. row ***************************
       Table: info
Create Table: CREATE TABLE `info` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  `age` int(3) DEFAULT NULL,
  `sex` enum('male','female') DEFAULT NULL,
  `phone` bigint(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

ERROR:
No query specified

数据类型

MySQL支持所有标准的SQL数值数据类型,大概可以分为以下几类,这些类型是创建表时指定的字段的数据类型。

整数类型

MySQL支持的整数类型有TINYINT、MEDIUMINT和BIGINT;对于小数的表示,MySQL分为两种方式:浮点数和定点数。浮点数包括float(单精度)和double(双精度),而定点数只有decimal一种,在MySQL中以字符串的形式存放,比浮点数更精确,适合用来表示货币等精度高的数据。

下表中显示了每种数字类型的存储和范围以及大致用途:

类型

大小

范围(有符号)

范围(无符号)unsigned约束

用途

TINYINT

1 字节

(-128,127)

(0,255)

小整数值

SMALLINT

2 字节

(-32 768,32 767)

(0,65 535)

大整数值

MEDIUMINT

3 字节

(-8 388 608,8 388 607)

(0,16 777 215)

大整数值

INT或INTEGER

4 字节

(-2 147 483 648,2 147 483 647)

(0,4 294 967 295)

大整数值

BIGINT

8 字节

(-9 233 372 036 854 775 808,9 223 372 036 854 775 807)

(0,18 446 744 073 709 551 615)

极大整数值

TINYINT:

默认情况下带正负号,如果存储的数值超过该类型可接受的范围默认存储最大可接受数值

mysql> create table t(id int, num tinyint);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t values(1, -129), (2, -100), (3, 100), (4, 128);
Query OK, 4 rows affected, 2 warnings (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 2

-- 数据超过该数据类型范围时就只能存储支持的最大接收数值
mysql> select * from t;
+------+------+
| id   | num  |
+------+------+
|    1 |  128 |
|    2 |  100 |
|    3 |  100 |
|    4 |  127 |
+------+------+
4 rows in set (0.00 sec)

如果不想存入负数的话可以使用 unsigned 约束条件,只能存储正数;

mysql> create table t1(id int, num tinyint unsigned);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 values(1, -129), (2, -100),(3,100),(4, 128);
Query OK, 4 rows affected, 2 warnings (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 2

mysql> desc t1;
+-------+---------------------+------+-----+---------+-------+
| Field | Type                | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id    | int(11)             | YES  |     | NULL    |       |
| num   | tinyint(3) unsigned | YES  |     | NULL    |       |
+-------+---------------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> select * from t1;
+------+------+
| id   | num  |
+------+------+
|    1 |    0 |
|    2 |    0 |
|    3 |  100 |
|    4 |  128 |
+------+------+
4 rows in set (0.00 sec)

INT

默认也是带有正负号,可以通过unsigned约束条件限制只存储正数:

mysql> create table t2(id int(8));
Query OK, 0 rows affected (0.01 sec)

在介绍创建表的语法时介绍在数据类型后的小括号中的数字表示宽度,针对其他类型来讲宽度就是字符的长度,但是对整形来讲宽度指的是字节数,比如上述创建表时定义int(8)如果数字没有超出8位,默认用空格填充至8位,如果数字超出了8位,那么有几位就存几位(但是还是要遵循最大范围)

mysql> create table t2(id int(8));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t2 values(123), (123456789);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from t2;
+-----------+
| id        |
+-----------+
|       123 |
| 123456789 |
+-----------+
2 rows in set (0.00 sec)

使用空格填充的方式不是很方便看出是否被填充,我们可以使用zerofill约束条件,用0进行填充:

mysql> create table t3(id int(8) zerofill);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t3 values(1),(123456789);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from t3;
+-----------+
| id        |
+-----------+
|  00000001 |
| 123456789 |
+-----------+
2 rows in set (0.00 sec)

总结来说,针对整形字段括号内无需指定宽度,因为默认的宽度以及足够显示所有的数据了。

小数类型

对于小数的表示,MySQL分为两种方式:浮点数和定点数。浮点数包括float(单精度)和double(双精度),而定点数只有decimal一种,在MySQL中以字符串的形式存放,比浮点数更精确,适合用来表示货币等精度高的数据。

# 存储限制
float(255,30)# 总共255位,小数部分占30位
duble(255,30)# 总共255位,小数部分占30位
decimal(65,30)# 总共65位,小数部分占30位


# 精确度验证
create table t(id float(255,30));
create table t(id double(255,30));
create table t(id decimal(65,30));

insert into t values(1.1111111111111111);

# 精确度比较,结合实际应用场景,看情况
float<double<decimal

字符类型

字符串类型指CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT。这里介绍CHAR和VARCHAR两种,这两种类型类似,但它们保存和检索的方式不同。它们的最大长度和是否尾部空格被保留等方面也不同。在存储或检索过程中不进行大小写转换。

类型

大小

用途

CHAR

0-255字节

定长字符串

VARCHAR

0-65535 字节

变长字符串

TINYBLOB

0-255字节

不超过 255 个字符的二进制字符串

TINYTEXT

0-255字节

短文本字符串

BLOB

0-65 535字节

二进制形式的长文本数据

TEXT

0-65 535字节

长文本数据

MEDIUMBLOB

0-16 777 215字节

二进制形式的中等长度文本数据

MEDIUMTEXT

0-16 777 215字节

中等长度文本数据

LONGBLOB

0-4 294 967 295字节

二进制形式的极大文本数据

LONGTEXT

0-4 294 967 295字节

极大文本数据

CHAR列的长度固定为创建表是声明的长度,范围(0-255),而VARCHAR的值是可变长字符串范围(0-65535)。

mysql> create table t4 (v varchar(4),c char(4));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t4 values ('ab  ','ab  ');
Query OK, 1 row affected (0.00 sec)

# 在检索的时候char数据类型会去掉空格
mysql> select * from t4;
+------+------+
| v    | c    |
+------+------+
| ab   | ab   |
+------+------+
row in set (0.00 sec)

# 对查询结果计算长度
mysql> select length(v),length(c) from t4;
+-----------+-----------+
| length(v) | length(c) |
+-----------+-----------+
|         4 |         2 |
+-----------+-----------+
row in set (0.00 sec)

# 当存储的长度超出定义的长度,会截断
mysql> insert into t4 values ('abcd  ','abcd  ');
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> select * from t4;
+------+------+
| v    | c    |
+------+------+
| ab   | ab   |
| abcd | abcd |
+------+------+
rows in set (0.00 sec)

日期类型

表示时间值的日期和时间类型为DATETIME、DATE、TIME和YEAR。每个时间类型有一个有效值范围和一个”零”值,当指定不合法的MySQL不能表示的值时使用”零”值。

date/time/datetime

mysql> create table t4 (d date,t time,dt datetime);
Query OK, 0 rows affected (0.02 sec)

mysql> desc t4;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| d     | date     | YES  |     | NULL    |       |
| t     | time     | YES  |     | NULL    |       |
| dt    | datetime | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
rows in set (0.01 sec)

mysql> insert into t4 values (now(),now(),now());  -- now()表示现在的时间
Query OK, 1 row affected, 1 warning (0.01 sec)

mysql> select * from t4;
+------------+----------+---------------------+
| d          | t        | dt                  |
+------------+----------+---------------------+
| 2021-06-15 | 14:31:21 | 2021-06-15 14:31:21 |
+------------+----------+---------------------+
row in set (0.00 sec)

枚举与集合类型

ENUM中文名称叫枚举类型,它的值范围需要在创建表时通过枚举方式显示。ENUM只允许从值集合中选取单个值,而不能一次取多个值也不能插入枚举中没有的值。简而言之就是多选一

SET和ENUM非常相似,set类型可以允许值集合中任意选择1或多个元素进行组合。对不属于集合的内容将不允许插入数据,而对重复的值将进行自动去重。是多选多

枚举和集合在创建表的时候都可以使用default指定默认值,当然也可以不指定。

-- 枚举字段:后期在存数据的时候只能从枚举里面选择一个存储
create table user(
	id int,
    name char(16),
    gender enum('male','female','other') default 'other'

);
insert into user values(1,'jason','male');
insert into user valuses(2,'egon','xxxooo') # 报错


-- 集合:可以只选一个,也可以选多个,但是不能写没有列举的
create table teacher(
	id int,
    name char(16),
    gender enum('male','female','others'),
    hobby set('read','hecha')
)
insert into teacher values(1,'python','male','read,hecha');
insert into teacher values(2,'java','others','生蚝'); # 报错

约束条件

约束条件是创建表时可选的,在上文中介绍了两个约束条件,分别是unsignedzerofill,一个是用来约束数字类型不存负数另一个是使用0进行填充,接下来就来介绍一下其他约束条件。

unique唯一

指定某列或者某几列的组合数据不能重复,即单列唯一和多列联合唯一:

-- 单列唯一,id列插入重复的数据就会报错
mysql> create table t(id int unique, name varchar(4));
Query OK, 0 rows affected (0.10 sec)

mysql> insert into t values(1, 'python'),(2, 'java');
Query OK, 2 rows affected, 1 warning (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 1

mysql> insert into t values(1, 'python'),(2, 'java');
ERROR 1062 (23000): Duplicate entry '1' for key 'id'

-- 联合唯一,比如ip和port单个都可以重复,但是加起来必须是唯一的
mysql> create table t(id int unique, ip varchar(12), port varchar(4), unique(ip, port));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t values(1,'127.0.0.1',8080);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t values(2,'127.0.0.1',8081);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t values(3,'127.0.0.2',8080);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t values(4,'127.0.0.1',8080);
ERROR 1062 (23000): Duplicate entry '127.0.0.1-8080' for key 'ip'

not null非空

该约束条件的意思就是在向表中插入数据的时候,有约定条件not null的列值不能为空,否则会报错。

mysql> create table t1(id int not null, name varchar(4));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 (id, name) values (1, 'python');
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> insert into t1 (id, name) values (1, null);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t1 (id, name) values (null, null);
ERROR 1048 (23000): Column 'id' cannot be null

primary key主键

该约束条件非常重要,但从约束条件的角度来看primary key的效果等价于not null + unique,即非空且唯一,主键除了有上述约束效果之外,还是innodb引擎组织存储数据的依据,innodb存储引擎在创建表的时候必须要有primary key,它类似与于的目录,能够帮助提示查询效率并且也是建表的依据。

一张表必须设置主键,并且一张表中有且只有一个主键,如果你没有设置主键,那么会从上往下搜索直到遇到一个非空且唯一的字段将它自动升级为主键。

mysql> create table t2(id int not null unique, name varchar(4));
Query OK, 0 rows affected (0.01 sec)

mysql> desc t2;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id    | int(11)    | NO   | PRI | NULL    |       |
| name  | varchar(4) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

如果表内没有主键也没有其他任何的非空且唯一的字段,那么innodb会采用自己内部提供的一个隐藏字段作为主键,隐藏意味无法使用,就无法提升查询速度。

一张表中通常都应该有一个主键字段,并且通常将id/uid/sid字段作为主键。

-- 单一主键
mysql> create table t3(id int primary key);
Query OK, 0 rows affected (0.01 sec)

mysql> desc t3;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   | PRI | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.01 sec)

-- 联合主键
mysql> create table t4(ip varchar(16), port varchar(6), primary key(ip, port));
Query OK, 0 rows affected (0.07 sec)

mysql> desc t4;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ip    | varchar(16) | NO   | PRI |         |       |
| port  | varchar(6)  | NO   | PRI |         |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

auto_increment自增

通常设置主键是将与表中的数据没有关联的数据作为主键,一般选择数字,就像排队时的号码牌,因此主键最好选择整数类型并且能够自增,而auto_increment约束条件就是加在主键字段上的,并且在插入数据的时候主键的值无需手动插入了。

-- 创建表的完整语法
mysql> create table t5(id int primary key auto_increment, name varchar(4), age int(3), phone varchar(11));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t5 (name, age, phone) values ('python', 5, '119'),('java', 10, '120');
Query OK, 2 rows affected, 1 warning (0.15 sec)
Records: 2  Duplicates: 0  Warnings: 1

mysql> select * from t5;
+----+------+------+-------+
| id | name | age  | phone |
+----+------+------+-------+
|  1 | pyth |    5 | 119   |
|  2 | java |   10 | 120   |
+----+------+------+-------+
2 rows in set (0.00 sec)

mysql> desc t5;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(4)  | YES  |     | NULL    |                |
| age   | int(3)      | YES  |     | NULL    |                |
| phone | varchar(11) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

修改表结构

-- 修改表名:alter table 表名 rename 新表名;
mysql> create table t1(id int unique);
Query OK, 0 rows affected (0.01 sec)

mysql> alter table t1 rename t;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+--------------+
| Tables_in_ex |
+--------------+
| t            |
+--------------+
1 row in set (0.00 sec)


-- 表增加字段: alter table 表名 add 字段名 数据类型(宽度) 约束条件;
mysql> alter table t add name varchar(4) not null;
Query OK, 0 rows affected (0.10 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id    | int(11)    | YES  | UNI | NULL    |       |
| name  | varchar(4) | NO   |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
2 rows in set (1.05 sec)

-- 表删除字段,字段删除后数据也会删除: alter table 表名 drop 字段名;
mysql> alter table t drop name;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  | UNI | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.01 sec)

-- 表修改字段名:alter table 表名 change 旧字段名 新字段名 数据类型(宽度) 约束条件;
mysql> alter table t change id uid int unique;
Query OK, 0 rows affected, 1 warning (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 1

mysql> desc t;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| uid   | int(11) | NO   | PRI | 0       |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.01 sec)

-- 修改字段数据类型、约束条件等:alter table 表名 modify 字段名 数据类型(宽度) 约束条件;
mysql> alter table t modify id int primary key auto_increment;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t;
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| id    | int(11) | NO   | PRI | NULL    | auto_increment |
+-------+---------+------+-----+---------+----------------+
1 row in set (0.01 sec)

-- 修改字段排列顺序
ALTER TABLE 表名 CHANGE 字段名  旧字段名 新字段名 新数据类型 [完整性约束条件…]  FIRST;
ALTER TABLE 表名 MODIFY 字段名  数据类型 [完整性约束条件…]  AFTER 字段名;

-- 增加字段时指定字段位置
ALTER TABLE 表名 ADD 字段名  数据类型 [完整性约束条件…]  FIRST;
ALTER TABLE 表名 ADD 字段名  数据类型 [完整性约束条件…]  AFTER 字段名;

-- 操作主键,如果发现主键建错了,可以先删除主键再增加主键,需要注意在增加主键前必须把重复主键值删除
alter table t drop primary key;
alter table t add primary key(id);

删除表

删除操作一定要谨慎,时刻谨记删库跑路的教训哦~

-- 切换到数据库下
use study;

-- 删除表
drop table 表名;

-- 删除表中的数据
delete from 表名;  -- 删除表中的所有数据,但是主键的自增不会停止
truncate 表名;  -- 清空表数据并重置主键