## 一、数据库的操作 首先检查是否有相同名字的数据库,展示数据库中的所含有的数据库的名称 ```mysql show databases; ``` ![image-20220308081529937](https://lsky.hhdxw.top/imghub/img/image-20220308081529937.png) 有名为:yggl 的数据库了,于是创建名为:yggl2 的数据库来完成以下操作。 创建名为:yggl2 的数据库 ```mysql create database yggl2; ``` 进入数据库 yggl2 ```mysql use yggl2 ``` ![image-20220308081928547](https://lsky.hhdxw.top/imghub/img/image-20220308081928547.png) 首先创建主表 departments(涉及到外键所以先创建) ```mysql create table departments( departmentid char(3) not null primary key, departmentname char(20) not null, note text(16) null ); ``` 如下图,创建完成主表 departments ![image-20220308083216423](https://lsky.hhdxw.top/imghub/img/image-20220308083216423.png) 然后检查是否创建成功表 ```mysql show tables; ``` ![image-20220308083338984](https://lsky.hhdxw.top/imghub/img/image-20220308083338984.png) 查看 departments 中各字段的内容 ```mysql describe departments; //也可 desc departments; ``` ![image-20220308083540263](https://lsky.hhdxw.top/imghub/img/image-20220308083540263.png) 创建从表 employees (day中不允许写长度) ```mysql create table employees( employeeid char(6) not null primary key, name char(10) not null, education char(4) not null, birthday date not null, sex char(2) not null, workyear tinyint(1) null, address varchar(20) null, phonenumber char(12) null, departmentid char(3) not null, constraint fr_deptid foreign key(departmentid) references departments(departmentid) ); //字段名可以不同,但是数据类型必须一致 //给外键起的名字fr_deptid ``` 创建完成如下图: ![image-20220308085743344](https://lsky.hhdxw.top/imghub/img/image-20220308085743344.png) 创建表 salary ```mysql create table salary( employeeid char(6) not null primary key, income float(8) not null, outcome float(8) not null ); ``` 如图创建成功 ![image-20220308090204366](https://lsky.hhdxw.top/imghub/img/image-20220308090204366.png) ## 二、方法 ### 1.修改表名(rename): ```mysql alter table salary rename salary2 salary 修改前 salary2 修改后 ``` ![image-20220308090629743](https://lsky.hhdxw.top/imghub/img/image-20220308090629743.png) ### 2.修改字段的数据类型(modify): ```mysql alter table employees modify name varchar(20); employees 表名 name 字段名 varchar(20) 数据类型 ``` ![image-20220308091240600](https://lsky.hhdxw.top/imghub/img/image-20220308091240600.png) ### 3.修改字段名(change): ```mysql alter table employees change name employees_name varchar(20); employees 表名 name 修改前字段名 employees_name 修改后字段名 varchar(20) 数据类型 //数据类型不能为空,但是可以和修改前一样,也可同时修改 ``` ![image-20220308094202062](https://lsky.hhdxw.top/imghub/img/image-20220308094202062.png) 说明:change 也可以指修改数据类型,实现和 modify 相同的效果,方法是将 sql 语句中的新字段名和旧字段名设置为相同的名称,只修改数据类型。 ### 4.添加字段(add): ```mysql alter table salary add shuishou float(8) not null first; salary 添加的表名 shuishou 添加的字段 float(8) 添加自段的类型 not null 约束条件(包括外键,主键等等) first 添加表的位置 (可选项) after 字段名 在字段名之后添加 ``` ![image-20220308094307879](https://lsky.hhdxw.top/imghub/img/image-20220308094307879.png) ### 5.删除字段 (drop): ```mysql alter table salary drop shuishou; salary 表名 shuishou 字段名 ``` ![image-20220308094428794](https://lsky.hhdxw.top/imghub/img/image-20220308094428794.png) ### 6.修改字段排列顺序(modify): ```mysql alter table salary modify shuishou float after income; salary 表名 shuishou float 需修改顺序的字段名以及数据类型 income 目标字段之后(first最前面) ``` ![image-20220308094400851](https://lsky.hhdxw.top/imghub/img/image-20220308094400851.png) ### 7.添加外键约束(add): ```mysql alter table salary add foreign key (employeeid) references employees (employeeid); salary 修改表 (employeeid) 字段名 employees 参考主表 (employeeid) 参考主表字段名 ``` ### 8.添加主键约束(add): ``` alter table salary add primary key (employeeid) references employees (employeeid); salary 修改表 (employeeid) 字段名 employees 参考主表 (employeeid) 参考主表字段名 ``` ### 9.修改表的存储引擎 (默认为InnoDB): ```mysql alter table 表名 engine=更改后存储引擎名; ``` 注意:外键不能跨引擎使用 ### 10.删除外键约束 ```mysql alter table 表名 drop foreign key 外键名字; ``` 若没有命名外键名,则课可以先使用**show create table 表名**查询表的默认外键名,此命令不仅可以查询外键名,还可以查询表的结构、存储引擎、编码方式等信息 查询到默认外键名后,再用drop删除即可,即 alter table 表名 drop foreign key 查询到的默认外键名; ![image-20220308110246033](https://lsky.hhdxw.top/imghub/img/image-20220308110246033.png) ### 11.删除数据表 ```mysql drop table 表名; ``` **注意:当存在外键约束时,主表不能直接删除,需要先删除外键约束再删表** ![image-20220308110515138](https://lsky.hhdxw.top/imghub/img/image-20220308110515138.png)