Vous êtes sur la page 1sur 10

CREATE TABLE Customer

(
CustID INT IDENTITY(1,1) PRIMARY KEY,
CustName VARCHAR(100),
OpBalance MONEY
)
GO

CREATE TABLE Trans


(
TransID INT IDENTITY(1,1) PRIMARY KEY,
CustID INT FOREIGN KEY REFERENCES Customer(CustID),
TransType CHAR(1) CHECK (TransType = 'C' OR TransType = 'D'),
Amount MONEY,
TransDate DATETIME
)
GO

INSERT INTO Customer (CustName, OpBalance)


VALUES('Cust1', 1000)

INSERT INTO Customer (CustName, OpBalance)


VALUES('Cust2', 500)

INSERT INTO Customer (CustName, OpBalance)


VALUES('Cust3', 600)

GO

Part I

1. What is Data Integrity, explain about


a. Declarative data integrity
b. Procedural data integrity
2. Explain about
a. entity integrity
b. domain integrity
c. referential integrity
d. user-defined integrity
3. What is Cascading referential integrity
4. What is Trigger and tell me three events automatically fire a trigger
5. Briefly explain “Classes of Triggers”

Part II based on Stored Procedure, Trigger, Data Integrity & Transaction.sql file
1. Create Stored Procedure USP_SaveTransaction, which should insert record in
Trans Table, with following parameters
a. @CustID, @TransType, @Amount, @TransDate

Note:
When “CustID” is not found in Customer Table then it should prompt the user
“Customer does not exists”
When “TransType” other than “C” and “D” then prompt the user “Invalid
Transaction Type”

2. Create Trigger Tri_Trans for Insert, Update, and Delete. Whenever trigger fires
following should be done
a. If “TransType” is “C” i.e. Credit then Opening Balance should be
increased in Customer Table
b. If “TransType” is “D” i.e. Debit then Opening Balance should be decrease
in Customer Table
c. If the “Trans” record is deleted then based on deleted.TransType
i. If it is “C”, i.e. Credit then Opening Balance should be decrease in
Customer Table
ii. If it is “D”, i.e. Debit then Opening Balance should be decrease in
Customer Table
d. If Customer -> OpBalance is less than Zero then nothing should be done.
Prompt the user “Balance is less than zero, so transaction rejected”

3. Create Trigger Tri_Customer_InsteadOf, which should not allow the user for
updating/deleting the record, but it should allow the user to insert the record in
customer table.

4. Create Stored Procedure UPS_TransactionReport, based on parameters


a. @CustID, @StartDate, @EndDate
b. it should give the following results columns
i. CustID, CustName, TransType, Amount, TransDate
c. Note: @CustID, @StartDate, @EndDate is optional.
i. If user do not pass @CustID it should give me all Customers
ii. If user do not pass @StartDate then @StartDate should be
“01/01/1900”
iii. If user do not pass @StartDate then @StartDate should be
“12/31/1900”
5. Create Stored Procedure UPS_TransactionSummaryReport, based on parameters
a. @CustID, @StartDate, @EndDate
b. it should give the following results columns
i. CustID, CustName, CreditAmount, DebitAmount, OpBalance
c. Note: @CustID, @StartDate, @EndDate is optional.
i. If user do not pass @CustID it should give me all Customers
ii. If user do not pass @StartDate then @StartDate should be
“01/01/1900”
iii. If user do not pass @StartDate then @StartDate should be
“12/31/1900”

6. Disable the Tri_Customer_InsteadOf then achieve the following task


a. Whenever a record is deleted in Customer Table, even it should delete
related Customer records in Trans Table also
i. Method I
1. Used Cascade
ii. Method II
1. Write a Stored Procedure “USP_CustomerDel” by passing
Parameter @CustID
2. Use Transactions while deleting the records from both
Customer Table and Trans Table

PART I-ANWERS:

1. Data integrity:-Tables in a SQL server database can include a


number of different types of properties that ensure the integrity
Of the data.Ensuring data integrity ensures the quality of data in
the database
SQL supports number of methods that we can use to enforce data
integrity including data types Not null definition, default
definitions, identity properties,constrainsts,rules,triggers and
indexes
a. Declarative data integrity :- Declarative data integrity is the
mechanisms to enforce integrity are declared as a part of the
definition of the objects in the database .
b. Procedural data integrity:-This means that we can use
programmatic structures and separate objects to enforce data
integrity.

2. a) Entity integrity:-A state in which all the rows in a database


have a Not-null primary key value.All tables have primary
keys and no table has any duplicate primary key values.
This ensures that there are no duplicate entries for anything
represented in the database.
b) Domain integrity:-Data types help determine what values are
valid for particular column.this is known as Domain
integrity.the domain is simply the set of valid values for a
particular column.
Nullability is another option to determine which values are
valid In the domain.

