From 7a3c40a8b59fd2d6714e8733ec819ac0671608af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E5=90=AF=E6=81=92?= <3013545725@qq.com> Date: Mon, 10 Oct 2022 05:37:28 +0000 Subject: [PATCH 1/4] =?UTF-8?q?=E7=BD=97=E5=90=AF=E6=81=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 罗启恒 <3013545725@qq.com> --- .../2022-10-10 \345\255\230\345\202\250.sql" | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 "44 \347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\344\275\234\344\270\232/2022-10-10 \345\255\230\345\202\250.sql" diff --git "a/44 \347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\344\275\234\344\270\232/2022-10-10 \345\255\230\345\202\250.sql" "b/44 \347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\344\275\234\344\270\232/2022-10-10 \345\255\230\345\202\250.sql" new file mode 100644 index 0000000..86fa1b0 --- /dev/null +++ "b/44 \347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\344\275\234\344\270\232/2022-10-10 \345\255\230\345\202\250.sql" @@ -0,0 +1,96 @@ +-- 1. 洢ʵֲѯ˻͵п˻Ϣ,ʾпţ˻ +select * from AccountInfo +select * from BankCard +select * from CardExchange +go +create proc proc_GetMin +as +begin + select CardNo п, RealName ,CardMoney ˻ from AccountInfo join BankCard on BankCard.AccountId=AccountInfo.AccountId + where CardMoney =(select min(CardMoney) from BankCard) +end +go +exec proc_GetMin +drop procedure proc_GetMin +-- 2. ģпǮпţǮʵִǮ +create proc proc_GeiMoneyBank +@cardNo varchar(30), +@MoneyInBank money +as +begin + update BankCard set CardMoney+=@MoneyInBank where CardNo=@cardNo + insert into CardExchange (CardNo,MoneyInBank,MoneyOutBank,ExchangeTime) + values (@cardNo,@MoneyInBank,0,GETDATE()) +end +go +exec proc_GeiMoneyBank '6225125478544587',3000 +drop proc proc_GeiMoneyBank +-- 3. ģпȡǮпţȡǮʵȡǮȡǮɹ1ȡǮʧܷ-1 +go +create proc proc_GeiMoneyBank +@cardNo varchar(30), +@MoneyOutBank money +as +begin + update BankCard set CardMoney-=@MoneyOutBank where CardNo=@cardNo + if (@@ERROR!=0) + return -1 + insert into CardExchange (CardNo,MoneyInBank,MoneyOutBank,ExchangeTime) + values (@cardNo,@MoneyOutBank,0,GETDATE()) + return 1 +end +go +declare @MoneyOutBank int +exec @MoneyOutBank = proc_GeiMoneyBank '6225547854125656',1000 +print @MoneyOutBank +drop proc proc_GeiMoneyBank +-- 4. **ѯijʱεдȡϢԼܽ**ȡܽ뿪ʼʱ䣬ʱ䣬ʾȡϢͬʱشܽȡܽ +go +create proc proc_Get + @startTime smalldatetime, + @endTime smalldatetime, + @SumIn money output, + @SumOut money output +as +begin + select @SumIn =sum(MoneyInBank),@SumOut=sum(MoneyOutBank)from CardExchange + where ExchangeTime between @startTime and @endTime + select * from CardExchange +where ExchangeTime between @startTime+' 00:00:00' and @endTime+' 23:59:59' +end +go +declare @SumIn money +declare @SumOut money +exec proc_Get '2022-09-20','2022-09-20',@SumIn output,@SumOut output +drop proc proc_Get +-- 5. ****û룬ûȷ볤<8Զ8λ +-- (ʾ 0-9 float(rand()*10)) rand():0.0-1.0С float:ȡ +go +create proc proc_PwdUpgrade +@cardno nvarchar(20), +@pwd nvarchar(20) output +as +begin + if not exists(select * from BankCard where CardNo=@cardno and CardPwd=@pwd) + set @pwd = '' + else + begin + if len(@pwd) < 8 + begin + declare @len int = 8- len(@pwd) + declare @i int = 1 + while @i <= @len + begin + + set @pwd = @pwd + cast(FLOOR(RAND()*10) as varchar(1)) + set @i = @i+1 + end + update BankCard set CardPwd = @pwd where CardNo=@cardno + end + end +end +go +declare @pwd nvarchar(20) = '123456' +exec proc_PwdUpgrade '6225547854125656',@pwd output +select @pwd +drop proc proc_PwdUpgrade \ No newline at end of file -- Gitee From de2c0d0f2310014091bd4330f71db06aed52fcfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E5=90=AF=E6=81=92?= <3013545725@qq.com> Date: Mon, 10 Oct 2022 05:37:44 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=E7=BD=97=E5=90=AF=E6=81=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 罗启恒 <3013545725@qq.com> --- ...0\345\202\250\350\277\207\347\250\213.sql" | 163 ++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 "44 \347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\347\254\224\350\256\260/\345\255\230\345\202\250\350\277\207\347\250\213.sql" diff --git "a/44 \347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\347\254\224\350\256\260/\345\255\230\345\202\250\350\277\207\347\250\213.sql" "b/44 \347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\347\254\224\350\256\260/\345\255\230\345\202\250\350\277\207\347\250\213.sql" new file mode 100644 index 0000000..4fb12ff --- /dev/null +++ "b/44 \347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\347\254\224\350\256\260/\345\255\230\345\202\250\350\277\207\347\250\213.sql" @@ -0,0 +1,163 @@ +--execute:ִ +exec sp_databases --鿴ݿ + +exec sp_tables --鿴 + +exec sp_columns course + + +exec sp_password '123456','654321' + + +use studenttest +go +--û룬 procedure +--1. 洢̲ѯѧ,СѧϢ +create proc proc_GetMinAge +as +begin + select stuname, stuage from stuinfo where stuage = (select min(stuage) from stuinfo) +end + + +--drop procedure proc_GetMinAge + +exec proc_GetMinAge + + +--2. +-- 洢̲ѯѧϢ +go +create proc proc_GetMinAgeByName +@stuName varchar(20) +as +begin + select * from stuinfo where stuname=@stuName +end +go + +exec proc_GetMinAgeByName '' + +--3. зֵ:ֻ 1.رֵͣ 2.ֵһű +go +create proc proc_GetMinAgeById +@stuNo varchar(20) +as +begin + declare @age int + select @age=stuage from stuinfo where stuNo=@stuNo + set @stuNo = 'S2022' + return @age +end +go + +--drop procedure proc_GetMinAgeById + +--@ageշصֵ +declare @age int +declare @stuid varchar(20) = 's2001' +exec @age = proc_GetMinAgeById @stuid +print @age +print @stuid + + + +--ѧѧţ +go +create proc proc_GetMinAgeBystuid +@stuid varchar(20) +as +begin + declare @stuName varchar(20) + select @stuName=stuname from stuinfo where stuNo=@stuid + --ص ˴洢Ч + return @stuname +end +go + +--ⲿ +declare @name varchar(20) +exec @name = proc_GetMinAgeBystuid 'S2001' +print @name + + + +--4.ͣ +--ѧ(stunum)洢ʵѧϢ name school major +use STUDENTS +go + + +create proc proc_GetStuInfoById +@stuNum varchar(20), +@name varchar(20) output, -- +@school varchar(20) output, +@major varchar(20) output +as +begin + select @name=name, @school=school,@major=major from tb_student where stu_num=@stunum +end + +--ĸ +declare @stunum varchar(20) = '16130203' +declare @name varchar(20),@school varchar(20),@major varchar(20) + +exec proc_GetStuInfoById @stunum,@name output, @school output , @major output +print @name + char(9) + @school + char(9) + @major + + + + + + + + + + + + + + + + +--5.һͬʱ߱룬Ĵ洢 +go +create proc proc_GetMinAgeBystuNo +@stuNo varchar(20) output --output +as +begin + declare @age int + set @stuNo = 'S2022' +end +go + +declare @stuno varchar(20) = 'S2001' + +exec proc_GetMinAgeBystuNo @stuno output +print @stuno + + +--ѯijʱεдȡϢԼܽ**ȡܽ +--뿪ʼʱ䣬ʱ䣬ʾȡϢͬʱشܽȡܽ +use BankTest +go + +select * from cardExchange + + +create proc GetMoneyByTime +@startTime smalldatetime, +@endTime smalldatetime, +@TotalSaveMoney money output, +@TotalWithdrawMoney money output +as +begin + select @TotalSaveMoney = sum(MoneyInBank),@TotalWithdrawMoney=sum(MoneyOutBank) from cardExchange + where ExchangeTime between @startTime and @endTime +end + +-- +declare @startTime smalldatetime = '2022-09-07',@endTime smalldatetime = '2022-09-18' +declare @TotalSaveMoney money,@TotalWithdrawMoney money +exec GetMoneyByTime @startTime,@endTime,@TotalSaveMoney output , @TotalWithdrawMoney output +select @TotalSaveMoney ܶ,@TotalWithdrawMoney ȡܶ \ No newline at end of file -- Gitee From 00fea77c9975697ce11766d3eb4b933de9f6a090 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E5=90=AF=E6=81=92?= <3013545725@qq.com> Date: Tue, 11 Oct 2022 12:18:38 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=E7=BD=97=E5=90=AF=E6=81=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 罗启恒 <3013545725@qq.com> --- .../\350\247\246\345\217\221\345\231\250.md" | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 "44 \347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\347\254\224\350\256\260/\350\247\246\345\217\221\345\231\250.md" diff --git "a/44 \347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\347\254\224\350\256\260/\350\247\246\345\217\221\345\231\250.md" "b/44 \347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\347\254\224\350\256\260/\350\247\246\345\217\221\345\231\250.md" new file mode 100644 index 0000000..4a4043b --- /dev/null +++ "b/44 \347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\347\254\224\350\256\260/\350\247\246\345\217\221\345\231\250.md" @@ -0,0 +1,47 @@ +触发器(trigger) +什么是触发器: insert 一系列随着插入操作自动执行的sql语句集合 + 触发器 trigger )是作为**对数据库修改的连带效果而由系统自动执行的一条语句。它是一种特殊的存储过程。也是一个事务(可以回滚)。为了定义一个[触发器] +我们必须: + +指明什么时候执行触发器。这被拆分为引起触发器被检测的一个事件和触发器继续执行所必须满足的一个条件。 +指明当触发器执行时所采取的动作。 + +触发器分类 + +分为 DML触发器 + +DML触发器 : 增删改 + +after触发器: 增删改 之后执行的 + +after触发器主要用于:insert,update,delete + +instead of 触发器 : 增删改 之前检查的 + + +创建触发器的语句 + +sql +CREATE TRIGGER <触发器名称> + +ON table_name +FOR [DELETE, INSERT, UPDATE] + +AS + + T-SQL语句 + +GO +--WITH ENCRYPTION表示加密触发器定义的SQL文本 + +--DELETE, INSERT, UPDATE指定触发器的类型 + + 1.插入操作(Insert) +inserted 表有数据,deleted 表无数据 + +2.删除操作(Delete) +inserted 表无数据,deleted 表有数据 + +3.更新操作(Update) +inserted 表有数据(新数据),deleted 表有数据(旧数据) + -- Gitee From a444318782f742ccfcf885707605855240f8a3ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E5=90=AF=E6=81=92?= <3013545725@qq.com> Date: Tue, 11 Oct 2022 12:18:53 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=E7=BD=97=E5=90=AF=E6=81=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 罗启恒 <3013545725@qq.com> --- .../2022-10-11 \344\275\234\344\270\232.sql" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 "44 \347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\344\275\234\344\270\232/2022-10-11 \344\275\234\344\270\232.sql" diff --git "a/44 \347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\344\275\234\344\270\232/2022-10-11 \344\275\234\344\270\232.sql" "b/44 \347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\344\275\234\344\270\232/2022-10-11 \344\275\234\344\270\232.sql" new file mode 100644 index 0000000..b44fe75 --- /dev/null +++ "b/44 \347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\344\275\234\344\270\232/2022-10-11 \344\275\234\344\270\232.sql" @@ -0,0 +1,35 @@ +select * from Department +select * from People + +--1вűԱԱʱ򣬸ԱIJűڲűҲԶӲϢΪ"²" + +create trigger tri_insert +on People for insert +as + declare @id varchar(20) (select DepartmentId from inserted) + if not exists(select * from Department where DepartmentId=@id) + begin + insert into Department values (@id,'²') + end + +insert into People(DepartmentId,PeopleName,PeopleSex,PeopleSalary) values('008','','',88888) + +--2ʵ֣ɾһŵʱ򽫲Աȫɾ +create trigger tri_delete +on Department for delete +as +delete from people where DepartmentId=(select DepartmentId from deleted) + +delete from Department where DepartmentId='008' + +--4޸һű֮󣬽òԱIJűͬ޸ +create trigger tri_d +on Department for update +as + declare @oldid varchar(20),@newid varchar(20) + select @oldid=DepartmentId from deleted + select @newid=DepartmentId from deleted + update people set DepartmentId=@newid where DepartmentId=@oldid + +update Department set DepartmentId='009' where DepartmentId='001' + -- Gitee