Triggers, Functions Procedures
Triggers, Functions Procedures
In Lab Exercise
“Stored Procedures, UDFs, Triggers”
Database Systems
Spring 2024
Department of Software
Engineering FAST-NU,
Islamabad, Pakistan
For this exercise use the following schema, the script to create this schema and populate data is
Part – B (Procedures)
1. Create a procedure that returns minimum balance in an output parameter.
2. Create a procedure Login that takes card number, PIN and returns the status in an output
parameter. @status =1 if card number and PIN is valid and @status=0 otherwise. (Hint:
IF/ELSE)
3. Create a procedure WithDraw using Login Procedure (Question#3), the procedure will take
CardNumber, PIN, amount for transaction. Now insert detail of the transaction in
Transaction Table if the transaction is successful with TransType =1(successful Transaction)
and next transID (calculate the max transID and increment transID, then insert in table). If
the transaction fails insert TransType=4 (Failed Transaction). Update the balance against the
card number accordingly.
Part – C (Triggers)
Task 1: Create an after-insert trigger on the "Transaction" table to update the balance in the
"Card" table after each transaction.
Task 2: Create an instead of insert trigger on the "User" table to enforce a restriction on
inserting users with certain names.
Task 3: Create an after-update trigger on the "Card" table to log any changes in the balance
Task 4: Create an instead of delete trigger on the "UserCard" table to prevent deletion of
user-card mappings.
Task 5: Create an after-insert trigger on the "UserType" table to update the description in
the "User" table when a user type is updated.
Part – D (Views)
1. Create User Information View
Task: Develop a SQL view named ViewUserInfo that encapsulates all user
information including user ID, name, phone number, and city. This view will
streamline user data retrieval across various modules of our system.
2. Create Card Details View
Task: Construct a view named ViewCardDetails that merges the card details with
their types. This view should include the card number, PIN, expiry date, balance, and
a description of the card type. This is crucial for customer service and backend
operations to quickly assess card features and status.
3. Create User Cards View
Task: Create a view ViewUserCards that links users to their respective card
numbers. This view should display user ID, name, city, and associated card numbers.
This will be particularly useful for operations needing to manage or audit user-card
relationships.
4. Create Recent Transactions View
Task: Design a view ViewRecentTransactions that lists transactions within the past
year. Include transaction ID, date, card number, and transaction amount in this view
to aid in financial reporting and monitoring of recent activity.
5. Create Card Balances View
Task: Formulate a view ViewCardBalances that shows the current balance for each
card. This simplified view will be essential for quick balance checks and financial
summaries in customer inquiries and internal audits.
Part – E (Indexing)
Task 1: Create an index on the "name" column in the "User" table.
Task 2: Create a composite index on the "userType" and "city" columns in the "User" table.
Task 3: Create an index on the "expireDate" column in the "Card" table.
Task 4: Create an index on the "transDate" column in the "Transaction" table.
Task 5: Create a unique index on the "cardNum" column in the "Card" table.