本学期在北京大学选修了《数据库概论》的实验班课程,由于 SQL 语法并不是特别理论的内容,因此课上暂时也没有特别展开。出于探索数据库领域的兴趣,使用国内普遍使用的数据库软件 MySQL 实践《数据库系统概念》中若干 SQL 语句主题;出于方便描述考虑,主要使用命令行界面操作。
MySQL 的安装
在 Mac OS X 下使用 brew install mysql 简单的安装 MySQL,其他的安装方式不作介绍。
大学模式的导入
首先打开 MySQL 的 Server 服务。
$ mysql.server start
Starting MySQL
. SUCCESS!
接着使用命令 mysqladmin -uroot create db-book 创建数据库 db-book 作为我们的试验场,这里 -uroot 根据具体的 MySQL 账号密码可能有所区别,默认情况下如此。
然后使用命令 mysql -uroot 接入 MySQL 命令行界面,输入 show databases; 看到输出的数据库列表中存在 db-book 确认数据库创建成功,输入 use db-book 切换到 db-book 数据库。
从 db-book.com 网站找到 MySQL 建表和插入小规模数据的 SQL 文件,输入下面命令执行。也可将末尾附录代码按照注释标记的文件名保存到当前目录的对应文件中。
mysql> \. DDL-MySQL+drop.sql
mysql> \. smallRelationsInsertFile.sql
最后按照下面方式测试,确认数据导入成功。
mysql> show tables;
+-------------------+
| Tables_in_db-book |
+-------------------+
| advisor |
| classroom |
| course |
| department |
| instructor |
| prereq |
| section |
| student |
| takes |
| teaches |
| time_slot |
+-------------------+
11 rows in set (0.00 sec)
mysql> select * from advisor;
+-------+-------+
| s_ID | i_ID |
+-------+-------+
| 12345 | 10101 |
| 44553 | 22222 |
| 45678 | 22222 |
| 00128 | 45565 |
| 76543 | 45565 |
| 23121 | 76543 |
| 98988 | 76766 |
| 76653 | 98345 |
| 98765 | 98345 |
+-------+-------+
9 rows in set (0.01 sec)
附录
把下面文件保存为 DDL-MySQL+drop.sql,执行时可能会因为 drop 命令报警告,这是因为新数据库没有表,不用太在意,忽略即可。
drop table prereq;
drop table time_slot;
drop table advisor;
drop table takes;
drop table student;
drop table teaches;
drop table section;
drop table instructor;
drop table course;
drop table department;
drop table classroom;
create table classroom
(building varchar(15),
room_number varchar(7),
capacity numeric(4,0),
primary key (building, room_number)
);
create table department
(dept_name varchar(20),
building varchar(15),
budget numeric(12,2) check (budget > 0),
primary key (dept_name)
);
create table course
(course_id varchar(8),
title varchar(50),
dept_name varchar(20),
credits numeric(2,0) check (credits > 0),
primary key (course_id),
foreign key (dept_name) references department(dept_name)
on delete set null
);
create table instructor
(ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
salary numeric(8,2) check (salary > 29000),
primary key (ID),
foreign key (dept_name) references department(dept_name)
on delete set null
);
create table section
(course_id varchar(8),
sec_id varchar(8),
semester varchar(6)
check (semester in ('Fall', 'Winter', 'Spring', 'Summer')),
year numeric(4,0) check (year > 1701 and year < 2100),
building varchar(15),
room_number varchar(7),
time_slot_id varchar(4),
primary key (course_id, sec_id, semester, year),
foreign key (course_id) references course(course_id)
on delete cascade,
foreign key (building, room_number) references classroom(building, room_number)
on delete set null
);
create table teaches
(ID varchar(5),
course_id varchar(8),
sec_id varchar(8),
semester varchar(6),
year numeric(4,0),
primary key (ID, course_id, sec_id, semester, year),
foreign key (course_id,sec_id, semester, year) references section(course_id,sec_id, semester, year)
on delete cascade,
foreign key (ID) references instructor(ID)
on delete cascade
);
create table student
(ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
tot_cred numeric(3,0) check (tot_cred >= 0),
primary key (ID),
foreign key (dept_name) references department(dept_name)
on delete set null
);
create table takes
(ID varchar(5),
course_id varchar(8),
sec_id varchar(8),
semester varchar(6),
year numeric(4,0),
grade varchar(2),
primary key (ID, course_id, sec_id, semester, year),
foreign key (course_id,sec_id, semester, year) references section(course_id,sec_id, semester, year)
on delete cascade,
foreign key (ID) references student(ID)
on delete cascade
);
create table advisor
(s_ID varchar(5),
i_ID varchar(5),
primary key (s_ID),
foreign key (i_ID) references instructor (ID)
on delete set null,
foreign key (s_ID) references student (ID)
on delete cascade
);
create table time_slot
(time_slot_id varchar(4),
day varchar(1),
start_hr numeric(2) check (start_hr >= 0 and start_hr < 24),
start_min numeric(2) check (start_min >= 0 and start_min < 60),
end_hr numeric(2) check (end_hr >= 0 and end_hr < 24),
end_min numeric(2) check (end_min >= 0 and end_min < 60),
primary key (time_slot_id, day, start_hr, start_min)
);
create table prereq
(course_id varchar(8),
prereq_id varchar(8),
primary key (course_id, prereq_id),
foreign key (course_id) references course(course_id)
on delete cascade,
foreign key (prereq_id) references course(course_id)
)
把下面的文件保存为 smallRelationsInsertFile.sql