文章目录
任务四 准备测试数据集
任务目标
为学习 openGauss DBMS 准备用于测试的数据库studentdb 及数据集。之后很多任务都需要用到这个测试数据集。
实施步骤
一、创建数据库用户 student
登陆数据库:
gsql -d postgres -p 26000 -r
创建student用户并配置表空间:
-- 创建用户student,密码设置为student@ustb2020,并授予用户student SYSADMIN权限
CREATE USER student IDENTIFIED BY 'student@ustb2020';
ALTER USER student SYSADMIN;
二、创建表空间 student_ts
在 gsql 中继续执行下面的语句,创建表空间 student_ts:
-- 创建表空间student_ts
CREATE TABLESPACE student_ts RELATIVE LOCATION 'tablespace/student_ts1';
三、创建数据库 studentdb
在 gsql 中继续执行下面的语句,创建数据库studentdb,并退出 gsql:
-- 创建数据库studentdb,数据库默认的表空间是student_ts
CREATE DATABASE studentdb WITH TABLESPACE =student_ts;
\q
如下图所示:
四、设置数据库 studentdb 的会话超时时间
使用 Linux 用户 omm,打开一个 Linux 终端窗口,执行如下命令,用用户student 连接 openGauss 的数据库studentdb:
gsql -d studentdb -h 192.168.100.91 -U student -p 26000 -W student@ustb2020 -r
执行下面的语句,设置会话的超时时间为86400s(24h),并退出gsql:
alter database studentdb set session_timeout=86400;
如下图所示:
五、创建大学应用模式表的脚本
create_db_tables.sql是用于创建大学数据库应用的脚本文件。
运行下面的命令创建脚本文件 create_db_tables.sql (生成的文件位于 /home/omm 目录下)
cat>~omm/create_db_tables.sql<<EOF
/*==============================================================*/
/* DBMS name: openGauss */
/* Created on: 2024/10/11 20:22:38 */
/*==============================================================*/
drop table if exists ADVISOR;
drop table if exists TAKES;
drop table if exists TEACHES;
drop table if exists PREREQ;
drop table if exists SECTION;
drop table if exists COURSE;
drop table if exists INSTRUCTOR;
drop table if exists STUDENT;
drop table if exists DEPARTMENT;
drop table if exists TIME_SLOT;
drop table if exists CLASSROOM;
/*==============================================================*/
/* Table: ADVISOR */
/*==============================================================*/
create table ADVISOR
(
STU_ID varchar(5) not null,
ID varchar(5),
primary key (STU_ID)
);
/*==============================================================*/
/* Table: CLASSROOM */
/*==============================================================*/
create table CLASSROOM
(
BUILDING varchar(15) not null,
ROOM_NUMBER varchar(7) not null,
CAPACITY numeric(4,0),
primary key (BUILDING, ROOM_NUMBER)
);
/*==============================================================*/
/* Table: COURSE */
/*==============================================================*/
create table COURSE
(
COURSE_ID varchar(8) not null,
DEPT_NAME varchar(20),
TITLE varchar(50),
CREDITS numeric(2,0),
primary key (COURSE_ID)
);
/*==============================================================*/
/* Table: DEPARTMENT */
/*==============================================================*/
create table DEPARTMENT
(
DEPT_NAME varchar(20) not null,
BUILDING varchar(15),
BUDGET numeric(12,2),
primary key (DEPT_NAME)
);
/*==============================================================*/
/* Table: INSTRUCTOR */
/*==============================================================*/
create table INSTRUCTOR
(
ID varchar(5) not null,
DEPT_NAME varchar(20),
NAME varchar(20) not null,
SALARY numeric(8,2),
primary key (ID)
);
/*==============================================================*/
/* Table: PREREQ */
/*==============================================================*/
create table PREREQ
(
COURSE_ID varchar(8) not null,
PRE_COURSE_ID varchar(8) not null,
primary key (COURSE_ID, PRE_COURSE_ID)
);
/*==============================================================*/
/* Table: SECTION */
/*==============================================================*/
create table SECTION
(
COURSE_ID varchar(8) not null,
SEC_ID varchar(8) not null,
SEMESTER varchar(6) not null,
YEAR numeric(4,0) not null,
BUILDING varchar(15),
ROOM_NUMBER varchar(7),
TIME_SLOT_ID varchar(4),
primary key (COURSE_ID, SEC_ID, SEMESTER, YEAR)
);
/*==============================================================*/
/* Table: STUDENT */
/*==============================================================*/
create table STUDENT
(
ID varchar(5) not null,
DEPT_NAME varchar(20),
NAME varchar(20) not null,
TOT_CRED numeric(3,0),
primary key (ID)
);
/*==============================================================*/
/* Table: TAKES */
/*==============================================================*/
create table TAKES
(
ID varchar(5) not null,
COURSE_ID varchar(8) not null,
SEC_ID varchar(8) not null,
SEMESTER varchar(6) not null,
YEAR numeric(4,0) not null,
GRADE varchar(2),
primary key (COURSE_ID, ID, SEC_ID, SEMESTER, YEAR)
);
/*==============================================================*/
/* Table: TEACHES */
/*==============================================================*/
create table TEACHES
(
ID varchar(5) not null,
COURSE_ID varchar(8) not null,
SEC_ID varchar(8) not null,
SEMESTER varchar(6) not null,
YEAR numeric(4,0) not null,
primary key (ID, COURSE_ID, SEC_ID, SEMESTER, YEAR)
);
/*==============================================================*/
/* Table: TIME_SLOT */
/*==============================================================*/
create table TIME_SLOT
(
TIME_SLOT_ID varchar(4) not null,
DAY varchar(1) not null,
START_HR numeric(2,0) not null,
START_MIN numeric(2,0) not null,
END_HR numeric(2,0),
END_MIN numeric(2,0),
primary key (TIME_SLOT_ID, DAY, START_HR, START_MIN)
);
alter table ADVISOR add constraint FK_SYS_C0011297 foreign key (ID)
references INSTRUCTOR (ID) on delete restrict on update restrict;
alter table ADVISOR add constraint FK_SYS_C0011298 foreign key (STU_ID)
references STUDENT (ID) on delete restrict on update restrict;
alter table COURSE add constraint FK_SYS_C0011276 foreign key (DEPT_NAME)
references DEPARTMENT (DEPT_NAME) on delete restrict on update restrict;
alter table INSTRUCTOR add constraint FK_SYS_C0011280 foreign key (DEPT_NAME)
references DEPARTMENT (DEPT_NAME) on delete restrict on update restrict;
alter table PREREQ add constraint FK_SYS_C0011305 foreign key (COURSE_ID)
references COURSE (COURSE_ID) on delete restrict on update restrict;
alter table PREREQ add constraint FK_SYS_C0011306 foreign key (PRE_COURSE_ID)
references COURSE (COURSE_ID) on delete restrict on update restrict;
alter table SECTION add constraint FK_SYS_C0011284 foreign key (COURSE_ID)
references COURSE (COURSE_ID) on delete restrict on update restrict;
alter table SECTION add constraint FK_SYS_C0011285 foreign key (BUILDING, ROOM_NUMBER)
references CLASSROOM (BUILDING, ROOM_NUMBER) on delete restrict on update restrict;
alter table STUDENT add constraint FK_SYS_C0011292 foreign key (DEPT_NAME)
references DEPARTMENT (DEPT_NAME) on delete restrict on update restrict;
alter table TAKES add constraint FK_SYS_C0011294 foreign key (COURSE_ID, SEC_ID, SEMESTER, YEAR)
references SECTION (COURSE_ID, SEC_ID, SEMESTER, YEAR) on delete restrict on update restrict;
alter table TAKES add constraint FK_SYS_C0011295 foreign key (ID)
references STUDENT (ID) on delete restrict on update restrict;
alter table TEACHES add constraint FK_SYS_C0011287 foreign key (ID)
references INSTRUCTOR (ID) on delete restrict on update restrict;
alter table TEACHES add constraint FK_SYS_C0011288 foreign key (COURSE_ID, SEC_ID, SEMESTER, YEAR)
references SECTION (COURSE_ID, SEC_ID, SEMESTER, YEAR) on delete restrict on update restrict;
EOF
六、创建未大学应用模式表填充测试数据的脚本
loaddata.sql是将数据装载到大学数据库的脚本文件。
运行下面的命令创建脚本文件 create_db_tables.sql (生成的文件位于 /home/omm 目录下)
cat>~omm/load_data.sql<<EOF
insert into classroom values ('Packard', '101', '500');
insert into classroom values ('Painter', '514', '10');
insert into classroom values ('Taylor', '3128', '70');
insert into classroom values ('Watson', '100', '30');
insert into classroom values ('Watson', '120', '50');
insert into department values ('Biology', 'Watson', '90000');
insert into department values ('Comp. Sci.', 'Taylor', '100000');
insert into department values ('Elec. Eng.', 'Taylor', '85000');
insert into department values ('Finance', 'Painter', '120000');
insert into department values ('History', 'Painter', '50000');
insert into department values ('Music', 'Packard', '80000');
insert into department values ('Physics', 'Watson', '70000');
insert into course(COURSE_ID,TITLE,DEPT_NAME,CREDITS) values ('BIO-101', 'Intro. to Biology', 'Biology', '4');
insert into course(COURSE_ID,TITLE,DEPT_NAME,CREDITS) values ('BIO-301', 'Genetics', 'Biology', '4');
insert into course(COURSE_ID,TITLE,DEPT_NAME,CREDITS) values ('BIO-399', 'Computational Biology', 'Biology', '3');
insert into course(COURSE_ID,TITLE,DEPT_NAME,CREDITS) values ('CS-101', 'Intro. to Computer Science', 'Comp. Sci.', '4');
insert into course(COURSE_ID,TITLE,DEPT_NAME,CREDITS) values ('CS-190', 'Game Design', 'Comp. Sci.', '4');
insert into course(COURSE_ID,TITLE,DEPT_NAME,CREDITS) values ('CS-315', 'Robotics', 'Comp. Sci.', '3');
insert into course(COURSE_ID,TITLE,DEPT_NAME,CREDITS) values ('CS-319', 'Image Processing', 'Comp. Sci.', '3');
insert into course(COURSE_ID,TITLE,DEPT_NAME,CREDITS) values ('CS-347', 'Database System Concepts', 'Comp. Sci.', '3');
insert into course(COURSE_ID,TITLE,DEPT_NAME,CREDITS) values ('EE-181', 'Intro. to Digital Systems', 'Elec. Eng.', '3');
insert into course(COURSE_ID,TITLE,DEPT_NAME,CREDITS) values ('FIN-201', 'Investment Banking', 'Finance', '3');
insert into course(COURSE_ID,TITLE,DEPT_NAME,CREDITS) values ('HIS-351', 'World History', 'History', '3');
insert into course(COURSE_ID,TITLE,DEPT_NAME,CREDITS) values ('MU-199', 'Music Video Production', 'Music', '3');
insert into course(COURSE_ID,TITLE,DEPT_NAME,CREDITS) values ('PHY-101', 'Physical Principles', 'Physics', '4');
insert into instructor(ID,NAME,DEPT_NAME,SALARY) values ('10101', 'Srinivasan', 'Comp. Sci.', '65000');
insert into instructor(ID,NAME,DEPT_NAME,SALARY) values ('12121', 'Wu', 'Finance', '90000');
insert into instructor(ID,NAME,DEPT_NAME,SALARY) values ('15151', 'Mozart', 'Music', '40000');
insert into instructor(ID,NAME,DEPT_NAME,SALARY) values ('22222', 'Einstein', 'Physics', '95000');
insert into instructor(ID,NAME,DEPT_NAME,SALARY) values ('32343', 'El Said', 'History', '60000');
insert into instructor(ID,NAME,DEPT_NAME,SALARY) values ('33456', 'Gold', 'Physics', '87000');
insert into instructor(ID,NAME,DEPT_NAME,SALARY) values ('45565', 'Katz', 'Comp. Sci.', '75000');
insert into instructor(ID,NAME,DEPT_NAME,SALARY) values ('58583', 'Califieri', 'History', '62000');
insert into instructor(ID,NAME,DEPT_NAME,SALARY) values ('76543', 'Singh', 'Finance', '80000');
insert into instructor(ID,NAME,DEPT_NAME,SALARY) values ('76766', 'Crick', 'Biology', '72000');
insert into instructor(ID,NAME,DEPT_NAME,SALARY) values ('83821', 'Brandt', 'Comp. Sci.', '92000');
insert into instructor(ID,NAME,DEPT_NAME,SALARY) values ('98345', 'Kim', 'Elec. Eng.', '80000');
insert into section values ('BIO-101', '1', 'Summer', '2009', 'Painter', '514', 'B');
insert into section values ('BIO-301', '1', 'Summer', '2010', 'Painter', '514', 'A');
insert into section values ('CS-101', '1', 'Fall', '2009', 'Packard', '101', 'H');
insert into section values ('CS-101', '1', 'Spring', '2010', 'Packard', '101', 'F');
insert into section values ('CS-190', '1', 'Spring', '2009', 'Taylor', '3128', 'E');
insert into section values ('CS-190', '2', 'Spring', '2009', 'Taylor', '3128', 'A');
insert into section values ('CS-315', '1', 'Spring', '2010', 'Watson', '120', 'D');
insert into section values ('CS-319', '1', 'Spring', '2010', 'Watson', '100', 'B');
insert into section values ('CS-319', '2', 'Spring', '2010', 'Taylor', '3128', 'C');
insert into section values ('CS-347', '1', 'Fall', '2009', 'Taylor', '3128', 'A');
insert into section values ('EE-181', '1', 'Spring', '2009', 'Taylor', '3128', 'C');
insert into section values ('FIN-201', '1', 'Spring', '2010', 'Packard', '101', 'B');
insert into section values ('HIS-351', '1', 'Spring', '2010', 'Painter', '514', 'C');
insert into section values ('MU-199', '1', 'Spring', '2010', 'Packard', '101', 'D');
insert into section values ('PHY-101', '1', 'Fall', '2009', 'Watson', '100', 'A');
insert into teaches values ('10101', 'CS-101', '1', 'Fall', '2009');
insert into teaches values ('10101', 'CS-315', '1', 'Spring', '2010');
insert into teaches values ('10101', 'CS-347', '1', 'Fall', '2009');
insert into teaches values ('12121', 'FIN-201', '1', 'Spring', '2010');
insert into teaches values ('15151', 'MU-199', '1', 'Spring', '2010');
insert into teaches values ('22222', 'PHY-101', '1', 'Fall', '2009');
insert into teaches values ('32343', 'HIS-351', '1', 'Spring', '2010');
insert into teaches values ('45565', 'CS-101', '1', 'Spring', '2010');
insert into teaches values ('45565', 'CS-319', '1', 'Spring', '2010');
insert into teaches values ('76766', 'BIO-101', '1', 'Summer', '2009');
insert into teaches values ('76766', 'BIO-301', '1', 'Summer', '2010');
insert into teaches values ('83821', 'CS-190', '1', 'Spring', '2009');
insert into teaches values ('83821', 'CS-190', '2', 'Spring', '2009');
insert into teaches values ('83821', 'CS-319', '2', 'Spring', '2010');
insert into teaches values ('98345', 'EE-181', '1', 'Spring', '2009');
insert into student(ID,NAME,DEPT_NAME,TOT_CRED) values ('00128', 'Zhang', 'Comp. Sci.', '102');
insert into student(ID,NAME,DEPT_NAME,TOT_CRED) values ('12345', 'Shankar', 'Comp. Sci.', '32');
insert into student(ID,NAME,DEPT_NAME,TOT_CRED) values ('19991', 'Brandt', 'History', '80');
insert into student(ID,NAME,DEPT_NAME,TOT_CRED) values ('23121', 'Chavez', 'Finance', '110');
insert into student(ID,NAME,DEPT_NAME,TOT_CRED) values ('44553', 'Peltier', 'Physics', '56');
insert into student(ID,NAME,DEPT_NAME,TOT_CRED) values ('45678', 'Levy', 'Physics', '46');
insert into student(ID,NAME,DEPT_NAME,TOT_CRED) values ('54321', 'Williams', 'Comp. Sci.', '54');
insert into student(ID,NAME,DEPT_NAME,TOT_CRED) values ('55739', 'Sanchez', 'Music', '38');
insert into student(ID,NAME,DEPT_NAME,TOT_CRED) values ('70557', 'Snow', 'Physics', '0');
insert into student(ID,NAME,DEPT_NAME,TOT_CRED) values ('76543', 'Brown', 'Comp. Sci.', '58');
insert into student(ID,NAME,DEPT_NAME,TOT_CRED) values ('76653', 'Aoi', 'Elec. Eng.', '60');
insert into student(ID,NAME,DEPT_NAME,TOT_CRED) values ('98765', 'Bourikas', 'Elec. Eng.', '98');
insert into student(ID,NAME,DEPT_NAME,TOT_CRED) values ('98988', 'Tanaka', 'Biology', '120');
insert into takes values ('00128', 'CS-101', '1', 'Fall', '2009', 'A');
insert into takes values ('00128', 'CS-347', '1', 'Fall', '2009', 'A-');
insert into takes values ('12345', 'CS-101', '1', 'Fall', '2009', 'C');
insert into takes values ('12345', 'CS-190', '2', 'Spring', '2009', 'A');
insert into takes values ('12345', 'CS-315', '1', 'Spring', '2010', 'A');
insert into takes values ('12345', 'CS-347', '1', 'Fall', '2009', 'A');
insert into takes values ('19991', 'HIS-351', '1', 'Spring', '2010', 'B');
insert into takes values ('23121', 'FIN-201', '1', 'Spring', '2010', 'C+');
insert into takes values ('44553', 'PHY-101', '1', 'Fall', '2009', 'B-');
insert into takes values ('45678', 'CS-101', '1', 'Fall', '2009', 'F');
insert into takes values ('45678', 'CS-101', '1', 'Spring', '2010', 'B+');
insert into takes values ('45678', 'CS-319', '1', 'Spring', '2010', 'B');
insert into takes values ('54321', 'CS-101', '1', 'Fall', '2009', 'A-');
insert into takes values ('54321', 'CS-190', '2', 'Spring', '2009', 'B+');
insert into takes values ('55739', 'MU-199', '1', 'Spring', '2010', 'A-');
insert into takes values ('76543', 'CS-101', '1', 'Fall', '2009', 'A');
insert into takes values ('76543', 'CS-319', '2', 'Spring', '2010', 'A');
insert into takes values ('76653', 'EE-181', '1', 'Spring', '2009', 'C');
insert into takes values ('98765', 'CS-101', '1', 'Fall', '2009', 'C-');
insert into takes values ('98765', 'CS-315', '1', 'Spring', '2010', 'B');
insert into takes values ('98988', 'BIO-101', '1', 'Summer', '2009', 'A');
insert into takes values ('98988', 'BIO-301', '1', 'Summer', '2010', null);
insert into advisor values ('00128', '45565');
insert into advisor values ('12345', '10101');
insert into advisor values ('23121', '76543');
insert into advisor values ('44553', '22222');
insert into advisor values ('45678', '22222');
insert into advisor values ('76543', '45565');
insert into advisor values ('76653', '98345');
insert into advisor values ('98765', '98345');
insert into advisor values ('98988', '76766');
insert into time_slot values ('A', 'M', '8', '0', '8', '50');
insert into time_slot values ('A', 'W', '8', '0', '8', '50');
insert into time_slot values ('A', 'F', '8', '0', '8', '50');
insert into time_slot values ('B', 'M', '9', '0', '9', '50');
insert into time_slot values ('B', 'W', '9', '0', '9', '50');
insert into time_slot values ('B', 'F', '9', '0', '9', '50');
insert into time_slot values ('C', 'M', '11', '0', '11', '50');
insert into time_slot values ('C', 'W', '11', '0', '11', '50');
insert into time_slot values ('C', 'F', '11', '0', '11', '50');
insert into time_slot values ('D', 'M', '13', '0', '13', '50');
insert into time_slot values ('D', 'W', '13', '0', '13', '50');
insert into time_slot values ('D', 'F', '13', '0', '13', '50');
insert into time_slot values ('E', 'T', '10', '30', '11', '45 ');
insert into time_slot values ('E', 'R', '10', '30', '11', '45 ');
insert into time_slot values ('F', 'T', '14', '30', '15', '45 ');
insert into time_slot values ('F', 'R', '14', '30', '15', '45 ');
insert into time_slot values ('G', 'M', '16', '0', '16', '50');
insert into time_slot values ('G', 'W', '16', '0', '16', '50');
insert into time_slot values ('G', 'F', '16', '0', '16', '50');
insert into time_slot values ('H', 'W', '10', '0', '12', '30');
insert into prereq values ('BIO-301', 'BIO-101');
insert into prereq values ('BIO-399', 'BIO-101');
insert into prereq values ('CS-190', 'CS-101');
insert into prereq values ('CS-315', 'CS-101');
insert into prereq values ('CS-319', 'CS-101');
insert into prereq values ('CS-347', 'CS-101');
insert into prereq values ('EE-181', 'PHY-101');
commit;
EOF
运行完脚本之后,可以在 /home/omm 目录下看到 create_db_tables.sql
和load_data.sql
两个脚本文件,如下图所示:
七、准备测试数据库的数据集
使用Linux用户omm,执行下面的命令和语句,创建测试表并装载测试数据集:
使用下面的命令用 student 的身份登陆到数据库 studentdb 中:
gsql -d studentdb -h 192.168.100.91 -U student -p 26000 -W student@ustb2020 -r -q
然后使用下面的 sql 语言操作数据库:
-- 配置gsql,关闭事务的自动提交,注意,此处ATUOCOMMIT必须用大写!
\set AUTOCOMMIT off
-- 创建大学数据库应用的模式表
\i create_db_tables.sql
-- 将测试数据插入大学数据库应用的模式表中
\i load_data.sql
-- 退出gsql
\q
运行结果如下图所示:
gsql 的-q
选项让 gsql安静地执行SQL语句。要在gsq!中执行某个SQL脚本文件,可使用元命令\iscriptFileName
来实现。
使用 Linux用户omm,执行下面的命令和语句,验证测试数据已经成功装载:
gsql -d studentdb -h 192.168.100.91 -U student -p 26000 -W student@ustb2020 -c "\dt"
gsql -d studentdb -h 192.168.100.91 -U student -p 26000 -W student@ustb2020 \
-c "select * from instructor where salary=80000"
运行结果如下图所示: