LabCycle DBD PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

LAB EXERCISE #0 Familiarisation with basic SQL commands Use SQL statements to create the following tables that

t constitute the ODI Cricket database Match(MatchID, Team1, Team2, Ground, Date, Winner) Player(PlayerID, Name, Country, YBorn, BPlace, FirstMatch) Batting(MatchID, PID, NRuns, Average) Bowling(MatchID, PID, Novers,Nmaidens,NRuns,NWickets)

Define the PRIMARY KEY, FOREIGN KEY, NOT NULL, DEFAULT, CHECK and UNIQUE constraints wherever appropriate . Spool your statements and results to a file named LAB0.LST Also populate the tables with necessary data. Use SQL queries to perform the following operations. 1. Add a column Not_Out to the table Batting to include information on how many times a batsman was not out. 2. Print the Player table. 3. Find the IDs, Names and Countries of all players. 4. Find all information about players from India. 5. Find all information about players from India who were born after 1980. 6. Find the details of matches that have been played in Australia. 7. List matches in which India or Australia was Team1. 8. Find IDs of all players who have bowled in an ODI match. 9. Display a sorted list of ground names where Australia has played as Team1. 10. Create a new table IndianTeam with playerID, Name,YBorn, FirstMatch as attributes with values from Player table(Use a single Query). 11. Create a new table Australian Team with playerID, Name,YBorn, FirstMatch as attributes without copying values from Player table(Use a single Query). 12. Delete all rows from IndianTeam 13. Insert values into IndianTeam from Player table. 14. Delete the entries of those players with no wickets from Bowling table. 15. Update the ground attribute value to Chennai for those rows having Madras as the attribute value. 16. Drop the column Not_out from the Batting table. 17. Drop the table Batting

Exercise No.1 Create following tables that constitute an Institute Database 1. Student(RollNo, name,degree, year, gender, deptNo, advisor) ; degree must be Btech, Mtech, MS, PhD 2. Department(Deptid, name,hod,phone) 3. Professor(Empid, name,gender,doj,deptNo, phone,salary) 4. Course(courseId, name, credits,deptNo) 5. Enrolment(rollno,courseid,sem,year,grade) ; 6. Teaching(Empid, courseid, sem,year,classroom,courseTA) ; Assume there is only one course TA for the course. 7. Prerequisite(prerequisiteId, CourseId)

Define the primary key, foreign key, default, check and unique constraints, whenever appropriate. Spool your statements and results to a file named Lab1.txt. Also populate tables with necessary values. Use SQL queries to perform following operations 1. 2. 3. 4. 5. 6. 7. Find the names of all departments in department relation. Display annual salary of professors. Display list of students who have joined for MS Degree after 2010. How many courses are offered by CS Department? Display employee id and name of professors offering 2 courses How many students are enrolled for the courses offered by CS Department? For all students enrolled for the courses offered by CS Department, display their names and rollnumbers 8. Find the names of all branches where faculty number is greater than that of CS department. 9. Find the names of all students whose name includes the substring 'an' 10. List in alphabetic order, all students who have enrolled for the course 'CS110' 11. Display all professors in decreasing order of their salary. If two profs are drawing the same salary, display their names in alphabetical order. 12. Find all students who have enrolled for a course offered by CS dept, EC dept or both. 13. Find all students who have enrolled for a course offered by CS dept and EC dept. 14. Find all students who have enrolled for a course offered by CS dept,but not EC. 15. Find the average salary of professors in CS department. 16. Find the average salary of professors in each branch. 17. Find the number of professors in each branch. 18. Find the salary of each professor from CS offering 3 courses. 19. Find the list of courses taught by the advisor of the student with rollno 'CS01B004' 20. Find the professors using Classroom 'CS112' in the current semester(Year 2013 and sem -odd) 21. Find the students who have enrolled for any course taught by their advisor in the odd semester

of the year 2010. 22. Find the students who have enrolled for all courses taught by their advisor in the odd semester of the year 2010. 23. Find the students who have not enrolled for any course offered by ME. 24. Get the empId and name of the Professors where currently HODs and are not teaching any courses. 25. Get the names of seniormost professors in the CSE Department 26. Find the second level pre-requisites for the course CS750. 27. Which are the departments that dont have any lady professors? 28. List the TAs who have been assigned to lady professors. 29. Find the TAs of all courses enrolled by girl students of CS department 30. Who are the TAs of the courses engaged by HODs of all Departments?

LAB EXERCISE 2 1. Create the tables described below: 1. Table Name : Client

Column Name
Client_No

Data type
Varchar2(6)

Remarks
Primary key, first letter must start with C

Name Address City Pincode Bal_due

Varchar2(15) Varchar2(15) Varchar2(15) Number(6) Number(6,2)

2. Table Name : Product

Column Name
Product_No

Data type
Varchar2(6)

Remarks
Primary key, first letter must start withP Not null Not null Not null Not null Not null, cannot be zero Not null, cannot be zero

