数据库原理及应用教程(第4版)源代码

1章 数据库系统概述

无源代码

第2章 关系数据库

【例2-5】查询计算机系的全体学生。

sDept='计算机' (S)或

s5='计算机' (S)(其中5为属性Dept的序号)

【例2-6】查询工资高于1000元(不包括1000元)的男教师。

s (Sal>1000) ∧(Sex= '男')(T)

【例2-7】查询教师的姓名、教师号及其职称。

ΠTN,TNo,Prof(T)或Π2,1,5(T)(其中2,1,5分别为属性TN,TNo和Prof的序号)

【例2-8】查询教师关系中有哪些系。

ΠDept(T)

【例2-9】查询讲授C5课程的教师号。

ΠTNo(σCNo='C5'(TC))

【例2-11】查询讲授“数据库”课程的教师姓名。

 【例2-13】查询选修了全部课程的学生学号和姓名。

 

【例2-14】查询至少选修了C1课程和C3课程的学生学号。

ΠSno,Cno(SC)÷ΠCNo(sCNo='C1'∨Cno=C3'(C))

【例2-15】查询所有学生的数据。

GET W (S)

【例2-16】查询所有被选修的课程号码。

GET W (SC.CNo)

【例2-17】查询计算机系工资高于1000元(不包括1000元)的教师的姓名和工资。

GET W (T.TN,T.Sal):T.Dept= '计算机'∧T.Sal>1000

【例2-18】查询S3同学所选课程号及成绩,并按成绩降序排列。

GET W (SC.CNo,SC.Score):SC.SNo= 'S3' DOWN SC.Score

【例2-19】查询一名男教师的教师号和姓名。

GET W (1) (T.TNo,T.TN):T.Sex= '男'

【例2-20】查询一名男教师的教师号和姓名,并使他的年龄最小。

GET W (1) (T.TNo,T.TN):T.Sex= '男' UP T.Age

【例2-21】查询S3同学所选课程号。

RANGE SC X

GET W (X.CNo):X.SNo= 'S3'

【例2-22】查询S3同学所选课程名。

RANGE SC X

GET W (C.CN):$X(C.CNo=X.CNo∧X.SNo= 'S3')

【例2-23】查询至少选修一门其课时数为80的课程的学生的姓名。

RANGE C CX

      SC SCX

GET W (S.SN):$SCX(SCX.SNo=S.SNo∧$CX(CX.CNo=SCX.CNo∧CX.CT=80))

【例2-24】查询选修全部课程的学生姓名。

RANGE C CX

      SC SCX

GET W (S.SN):"CX$SCX(SCX.SNo=S.SNo∧CX.CNo=SCX.CNo)

【例2-25】求学号为S1学生的平均分。

GET W (AVG(SC.Score):S.SNo= 'S1'

【例2-26】求学校共有多少个系。

GET W (COUNT(S.Dept))

【例2-27】把刘伟教师转到信息系。

HOLD W(T.Dept):T.TN= '刘伟'

MOVE '信息' TO W.Dept

UPDATE W

【例2-28】在SC表中插入一条选课记录(S6,C1,85)。

MOVE 'S6' TO W.SNo

MOVE 'C1' TO W.CNo

MOVE 85 TO W.Score

PUT W(SC)

【例2-29】删除学号为S6的学生的信息。

HOLD W(S):S.SNo= 'S6'

DELETE W

【例2-30】删除全部学生的信息。

HOLD W(S)

DELETE W

【例2-31】定义学生关系S。

CREATE S (SNo=C8,SN=C20,Age=I3,Sex=C2,Dept=C20)

【例2-32】查询计算机系工资高于1000元的教师的姓名和工资。

RANGE OF TX IS T

RETRIEVE (TX.TN,TX.Sal)

WHERE TX.Dept= '计算机'∧TX.Sal>1000

【例2-33】查询讲授C5课程的教师的姓名。

RANGE OF TX IS T

RANGE OF TCX IS TC

RETRIEVE (TX.TN)

WHERE TX.TNo=TCX.TNo AND TCX.CNo= 'C5'

【例2-34】把刘伟教师转到信息系。

RANGE OF TX IS T

REPLACE(TX.Dept= '信息')

WHERE TX.TN= '刘伟'

【例2-35】在SC表中插入一条选课记录(S6,C2,80)。

APPEND TO SC(SNo= 'S6',CNo= 'C2',Score=80)

【例2-36】删除学号为S6的学生的信息。

RANGE OF SX IS S

DELETE SX

WHERE SX.SNo='S6'

3章 关系数据库标准语言——SQL

【例3-1】

CREATE DATABASE Teach

ON

(  NAME=Teach_Data,

    FILENAME='D:\TeachData.mdf',

    SIZE=10,

    MAXSIZE=500,

    FILEGROWTH=10)

LOG ON

(   NAME=Teach_Log,

    FILENAME='D:\TeachData.ldf',

    SIZE=5,

    MAXSIZE=500,

    FILEGROWTH=5)

3-2

ALTER DATABASE Teach

MODIFY FILE

( NAME = Teach_Data,

   FILEGROWTH = 20)

【例3-3

ALTER DATABASE Teach

ADD FILE(

          NAME=Teach_Datanew,

          FILENAME='E:\Teach_Datanew.ndf',

          SIZE=100,

          MAXSIZE=200,

          FILEGROWTH=10)

【例3-4

ALTER DATABASE Teach

REMOVE FILE Teach_Datanew

【例3-5

DROP DATABASE Teach

【例3-6

CREATE TABLE S

(  SNo VARCHAR(6),

   SN NVARCHAR(10),

   Sex NCHAR(1) DEFAULT '男',

   Age INT,

   Dept NVARCHAR(20))

【例3-7

CREATE TABLE S

(  SNo VARCHAR(6) CONSTRAINT S_CONS NOT NULL,

   SN NVARCHAR(10),

   Sex NCHAR(1),

   Age INT,

   Dept NVARCHAR(20))

【例3-8

CREATE TABLE S

(  SNo VARCHAR(6),

   SN NVARCHAR(10) CONSTRAINT SN_UNIQ UNIQUE,

   Sex NCHAR(1),

   Age INT,

   Dept NVARCHAR(20))

【例3-9

CREATE TABLE S

(  SNo VARCHAR(6),

   SN NVARCHAR(10) UNIQUE,

   Sex NCHAR(1),

   Age INT,

   Dept NVARCHAR(20)

   CONSTRAINT S_UNIQ UNIQUE(SN, Sex))

【例3-10】

CREATE TABLE S

(  SNo VARCHAR(6) CONSTRAINT S_Prim PRIMARY KEY,

   SN NVARCHAR(10) UNIQUE,

   Sex NCHAR(1),

   Age INT,

   Dept NVARCHAR(20))

CREATE TABLE C

(  CNo VARCHAR(6) CONSTRAINT C_Prim PRIMARY KEY,

   CN NVARCHAR(20),

   CT INT)

【例3-11

CREATE TABLE SC

(  SNo VARCHAR(6) NOT NULL,

    CNo VARCHAR(6) NOT NULL,

    Score NUMERIC(4,1),

    CONSTRAINT SC_Prim PRIMARY KEY(SNo,CNo))

【例3-12】

CREATE TABLE SC

(  SNo VARCHAR(6) NOT NULL CONSTRAINT S_Fore FOREIGN KEY REFERENCES S(SNo),

   CNo VARCHAR(6) NOT NULL CONSTRAINT C_Fore FOREIGN KEY REFERENCES C(CNo),

   Score NUMERIC(4,1),

   CONSTRAINT S_C_Prim PRIMARY KEY (SNo,CNo))

【例3-13】

CREATE TABLE SC

(  SNo VARCHAR(6),

   CNo VARCHAR(6),

   Score NUMERIC(4,1) CONSTRAINT Score_Chk CHECK(Score>=0 AND Score <=100))

【例3-14】

CREATE TABLE S

(  SNo VARCHAR(6) CONSTRAINT S_Prim PRIMARY KEY,

   SN NVARCHAR(10) CONSTRAINT SN_Cons NOT NULL,

   Sex NCHAR(1) CONSTRAINT Sex_Cons NOT NULL DEFAULT '男',

   Age INT CONSTRAINT Age_Cons NOT NULL

                   CONSTRAINT Age_Chk CHECK (Age BETWEEN 15 AND 50),

   Dept NVARCHAR(20) CONSTRAINT Dept_Cons NOT NULL)

【例3-15】

ALTER TABLE S

ADD

Class_No VARCHAR(6),

Address NVARCHAR(20)

【例3-16

ALTER TABLE SC

ADD

CONSTRAINT Score_Chk CHECK(Score BETWEEN 0 AND 100)

【例3-17

ALTER TABLE S

ALTER COLUMN

SN NVARCHAR(12)

【例3-18

ALTER TABLE S

DROP CONSTRAINT S_Prim

【例3-19

DROP TABLE S

【例3-20

SELECT SNo, SN, Age

FROM S

【例3-21

SELECT * 

FROM S

【例3-22

SELECT DISTINCT SNo  

FROM SC

【例3-23

SELECT SN Name, SNo, Age 

FROM S

SELECT SN AS Name, SNo, Age

FROM S

【例3-24

SELECT SNo,Score

FROM SC

WHERE CNo= 'C1'

【例3-25

SELECT SNo,CNo,Score

FROM SC

WHERE Score>85

【例3-26

SELECT SNo, CNo, Score

FROM SC

WHERE (CNo = 'C1' OR CNo = 'C2') AND (Score >= 85)

【例3-27

SELECT TNo,TN,Prof

FROM T

WHERE Sal BETWEEN 1000 AND 1500

【例3-28

SELECT TNo,TN,Prof

FROM T

WHERE Sal NOT BETWEEN 1000 AND 1500

【例3-29

SELECT SNo, CNo, Score

FROM SC

WHERE CNo IN('C1','C2')

【例3-30

SELECT SNo, CNo, Score FROM SC

WHERE CNo NOT IN('C1','C2')

【例3-31

SELECT TNo, TN 

FROM T

WHERE TN LIKE '张%'

【例3-32

SELECT TNo, TN 

FROM T

WHERE TN LIKE'_力%'

【例3-33

SELECT SNo, CNo

FROM SC

WHERE Score IS NULL

【例3-34】

SELECT SUM(Score) AS TotalScore, AVG(Score) AS AvgScore

FROM SC

WHERE (SNo = 'S1')

【例3-35

SELECT MAX(Score) AS MaxScore, MIN(Score) AS MinScore, MAX(Score)-MIN(Score) AS Diff

FROM SC

WHERE (CNo = 'C1')

【例3-36

SELECT COUNT(SNo)FROM S

WHERE Dept= '计算机'

【例3-37

SELECT COUNT(DISTINCT Dept) AS DeptNum

FROM S

【例3-38

SELECT COUNT (Score)

FROM SC

【例3-39】

SELECT COUNT(*) FROM S

WHERE Dept='计算机'

【例3-40】

SELECT TNo,COUNT(*) AS C_Num

FROM TC

GROUP BY TNo

【例3-41

SELECT SNo, COUNT(*) AS SC_Num

FROM SC

GROUP BY SNo

HAVING (COUNT(*) >= 2)

【例3-42

SELECT SNo, Score

FROM SC

WHERE (CNo = 'C1')

ORDER BY Score DESC

【例3-43

SELECT SNo, CNo, Score

FROM SC

WHERE CNo IN ('C2', 'C3', 'C4', 'C5')

ORDER BY SNo, Score DESC

【例3-44

(1)方法1:

SELECT T.TNo,TN,CNo

FROM T,TC

WHERE (T.TNo = TC.TNo) AND (TN='刘伟')

(2)方法2:

SELECT T.TNo, TN, CNo

FROM T INNER JOIN TC

ON T.TNo = TC.TNo

WHERE (TN = '刘伟')

(3)方法3:

SELECT R1.TNo R2.TN, R1.CNo

FROM

(SELECT TNo,CNo FROM TC ) AS R1

INNER JOIN

(SELECT TNo ,TN FROM T

WHERE TN='刘伟') AS R2

ON R1.TNo=R2.TNo

【例3-45

SELECT S.SNo,SN,CN,Score

FROM S,C,SC

WHERE S.SNo=SC.SNo AND SC.CNo=C.CNo

【例3-46

SELECT C.CNO,CN,COUNT(SC.SNo) as 选课人数

FROM C,SC

WHERE SC.CNo=C.CNo

GROUP BY C.CNo,CN

【例3-47

SELECT S.SNo,SN,CN,Score

FROM S

LEFT OUTER JOIN SC

ON S.SNo=SC.SNo

LEFT OUTER JOIN C

ON C.CNo=SC.CNo

【例3-48

SELECT *

FROM S CROSS JOIN C

【例3-49】

方法1:

SELECT X.TN,X.Sal AS Sal_a,Y.Sal AS Sal_b

FROM T AS X ,T AS Y

WHERE X.Sal>Y.Sal AND Y.TN='刘伟'

方法2:

SELECT X.TN, X.Sal,Y.Sal

FROM T AS X INNER JOIN T AS Y

ON X.Sal>Y.Sal AND Y.TN='刘伟'

方法3:

SELECT R1.TN,R1.Sal, R2.Sal

FROM

(SELECT TN,Sal FROM T ) AS R1

INNER JOIN

(SELECT Sal FROM T

WHERE TN='刘伟') AS R2

ON R1.Sal>R2.Sal

【例3-50

方法1:

SELECT SN,Age,CN

FROM S,C,SC

WHERE S.SNo=SC.SNo AND SC.CNo=C.CNo

方法2:

SELECT R3.SNo,R3.Age,R4.CN

FROM

(SELECT SNo,SN,Age FROM S) AS R3

INNER JOIN

(SELECT R2.SNo,R1.CN

FROM

(SELECT CNo,CN FROM C) AS R1

INNER JOIN

(SELECT SNo,CNo FROM SC) AS R2

ON R1.CNo=R2.CNo) AS R4

ON R3.SNo=R4.SNo

【例3-51

SELECT TNo,TN

FROM T

WHERE Prof=   (SELECT Prof

                 FROM T

                 WHERE TN= '刘伟')

【例3-52

SELECT TN FROM T

WHERE (TNo = ANY ( SELECT TNo

                       FROM TC

                       WHERE CNo = 'C5'))

【例3-53

SELECT TN, Sal

FROM T

WHERE (Sal > ANY (  SELECT Sal

                        FROM T

                        WHERE Dept = '计算机'))

         AND (Dept <> '计算机')

【例3-54

SELECT TN

FROM T

WHERE (TNo IN ( SELECT TNo

                    FROM TC

                   WHERE CNo = 'C5'))

【例3-55

SELECT TN, Sal

FROM T

WHERE (Sal > ALL (  SELECT Sal

                        FROM T

                        WHERE Dept = '计算机'))

        AND (Dept <> '计算机')

【例3-56

SELECT DISTINCT TN

FROM T

WHERE  ('C5' <> ALL ( SELECT CNo

                          FROM TC

                          WHERE TNo = T.TNo))

【例3-57

SELECT TN

FROM T

WHERE EXISTS ( SELECT *

                  FROM TC

                  WHERE TNo = T.TNo AND CNo = 'C5')

【例3-58

SELECT TN

FROM T

WHERE (NOT EXISTS (   SELECT *

                          FROM TC

                          WHERE TNo = T.TNo AND CNo = 'C5'))

【例3-59

SELECT SN

FROM S

WHERE (NOT EXISTS (  SELECT *

                         FROM C

                         WHERE NOT EXISTS (  SELECT *

                                                 FROM SC

                                                 WHERE SNo = S.SNo

                                                   AND CNo = C.CNo)))

【例3-60

SELECT SNo AS 学号, SUM(Score) AS 总分

FROM SC

WHERE (SNo = 'S1')

GROUP BY SNo

UNION

SELECT SNo AS 学号, SUM(Score) AS 总分

FROM SC

WHERE (SNo = 'S5')

GROUP BY SNo

【例3-61

SELECT SNo AS 学号, SUM(Score) AS 总分

INTO Cal_Table

FROM SC

GROUP BY SNo

【例3-62

INSERT INTO S (SNo, SN, Age, Sex, Dept)

VALUES ('S7', '郑冬', 21, '女', '计算机')

【例3-63

INSERT INTO SC (SNo, CNo)

VALUES ('S7', 'C1')

【例3-64

CREATE TABLE AvgSal

( Department VARCHAR(20),

   Average SMALLINT)

INSERT INTO AvgSal

SELECT Dept,AVG(Sal)

FROM T

GROUP BY Dept

【例3-65

UPDATE T

SET Dept= '信息'

WHERE TN= '刘伟'

【例3-66

UPDATE S

SET Age=Age+1

【例3-67

UPDATE T

SET Sal = 1.2 * Sal

WHERE (Prof = '讲师 ') AND (Sal <= 1000)

【例3-68

UPDATE T

SET Comm = Comm + 100

WHERE (TNo IN ( SELECT TNo

                     FROM T, TC

                     WHERE T.TNo = TC.TNo AND TC.CNo = 'C5'))

【例3-69】

UPDATE T

SET Sal = (  SELECT 1.2 * AVG(Sal)

              FROM T)

【例3-70

DELETE

FROM T

WHERE TN= '刘伟'

【例3-71

DELETE

FROM TC

【例3-72

DELETE

FROM TC

WHERE (TNo = ( SELECT TNo

                  FROM T

                  WHERE TN = '刘伟'))

【例3-73

CREATE VIEW Sub_T

AS SELECT TNo, TN, Prof

     FROM T 

     WHERE Dept = '计算机'

【例3-74

CREATE VIEW S_SC_C(SNo, SN, CN, Score)

AS SELECT S.SNo, SN, CN, Score

    FROM S, C, SC

    WHERE S.SNo = SC.SNo AND SC.CNo = C.CNo

【例3-75】

CREATE VIEW S_Avg(SNo, Avg)

AS SELECT SNo, Avg(Score)

    FROM SC

    GROUP BY SNo

【例3-76

ALTER VIEW S_SC_C(SN, CN, Score)

AS SELECT SN, CN, Score

    FROM S, C, SC

   WHERE S.SNo = SC.SNo AND SC.CNo = C.CNo

【例3-77

DROP VIEW Sub_T

【例3-78

SELECT TNo, TN

FROM Sub_T

WHERE Prof = '教授'

【例3-79

INSERT INTO Sub_T (TNo, TN, Prof)

VALUES ('T6', '李丹', '副教授')

【例3-80

UPDATE Sub_T

SET Prof = '副教授'

WHERE (TN = '刘伟')

【例3-81】

DELETE FROM Sub_T

WHERE TN = '刘伟'

【例3-82】

CREATE UNIQUE INDEX SCI ON SC(SNo,CNo)

【例3-83】

CREATE CLUSTERED INDEX TI ON T(TN)

【例3-84

EXEC Sp_helpindex SC

【例3-85

EXEC Sp_rename 'T.TI', 'T_Index'

第4章 关系数据库理论

无源代码

第5章 数据库安全保护

【例5-1

CREATE RULE age_rule

AS @age >= 18 and @age <= 50

【例5-2

EXEC sp_bindrule 'age_rule', 'S.Age'

【例5-3

EXEC sp_unbindrule 'S.Age'

【例5-4

DROP RULE age_rule

【例5-5

CREATE DEFAULT birthday_defa

AS '1990-1-1'

【例5-6

EXEC sp_bindefault,birthday_defa,'S.Birthday'

【例5-7

EXEC sp_unbindefault 'S.Birthday'

【例5-8

DROP DEFAULT birthday_defa

第6章 数据库设计

无源代码

第7章 SQL Server 2012高级应用

【例7-1

DECLARE @id char(8)

SELECT @id='10010001'

【例7-2

DECLARE @sno varchar(10), @sn varchar(10)

SELECT @sno = SNo, @sn = SN FROM S WHERE SNo = 'S7'

【例7-3

USE Teach  /*将教学管理数据库Teach置为当前数据库*/

GO

SELECT * FROM SC

SELECT COUNT(*) FROM S

GO

【例7-4】

USE Teach

GO

IF (SELECT AVG(Score) FROM SC WHERE SNo='S1')>=60

   PRINT 'Pass!'

ELSE

   PRINT 'Fail!'

GO

【例7-5

USE Teach                       

GO

DECLARE @message VARCHAR(255) /*定义变量message */

IF EXISTS (SELECT * FROM S WHERE SNo='S1')

   SET @message='存在学号为S1的学生'

ELSE

   SET @message='不存在学号为S1的学生'

PRINT @message

GO

【例7-6

USE Teach

GO

SELECT SNo,

     Sex=

     CASE Sex

        WHEN '男' THEN 'M'

        WHEN '女' THEN 'F'

     END

FROM S

GO

【例7-7

USE Teach

GO

SELECT SNo,CNo, Score=

      CASE  

        WHEN Score IS NULL THEN '未考'

        WHEN Score<60 THEN '不及格'

        WHEN Score>=60 AND Score<70 THEN '及格'

        WHEN Score>=70 AND Score<90 THEN '良好'

        WHEN Score>=90 THEN '优秀'

      END

FROM SC

GO

【例7-8

DECLARE @s SMALLINT,@i SMALLINT,@nums SMALLINT

SET @s=0

SET @i=1

SET @nums=0

WHILE (@i<=100)

     BEGIN

        IF (@i%3=0)

           BEGIN

              SET @s=@s+@i

              SET @nums=@nums+1

           END

        SET @i=@i+1

     END

PRINT @s

PRINT @nums

【例7-9

WAITFOR DELAY '01:02:03'

SELECT * FROM S

【例7-10

WAITFOR TIME '11:24:00'

SELECT * FROM S

【例7-11

DECLARE @s SMALLINT,@i SMALLINT

SET @i=1

SET @s=0

BEG:

IF (@i<=10)

   BEGIN

       SET @s=@s+@i

       SET @i=@i+1

       GOTO BEG   /*使程序跳转到标号为BEG的地方执行*/

   END

PRINT @s

【例7-12

CREATE FUNCTION dbo.Fun1(@n AS INT)

RETURNS INT

AS  

BEGIN

    DECLARE @i INT

    DECLARE @sign INT

    SET @sign=1

    SET @i=2

    WHILE  @i<=SQRT(@n)

       BEGIN

          IF @n % @i=0

             BEGIN

                 SET @sign=0

                 BREAK

             END

           SET @i=@i+1

       END

    RETURN @sign

END

【例7-13

CREATE FUNCTION dbo.Fun2()

RETURNS TABLE

AS  

return select SNo,SN from S

【例7-14

CREATE FUNCTION Score_Table

(@student_id CHAR(6))

RETURNS @T_score TABLE

(Cname VARCHAR(20),

Grade INT

)

AS

BEGIN

   INSERT INTO @T_score

   SELECT CN,Score

   FROM SC,C

   WHERE  SC.CNo=C.CNo and SC.SNo=@student_id and Score<60

  RETURN

END

【例7-15

USE Teach

GO

CREATE PROCEDURE MyProc AS

SELECT * FROM S WHERE Sex='男'

【例7-16

USE Teach

GO

CREATE PROCEDURE InsertRecord

(   @sno VARCHAR(6),

    @sn NVARCHAR(10),

    @sex NCHAR(1),

    @age INT,

    @dept NVARCHAR(20)

)

AS

INSERT INTO S VALUES(@sno,@sn,@sex,@age, @dept)

【例7-17

USE Teach

GO

CREATE PROCEDURE InsertRecordDefa

(   @sno VARCHAR(6),

    @sn NVARCHAR(10),

    @sex NCHAR(1),

    @age INT,

    @dept NVARCHAR(20)= '无'

)

AS

INSERT INTO S VALUES(@sno, @sn, @sex, @age, @dept)

【例7-18

USE Teach

GO

CREATE PROCEDURE QueryTeach

(   @sno VARCHAR(6),

    @sn NVARCHAR(10) OUTPUT,

    @dept NVARCHAR(20) OUTPUT

)

AS

SELECT @sn=SN,@dept=Dept

FROM S

WHERE SNo=@sno

【例7-19

USE Teach

GO

EXEC sp_helptext MyProc

【例7-20

USE Teach

GO

DROP PROCEDURE MyNewProc

【例7-21

USE Teach

GO

EXEC MyProc

【例7-22

USE Teach

GO

EXEC InsertRecord @sno='S7', @sn='王大利', @sex='男', @age=18, @dept='计算机系'

【例7-23

USE Teach

GO

EXEC InsertRecordDefa @sno='S10', @sn='高平', @age=18, @sex='女'

【例7-24

USE Teach

GO

DECLARE @sn NVARCHAR(10)

DECLARE @dept NVARCHAR(20)

EXEC QueryTeach 'S10',@sn OUTPUT,@dept OUTPUT

SELECT '姓名'=@sn, '系别'=@dept

【例7-25

USE Teach

GO

CREATE TRIGGER del_S ON S

AFTER DELETE

AS  

DELETE FROM SC

WHERE SC.SNo  

IN (SELECT SNo FROM DELETED)

GO

DELETE FROM S WHERE SNo='S1'

【例7-26】

USE Teach

GO

CREATE TRIGGER insert_sc ON SC

AFTER INSERT

AS

  IF EXISTS (SELECT * FROM INSERTED WHERE Sno IN (SELECT Sno FROM S))

     PRINT '添加成功!'  

  ELSE   

    BEGIN   

       PRINT '学生表S中没有该学生的基本信息。拒绝插入!'

       ROLLBACK TRANSACTION  

    END

【例7-27

USE Teach

GO

CREATE TRIGGER safety

ON DATABASE

FOR DROP_TABLE, ALTER_TABLE

AS  PRINT '不能删除或修改数据库表!'

ROLLBACK

GO

【例7-28

USE Teach

GO

EXEC sp_helptrigger 'S'

GO

【例7-29

USE Teach

GO

EXEC sp_helptext 'insert_sc'

GO

【例7-30

USE Teach

GO

EXEC sp_help 'insert_sc'

GO

【例7-31

SELECT * INTO TS FROM S  -- 产生一个临时表TS

DROP TABLE TS              -- 删除表TS失败

GO

DISABLE TRIGGER safety ON DATABASE  --使safety触发器无效

DROP TABLE TS              -- 成功删除表TS

GO

【例7-32

USE Teach

GO

EXEC sp_addumpdevice 'disk','pubss','c:\backdev\backdevpubs.bak'

【例7-33

USE Teach

GO

EXEC sp_addumpdevice 'disk','networkdevice','\\servername\sharename\path\filename. ext'

【例7-34

USE Teach

GO

EXEC sp_dropdevice 'pubss', 'c:\backdev\backdevpubs.bak'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bryan Ding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值