当前位置:网站首页>Database computer experiment 4 (data integrity and stored procedure)
Database computer experiment 4 (data integrity and stored procedure)
2022-04-23 18:33:00 【Sanjin C_ C】
This database mainly takes data integrity and stored procedures as an example , The experimental textbook is Li Chunbao's 《 Database principle and technology 》, The content of this experiment is
1) complete P245 The first 8 topic 、 The first 9 topic .
2) complete P395 Computer experiment questions 5 Various topics 、 Computer experiment questions 6 Of (1) and (2) Little topic .
3) complete P396 Computer experiment questions 7 Various topics .
In the last experiment, the index and view were too simple, so I stopped blogging , If necessary, you can comment and leave a message .
Of course, for data integrity , It's more important , But in general engineering design, I don't mind setting the foreign key of data ( Ali company specially emphasized ), The reason is also obvious , Foreign key constraints are cumbersome for later modifications , So you don't have to or don't mind using .
The experiment will be described below :
- Add a non empty constraint to the student number ( When you start building tables, don't forget )
create database test
use test
create table table10(
Student number int,
full name nvarchar(10),
major nvarchar(20),
fraction int
)
-- 245-1
alter table table10
alter column Student number int not null
- Set the student ID as the primary key
-- 245-2
alter table table10
add primary key ( Student number )
3. Set... On the score column 1-100 Value range of
-- 245-3
alter table table10
add check( fraction >0 and fraction <101)
4. Set the default value on the specialty column “ Computer science and technology ”
-- 245-4
alter table table10
add default ' Computer science and technology ' for major
5. Give it “ Borrower's student number ” Column references table10 Learn about the foreign key relationship of the number column ALTER TABLE sentence
-- 245-question9
create table table11(
Book number int,
Title nvarchar(30),
Borrower's student number int
)
alter table table11
add foreign key ( Borrower's student number ) references table10 ( Student number )
Experiment 5 after the book - 7、 ... and
6. take student The gender column in the table is set to take only “ male ” or “ Woman ” value
-- test5 -1
use Library2128
alter table student2128
add constraint Gender check ( Gender in (' male ',' Woman '))
7. take student The default value in the table is male
--test5-2
alter table student2128
add default ' male ' for Gender
8. modify student surface , Take their shift number as depart The foreign key of the flight number
--test5-3
alter table student2128
add foreign key ( Class number ) references depart2128( Class number )
9. take borrow The student number and book number in the table are defined as the primary key
--test5-4, Notice how to delete the primary key here , The keys here are written according to their own
alter table borrow2128
drop CONSTRAINT PK__borrow21__7B9FD30EF71AFC24
alter table borrow2128
add primary key( Student number , Book number )
10. Delete the previously created constraint
-- test5-5
alter table student2128
drop constraint Gender
alter table student2128
drop constraint FK__student2128__ Class number __49C3F6B7
11. Create a transaction to depart Table insert 2 Record and roll back the transaction , Finally, check the table
-- Test 6-1
begin transaction
insert into depart2128 values ('2001',' Department of mathematics ')
insert into depart2128 values ('2003',' physics department ')
--insert into depart2128 values ('2004',' Department of Chemistry ')
rollback
select * from depart2128
12. Create a transaction to depart Table insert 3 A record , Return to the rollback point by setting the rollback point , Finally, check the situation .
-- Test 6-2
begin transaction
insert into depart2128 values ('2001',' Department of mathematics ')
insert into depart2128 values ('2003',' physics department ')
save transaction Mysavp
insert into depart2128 values ('2004',' Department of Chemistry ')
rollback transaction Mysavp
select * from depart2128
13. stay Library Create a scalar valued function in the database Sum(n), seek 1+2+…+n The sum of the , And test with relevant data
-- Test 7-1
use Library2128
create function getSum(@n int)
returns int
as
begin
return @n*(@n+1)/2
end
-- Test data , test n=100,getSum(100)
print dbo.getSum(100)
14. stay Library Create an inline table valued function in the database nbook, Returns the student number of the specified Department 、 full name 、 class 、 The name and date of the book borrowed
-- Test7-2
--drop function nbook
create function nbook(@ch nvarchar(10))
returns table
as
return
(select s. Student number ,s. Class number ,bo. Book name ,br. Borrowing date
from depart2128 d,student2128 s,borrow2128 br,book2128 bo
where d. Department name =@ch and d. Class number =s. Class number and s. Student number =br. Student number and bo. Book number =br. Book number )
-- Test data
select * from dbo.nbook(' Department of electronic engineering ')
15. stay Library Create a multi statement table valued function in the database pbook, Return the Department name and all the students in the Department The average price of books borrowed by students , And test with relevant data
-- Test7-3
create function pbook(@ch nvarchar(10))
returns @st table
(
Department name nvarchar(10),
The average price float
)
as
begin
insert @st
select d. Department name ,avg(bo. pricing *1.0)
from student2128 s,depart2128 d,borrow2128 br,book2128 bo
where d. Department name =@ch and s. Class number =d. Class number and s. Student number =br. Student number and br. Student number =bo. Book number
group by d. Department name
return
end
-- Test data
select * from dbo.pbook(' Department of Computer Science ')
16. Design a stored procedure , Check the number of each book variety , And test with relevant data
-- Test7-4
create procedure getBookNum(@ch nvarchar(10))
as
select Book name ,count(*) as ' number '
from book2128
where Book name =@ch
group by Book name
-- Test data
exec getBookNum 'C Programming '
17. Design a stored procedure , Use the fuzzy query method to find the students who borrow the specified book title , Output student number 、 full name 、 Class number and title , And test with relevant data
create procedure checkStud(@ch nvarchar(10))
as
select s. Student number ,s. full name ,s. Class number ,bo. Book name
from student2128 s,book2128 bo,borrow2128 br
where s. Student number =br. Student number and br. Book number =bo. Book number and bo. Book name =@ch
-- Test data
exec checkStud 'C Programming '
版权声明
本文为[Sanjin C_ C]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231828562387.html
边栏推荐
- The vivado project corresponding to the board is generated by TCL script
- os_authent_prefix
- CISSP certified daily knowledge points (April 19, 2022)
- 【ACM】376. 摆动序列
- Matlab tips (6) comparison of seven filtering methods
- JD freefuck Jingdong HaoMao control panel background Command Execution Vulnerability
- 硬核解析Promise對象(這七個必會的常用API和七個關鍵問題你都了解嗎?)
- Quantexa CDI(场景决策智能)Syneo平台介绍
- powerdesigner各种字体设置;preview字体设置;sql字体设置
- 机器学习理论之(7):核函数 Kernels —— 一种帮助 SVM 实现非线性化决策边界的方式
猜你喜欢
Resolves the interface method that allows annotation requests to be written in postman
Jeecg boot microservice architecture
Robocode tutorial 3 - Robo machine analysis
powerdesigner各种字体设置;preview字体设置;sql字体设置
In win10 system, all programs run as administrator by default
Vulnérabilité d'exécution de la commande de fond du panneau de commande JD - freefuck
Introduction to quantexa CDI syneo platform
QT tablewidget insert qcombobox drop-down box
机器学习理论之(7):核函数 Kernels —— 一种帮助 SVM 实现非线性化决策边界的方式
【ACM】376. Swing sequence
随机推荐
Jeecg boot microservice architecture
listener. log
Notepad + + replaces tabs with spaces
Daily CISSP certification common mistakes (April 12, 2022)
logstash 7. There is a time problem in X. the difference between @ timestamp and local time is 8 hours
Halo open source project learning (VII): caching mechanism
Using transmittablethreadlocal to realize parameter cross thread transmission
Creation and use of QT dynamic link library
ctfshow-web362(SSTI)
Daily network security certification test questions (April 15, 2022)
QT error: no matching member function for call to ‘connect‘
Rust: how to implement a thread pool?
配置iptables
Connection mode of QT signal and slot connect() and the return value of emit
Mysql database backup command -- mysqldump
C language simulates entering and leaving the stack, first in first out, first in first out, shared memory
教你用简单几个步骤快速重命名文件夹名
Correct opening method of option
Test post and login function
Domestic GD chip can filter