實驗二 數據庫和表的創建與管理
實驗二 數據庫和表的創建與管理
創建用於企業管理的員工管理數據庫,數據庫名為YGGL中,YGGL數據庫中包括三個表:Employees(員工信息表)、Departments(部門信息表)、Salary(員工薪水情況表)。各表的結構如下表:
表1 Employees表結構
列名 |
數據類型 |
長度 |
是否允許為空 |
說明 |
EmployeeID |
char |
6 |
not null |
員工編號,主鍵 |
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 |
null |
員工部門號,外鍵 |
表2 Departments表結構
列名 |
數據類型 |
長度 |
是否允許為空 |
說明 |
DepartmentID |
char |
3 |
not null |
部門編號,主鍵 |
Departmentname |
char |
20 |
not null |
部門名 |
Note |
text |
16 |
null |
備註 |
表 3 Salary表結構
列名 |
數據類型 |
長度 |
是否允許為空 |
說明 |
EmployeeID |
char |
6 |
not null |
員工編號,主鍵 |
Income |
float |
8 |
not null |
收入 |
Outcome |
float |
8 |
not null |
支出 |
1、 創建數據庫YGGL;
Create database yggl;
2、 使用“show create database數據庫名”查看數據庫YGGL的字符集;
Show create database yggl;
3、 修改YGGL數據庫的默認字符集為utf8;
Set character_setdatabase=’utf-8’;
4、 在YGGL數據庫中創建表Employees;
Use yggl;
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) null
);
5、 使用“desc(describe的縮寫)表名”查看表信息;
Desc employees;
6、 使用“show create table 表名”查看創建表命令的詳細信息;
Show create table employees;
7、 在YGGL數據庫中創建表Departments;
Create table departments
(departments char(3) not null primary key,
Departmentname char(20) not null,
Note text(16) null
);
8、 在YGGL數據庫中創建表Salary;
Create table salary
(employeeid char(6) not null primary key,
Income float(8) not null,
Outcome float(8) not null
);
9、 在YGGL數據庫中創建表Salary1,要求使用數據庫存儲引擎為MyISAM,表結構與Salary相同;
Create table salary
(employeeid char(6) not null primary key,
Income float(8) not null,
Outcome float(8) not null
)engine=MyISAM;
10、 復制一個表Salary2,要求表結構與Salary表相同。
Create table salary2 like salary;
11、 在Salary1表中增加列:Salaries float;
Alter table salary1 add column salaries float;
12、 修改Salary1表中Income的默認值為2000,修改Salary表列名Salaries為“實發工資”;
Alter table salary1 alter income set default 2000;
Alter table salary change salaries truecome float;
13、 刪除列“實發工資”;
Alter table salary drop column truecome;
14、 修改表名Salary1為“薪水表”
Alter table salary1 rename to money;
15、 刪除表Salary1。
Drop table money;
實驗二 數據庫和表的創建與管理