Views, triggers, storage, security, etc.
Experimental requirements
(1) Master the definition and operation of views
(2) Master the definition of triggers
(3) Master the definition of stored procedures
(4) Master how to authorize and revoke permissions for users
(5) Master the methods of user-defined integrity
( 6) Write out the experiment report
Experimental steps
(1) Create a view of the table
(2) Use the view to complete the query of the table
(3) Delete the view of the table
(4) Create a trigger
(5) Create a stored procedure
(6) Authorize and query
users (7) Complete user definition Sex
view
(1) Create
Create CS view CS_View CS view CS_View
Encounter problems
'CREATE VIEW' must be the first statement in the query batch. The
batch must start with a CREATE statement. In other words, only one batch statement in a query analyzer is the standard syntax. CREATE DEFAULT, CREATE FUNCTION, CREATE PROCEDURE, CREATE RULE, CREATE SCHEMA, CREATE TRIGGER, and CREATE VIEW statements cannot be combined with other statements in batch processing.
All other statements following the batch will be interpreted as part of the definition of the first CREATE statement. Just add GO keywords in batches.
You can also re-create a query to write this batch statement
go
create view CS_View
as
select *
from Student
where Sdept='CS'
with check option
(2) Query
In the view CS_View, query the students of the CS department who have taken the No. 1 course
go
select cs_view.sno,sname,ssex,sage,cno
from CS_View,sc
where cs_view.sno=sc.sno and cno=1
(3) Create IS system
Create a view IS_View for students with an IS department score greater than 80
go
create view IS_View
as
select Student.Sno,Sname,cno,grade
from Student,sc
where Student.sno=sc.Sno and Grade>80
(4) IS_View query
In the view IS_View, query the students whose IS department score is greater than 80
select *
from IS_View
where grade>80
(5) Delete
Delete view IS_View
drop view IS_View
(6) Different user operations
Use the visual window to create two different users U1 and U2, use the system administrator to grant U1 the
query and update permissions for the Student table, and grant U2 the insert permissions for the SC table. then
Then enter the database S_T_ user set permissions
But there is a problem when logging in
Perform the following operations to successfully log in. Solution reference source
Login with U1, respectively
- 1) Query the information of the student table;
- 2) Increase the age of all students by 1 year, and then query;
- 3) Delete students from IS department;
- 4) Query the course selection information of the CS department.
Only the student table can be viewed after login
1)and 2)
select *
from Student
update Student
set Sage+=1
select *
from Student
3)
delete
from Student
where Sdept='IS'
4)
select sc.Sno,sname,sc.Cno
from Student,sc
where Sdept='CS'and Student.Sno=sc.Sno
Login with U2, respectively
- 1) Insert 1 record ('200215122', '1', 75) in the SC table;
- 2) Query the information of the SC table,
- 3) Query the information of view CS_View.
insert into sc values('200215122','1',75)
select *
from sc
select *
from CS_view
(7) Withdraw permission
Log in as a system administrator and take back all permissions of U1
(8) U1 query again
Log in with U1 to query the information in the student table
(9) System administrator login
Log in as system administrator
trigger
(10) Create a trigger
Create an update trigger for the SC table. When the score of the SC table is
updated , if the updated score is greater than or equal to 95, check whether the student with the score has a scholarship, and if the scholarship is "No", modify it to "Yes" .
If the modified score is less than 95, check whether the student's other scores are greater than 95. If there are none, and the modified score is greater than 95, then the scholarship will be modified to "No".
Then modify the score and verify whether the trigger is executed correctly.
For the update operation, two virtual tables deleted (data before update) and inserted (data after update) will be created. After completion, the deleted data in the student table will be deleted and inserted data will be inserted
I didn’t write the trigger very well, I hope I can point out the deficiencies
go
create trigger sc_trigger on sc --对sc表建立触发器
after update--语法:{After|Instead of} {insert|update|delete}
as
begin --begin后面输入sql语句
update Student
set Scholarship='是'
where Sno in
(select sc.sno
from sc,inserted
where inserted.Grade>=95 and Student.sno=sc.Sno
and sc.sno =inserted.Sno and scholarship='否' )
update Student
set Scholarship='否'
where sno in
(select sc.sno from sc,inserted,deleted
/*修改后小于95*/
where inserted.Grade<95 and sc.sno =inserted.sno
/*大于95的数为0*/
and (select count(grade) from sc where Grade>=95 and sno = inserted.sno)<1
and deleted.grade>=95 --该学生修改前分数大于95
and sc.sno=Student.sno)
end
- 1) First, modify the score of a certain student to 98 to check the scholarship.
- 2) Modify the score just now to 80, and then check the scholarship.
update sc
set Grade=98
where sno='200215122' and cno=2
select scholarship
from Student
where sno='200215122'
update sc
set Grade=80
where sno='200215122' and cno=2
select scholarship
from Student
where sno='200215122'
(11) Delete trigger
drop trigger sc_trigger
Delete the trigger just defined
Stored procedure
- The stored procedure Procedure is a set of SQL statements to complete a specific function, afterStored in the database after compilation, The user specifiesThe name of the stored procedureAnd give parameters to execute
- The stored procedure can contain logic control statements and data manipulation statements , which can accept parameters, output parameters, return single or multiple result sets, and return values.
- Because the stored procedure is compiled on the database server and stored in the database when it is created, the stored procedure runs faster than a single SQL statement block. At the same time, because you only need to useProvide the stored procedure namewithNecessary parameter information, So it can reduce network traffic and simple network burden to a certain extent.
(12) Define a stored procedure
Define a stored procedure to calculate the average and highest grades of courses in the CS department, execute the stored procedure in the query analyzer or query editor, and view the results.
格式:
go
create proc(全称procedure可以简写为proc) [存储过程的名字]
( [@参数1 参数类型1],[@参数2 varhcar(50)],[参数3 varchar(50)='小白'],
[参数4 int output] ) /*output加上output后表示该参数是返回的参数*/
as
SQL语句
use S_T_
if (object_id('cs_proc', 'P') is not null)
drop proc cs_proc --判断该存储过程是否不存在,存在则删除
go
create procedure cs_proc
as
select avg(grade) ,max(grade)
from sc,student
where sc.sno =student.sno and Sdept='CS'
go
execute cs_proc --调用
go
(13)
Define a student ID as a parameter to view the results of all courses of a certain student ID. The query result must include the student's name. authenticating.
if (object_id('grade_proc', 'P') is not null)
drop proc grade_proc --判断该存储过程是否不存在,存在则删除
go
create procedure grade_proc( @sno_s varchar(10))
as
select sname,sc.sno,cno,grade
from Student,sc
where sc.sno=Student.sno and sc.Sno=@sno_s
go
declare @sno_in varchar(10)='200215122'
exec grade_proc @sno_s=@sno_in
go
(14) Function
Change the previous question to a function. Verify again.
if OBJECT_ID(N'dbo.search') is not null
drop function dbo.Search
go
create function dbo.Search(@Stu_No varchar(10))
returns table
as
return
(select Sname,sc.sno,cno,Grade from Sc,Student where Student.Sno=Sc.Sno and Student.Sno=@Stu_No )
go
--调用一下函数查询学号为200215122的学生成绩
select * from dbo.Search('200215122')
Security (constraint)
(15) Integrity constraints
Define an integrity constraint on the SC table, requiring the score to be between 0-100. Before defining the constraints, first modify a student's score to 120, perform a query, and then modify it back. After defining the constraints, modify the student's score to 120, and then query
update sc
set grade =120
where sno='200215122' and cno =2;
select sno,cno,grade from sc
where sno='200215122' and cno =2;
update sc
set grade =80
where sno='200215122' and cno =2
alter table sc
add constraint c3 check(grade between 0 and 100)
update sc
set grade =120
where sno='200215122' and cno =2;
select sno,cno,grade from sc
where sno='200215122' and cno =2