2023-08-31 11:30:31 +08:00
|
|
|
|
# 综合案例1——数据表的基本操作
|
|
|
|
|
|
|
|
|
|
在全面介绍了MySQL中数据表的各种操作(如创建表、添加各类约束,查看表结构,以及修改和删除表)后,同学们应该掌握这些基本操作,为以后的学习打下坚实的基础。在这里,给出一个综合案例,让同学们全面回顾一下本章的知识要点,并通过这些操作来检验自己是否已经掌握了数据表的常用操作。
|
|
|
|
|
|
|
|
|
|
## 1、案例目的
|
|
|
|
|
|
|
|
|
|
创建、修改和删除表,掌握数据表的基本操作。
|
|
|
|
|
|
|
|
|
|
创建数据库company,按照表1和表2给出的表结构在company数据库中创建两个数据表offices和employees,按照操作过程完成对数据表的基本操作。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 表1 offices表结构
|
|
|
|
|
|
|
|
|
|
| 字段名 | 数据类型 | 主键 | 外键 | 非空 | 唯一 | 自增 |
|
|
|
|
|
| ---------- | ------------- | ---- | ---- | ---- | ---- | ---- |
|
|
|
|
|
| officecode | int(10) | 是 | 否 | 是 | 是 | 否 |
|
|
|
|
|
| city | int(11) | 否 | 否 | 是 | 否 | 否 |
|
|
|
|
|
| address | varchar(50) | 否 | 否 | 否 | 否 | 否 |
|
|
|
|
|
| country | varchar(50) | 否 | 否 | 是 | 否 | 否 |
|
|
|
|
|
| postalcode | varchar(25) | 否 | 否 | 否 | 是 | 否 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 表2 employees表结构
|
|
|
|
|
|
|
|
|
|
| 字段名 | 数据类型 | 主键 | 外键 | 非空 | 唯一 | 自增 |
|
|
|
|
|
| -------------- | -------------- | ---- | ---- | ---- | ---- | ---- |
|
|
|
|
|
| employeenumber | int(11) | 是 | 否 | 是 | 是 | 是 |
|
|
|
|
|
| lastname | varchar(50) | 否 | 否 | 是 | 否 | 否 |
|
|
|
|
|
| firstname | varchar(50) | 否 | 否 | 是 | 否 | 否 |
|
|
|
|
|
| mobile | varchar(25) | 否 | 否 | 否 | 是 | 否 |
|
|
|
|
|
| officecode | int(10) | 否 | 是 | 是 | 否 | 否 |
|
|
|
|
|
| jobtitle | varchar(50) | 否 | 否 | 是 | 否 | 否 |
|
|
|
|
|
| birth | datetime | 否 | 否 | 是 | 否 | 否 |
|
|
|
|
|
| note | varchar(255) | 否 | 否 | 否 | 否 | 否 |
|
|
|
|
|
| sex | varchar(5) | 否 | 否 | 否 | 否 | 否 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 2、案例操作过程
|
|
|
|
|
|
|
|
|
|
### (1)登录MySQL数据库。
|
|
|
|
|
|
|
|
|
|
```mysql
|
|
|
|
|
mysql -h local -u root -p
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
截图:
|
|
|
|
|
|
2023-08-31 12:43:36 +08:00
|
|
|
|
![img](https://lsky.hhdxw.top/imghub/img/wpsD882.tmp.jpg)
|
2023-08-31 11:30:31 +08:00
|
|
|
|
|
|
|
|
|
### (2)创建数据库company并选择使用此数据库。
|
|
|
|
|
|
|
|
|
|
```mysql
|
|
|
|
|
create database company;
|
|
|
|
|
|
|
|
|
|
use company;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
截图:
|
|
|
|
|
|
2023-08-31 12:43:36 +08:00
|
|
|
|
![img](https://lsky.hhdxw.top/imghub/img/wpsD883.tmp.jpg)
|
2023-08-31 11:30:31 +08:00
|
|
|
|
|
|
|
|
|
### (3)创建表offices,创建成功后用desc查看表结构。
|
|
|
|
|
|
|
|
|
|
```mysql
|
|
|
|
|
create table offices(
|
|
|
|
|
officecode int(10) not null unique primary key,
|
|
|
|
|
city int(11) not null ,
|
|
|
|
|
address varchar(50) null ,
|
|
|
|
|
country varchar(50) not null,
|
|
|
|
|
postalcode varchar(25) null unique
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
desc offices;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
截图:
|
|
|
|
|
|
2023-08-31 12:43:36 +08:00
|
|
|
|
![img](https://lsky.hhdxw.top/imghub/img/wpsD884.tmp.jpg)
|
2023-08-31 11:30:31 +08:00
|
|
|
|
|
|
|
|
|
### (4)创建表employees,创建成功后用desc查看表结构。
|
|
|
|
|
|
|
|
|
|
```mysql
|
|
|
|
|
create table employees(
|
|
|
|
|
employeenumber int(11) not null unique auto_increment primary key,
|
|
|
|
|
lastname varchar(50) not null,
|
|
|
|
|
firstname varchar(50) not null,
|
|
|
|
|
mobile varchar(25) null unique,
|
|
|
|
|
officecode int(10) not null ,
|
|
|
|
|
birth datetime not null,
|
|
|
|
|
note varchar(255) null,
|
|
|
|
|
sex varchar(5) null,
|
|
|
|
|
foreign key(officecode) references offices(officecode)
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
截图:
|
|
|
|
|
|
2023-08-31 12:43:36 +08:00
|
|
|
|
![image-20220308170243379](https://lsky.hhdxw.top/imghub/img/image-20220308170243379.png)
|
2023-08-31 11:30:31 +08:00
|
|
|
|
|
|
|
|
|
### (5)使用show tables命令查看数据库中的表。
|
|
|
|
|
|
|
|
|
|
```mysql
|
|
|
|
|
show tables
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
截图:
|
|
|
|
|
|
2023-08-31 12:43:36 +08:00
|
|
|
|
![image-20220308170414341](https://lsky.hhdxw.top/imghub/img/image-20220308170414341.png)
|
2023-08-31 11:30:31 +08:00
|
|
|
|
|
|
|
|
|
### (6)将表employees的mobile字段修改到officecode字段后面,成功后使用desc查看修改后的表结构。
|
|
|
|
|
|
|
|
|
|
```mysql
|
|
|
|
|
alter table employees modify mobile varchar(25) after officecode;
|
|
|
|
|
|
|
|
|
|
desc employees;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
截图:
|
|
|
|
|
|
2023-08-31 12:43:36 +08:00
|
|
|
|
![image-20220308170720963](https://lsky.hhdxw.top/imghub/img/image-20220308170720963.png)
|
2023-08-31 11:30:31 +08:00
|
|
|
|
|
|
|
|
|
### (7)将表employees的birth字段名改为employee_birth,成功后使用desc查看修改后的表结构。
|
|
|
|
|
|
|
|
|
|
```mysql
|
|
|
|
|
alter table employees change birth employees_birth datetime;
|
|
|
|
|
|
|
|
|
|
desc employees;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
截图:
|
|
|
|
|
|
2023-08-31 12:43:36 +08:00
|
|
|
|
![image-20220308171008325](https://lsky.hhdxw.top/imghub/img/image-20220308171008325.png)
|
2023-08-31 11:30:31 +08:00
|
|
|
|
|
|
|
|
|
### (8)修改sex字段,数据类型为char(1),非空约束,成功后使用desc查看修改后的表结构。
|
|
|
|
|
|
|
|
|
|
```mysql
|
|
|
|
|
alter table employees modify sex char(1) not null;
|
|
|
|
|
|
|
|
|
|
desc employees;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
截图:
|
|
|
|
|
|
2023-08-31 12:43:36 +08:00
|
|
|
|
![image-20220308171528384](https://lsky.hhdxw.top/imghub/img/image-20220308171528384.png)
|
2023-08-31 11:30:31 +08:00
|
|
|
|
|
|
|
|
|
### (9)删除字段note,成功后使用desc查看修改后的表结构。
|
|
|
|
|
|
|
|
|
|
```mysql
|
|
|
|
|
alter table employees drop note;
|
|
|
|
|
|
|
|
|
|
desc employees;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
截图:
|
|
|
|
|
|
2023-08-31 12:43:36 +08:00
|
|
|
|
![image-20220308171326509](https://lsky.hhdxw.top/imghub/img/image-20220308171326509.png)
|
2023-08-31 11:30:31 +08:00
|
|
|
|
|
|
|
|
|
### (10)增加字段名favoriate_activity,数据类型为varchar(100) ,成功后使用desc查看修改后的表结构。
|
|
|
|
|
|
|
|
|
|
```mysql
|
|
|
|
|
alter table employees add favoriate_activity varchar(100);
|
|
|
|
|
|
|
|
|
|
desc employees;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
截图:
|
|
|
|
|
|
2023-08-31 12:43:36 +08:00
|
|
|
|
![image-20220308171746485](https://lsky.hhdxw.top/imghub/img/image-20220308171746485.png)
|
2023-08-31 11:30:31 +08:00
|
|
|
|
|
|
|
|
|
### (11)删除表offices,操作成功后用show tables查看数据库中的表。
|
|
|
|
|
|
|
|
|
|
```mysql
|
|
|
|
|
drop table offices;
|
|
|
|
|
|
|
|
|
|
alter table employees drop foreign key employees_ibfk_1;
|
|
|
|
|
|
|
|
|
|
drop table offices;
|
|
|
|
|
|
|
|
|
|
show tables;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
截图:
|
|
|
|
|
|
2023-08-31 12:43:36 +08:00
|
|
|
|
![image-20220308172132982](https://lsky.hhdxw.top/imghub/img/image-20220308172132982.png)
|
2023-08-31 11:30:31 +08:00
|
|
|
|
|
|
|
|
|
### (12)修改表employees存储引擎为myisam,执行成功后用show create table语句查看表结构。
|
|
|
|
|
|
|
|
|
|
```mysql
|
|
|
|
|
alter table employees engine = myisam;
|
|
|
|
|
|
|
|
|
|
show create table employees;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
截图:
|
|
|
|
|
|
2023-08-31 12:43:36 +08:00
|
|
|
|
![image-20220308172645359](https://lsky.hhdxw.top/imghub/img/image-20220308172645359.png)
|
2023-08-31 11:30:31 +08:00
|
|
|
|
|
|
|
|
|
### (13)将表employees名称修改为employees_info,执行成功后用show tables查看数据库中的表。
|
|
|
|
|
|
|
|
|
|
```mysql
|
|
|
|
|
alter table employees rename employees_info;
|
|
|
|
|
|
|
|
|
|
show tables;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
截图:
|
|
|
|
|
|
2023-08-31 12:43:36 +08:00
|
|
|
|
![image-20220308172838692](https://lsky.hhdxw.top/imghub/img/image-20220308172838692.png)
|