MySQL "View"

create database StudentManage;

use StudentManage;

create table Student
(Sno int primary key,
Sname nchar(10) ,
Ssex nchar(2),
Sage int,
Sdept nvarchar(30));

create table Course
(Cno int primary key,
Cname nvarchar(30),
Cpno int,
Ccredit int);

create table SC
(Sno int,
Cno int,
Grade int,
primary key(Sno,Cno));

insert into Student
values(201215121,'李勇','男',20,'CS'),
(201215122,'刘晨','女',19,'CS'),
(201215123,'王敏','女',18,'MA'),
(201215124,'张立','男',19,'IS');


insert into Course
values(1,'数据库',5,4),(2,'数学',NULL,2),
(3,'信息系统',1,4),(4,'操作系统',6,3),
(5,'数据结构',7,4),(6,'数据处理',NULL,2),
(7,'PASCAL',6,4);

insert into SC 
values(201215121,1,92),(201215121,2,85),
(201215121,3,88),(201215122,2,90),(201215122,3,80);

1. Create a view STU_SEX to query all boys' information from the Student table. The column names of the view are SNO, SNAME, SSEX, and SAGE. Through this view, add a male information (201215125,'Sun Wu','male',21) and a female information (201215126,'Li Lan','female',20) respectively to see if it can be successful.

①Create view command; ②Add boy information command; ③Whether adding boy information is successful (answer yes or no); ④Add girl information command; ⑤Whether adding girl information is successful (answer yes or no).
correct answer:

第一空:
CREATE VIEW STU_SEX (SNO, SNAME, SSEX, SAGE) AS SELECT sno,sname,ssex,sage FROM student WHERE ssex = ‘男’;

Second empty:
INSERT INTO STU_SEX VALUES(201215125,'Sun Wu','male',21);

INSERT INTO STU_SEX(SNO,SNAME,SSEX,SAGE) VALUES(201215125,'Sun Wu','male',21);

The third empty:
yes

Fourth empty:
INSERT INTO STU_SEX VALUES(201215126,'Li Lan','女',20);

INSERT INTO STU_SEX(SNO,SNAME,SSEX,SAGE) VALUES(201215126,'Li Lan','Female',20);

Fifth empty:
yes

2. From the Course table, create a view COURSE_PRE for querying the information of all the course prerequisites. The column names of the view are Course Number, Course Name, and Prerequisite Course Name.

CREATE VIEW COURSE_PRE (course number, course name, prerequisite name)
AS SELECT c1.cno, c1.cname, c2.cname
FROM course c1
JOIN course c2
ON c1.cpno = c2.cno;

3. From the SC table, create a view STU_CJ1 to query the score information of students whose scores are greater than or equal to 85. The column names of the view are student number, course number and grade.

CREATE VIEW STU_CJ1 (student number, course number, grade)
AS SELECT sno, cno, grade FROM SC WHERE grade >= 85;

4. Establish the view STU_CJ2 to query students' electives from the three tables of Student, SC and Course. The column names of the view are Name, Course Name, and Grade.

CREATE VIEW STU_CJ2 (name, course name, grade)
AS SELECT s.sname, c.cname, sc.grade
FROM student s
JOIN sc
ON s.sno = sc.sno
JOIN course c
ON sc.cno = c.cno;

5. Establish the view STU_CJ3 from the three tables of Student, SC and Course to query the scores of students whose elective courses are less than 90. The column names of the view are Name, Course Name, and Grade.

create view stu_cj3(姓名,课程名称,成绩) as select
s.Sname,c.Cname,sc.Grade from student s join sc
on s.Sno=sc.Sno
join course c
on sc.Cno=c.Cno
where sc.grade<90;

6. Use SQL commands to modify the view STU_SEX. Change the column name of the view to student ID, name, gender and age, and add the "WITH CHECK OPTION" option. Through this view, add a piece of male information (201215127,'Wang Gang','male',20) and female information (201215128,'Lin Xu','female',18) respectively to see if it can be successful. Summarize the role played by the with check option statement. ①Modify the view command; ②Add the boy information command; ③Whether the addition of the boy information is successful (answer yes or no); ④The add the girl information command; ⑤The addition of the girl information is successful (answer yes or no); ⑥Summary of the statement with check option Role played

The first empty:
create or replace view stu_sex (student number, name, gender, age) as select

sno,sname,ssex,sage

from student

where ssex=‘男’

with check option;

Second empty:
insert into stu_sex values(201215127,'Wang Gang','male',20);

The third empty:
yes

Fourth empty:
insert into stu_sex values

(201215128,'Lin Xu','Female', 18);

Fifth empty:
no

Sixth empty:
add the with check option clause when creating a view. When inserting (or updating) data in the view, check the conditions. If the gender is male, it can be inserted. If the gender is female, it will return. error.

7. Delete the view STU_CJ3.

DROP VIEW STU_CJ3; DROP VIEW IF EXISTS STU_CJ3;

8. Create a simple user1 with a password of 123456.

CREATE USER user1 IDENTIFIED BY ‘123456’;

9. Create view view_1, query the name and department of all students (no special instructions, no need to define the view field name, the same below).

CREATE VIEW view_1 AS SELECT sname, sdept FROM student;

10. Create view view_2, query the names and departments of all students, and specify DEFINER as user1.

create definer=‘user1’ view view_2
as select Sname,Sdept
from student;

11. Create view view_3, query the names and departments of all students, and specify SQL SECURITY as INVOKER.

CREATE SQL SECURITY INVOKER VIEW view_3
AS SELECT Sname,Sdept FROM student;

12. Grant user1 the permissions to query the views view_1, view_2, and view_3. ① Write here only the command that grants user1 user permission to query view view_1.

GRANT SELECT ON Studentmanage.view_1 TO user1;

13. Switch user1 user login and view the views view_1, view_2, and view_3 respectively. According to the query results, explain the role of DFINER and SQL SECURITY INVOKER. ①Whether the view_1 data can be queried and answer yes or no; ②Whether the view_2 data can be queried, and answer yes or no; ③Whether the view_3 data can be queried, and answer yes or no; ④The role played by DFINER; ⑤The role played by SQL SECURITY INVOKER The role of.

correct answer:

First empty:
YES

Second empty:
NO

Third empty:
NO

The fourth space:
Specify the definer of the view.

Fifth empty:
Specify whose authority to operate the view.

Guess you like

Origin blog.csdn.net/ziyue13/article/details/112073533