1.6 表的操作

mysql> create database data;
Query OK, 1 row affected (0.00 sec)

mysql> use data;
Database changed

1.6.1 创建表

语法:

create table [if not exists] `表名`(
`字段名` 数据类型 [null|not null] [default] [auto_increment] [primary key] [comment],
`字段名 数据类型 …
)[engine=存储引擎] [charset=字符编码]

null|not null 是否为空
default: 默认值
auto_increment 自动增长,默认从1开始,每次递增1
primary key 主键,主键的值不能重复,不能为空,每个表必须只能有一个主键
comment: 备注
engine 引擎决定了数据的存储和查找 myisam、innodb

脚下留心:表名和字段名如果用了关键字,要用反引号引起来。

例题:

-- 设置客户端和服务器通讯的编码
mysql> set names gbk;
Query OK, 0 rows affected (0.00 sec)

-- 创建简单的表
mysql> create table stu1(
-> id int auto_increment primary key,
-> name varchar(20) not null
-> )engine=innodb charset=gbk;
Query OK, 0 rows affected (0.11 sec)

-- 创建复杂的表
mysql> create table stu2(
-> id int auto_increment primary key comment '主键',
-> name varchar(20) not null comment '姓名',
-> `add` varchar(50) not null default '地址不详' comment '地址',
-> score int comment '成绩,可以为空'
-> )engine=myisam;
Query OK, 0 rows affected (0.06 sec)

小结:

1、如果不指定引擎,默认是innodb

2、如果不指定字符编码,默认和数据库编码一致

3、varchar(20) 表示长度是20个字符

数据表的文件

一个数据库对应一个文件夹
一个表对应一个或多个文件
引擎是myisam,一个表对应三个文件
.frm :存储的是表结构
.myd :存储的是表数据
.myi :存储的表数据的索引
引擎是innodb,一个表对应一个表结构文件,innodb的都有表的数据都保存在ibdata1文件中,如果数据量很大,会自动的创建ibdata2,ibdata3...

innodb和myisam的区别

引擎

myisam

1、查询速度快

2、容易产生碎片

3、不能约束数据

innodb

1、以前没有myisam查询速度快,现在已经提速了

2、不产生碎片

3、可以约束数据

脚下留心:推荐使用innodb。

1.6.2 显示所有表

语法

show tables;

例题:

mysql> show tables;
Empty set (0.00 sec)

1.6.3 显示创建表的语句

语法

show create table;   -- 结果横着排列
show create table \G -- 将结果竖着排列

例题

数据库-表的操作_数据库

1.6.4 查看表结构

语法

desc[ribe] 表名

例题

-- 方法一
mysql> describe stu2;
+-------+-------------+------+-----+----------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+----------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| add | varchar(50) | NO | | 地址不详 | |
| score | int(11) | YES | | NULL | |
+-------+-------------+------+-----+----------+----------------+
4 rows in set (0.05 sec)

-- 方法二
mysql> desc stu2;
+-------+-------------+------+-----+----------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+----------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| add | varchar(50) | NO | | 地址不详 | |
| score | int(11) | YES | | NULL | |
+-------+-------------+------+-----+----------+----------------+
4 rows in set (0.00 sec)

1.6.5 复制表

**语法一:**create table 新表 select 字段 from 旧表

特点:不能复制父表的键,能够复制父表的数据

数据库-表的操作_表名_02

**语法二:**create table 新表 like 旧表

特点:只能复制表结构,不能复制表数据

数据库-表的操作_mysql_03

小结:

*表示所有字段

1.6.6 删除表

语法:

drop table [if exists] 表1,表2,…

例题:

-- 删除表
mysql> drop table stu4;
Query OK, 0 rows affected (0.06 sec)

-- 如果表存在就删除
mysql> drop table if exists stu4;
Query OK, 0 rows affected, 1 warning (0.00 sec)

-- 一次删除多个表
mysql> drop table stu2,stu3;
Query OK, 0 rows affected (0.03 sec)

1.6.7 修改表

语法:alter table 表名

创建初始表

mysql> create table stu(
-> id int,
-> name varchar(20)
-> );
Query OK, 0 rows affected (0.00 sec)

1、添加字段:alter table 表名add [column] 字段名 数据类型 [位置]

mysql> alter table stu add `add` varchar(20); -- 默认添加字段放在最后
Query OK, 0 rows affected (0.05 sec)

mysql> alter table stu add sex char(1) after name; -- 在name之后添加sex字段
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table stu add age int first; -- age放在最前面
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc stu;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| age | int(11) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| add | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

2、删除字段:alter table 表 drop [column] 字段名

mysql> alter table stu drop age;   -- 删除age字段
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

3、修改字段(改名):alter table 表 change [column] 原字段名 新字段名 数据类型 …

-- 将name字段更改为stuname varchar(10)
mysql> alter table stu change name stuname varchar(10);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc stu;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| stuname | varchar(10) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| add | varchar(20) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

4、修改字段(不改名):alter table 表 modify 字段名 字段属性…

-- 将sex数据类型更改为varchar(20)
mysql> alter table stu modify sex varchar(20);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

-- 将add字段更改为varchar(20) 默认值是‘地址不详’
mysql> alter table stu modify `add` varchar(20) default '地址不详';
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

5、修改引擎:alter table 表名 engine=引擎名

mysql> alter table stu engine=myisam;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

6、修改表名:alter table 表名 rename to 新表名

-- 将stu表名改成student
mysql> alter table stu rename to student;
Query OK, 0 rows affected (0.00 sec)

7、将表移动到其他数据库

-- 将当前数据库中的student表移动到php74数据库中改名为stu
mysql> alter table student rename to php74.stu;
Query OK, 0 rows affected (0.00 sec)