Skip to main content

表操作

表操作

在进行表相关操作之前,先要切换到具体的数据库:

use db_name;

查看表

// 查看 db_name 中所有的表
show tables;
// show tables from db_name;

// 查看表结构信息
desc table_name;

创建表

创建表的语法格式为:

create table if not exists table_name (
<字段1> <数据类型(长度)>,
<字段2> <数据类型(长度)> <字段约束>,
<字段3> <数据类型(长度)> <字段约束>
)engine=engine_name

例如,创建一个 tb_students 数据表:

create table if not exists tb_students (
stu_no char(8) not null unique,
stu_name varchar(10) not null,
stu_gender char(2) not null,
stu_age int not null,
stu_phone char(11) not null unique,
stu_wechat varchar(50) unique
)

show tables;
/*
+-------------------+
| Tables_in_db_name |
+-------------------+
| tb_students |
+-------------------+
*/

desc tb_students;
/*
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| stu_no | char(8) | NO | PRI | NULL | |
| stu_name | varchar(10) | NO | | NULL | |
| stu_gender | char(2) | NO | | NULL | |
| stu_age | int | NO | | NULL | |
| stu_phone | char(11) | NO | UNI | NULL | |
| stu_wechat | varchar(50) | YES | UNI | NULL | |
+------------+-------------+------+-----+---------+-------+
*/
  • not null 表示该字段为必填项,unique 表示该字段的数据不可重复存入。
  • 字段用下划线做单词分隔,不要使用大小写组合,最后一个字段不能写逗号。
  • db_ 命名是用于数据库,数据表一般使用 tb_,具体根据实际情况而定。
  • 字段的顺序:<字段名> <数据类型(长度)> <约束集合>

删除表

使用 drop 关键字进行表删除:

drop table if exists table_name;
// drop table table_name;

修改表

修改表名
alter table <原表名> rename <新表名>;
// rename table <原表名> to <新表名>;
修改字符集
alter table table_name character set uf8;
新增字段

新增字段的语法格式为:

alter table table_name add <字段> <字段类型> <字段约束> <位置>; 

默认情况下会将新增的字段放到最后面,如果想让新增的字段放到最前面或者某个字段之后,可使用 firstafter 关键字:

// 放到最后面
alter table tb_students add qq_account varchar(15) unique;
// 放到最前面
alter table tb_students add weibo_account varchar(15) unique first;
// 放到某个字段之后
alter table tb_students add douyin_account varchar(15) unique after stu_no;
/*
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| weibo_account | varchar(15) | YES | UNI | NULL | |
| stu_no | char(8) | NO | PRI | NULL | |
| douyin_account | varchar(15) | YES | UNI | NULL | |
| stu_name | varchar(10) | NO | | NULL | |
| stu_gender | char(2) | NO | | NULL | |
| stu_age | int | NO | | NULL | |
| stu_phone | char(11) | NO | UNI | NULL | |
| stu_wechat | varchar(50) | YES | UNI | NULL | |
| qq_account | varchar(15) | YES | UNI | NULL | |
+----------------+-------------+------+-----+---------+-------+
*/
修改字段

可通过 modify 关键字修改字段数据类型,语法为:

alter table table_name modify <字段> <数据类型>

例如:

alter table tb_students modify stu_name varchar(50);

此外,可通过 change 关键字修改字段名称和数据类型,语法为:

alter table table_name change <原字段名称> <新字段名称> <新数据类型>

例如:

alter table tb_students change qq_account stu_qq_account varchar(15);
删除字段

删除字段可通过 drop 关键字:

alter table table_name drop <字段>;

例如:

alter table tb_students drop douyin_account;
修改存储引擎
alter table table_name engine=<引擎名称>;