c) Referential integrity refers to the maintenance of relationships


between data rows in multiple tables we enforce referential
integrity with Declarative data integrity.

d) User-defined integrity:-Userdefined integrity enables you to


Define specific business rules that do not fall into one of the
other integrity categories.
All of the integrity categories support user –defined integrity.
3. Cascading referential integrity:- Cascading referential integrity
is used to delete the information from multiple tables which are
under relation ship.

4. Triggers:-Triggers are a special class of stored procedures


defined to execute automatically when an update,insert,delete
statement is issued against a table or a view.
5. There are two types of triggers
a) After/For triggers:- These can be created on only tables and
executed after constraint checking
b) Instead of Triggers:-These can be created on tables and views
And executed before constraint checking.

Part II based on Stored Procedure, Trigger, Data Integrity & Transaction.sql


file

1. Create Stored Procedure USP_SaveTransaction, which should insert


record
in Trans Table, with following parameters
a. @CustID, @TransType, @Amount, @TransDate

Note:
When “CustID” is not found in Customer Table then it should prompt the
user
“Customer does not exists”
When “TransType” other than “C” and “D” then prompt the user “Invalid
Transaction Type”
SELECT * FROM CUSTOMER
SELECT * FROM TRANS

ALTER PROC USP_SaveTransaction


(
@CustID INT,
@TransType CHAR(1),
@Amount MONEY,
@TransDate DATETIME
)
AS
BEGIN
IF NOT EXISTS(SELECT * FROM CUSTOMER
WHERE CUSTID=@CUSTID)

BEGIN
PRINT 'CUSTOMER DOES NOT EXIST'
END
ELSE IF @TransType<>'C' AND @TransType<>'D'
BEGIN
PRINT 'Invalid Transaction Type'
END
ELSE
BEGIN
INSERT INTO TRANS(CustID, TransType, Amount, TransDate)
VALUES(@CustID, @TransType, @Amount, @TransDate)
END
END

--EXEC USP_SaveTransaction 19,'D',7000,'5-4-06'

2. Create Trigger Tri_Trans for Insert, Update, and Delete.


Whenever trigger fires following should be done
a. If “TransType” is “C” i.e. Credit then Opening Balance should be
increased in Customer Table
b. If “TransType” is “D” i.e. Debit then Opening Balance should be
decrease in Customer Table
c. If the “Trans” record is deleted then based on deleted.TransType
i. If it is “C”, i.e. Credit then Opening Balance should be decrease
in Customer Table
ii. If it is “D”, i.e. Debit then Opening Balance should be decrease
in Customer Table
d. If Customer -> OpBalance is less than Zero then nothing should be done.
Prompt the user “Balance is less than zero, so transaction rejected”
ALTER TRIGGER TRI_TRANS2
ON TRANS
FOR INSERT, UPDATE, DELETE
AS
DECLARE @TransType CHAR(1)
DECLARE @Amount MONEY
DECLARE @CustID INT
SELECT @TransType = TransType, @Amount = Amount, @CustID = CustID
FROM INSERTED
IF @TransType ='C'
BEGIN
UPDATE CUSTOMER
SET OpBalance=ISNULL(OpBalance, 0)+ @Amount
WHERE CustID = @CustID
END
IF @TransType ='D'
BEGIN
UPDATE CUSTOMER
SET OpBalance=ISNULL(OpBalance, 0) - @Amount
WHERE CustID = @CustID
END
IF NOT EXISTS(SELECT * FROM INSERTED) AND EXISTS(SELECT *
FROM DELETED)
BEGIN
SELECT @TransType = TransType, @Amount = Amount, @CustID =
CustID FROM DELETED
IF @TransType ='C'
BEGIN
UPDATE CUSTOMER
SET OpBalance=ISNULL(OpBalance, 0) - @Amount
WHERE CustID = @CustID
END
IF @TransType ='D'
BEGIN
UPDATE CUSTOMER
SET OpBalance=ISNULL(OpBalance, 0) + @Amount
WHERE CustID = @CustID
END
END

SELECT * FROM TRANS


SELECT * FROM CUSTOMER

3)Create Trigger Tri_Customer_InsteadOf, which should not allow the user


for updating/deleting the record, but it should allow the user to
insert the record in customer table.

ALTER Trigger Tri_Customer_InsteadOf


ON Customer
INSTEAD OF UPDATE, DELETE
AS
PRINT 'UPDATE & DELETE is not allowed on this Customer Table'

--UPDATE CUSTOMER
--SET OPBALANCE=300

--DELETE FROM CUSTOMER WHERE CUSTID=8


--INSERT INTO CUSTOMER(CUSTNAME,OPBALANCE)
VALUES('CUST5',600)
SELECT * FROM CUSTOMER
SELECT * FROM TRANS

