DBMSII PracBook
DBMSII PracBook
DBMSII PracBook
: : :
Lecture-in-charge
Instructions to students: This book must be brought for all the lab classes. The attached grading sheet complete with instructor recommendations must be handed over at the time of the viva. This book consists of three Assignments. Complete the assigned exercises as part of the assignments on a weekly basis. Demonstrate to the lab instructor that the work has been done during each practical session and complete the grading sheet. Otherwise, it will be considered as not attempted in class. All answers should be written by hand. Do not attach printed answers to the book. A copy of each completed assignment must be uploaded into the course web available at http://moodle.sliit.lk
Laboratory Assignment 1
Session 1 Objective: Designing E-ER Models for data requirements.
Exercise 1: Bank Data Requirements Consider a company called ABC Consolidated which contains many banks under it. Each bank contains a code, name and a registration number. The code and name are unique. The bank has many branches which contain a branch number, branch name and an address. The branch number is unique for each bank. Each bank contains many accounts. An account contains an account number, balance, a customer and an account type. Each account type contains a name, a code and a description. Account code is unique each account type. Account type may be Savings or Current. The Savings account type contains an interestrate. The Current account type has max-cheques which pertains to maximum cheques that can be written per month. Each customer has a customer number, NIC number, name, address, phone and PIN. Both NIC and customer number are unique. There are many transactions processed in a bank. Each transaction has a transaction id, date & time of transaction, amount and responsible party or application for the transaction (i.e. bank, ATM, teller, cheque, etc.) and description (interest calculation, fund transfer, cheque number etc.) Each transaction has one or many accounts that it affects and the type of affect that the transaction (either a credit/debit) has on the account.
Exercise 2: Library Data Requirements The requirements for the library catalog are as follows:
The library contains a list of Items Every item has an item number, title, and a description Every item is identified by the item number Items are classified into the following categories: books, periodicals, and software & manuals Every book has authors An author has an author_id (unique), a first name, last name and other names. An author can write many books and a book can have many authors. Every periodical has a publisher and frequency (e.g., daily, weekly, monthly, quarterly and yearly) Every software & manual has a manufacturer and a version Books are further classified as Text Books and Published Books Periodicals are classified into Magazines and NewsPapers Every Text Book has a semester, year and section (such as lecture, lab or tutorial) Every Published Book has an edition Published Book has a Published Book Type depending on publisher of the book (for example, same book can be published by different publishers such as US edition by McGrawHill and Indian edition of the book by TATA McGrawHill) Published Book Type is identified by ISBN number. Also, Published Book Type contains Publisher, Cover (softback or hardback) and Year Published. Periodical also has a Periodical Type which contains the date that the periodical was issued (for example, November issue of Sports Illustrated). The items, (i.e. Text Book, News Papers, Published Book Type, Periodical Type and Software & Manuals), can have multiple copies Copy of Item. Copy of Item is identified by the access_no. Copy of Item contains a status field which indicates whether the book is borrowed, lost, stolen or available.
Draw an E-ER diagram for the above-mentioned requirements (Hint: You may need to use aggregation to model the requirements).
Bank Schema: Ensure that the interest_rate of Savings account is a non-negative number (>= 0). Ensure that the customers PIN number is a four digit (0-9) number Ensure that the customers NIC number contains 10 digits Ensure that the customers name is not null. Ensure that the balance in an account is always non-negative number (>= 0). In the Transaction table, the default value for date&time should be the system datetime. In the Transaction table, the value for by attribute must be one of the following: ATM, Teller, Bank, Standing Order, Cheque, On-Line or Other. The type descriptive attribute of the has relationship between Transaction table and Account table should have one of the following values: credit or debit.
Library Schema: The title attribute of Books, Periodicals and Software_Manuals cannot contain a null value. Ensure that item_no of Books, Periodicals, Software_Manuals start with B, P, S respectively Use IDENTITY property for author_id attribute of Authors table to ensure that a unique id is generated for authors. Ensure that Magazine or Newspaper are the only allowed values for type attribute in Periodicals table. Ensure that the book_section attribute of Text_Books can be only Lecture, Lab, or Tutorial values. Ensure that the year attribute of Text_Books is between 1999 and 3000. Ensure that the cover attribute of Published_Book_Type can only have Soft Cover or Hard Cover values.
Ensure that the yr_published attribute of Published_Book_Type is between 1000 and 3000. Ensure that the status attribute of Copy_Item is only Borrowed, Lost, Stolen or Available.
SLIIT Schema: Ensure that the SLIIT students name is not null. The default value for date descriptive attribute of the registers relationship should be the system date. Ensure that semester and year attributes of Semester table are not null. Ensure that groupNo of Batch table is not null.
End of Assignment 1
Laboratory Assignment 2
Session 6
Objective: Data Entry Export the data given in the excel sheet to the database. You may use Data Transformation Services (Import/Export Wizard) to do this. If you write any scripts to load to tables, you should save them as DataEntry.sql
Laboratory Assignment 3
Session 9
Objective: Indexes and Query Plans Note: In SQL Server 2008, by default, a clustered index is created for primary key and a nonclustered index is created for each attribute(s) having unique constraints. A. Consider the following queries: Find the PIN number given the customer number. Find all accounts belonging to the customer given the customer number Given the customer number and account number, return the balance Find the total amount of the transactions for each responsible party or application (i.e. bank, ATM, teller, cheque, etc.). Find the PIN number given the customer NIC. Write SQL statements to return the information needed. Provide sample inputs. B. Consider the query plans generated by SQL Server for the above mentioned queries. (Hint: In SQL Server 2008, you can view the execution plan by enabling Display Estimated Plan or Include Actual Execution Plan options on the query window). C. Assume that the workload given in A is frequent. Create appropriate indexes that you think will improve the performance of the above workload. (Hint. In SQL Server 2008, you can create indexes using the CREATE INDEX statement. Use Books Online and other references to find out more about SQL Servers indexes). D. Consider the query plans generated by SQL Server 2008 after the indexes. E. Write a brief summary of your findings.
Simulation 2: Concurrency Control Open two database connections (say A and B). Load the Lab7.sql script to both applications. Use the inputs of simulation 1 for both scripts. In Database Connection A: Execute only the part of the script till the first update statement In Database Connection B: Execute only the part of the script till the first update statement. Expected Behavior: The script in B has not completed execution. The transaction is waiting for the release of locks from As script. Then complete the rest of script in A. Expected Behavior: The update in B completes Then execute a SELECT * FROM Account in A. Expected Behavior: The query executes and waits until B releases the locks Now execute the rest of Bs transaction. Expected Behavior: As select statement completes and results are output. Briefly explain these simulation results discussing the reasons for their behavior.
In this lab assignment, you need to simulate a deadlock occurring using SQL Server 2008. Step 1: Consider a deadlock scenario using two or more transactions Step 2: Write SQL script for each transaction Step 3: Execute a part of each transaction until the deadlock occurs. (Hint: Use a separate database connection for each transaction). Hint: Use SERIALIZABLE isolation level for each transaction.
Expected Behavior: A victim transaction is aborted by the SQL Server with a similar message: Msg
1205, Level 13, State 56, Line 1
Transaction (Process ID 53) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
End of Assignment 3
Grading Sheet
Student Reg. no. : Student name : Date Comments
Session 1
Session 2
Session 3
Session 4
Session 5
Session 6
Session 7
Session 8
Session 9
Session 10
Session 11