本篇将介绍学校数据模型以及学校数据模型表操作。

1.4 学校数据模型

1.4.1 关于本实验

以学校数据库模型为例,介绍openGauss数据库数据库、表、表空间、用户及其它对象,以及SQL语法使用的介绍。
假设A市B学校为了加强对学校的管理,引入了华为openGauss数据库。在B学校里,主要涉及的对象有学生、教师、班级、院系和课程。本实验假设在B学校数据库中,教师会教授课程,学生会选修课程,院系会聘请教师,班级会组成院系,学生会组成班级。因此,根据此关系,本实验给出了相应的关系模式和ER图,并对其进行基本的数据库操作。

1.4.2 关系模型

对于B校中的5个对象,分别建立属于每个对象的属性集合,具体属性描述如下:
● 学生(学号,姓名,性别,出生日期,入学日期,家庭住址)
● 教师(教师编号,教师姓名,职称,性别,年龄,入职日期)
● 班级(班级编号,班级名称,班主任)
● 院系(系编号,系名称,系主任)
● 课程(课程编号,课程名称,课程类型,学分)

上述属性对应的编号为:
● student(std_id,std_name,std_sex,std_birth,std_in,std_address)
● teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in)
● class(cla_id,cla_name,cla_teacher)
●school_department(depart_id,depart_name,depart_teacher)
● course(cor_id,cor_name,cor_type,credit)

对象之间的关系:
● 一位学生可以选择多门课程,一门课程可被多名学生选择
● 一位老师可以选择多门课程,一门课程可被多名老师教授
● 一个院系可由多个班级组成
● 一个院系可聘请多名老师
● 一个班级可由多名学生组成

1.4.3 E-R图

在这里插入图片描述

图1-1 E-R图

1.5 学校数据模型表操作

1.5.1 表的创建

根据B学校的场景描述,本实验分别针对学生(student),教师(teacher),班级(class),院系(school_department)和课程(course)创建相应的表。具体的实验步骤如下所示:

步骤 1 创建学生信息表。

DROP TABLE IF EXISTS student;
CREATE TABLE student
(
        std_id INT PRIMARY KEY,
        std_name NCHAR(20) NOT NULL,
        std_sex NCHAR(6),
        std_birth DATE,
        std_in DATE NOT NULL,
        std_address VARCHAR(100)
);

步骤 2 创建教师信息表。

DROP TABLE IF EXISTS teacher;
CREATE TABLE teacher
(
        tec_id INT PRIMARY KEY,
        tec_name CHAR(20) NOT NULL,
        tec_job CHAR(15),
        tec_sex CHAR(6),
        tec_age INT,
        tec_in DATE NOT NULL
);

步骤 3 创建班级信息表。

DROP TABLE IF EXISTS class;
CREATE TABLE class
(
        cla_id INT PRIMARY KEY,
        cla_name CHAR(20) NOT NULL,
        cla_teacher INT NOT NULL
);

步骤 4 创建院系信息表。

DROP TABLE IF EXISTS school_department;
CREATE TABLE school_department
(
        depart_id INT PRIMARY KEY,
        depart_name NCHAR(30) NOT NULL,
        depart_teacher INT NOT NULL
);

步骤 5 创建课程信息表。

DROP TABLE IF EXISTS course;
CREATE TABLE course
(
        cor_id INT PRIMARY KEY,
        cor_name NCHAR(30) NOT NULL,
        cor_type NCHAR(20),
        credit numeric
);

1.5.2 表数据的插入

步骤 1 向student表中插入数据。

INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (1,'张一','男','1993-01-01','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (2,'张二','男','1993-01-02','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (3,'张三','男','1993-01-03','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (4,'张四','男','1993-01-04','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (5,'张五','男','1993-01-05','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (6,'张六','男','1993-01-06','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (7,'张七','男','1993-01-07','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (8,'张八','男','1993-01-08','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (9,'张九','男','1993-01-09','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (10,'李一','男','1993-01-10','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (11,'李二','男','1993-01-11','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (12,'李三','男','1993-01-12','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (13,'李四','男','1993-01-13','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (14,'李五','男','1993-01-14','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (15,'李六','男','1993-01-15','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (16,'李七','男','1993-01-16','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (17,'李八','男','1993-01-17','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (18,'李九','男','1993-01-18','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (19,'王一','男','1993-01-19','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (20,'王二','男','1993-01-20','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (21,'王三','男','1993-01-21','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (22,'王四','男','1993-01-22','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (23,'王五','男','1993-01-23','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (24,'王六','男','1993-01-24','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (25,'王七','男','1993-01-25','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (26,'王八','男','1993-01-26','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (27,'王九','男','1993-01-27','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (28,'钱一','男','1993-01-28','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (29,'钱二','男','1993-01-29','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (30,'钱三','男','1993-01-30','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (31,'钱四','男','1993-02-01','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (32,'钱五','男','1993-02-02','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (33,'钱六','男','1993-02-03','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (34,'钱七','男','1993-02-04','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (35,'钱八','男','1993-02-05','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (36,'钱九','男','1993-02-06','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (37,'吴一','男','1993-02-07','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (38,'吴二','男','1993-02-08','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (39,'吴三','男','1993-02-09','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (40,'吴四','男','1993-02-10','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (41,'吴五','男','1993-02-11','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (42,'吴六','男','1993-02-12','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (43,'吴七','男','1993-02-13','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (44,'吴八','男','1993-02-14','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (45,'吴九','男','1993-02-15','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (46,'柳一','男','1993-02-16','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (47,'柳二','男','1993-02-17','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (48,'柳三','男','1993-02-18','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (49,'柳四','男','1993-02-19','2011-09-01','江苏省南京市雨花台区');
INSERT INTO student(std_id,std_name,std_sex,std_birth,std_in,std_address) VALUES (50,'柳五','男','1993-02-20','2011-09-01','江苏省南京市雨花台区');

步骤 2 向teacher表中插入数据。

INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (1,'张一','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (2,'张二','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (3,'张三','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (4,'张四','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (5,'张五','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (6,'张六','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (7,'张七','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (8,'张八','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (9,'张九','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (10,'李一','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (11,'李二','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (12,'李三','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (13,'李四','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (14,'李五','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (15,'李六','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (16,'李七','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (17,'李八','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (18,'李九','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (19,'王一','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (20,'王二','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (21,'王三','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (22,'王四','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (23,'王五','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (24,'王六','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (25,'王七','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (26,'王八','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (27,'王九','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (28,'钱一','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (29,'钱二','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (30,'钱三','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (31,'钱四','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (32,'钱五','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (33,'钱六','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (34,'钱七','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (35,'钱八','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (36,'钱九','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (37,'吴一','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (38,'吴二','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (39,'吴三','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (40,'吴四','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (41,'吴五','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (42,'吴六','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (43,'吴七','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (44,'吴八','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (45,'吴九','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (46,'柳一','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (47,'柳二','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (48,'柳三','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (49,'柳四','讲师','男',35,'2009-07-01');
INSERT INTO teacher(tec_id,tec_name,tec_job,tec_sex,tec_age,tec_in) VALUES (50,'柳五','讲师','男',35,'2009-07-01');

步骤 3 向class表插入数据。

INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (1,'计算机',1);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (2,'自动化',3);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (3,'飞行器设计',5);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (4,'大学物理',7);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (5,'高等数学',9);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (6,'大学化学',12);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (7,'表演',14);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (8,'服装设计',16);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (9,'工业设计',18);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (10,'金融学',21);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (11,'医学',23);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (12,'土木工程',25);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (13,'机械',27);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (14,'建筑学',29);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (15,'经济学',32);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (16,'财务管理',34);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (17,'人力资源',36);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (18,'力学',38);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (19,'人工智能',41);
INSERT INTO class(cla_id,cla_name,cla_teacher) VALUES (20,'会计',45);

步骤 4 向school_department表插入数据。

INSERT INTO school_department(depart_id,depart_name,depart_teacher) VALUES (1,'计算机学院',2);
INSERT INTO school_department(depart_id,depart_name,depart_teacher) VALUES (2,'自动化学院',4);
INSERT INTO school_department(depart_id,depart_name,depart_teacher) VALUES (3,'航空宇航学院',6);
INSERT INTO school_department(depart_id,depart_name,depart_teacher) VALUES (4,'艺术学院',8);
INSERT INTO school_department(depart_id,depart_name,depart_teacher) VALUES (5,'理学院',11);
INSERT INTO school_department(depart_id,depart_name,depart_teacher) VALUES (6,'人工智能学院',13);
INSERT INTO school_department(depart_id,depart_name,depart_teacher) VALUES (7,'工学院',15);
INSERT INTO school_department(depart_id,depart_name,depart_teacher) VALUES (8,'管理学院',17);
INSERT INTO school_department(depart_id,depart_name,depart_teacher) VALUES (9,'农学院',22);
INSERT INTO school_department(depart_id,depart_name,depart_teacher) VALUES (10,'医学院',28);

步骤 5 向course表插入数据。

INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (1,'数据库系统概论','必修',3);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (2,'艺术设计概论','选修',1);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (3,'力学制图','必修',4);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (4,'飞行器设计历史','选修',1);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (5,'马克思主义','必修',2);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (6,'大学历史','必修',2);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (7,'人力资源管理理论','必修',2.5);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (8,'线性代数','必修',4);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (9,'JAVA程序设计','必修',3);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (10,'操作系统','必修',4);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (11,'计算机组成原理','必修',3);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (12,'自动化设计理论','必修',2);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (13,'情绪表演','必修',2.5);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (14,'茶学历史','选修',1);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (15,'艺术论','必修',1.5);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (16,'机器学习','必修',3);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (17,'数据挖掘','选修',2);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (18,'图像识别','必修',3);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (19,'解剖学','必修',4);
INSERT INTO course(cor_id,cor_name,cor_type,credit) VALUES (20,'3D max','选修',2);

1.5.3 数据查询

1.5.3.1 单表查询

查询B校课程信息表的所有信息。

postgres=# SELECT * from course;
 cor_id |            cor_name            |      cor_type      | credit
--------+--------------------------------+--------------------+--------
      2 | 艺术设计概论                   | 选修               |      1
      3 | 力学制图                       | 必修               |      4
      4 | 飞行器设计历史                 | 选修               |      1
      5 | 马克思主义                     | 必修               |      2
      6 | 大学历史                       | 必修               |      2
      7 | 人力资源管理理论               | 必修               |    2.5
      8 | 线性代数                       | 必修               |      4
      9 | JAVA程序设计                   | 必修               |      3
     10 | 操作系统                       | 必修               |      4
     11 | 计算机组成原理                 | 必修               |      3
     12 | 自动化设计理论                 | 必修               |      2
     13 | 情绪表演                       | 必修               |    2.5
     14 | 茶学历史                       | 选修               |      1
     15 | 艺术论                         | 必修               |    1.5
     16 | 机器学习                       | 必修               |      3
     17 | 数据挖掘                       | 选修               |      2
     18 | 图像识别                       | 必修               |      3
     19 | 解剖学                         | 必修               |      4
     20 | 3D max                         | 选修               |      2
      1 | C语言程序设计                  | 必修               |    3.5
(20 rows)
1.5.3.2 条件查询

在教师信息表中查询教师编号大于45的老师的入学年份。

postgres=# SELECT tec_id, tec_in FROM teacher WHERE tec_id>45;
 tec_id |       tec_in
--------+---------------------
     46 | 2009-07-01 00:00:00
     47 | 2009-07-01 00:00:00
     48 | 2009-07-01 00:00:00
     49 | 2009-07-01 00:00:00
     50 | 2009-07-01 00:00:00
(5 rows)

查询B校中所有选修的课程的信息。

postgres=# SELECT * FROM course WHERE cor_type='选修';
 cor_id |            cor_name            |      cor_type      | credit
--------+--------------------------------+--------------------+--------
      2 | 艺术设计概论                   | 选修               |      1
      4 | 飞行器设计历史                 | 选修               |      1
     14 | 茶学历史                       | 选修               |      1
     17 | 数据挖掘                       | 选修               |      2
     20 | 3D max                         | 选修               |      2
(5 rows)

1.5.4 数据的修改和删除

1.5.4.1 修改数据

修改/更新课程信息表数据。

postgres=# UPDATE course SET cor_name='C语言程序设计',cor_type='必修',credit=3.5 WHERE cor_id=1;
UPDATE 1
postgres=# SELECT * FROM course WHERE cor_id=1;
 cor_id |         cor_name         |      cor_type      | credit
--------+--------------------------+--------------------+--------
      1 | C语言程序设计            | 必修               |    3.5
(1 row)
1.5.4.2 删除指定数据

在B校中删除教师编号8和15所管理的院系。

postgres=# DELETE FROM school_department WHERE depart_teacher=8 OR depart_teacher=15;
DELETE 0
postgres=# SELECT * FROM school_department;
 depart_id |         depart_name         | depart_teacher
-----------+-----------------------------+----------------
         1 | 计算机学院                  |              2
         2 | 自动化学院                  |              4
         3 | 航空宇航学院                |              6
         5 | 理学院                      |             11
         6 | 人工智能学院                |             13
         8 | 管理学院                    |             17
         9 | 农学院                      |             22
        10 | 医学院                      |             28
(8 rows)

本实验结束。

2 附录一:openGauss数据库基本操作

2.1 查看数据库对象

● 查看帮助信息:

postgres=# \?

● 切换数据库:

postgres=# \c dbname

● 列举数据库:
使用\l元命令查看数据库系统的数据库列表。

postgres=# \l

使用如下命令通过系统表pg_database查询数据库列表。

postgres=# SELECT datname FROM pg_database;

● 列举表:

postgres=# \dt

● 列举所有表、视图和索引:

postgres=# \d+

● 使用gsql的\d+命令查询表的属性。

postgres=# \d+ tablename

● 查看表结构:

postgres=# \d tablename

● 列举schema:

postgres=# \dn

● 查看索引:

postgres=# \di

● 查询表空间:
使用gsql程序的元命令查询表空间。

postgres=# \db

检查pg_tablespace系统表。如下命令可查到系统和用户定义的全部表空间。

postgres=# SELECT spcname FROM pg_tablespace;

● 查看数据库用户列表:

postgres=# SELECT * FROM pg_user;

● 要查看用户属性:

postgres=# SELECT * FROM pg_authid;

● 查看所有角色:

postgres=# SELECT * FROM PG_ROLES;

2.2 其他操作

● 查看openGauss支持的所有SQL语句。

postgres=#\h

● 切换数据库:

postgres=# \c dbname

● 切换用户:

postgres=# \c – username

● 退出数据库:

postgres=# \q

以上是openGauss数据库开发指导手册全部内容,感谢您的阅读。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