Description Profit_Percent Qty_on_hand Reorder_level Sell_price Cost_price

Varchar2(15) Number(4,2) Number(8) Number(8) Number(6,2) Number(6,2)

3. Table Name : Salesman

Column Name
Salesman_no

Data type
Varchar2(6)

Remarks
Primary key, first letter must

start withS name address salary Varchar2(15) Varchar2(15) Number(8) Not null Not null Not null,cannot be zero

4. Table Name : Salesorder

Column Name
Order_No

Data type
Varchar2(6)

Remarks
Primary key, first letter must start with O

Order_date Client_no

date Varchar2(6) Foreign key references Client_no of Client table Foreign key references salesman_no of Salesman table Delivery: Part(P) or Full(F) : default F cannot be less than order_date Values (In Process , Delivered, Canceled)

Salesman_no

Varchar2(6)

Dely_type

Varchar2(1)

Dely_date Order_Status

Date Varchar2(10)

5. Table Name : Orderdetails

Column Name
Order_No

Data type
Varchar2(6)

Remarks
Primary key/ Foreign key references Order_no of Salesorder table Foreign key references Product_no of Product table

Product_no

Varchar2(6)

Qty_ordered

Number(9)

Product_rate

Number(8,2)

1. Populate all the 5 tables with sufficient data 2. Find the names of all clients having a as the second letter in their names. 3. Find out the clients who stay in a city whose second letter is a. 4. Find the list of all clients who stay in Mumbai or Kolkatta 5. Print the list of clients whose bal_due is greater than 10,000. 6. Print the details of orders placed in the month of January. 7. Display the order information for the clients C00001 and C00002. 8. Find the products whose selling price is greater than 5000 and less than or equal to 10,000. 9. Find products whose selling price is more than 2000. Calculate a new selling price as original selling price * 0.15. Rename the new column in the above query as new_price. 10. Count the total number of orders. 11. Calculate the average price for all products. 12. Determine the minimum and maximum product prices. Rename the output as min_price and max_price respectively. 13. Count the number of products having selling prices greater than or equal to 5000. 14. Find all the products whose qty_on_hand is less than reorder level. 15. Display the order number and day on which clients placed their order. 16. Display the month (in alphabets) and date when the order must be delivered. 17. Display the order_date in the formatdd-month-yy Eg:- 12-February-11 18. Find the date, 15 days after todays date. 19. Find the number of days elapsed between todays date and the delivery date of the orders placed by the clients. 20. Print the description and total quantity sold for each product. 21. Calculate the average quantity ordered by each client. 22. Find the products which have been sold to Amit.

23. Find out the products and their quantities which have to be delivered in the current month. 24. Find the names of clients who have purchased CD Drive 25. Find the products and their quantities for the orders placed by Tom and John. 26. Find the products and their quantities for the orders placed by client_no C00001 and C00002. 27. Find the customer name, address, city and pin code for the client who has placed order number O19001. 28. Find the name of the clients who have placed orders before the month of June 2011. 29. Find the salesman who has sold maximum products. 30. Find out if the product P00003 has been ordered by any client and print the client number, name to whom it was sold. 31. Find the names of clients who have placed orders worth Rs.15, 000 or more. 32. Find the number of products which have been delivered only partial. 33. Print the name of the products which have been delivered fully.

Lab exercise 3 Use tables created in the previous exercises , unless otherwise specified. 1. Create a view to include all Professors information , but hide salary and date of birth. 2. 3. 4. 5. 6. Try to insert new values into the parent table using the view you have created. Create a view to include department name and average salary by department. Create a view that will display name, department number, and salary of each professors in Department S. Prevent a change of department through the view. Create an index to search Professors faster based on their employee ID. Create a sequence deptnum_seq to generate department number (in the DEPARTMENT table) and another sequence rollnum_seq to generate rollno (in the STUDENT table). Use deptnum_seq to add a new department in the DEPT table. Now, add yourself as a new student with rollnum_seq in the department you just added. Create a sequence inv_seq that generates numbers from 1 to 999 in ascending order. Create a new table 'Invoice_Header' with fields inv_no, inv_date and clientname. Use the sequence inv_seq to generate values for inv_no column in the table. Note that the inv_no must start with 'I'. Give the user 'S10xy' the permission only to view records in the table Professor and Department, along with an option to further grant permission on these tables to other users. Give the user 'S10ab' all data manipulation privileges on the table Client without an option to further grant permission on the table to other users. View the product_no and description of the Product table that belongs to 'S10cd'. Give the user 's10yz' to view records from Salesman table . The table originally belong to 'S10ab' , who has granted privilege to grant further privileges on the table Salesman to you. Take back all privileges on the table Salesman from 's10bb'. 'S10ff' has the permission to view and delete records from the Professor table. Take back the delete permission. 's10gh' is the orginal owner of the Professor table.

7.

8. 9. 10. 11. 12. 13.