4. Create Stored Procedure UPS_TransactionReport, based on parameters


a. @CustID, @StartDate, @EndDate

b. it should give the following results columns

i. CustID, CustName, TransType, Amount, TransDate

c. Note: @CustID, @StartDate, @EndDate is optional.

i. If user do not pass @CustID it should give me all Customers


ii. If user do not pass @StartDate then @StartDate should be “01/01/1900”
iii. If user do not pass @EndtDate then @ENDDate should be “12/31/9999”

ALTER Proc UPS_TransactionReport


(
@CustID INT=null,
@StartDate DATETIME=null,
@EndDate DATETIME=null
)
AS
SELECT
C.CUSTID,C.CUSTNAME,T.TRANSTYPE,T.AMOUNT,T.TRANSDATE
FROM CUSTOMER C INNER JOIN TRANS T ON C.CUSTID=T.CUSTID
WHERE C.CUSTID=ISNULL(@CUSTID,C.CUSTID)
AND TRANSDATE BETWEEN ISNULL(@StartDate,'01/01/1900') AND
ISNULL(@EndDate, '12/31/9999')
--EXEC UPS_TransactionReport
5. Create Stored Procedure UPS_TransactionSummaryReport, based on parameters
a. @CustID, @StartDate, @EndDate
b. it should give the following results columns
i. CustID, CustName, CreditAmount, DebitAmount, OpBalance
c. Note: @CustID, @StartDate, @EndDate is optional.
i. If user do not pass @CustID it should give me all Customers
ii. If user do not pass @StartDate then @StartDate should be “01/01/1900”
iii. If user do not pass @ENDDate then @ENDDate should be “12/31/9900”

CREATE Proc UPS_TransactionSummaryReport


(
@CustID INT=null,
@StartDate DATETIME=null,
@EndDate DATETIME=null
)
AS
SELECT
C.CustID, C.CustName,
Cr.CreditAmount, Dr.DebitAmount,
(Cr.CreditAmount - Dr.DebitAmount) OpBalance
FROM Customer C
INNER JOIN
(
SELECT CUSTID, SUM(AMOUNT) CreditAmount
FROM TRANS
WHERE TRANSTYPE = 'C'
AND TRANSDATE BETWEEN
ISNULL(@StartDate,'01/01/1900') AND ISNULL(@EndDate, '12/31/9999')
GROUP BY CUSTID
) Cr ON (Cr.CustID = C.CustID)
INNER JOIN
(
SELECT CUSTID, SUM(AMOUNT) DebitAmount
FROM TRANS
WHERE TRANSTYPE = 'D'
AND TRANSDATE BETWEEN
ISNULL(@StartDate,'01/01/1900') AND ISNULL(@EndDate, '12/31/9999')
GROUP BY CUSTID
) Dr ON (Dr.CustID = C.CustID)
WHERE C.CUSTID=ISNULL(@CUSTID,C.CUSTID)

EXEC UPS_TransactionSummaryReport
6. Disable the Tri_Customer_InsteadOf then achieve the following task
a. Whenever a record is deleted in Customer Table, even it should delete
related Customer records in Trans Table also
i. Method I
1. Used Cascade
ii. Method II
1. Write a Stored Procedure “USP_CustomerDel” by passing Parameter @CustID
2. Use Transactions while deleting the records from both Customer Table
and Trans Table

Create Trigger Tri_Customer_InsteadOf, which should not allow the user


for updating/deleting the record, but it should allow the user to
insert the record in customer table.
--6ANSWER)FOR DISABLING TRIGGER:-
ALTER TABLE CUSTOMER DISABLE TRIGGER Tri_Customer_InsteadOf
--ALTER TABLE CUSTOMER DISABLE TRIGGER TRI_TRANS2
a)METHOD I
ALTER TABLE CUSTOMER
DROP CONSTRAINT FK_Trans_Customer
ALTER TABLE TRANS
ADD CONSTRAINT FK_Trans_Customer
FOREIGN KEY(CUSTID) REFERENCES TRANS(CUSTID)
ON DELETE CASCADE
--DOUBT
METHOD II:-
ALTER PROC USP_CustomerDel
(
@CustID INT
)
AS
BEGIN TRANSACTION
DECLARE @@ERROR1 INT
DECLARE @@ERROR2 INT
DELETE FROM TRANS WHERE
CUSTID=@CUSTID
SET @@ERROR1=@@ERROR
DELETE FROM CUSTOMER WHERE
CUSTID=@CUSTID
SET @@ERROR2=@@ERROR
IF @@ERROR1=0 AND @@ERROR2=0
ROLLBACK TRANSACTION
ELSE
COMMIT TRANSACTION

EXEC USP_CustomerDel 22
SP_HELP TRANS
SELECT * FROM CUSTOMER
SELECT * FROM TRANS

Vous aimerez peut-être aussi