@SovietPower
2022-03-18T15:06:26.000000Z
字数 23005
阅读 2825
ECNU
运行实验1创建的mygauss容器,创建一个新文件夹:
PS H:\> docker start opengaussPS H:\> docker exec -it opengauss bashroot@97375c3acbd1:/# su ommomm@97375c3acbd1:/$ cd /home/ommomm@97375c3acbd1:~$ mkdir db2022omm@97375c3acbd1:~$ cd db2022omm@97375c3acbd1:~/db2022$
在一个新powershell窗口中,将下载的SQL脚本文件复制到容器中的相应目录:
PS H:\> docker cp DDL+drop.sql opengauss:/home/omm/db2022/PS H:\> docker cp largeRelationsInsertFile.sql opengauss:/home/omm/db2022/omm@97375c3acbd1:~/db2022$ ls -ltotal 2248-rwxr-xr-x 1 root root 3693 Mar 15 01:53 DDL+drop.sql-rwxr-xr-x 1 root root 2296465 Mar 15 01:53 largeRelationsInsertFile.sql
使用客户端工具gsql通过脚本文件DDL+drop.sql, largeRelationsInsertFile.sql向数据表中插入数据:
omm@97375c3acbd1:~/db2022$ gsql -d db2022 -f DDL+drop.sqlDROP TABLEDROP TABLEDROP TABLEDROP TABLEDROP TABLEDROP TABLEDROP TABLEDROP TABLEDROP TABLEDROP TABLEDROP TABLEgsql:DDL+drop.sql:21: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "classroom_pkey" for table "classroom"CREATE TABLE...gsql:DDL+drop.sql:130: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "prereq_pkey" for table "prereq"CREATE TABLEtotal time: 8857 msomm@97375c3acbd1:~/db2022$ gsql -d db2022 -U gxb -W 'Gxb12345' -f largeRelationsInsertFile.sql...INSERT 0 1INSERT 0 1INSERT 0 1INSERT 0 1total time: 3509318 ms
连接数据库,然后检查一下记录的数量:
omm@97375c3acbd1:~/db2022$ gsql -d db2022 -U gxb -W 'Gxb12345' -rgsql ((openGauss 2.1.0 build 590b0f8e) compiled at 2021-09-30 14:29:04 commit 0 last mr )Non-SSL connection (SSL connection is recommended when requiring high-security)Type "help" for help.db2022=# select count(*) from advisor;count-------2000(1 row)db2022=# select count(*) from classroom;count-------30(1 row)...db2022=# select count(*) from time_slot;count-------20(1 row)
gsql -d [db] -c 'select * from table;' -o output.csv 可用客户端工具gsql执行单条SQL语句,并将查询结果保存到文件output.csv中。
omm@97375c3acbd1:~/db2022$ gsql -d db2022 -c 'select * from department;' -o department.csvomm@97375c3acbd1:~/db2022$ ls -ltotal 2252-rwxr-xr-x 1 root root 3693 Mar 15 01:53 DDL+drop.sql-rw-rw-r-- 1 omm omm 871 Mar 15 03:25 department.csv-rwxr-xr-x 1 root root 2296465 Mar 15 01:53 largeRelationsInsertFile.sqlomm@97375c3acbd1:~/db2022$ cat department.csvdept_name | building | budget-------------+-------------+-----------Civil Eng. | Chandler | 255041.46Biology | Candlestick | 647610.55History | Taylor | 699140.86Physics | Wrigley | 942162.76Marketing | Lambeau | 210627.58Pol. Sci. | Whitman | 573745.09English | Palmer | 611042.66Accounting | Saucon | 441840.92Comp. Sci. | Lamberton | 106378.69Languages | Linderman | 601283.60Finance | Candlestick | 866831.75Geology | Palmer | 406557.93Cybernetics | Mercer | 794541.46Astronomy | Taylor | 617253.94Athletics | Bronfman | 734550.70Statistics | Taylor | 395051.74Psychology | Thompson | 848175.04Math | Brodhead | 777605.11Elec. Eng. | Main | 276527.61Mech. Eng. | Rauch | 520350.65(20 rows)
13. Find the names of all the instructors from Biology department.
db2022=# select namedb2022-# from instructordb2022-# where dept_name='Biology';name----------QueirozValtchev(2 rows)
14. Find the names of courses in Computer science department which have 3 credits.
db2022=# select titledb2022-# from coursedb2022-# where dept_name='Comp. Sci.' and credits=3;title-----------------------International FinanceJapaneseComputability Theory(3 rows)
15. For the student with ID 13403 (or any other value), show all course_id and title of all courses taken by the student.
db2022=# select course_id, titledb2022-# from takes natural join coursedb2022-# where id=13403;course_id | title-----------+-------------------------------486 | Accounting349 | Networking696 | Heat Transfer319 | World History400 | Visual BASIC258 | Colloid and Surface Chemistry443 | Journalism158 | Elastic Structures192 | Drama489 | Journalism795 | Death and Taxes493 | Music of the 50s352 | Compiler Design338 | Graph Theory366 | Computational Biology400 | Visual BASIC748 | Tort Law(17 rows)
16. As above, but show the total number of credits for such courses (taken by that student). Don’t display the tot_creds value from the student table, you should use SQL aggregation on courses taken by the student.
db2022=# select sum(credits)db2022-# from takes natural join coursedb2022-# where id=13403;sum-----61(1 row)db2022=# select tot_creddb2022-# from studentdb2022-# where id=13403;tot_cred----------82(1 row)
17. Display the total credits for each of the students, along with the ID of the student; don’t bother about the name of the student. (Don’t bother about students who have not registered for any course, they can be omitted).
db2022=# select id, sum(credits)db2022-# from takes natural join coursedb2022-# group by id;id | sum-------+-----3039 | 4086327 | 4422050 | 3989551 | 7112326 | 3619321 | 4868516 | 627035 | 5681396 | 67...
18. Find the names of all students who have taken any Comp. Sci. course ever (there should be no duplicate names).
db2022=# select distinct namefrom student natural join takes join course using (course_id)where course.dept_name='Comp. Sci.';name-------------------MowbrayMendelzonUmeharaKamaeSrivastava...
19. Display the IDs of all instructors who have never taught a couse (interpret “taught” as “taught or is scheduled to teach”).
db2022=# (select iddb2022(# from instructor)db2022-# exceptdb2022-# (select iddb2022(# from instructor natural join teaches);id-------37687355797442678699648715088559795973025718079653526473195596896585586339572553168079503040341(19 rows)
20. As above, but display the names of the instructors also, not just the IDs.
db2022=# select id, namedb2022-# from instructordb2022-# where id not in (select iddb2022(# from instructor natural join teaches);id | name-------+-------------------63395 | McKinnon78699 | Pingr96896 | Mird40341 | Murata50885 | Konstantinides79653 | Levine97302 | Bertolino57180 | Hau35579 | Soisalon-Soininen31955 | Moreira37687 | Arias16807 | Yazdi95030 | Arinb74426 | Kenje58558 | Dusserre59795 | Desyl52647 | Bancilhon72553 | Yin64871 | Gutierrez(19 rows)
21. You need to create a movie database. Create three tables, one for actors(AID, name), one for movies(MID, title) and one for actor_role(MID, AID, rolename). Use appropriate data types for each of the attributes, and add appropriate primary/foreign key constraints.
db2022=# create table actorsdb2022-# (AID varchar(20),db2022(# name varchar(50) not null,db2022(# primary key (AID));NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "actors_pkey" for table "actors"CREATE TABLEdb2022=# create table moviesdb2022-# (MID varchar(20),db2022(# title varchar(50),db2022(# primary key (MID));NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "movies_pkey" for table "movies"CREATE TABLEdb2022=# create table actor_roledb2022-# (MID varchar(20),db2022(# AID varchar(20),db2022(# rolename varchar(20) not null,db2022(# primary key (MID, AID, rolename),db2022(# foreign key (MID) references movies,db2022(# foreign key (AID) references actors);NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "actor_role_pkey" for table "actor_role"CREATE TABLE
22. Insert data to the above tables (approx 3 to 6 rows in each table), including data for actor “Charlie Chaplin”, and for yourself (using your student number as ID).
db2022=# insert into actors valuesdb2022-# ('10205', 'GXB'),db2022-# ('10000', 'Charlie Chaplin'),db2022-# ('10001', 'Paulette Goddard'),db2022-# ('10002', 'Emma Watson');INSERT 0 4db2022=# insert into movies valuesdb2022-# ('10000', 'City Lights'),db2022-# ('10001', 'Modern Times'),db2022-# ('10002', 'The Gold Rush'),db2022-# ('10003', 'Harry Potter');INSERT 0 4db2022=# insert into actor_role values('10000', '10000', 'A Tramp'),('10000', '10000', 'Director'),('10001', '10000', 'Charles'),('10001', '10001', 'An Orphan'),('10002', '10000', 'Charles'),('10003', '10002', 'Hermione Granger');INSERT 0 6
23. Write a query to list all movies in which actor “Charlie Chaplin” has acted, along with the number of roles he had in that movie.
db2022=# select MID, title, cntdb2022-# from movies natural join (select MID, count(*) as cntdb2022(# from actor_roledb2022(# where AID='10000'db2022(# group by MID);mid | title | cnt-------+---------------+-----10000 | City Lights | 210001 | Modern Times | 110002 | The Gold Rush | 1(3 rows)
24. Write a query to list the names of actors who have not acted in any movie.
db2022=# select AID, namedb2022-# from actorsdb2022-# where AID not in (select AID from actor_role);aid | name-------+------10205 | GXB(1 row)
25. List names of actors, along with titles of movies they have acted in. If they have not acted in any movie, show the movie title as null. (Do not use SQL outerjoin syntax here, write it from scratch.)
with temp(AID, name, title) as(select AID, name, titlefrom actors natural join actor_role natural join movies)(select name, 'null' as title from actorswhere AID not in (select AID from temp))union(select name, title from temp);
db2022=# with temp(AID, name, title) asdb2022-# (select AID, name, titledb2022(# from actors natural join actor_role natural join movies)db2022-# (select name, 'null' as title from actorsdb2022(# where AID not in (select AID from temp))db2022-# uniondb2022-# (select name, title from temp);name | title------------------+---------------Charlie Chaplin | The Gold RushGXB | nullEmma Watson | Harry PotterCharlie Chaplin | City LightsCharlie Chaplin | Modern TimesPaulette Goddard | Modern Times(6 rows)
26. Find the maximum and minimum enrollment across all sections, considering only sections that had some enrollment, don’t worry about those that had no students taking that section.
一个学生在一个section内上了两节课,是算2还是算1?感觉两种都有意义?按2计算了。
db2022=# select min(cnt), max(cnt)db2022-# from (select count(*) as cntdb2022(# from takesdb2022(# group by sec_id);min | max-----+-------322 | 25512(1 row)db2022=# select min(cnt), max(cnt)db2022-# from (select count(*) as cntdb2022(# from (select distinct ID, sec_id from takes)db2022(# group by sec_id);min | max-----+------322 | 2000(1 row)
27. Find all sections that had the maximum enrollment (along with the enrollment).
with max_enroll as(select max(cnt) as mxfrom (select sec_id, count(*) as cntfrom takesgroup by sec_id))select sec_id, count(*) as cntfrom takesgroup by sec_idhaving cnt in (select mx from max_enroll);
db2022=# with max_enroll asdb2022-# (select max(cnt) as mxdb2022(# from (select sec_id, count(*) as cntdb2022(# from takesdb2022(# group by sec_id))db2022-# select sec_id, count(*) as cntdb2022-# from takesdb2022-# group by sec_iddb2022-# having cnt in (select mx from max_enroll);sec_id | cnt--------+-------1 | 25512(1 row)
28. As in step 26, but now also include sections with no students taking them; the enrollment for such sections should be treated as 0. Do this in two different ways.
a. Using a scalar subquery;
select min(cnt), max(cnt)from (select distinct sec_id,(select count(*)from takeswhere section.sec_id=takes.sec_id)as cntfrom section);
b. Using aggregation on a left outer join (use the SQL natural left outer join syntax);
select min(cnt), max(cnt)from (select count(grade) as cntfrom section natural left outer join takesgroup by sec_id);
db2022=# select min(cnt), max(cnt)db2022-# from (db2022(# select distinct sec_id,db2022(# (select count(*)db2022(# from takesdb2022(# where section.sec_id=takes.sec_id)db2022(# as cntdb2022(# from section);min | max-----+-------0 | 25512(1 row)db2022=# select min(cnt), max(cnt)db2022-# from (select count(grade) as cntdb2022(# from section natural left outer join takesdb2022(# group by sec_id);min | max-----+-------0 | 25512(1 row)
29. Find all courses whose title starts with the string “Comp”.
db2022=# select course_id, titledb2022-# from coursedb2022-# where title like 'Comp%';course_id | title-----------+----------------------------814 | Compiler Design328 | Composition and Literature366 | Computational Biology781 | Compiler Design805 | Composition and Literature919 | Computability Theory877 | Composition and Literature352 | Compiler Design584 | Computability Theory348 | Compiler Design(10 rows)
30. Find instructors who have taught all courses in Comp. Sci. department.
a. Using the “not exists … except …” structure;
注意要用instructor.ID,ID, t.ID都指t.ID。
select ID, namefrom instructor as Iwhere not exists ((select course_id from course where dept_name='Comp. Sci.')except(select course_id from teaches as t where I.ID=t.ID));
b. Using matching of counts (don’t forget the distinct clause!).
如果用with cnt代替select count(*) from course where dept_name='Comp. Sci.',则不能直接写cnt=...,还是要写(select tot from cnt)=...。
select ID, namefrom instructor as Iwhere (select count(*) from course where dept_name='Comp. Sci.')=(select count(distinct course_id) from teaches as t where I.ID=t.ID);
db2022=# select ID, namedb2022-# from instructor as Idb2022-# where not exists (db2022(# (select course_id from course where dept_name='Comp. Sci.')db2022(# exceptdb2022(# (select course_id from teaches as t where I.ID=t.ID));id | name-------+-------34175 | Bondi(1 row)db2022=# select * from teaches where ID=34175;id | course_id | sec_id | semester | year-------+-----------+--------+----------+------34175 | 747 | 1 | Spring | 200434175 | 274 | 1 | Fall | 200234175 | 571 | 1 | Spring | 200434175 | 539 | 1 | Spring | 202234175 | 949 | 1 | Spring | 202234175 | 647 | 1 | Spring | 202234175 | 584 | 1 | Spring | 202234175 | 276 | 1 | Spring | 202234175 | 359 | 1 | Spring | 202234175 | 284 | 1 | Spring | 2022(10 rows)db2022=# select ID, namedb2022-# from instructor as Idb2022-# where (select count(*) from course where dept_name='Comp. Sci.')=(select count(distinct course_id) from teaches as t where I.ID=t.ID);id | name-------+-------34175 | Bondi(1 row)
31. Insert each instructor as a student, with tot_creds = 0, in the same department.
db2022=# insert into studentdb2022-# select ID, name, dept_name, 0db2022-# from instructor;INSERT 0 50db2022=# select count(*) from instructor;count-------50(1 row)
32. Now delete all the newly added “students” above (note: already existing students who happened to have tot_creds = 0 should not get deleted).
db2022=# delete from studentdb2022-# where (ID, name) in (select ID, name from instructor);DELETE 50
33. Some of you may have noticed that the tot_creds value for students did not match the credits from courses they have taken. Write and execute query to update tot_creds based on the credits passed, to bring the database back to consistency.
update studentset tot_cred=(select sum(credits)from takes natural join coursewhere takes.id=student.id);select sum(credits)from takes natural join coursewhere id=13403;select tot_credfrom studentwhere id=13403;
db2022=# update studentdb2022-# set tot_cred=(db2022(# select sum(credits)db2022(# from takes natural join coursedb2022(# where takes.id=student.id);UPDATE 2000db2022=# select sum(credits)db2022-# from takes natural join coursedb2022-# where id=13403;sum-----61(1 row)db2022=# select tot_creddb2022-# from studentdb2022-# where id=13403;tot_cred----------61(1 row)
34. Increase the salaries of instructors by 1000 times the number of course sections they have taught.
select salary from instructor where ID=34175;select count(*) from teaches where ID=34175;update instructorset salary=salary+1000*(select count(*) from teaches where teaches.ID=instructor.ID);select salary from instructor where ID=34175;
db2022=# select count(*) from teaches where ID=34175;count-------10(1 row)db2022=# select salary from instructor where ID=34175;salary-----------115469.11(1 row)db2022=# select count(*) from teaches where ID=34175;count-------10(1 row)db2022=# update instructordb2022-# set salary=salary+1000*db2022-# (select count(*) from teaches where teaches.ID=instructor.ID);UPDATE 50db2022=# select salary from instructor where ID=34175;salary-----------125469.11(1 row)
35. The university rules allow an F grade to be overridden by any pass grade (A, A-, B+, B, B-, C+, C, C-, D, D-). Now, create a view that lists information about all fail grades that have not been overridden (the view should contain all attributes from the takes relation).
db2022=# create view failed_takes asdb2022-# select * from takesdb2022-# where grade='F';CREATE VIEWdb2022=# select *db2022-# from failed_takes;id | course_id | sec_id | semester | year | grade-------+-----------+--------+----------+------+-------79446 | 808 | 1 | Fall | 2003 | F99710 | 169 | 2 | Fall | 2002 | F56598 | 192 | 1 | Fall | 2002 | F41890 | 599 | 1 | Spring | 2003 | F...52019 | 200 | 2 | Fall | 2002 | F19321 | 200 | 2 | Fall | 2002 | F(961 rows)
36. Find all students who have 2 or more non-overridden F grades, and list them along with the F grades.
db2022=# select ID, name, 'F' as gradefrom studentwhere 1<(select count(*)from failed_takeswhere failed_takes.ID=student.ID)order by ID;id | name | grade-------+-------------+-------1018 | Colin | F10481 | Grosch | F107 | Shabuno | F...99710 | Savolainen | F99949 | Samo | F(176 rows)db2022=# select * from failed_takes where ID=1018;id | course_id | sec_id | semester | year | grade------+-----------+--------+----------+------+-------1018 | 274 | 1 | Fall | 2002 | F1018 | 599 | 1 | Spring | 2003 | F(2 rows)
37. Grades are mapped to a grade point as follows.
Create a table to store these mappings, and write a query to find the GPA (Grade Point Average) of each student, using this table. Make sure students who have not got a non-null grade in any course are displayed with a GPA of null.
'A '和'A'是不一样的,而原表中用的'A ', 'B ', 'C ', 'D ', 'F',所以必须要一致才可正确比较?
原表中有A+,给的表没有。
create table grade_point(grade varchar(2),point numeric(2,1),primary key (grade));insert into grade_point values('A+', 4.3), ('A ', 4.0), ('A-', 3.7), ('B+', 3.3), ('B ', 3.0), ('B-', 2.7), ('C+', 2.3), ('C ', 2.0), ('C-', 1.5), ('D ', 1.3), ('D-', 1.0), ('F', 0);select * from grade_point;create or replace function gradeToPoint(in g varchar(2))returns numeric(2,1) as $$declareres numeric(2,1);beginselect point into resfrom grade_pointwhere grade=g;return res;end;$$ LANGUAGE plpgsql;select ID, sum(gradeToPoint(coalesce(grade, 'F')))/(select count(*) as GPA from takes as S where S.ID=T.ID)from takes as Tgroup by ID;
db2022=# create table grade_pointdb2022-# (grade varchar(2),db2022(# point numeric(2,1),db2022(# primary key (grade));NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "grade_point_pkey" for table "grade_point"CREATE TABLEdb2022=# insert into grade_point valuesdb2022-# ('A+', 4.3), ('A ', 4.0), ('A-', 3.7), ('B+', 3.3), ('B ', 3.0), ('B-', 2.7), ('C+', 2.3), ('C ', 2.0), ('C-', 1.5), ('D ', 1.3), ('D-', 1.0), ('F', 0);INSERT 0 12db2022=# select * from grade_point;grade | point-------+-------A+ | 4.3A | 4.0A- | 3.7B+ | 3.3B | 3.0B- | 2.7C+ | 2.3C | 2.0C- | 1.5D | 1.3D- | 1.0F | 0.0(12 rows)db2022=# create or replace function gradeToPoint(in g varchar(2))db2022-# returns numeric(2,1) as $$db2022$# declaredb2022$# res numeric(2,1);db2022$# begindb2022$# select point into resdb2022$# from grade_pointdb2022$# where grade=g;db2022$# return res;db2022$# end;db2022$# $$ LANGUAGE plpgsql;CREATE FUNCTIONdb2022=# select ID, sum(gradeToPoint(coalesce(grade, 'F')))/(select count(*) as GPA from takes as S where S.ID=T.ID)db2022-# from takes as Tdb2022-# group by ID;id | ?column?-------+--------------------3039 | 2.800000000000000086327 | 2.933333333333333322050 | 2.9272727272727273...53496 | 3.075000000000000025785 | 3.2533333333333333(2000 rows)
38. Find all classrooms that have been assigned to more than one section at the same time. Display the rooms along with the assigned sections; Using a with clause or a view to simplify this query.
with conflicted(building, room_number) as(select building, room_numberfrom classroom as Cwhere 1<(select count(*) from section as Swhere S.building=C.building and S.room_number=C.room_number))select building, room_number, course_id, sec_id, semester, yearfrom section natural join conflictedorder by building, room_number;
db2022=# with conflicted(building, room_number) asdb2022-# (select building, room_numberdb2022(# from classroom as Cdb2022(# where 1<(db2022(# select count(*) from section as Sdb2022(# where S.building=C.building and S.room_number=C.room_number))db2022-# select building, room_number, course_id, sec_id, semester, yeardb2022-# from section natural join conflicteddb2022-# order by building, room_number;building | room_number | course_id | sec_id | semester | year-----------+-------------+-----------+--------+----------+------Alumni | 547 | 362 | 2 | Fall | 2006Alumni | 547 | 445 | 1 | Spring | 2001Alumni | 547 | 581 | 1 | Spring | 2005Bronfman | 700 | 604 | 1 | Spring | 2009Bronfman | 700 | 362 | 3 | Spring | 2008...Whitman | 434 | 482 | 1 | Fall | 2021Whitman | 434 | 482 | 1 | Fall | 2005(114 rows)
39. Create a view faculty showing only the ID, name, and department of instructors.
db2022=# create view faculty asdb2022-# select ID, name, dept_namedb2022-# from instructor;CREATE VIEWdb2022=# select * from faculty;id | name | dept_name-------+-------------------+-------------63395 | McKinnon | Cybernetics78699 | Pingr | Statistics...99052 | Dale | Cybernetics(50 rows)
40. Create a view CSinstructors, showing all information about instructors from the Comp. Sci. department.
db2022=# create view CSinstructors asdb2022-# select ID, name, dept_name, salarydb2022-# from instructordb2022-# where dept_name='Comp. Sci.';CREATE VIEWdb2022=# select * from CSinstructors;id | name | dept_name | salary-------+----------+------------+-----------34175 | Bondi | Comp. Sci. | 125469.1133351 | Bourrier | Comp. Sci. | 91797.83(2 rows)
41. Insert appropriate tuple into each of the views faculty and CSinstructors, to see what updates your database allows on views; explain what happens.
不能直接插入,必须声明有instead of的触发器,指定insert时要干什么。
insert into faculty values (0, 'new', 'Comp. Sci.');create or replace rule r1_faculty_insert ason insert to faculty do insteadinsert into instructor values(new.ID, new.name, new.dept_name)returning instructor.ID, instructor.name, instructor.dept_name;insert into faculty values (0, 'new', 'Comp. Sci.');select * from faculty order by ID;create or replace rule r1_CSinstructors_insert ason insert to CSinstructors do insteadinsert into instructor values(new.ID, new.name, new.dept_name, new.salary);insert into CSinstructors values(0, 'new', 'Unknown Dept', 100);insert into CSinstructors values(0, 'new', 'Unknown Dept', 100000);insert into CSinstructors values(1, 'new', 'Unknown Dept', 100000);insert into CSinstructors values(1, 'new', 'Biology', 100000);insert into CSinstructors values(2, 'new', 'Comp. Sci.', 100000);select * from CSinstructors order by ID;
对CSinstructors的前三条插入均失败,第一条不满足salary范围限制,第二条0重复,第三条违反instructor对department的外键引用。
第四条成功,但其实不满足视图的where子句,需要自己在rule中修改。
第五条成功。
db2022=# insert into faculty valuesdb2022-# (0, 'new', 'Comp. Sci.');ERROR: cannot insert into view "faculty"HINT: You need an unconditional ON INSERT DO INSTEAD rule or an INSTEAD OF INSERT trigger.db2022=# create or replace rule r1_faculty_insert asdb2022-# on insert to faculty do insteaddb2022-# insert into instructor values(new.ID, new.name, new.dept_name)db2022-# returning instructor.ID, instructor.name, instructor.dept_name;CREATE RULEdb2022=# insert into faculty values (0, 'new', 'Comp. Sci.');INSERT 0 1db2022=# select * from faculty order by ID;id | name | dept_name-------+-------------------+-------------0 | new | Comp. Sci.14365 | Lembr | Accounting15347 | Bawa | Athletics...db2022=# create or replace rule r1_CSinstructors_insert asdb2022-# on insert to CSinstructors do insteaddb2022-# insert into instructor values(new.ID, new.name, new.dept_name, new.salary)db2022-# ;CREATE RULEdb2022=# insert into CSinstructors valuesdb2022-# (0, 'new', 'Unknown Dept', 100);ERROR: new row for relation "instructor" violates check constraint "instructor_salary_check"DETAIL: N/Adb2022=# insert into CSinstructors values(0, 'new', 'Unknown Dept', 100000);ERROR: duplicate key value violates unique constraint "instructor_pkey"DETAIL: Key (id)=(0) already exists.db2022=# insert into CSinstructors valuesdb2022-# (1, 'new', 'Unknown Dept', 100000);ERROR: insert or update on table "instructor" violates foreign key constraint "instructor_dept_name_fkey"DETAIL: Key (dept_name)=(Unknown Dept) is not present in table "department".db2022=# insert into CSinstructors valuesdb2022-# (1, 'new', 'Biology', 100000);INSERT 0 1
42. Create a new user and grant permission to the user to view all data in your student relation.
create user user1 with password 'Password123';grant select on student to user1;gsql -d db2022 -U user1 -W 'Password123' -rselect * from student;select * from faculty;
db2022=# create user user1 with password 'Password123';CREATE ROLEdb2022=# grant select on student to user1;GRANTdb2022=# \qomm@97375c3acbd1:/$ gsql -d db2022 -U user1 -W 'Password123' -rgsql ((openGauss 2.1.0 build 590b0f8e) compiled at 2021-09-30 14:29:04 commit 0 last mr )Non-SSL connection (SSL connection is recommended when requiring high-security)Type "help" for help.db2022=> select * from student;id | name | dept_name | tot_cred-------+--------------------+-------------+----------5925 | Maw | Languages | 4425611 | Sve | English | 5794569 | Yip | English | 46...db2022=> select * from faculty;ERROR: permission denied for relation facultyDETAIL: N/A
43. Now grant permission to all users to see all data in your faculty view.
grant select on faculty to public;select * from faculty;
db2022=> grant select on faculty to public;ERROR: permission denied for relation facultyDETAIL: N/Adb2022=> \qomm@97375c3acbd1:/$ gsql -d db2022 -rgsql ((openGauss 2.1.0 build 590b0f8e) compiled at 2021-09-30 14:29:04 commit 0 last mr )Non-SSL connection (SSL connection is recommended when requiring high-security)Type "help" for help.db2022=# grant select on faculty to public;GRANTdb2022=# \qomm@97375c3acbd1:/$ gsql -d db2022 -U user1 -W 'Password123' -rgsql ((openGauss 2.1.0 build 590b0f8e) compiled at 2021-09-30 14:29:04 commit 0 last mr )Non-SSL connection (SSL connection is recommended when requiring high-security)Type "help" for help.db2022=> select * from faculty;id | name | dept_name-------+-------------------+-------------63395 | McKinnon | Cybernetics78699 | Pingr | Statistics...