LAB EXERCISE: 4 PL/SQL basics 1. Create a program script that uses a PL/SQL anonymous block to perform the following: Use a host variable AREA to store the result. Declare a local variable RADIUS with numeric data type. Declare a constant PI with value 3.14. Assign a value to the variable RADIUS by using a substitution variable, Calculate area of a circle by using the formula AREA = P1 * RADIUS * RADIUS .Then, print the result in SQL * Plus. Write a PL/SQL block to find the square, cube, and double of a number inputted with a substitution variable, and print the results. Write a PL/SQL block to swap the values of two variables. Print the variables before and after swapping. Write a PL/SQL program with two variables, for the first name and the last name. Print the full name with the last name and first name separated by comma and a space. Write a PL/SQL block to find out if a year is a leap year. (Hint: The function MOD(n, d) divides n by d and returns the integer remainder from the operation) Write a PL/SQL block to print all odd numbers between 1 and 10 using a basic loop. Using a FOR loop, print the values 10 to 1 in reverse order. Create a table called ITEM with one column called ItemNum with the NUMBER type. Write a PL/ SQL program to insert values of one to five for ItemNum. Input a number with a substitution variable, and then print its multiplication table using a WHILE loop. Input a month number between 1 and 12 and a four-digit year, and print the number of days in that month. For February (month = 2), check for a leap year to display the number of days as equal to 28 or 29. Use a PL/SQL block to delete product number 'P0004' from the PRODUCT table created in lab activity 2 Write a PL/SQL block to ask a user to input a valid employee Id. Retrieve the the employees details from the 'employee' table. You went to a video store and rented a DVD that is due in 3 days from the rental date. Input the rental date, rental month, and rental year. Calculate and print the return date, return month, and return year. For example, Rental Date 2 30 Rental Month 12 12 Rental Year 2003 2003 Return Date 5 2 Return Month 12 1 Return Year 2003 2004

2. 3. 4. 6 7. 8. 9. 10. 11.

12 13 14.

LAB EXERCISE : 5 Cursors, Triggers, Procedures and Functions Create a table EMPLOYEE(first name, last name, date of birth,salary, hire date, department id ,qualification id) , COURSE( course id, course name, pre requisite) 1. Create a PL/SQL block to declare a cursor to select last name, first name, salary, and hire date from the EMPLOYEE table. Retrieve each row from the cursor, and print the employees information if the employees salary is greater than 50,000 and the hire date is before 31-DEC2007(explicit cursor problem). 2. Create a PL/SQL block that declares a cursor. Pass a parameter of the same type as the Salary column in the EMPLOYEE table to the cursor. Retrieve information into the cursor for a salary higher than the parameter value. And print each employees information from the cursor (cursor with parameter problem). 3. Create a PL/SQL block to increase the salary of employees in department 10.The salary increase is 15% for employees making less than 10,000 and 10% for employees making 20,000 or more. Use a cursor with a FOR UPDATE clause. Update the salary with a WHERE CURRENT OF clause in a cursor FOR loop (cursor FOR loop problem). 4. Write a PL/SQL block to retrieve employees from the EMPLOYEE table based on a qualification ID . If the qualification ID returns more than one row, handle the exception with the appropriate handler, and print the message More than one employee with such qualification. If the qualification ID returns no employee, handle the exception with the appropriate handler, and display the message No employees with such qualification. If the qualification ID returns one employee, print that employees name, qualification, and salary 5. Write a PL/SQL block that retrieves the entire COURSE table into a cursor. Then, ask the user to input a course ID to search. If the course exists, print its information. If the course does not exist, throw a user-defined exception, and display the message Course does not exist in the COURSE table when you execute the block with a course ID such as C15555. 6. Write a PL/SQL block that asks the user to input first number, second number, and an arithmetic operator (+, -, *, or /). If the operator is invalid, throw and handle a user-defined exception. If the second number is zero and the operator is /, handle the ZERO_DIVIDE predefined server exception. 7. Write a PL/SQL procedure that is passed an employee's identification number and returns the employees full name and salary from the EMPLOYEE table to the calling program. 8. Write a PL/SQL function, and pass a department number to it. If the DEPARTMENT table does not contain that department number, return a FALSE value; otherwise, return a TRUE value. Print the appropriate message in the calling program based on the result. 9. Write a trigger that is fired before the DML statements execution on the EMPLOYEE table. The trigger checks the day based on SYSDATE. If the day is Sunday, the trigger does not allow the DML statements execution and raises an exception. Write the appropriate message in the

exception-handling section. 10. Write a trigger that is fired after an INSERT/UPDATE/DELETE statement is executed for the EMPLOYEE table. The trigger writes the new,old and deleted fields from the EMPLOYEE table , along with the operation(INSERT/UPDATE/DELETE), date of performing the operation and the user who performed the operation in a table called TRACKING. (Note: You must create the TRACKING table first.)

You might also like