Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Cs2258 Database Management Systems Lab

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 12

CS2258 DATABASE MANAGEMENT SYSTEMS LAB

LIST OF EXPERIMENTS

1. Data Definition, Table Creation, Constraints.


2. Insert, Select Commands, Update & Delete Commands.
3. Nested Queries & Join Queries
4. Views
5. High level programming language extensions (Control structures, Procedures and
Functions).
6. Front end tools
7. Forms
8. Triggers
9. Menu Design

10. Reports.
11.Database Design and implementation (Mini Project).

using strings
System Configuration

Front end: Eclipse


Back end: my SQL
GUI : mysql-admin
Platform: Linux
1. Data Definition, Table Creation, Constraints.

1. Install MySQL and mySQLadmin and configure them


2. Connect to MySQL from the mySQLadmin GUI interface using the
login/password and carry out the following tasks
3. Create the University schema using the commands in theDDL.sql script; the
commands can be copy-pasted into mySQLadmin. (If you have already created
the tables and wish to drop them, you can use the script here to drop all tables.

2. Insert, Select Commands, Update & Delete Commands

1. Insert sample data using the command in the file smallRelationsInsertFile.sql.


2. Try out some of these queries, and see what they do. To run them, copy/paste
them into the mySQLadmin command window and run them.
a. select * from instructor
b.select name from instructor where dept_name = 'Comp. Sci.' and salary > 70000
c. select * from instructor, department where instructor.dept_name =
department.dept_name
3. Try inserting data about yourselves into these tables. Refer to the populate script
above for examples.
4. Write the following simple SQL Queries on the University Schema
a. Find the names of all the instructors from Comp. Sci. department
mysql> select name from instructor where dept_name='Comp. Sci.';
+------------+
| name |
+------------+
| Srinivasan |
| Katz |
| Brandt |
+------------+
3 rows in set (0.00 sec)

b. Find the course id and titles of all courses taught by an instructor named
'Srinivasan'
mysql> select course.course_id,course.title from course where
course.course_id=some(select teaches.course_id from teaches where
teaches.id=(select instructor.id from instructor where instructor.name='Srinivasan'));

+-----------+----------------------------+

| course_id | title |

+-----------+----------------------------+

| CS-101 | Intro. to Computer Science |

| CS-315 | Robotics |

| CS-347 | Database System Concepts |

+-----------+----------------------------+

3 rows in set (0.00 sec)


c. Find the ID and name of instructors who have taught a course in the Comp.
Sci. department, even if they are themselves not from the Comp. Sci.
department. To test this query, make sure you add appropriate data, and
include the corresponding insert statements along with your query.
mysql>insert into course values ('CS-555', 'DSP', 'Comp. Sci.', '4');
mysql>insert into instructor values ('12312', 'Dhaya', 'ECE', '90000');
mysql>insert into section values ('CS-555', '1', 'Fall', '2009', 'Watson', '100', 'A');
mysql>insert into teaches values ('12312', 'CS-555', '1', 'Fall', '2009');

mysql> select instructor.id, instructor.name from instructor where


instructor.dept_name != 'Comp. Sci.' and instructor.id=some(select teaches.id from
teaches where teaches.course_id=some(select course.course_id from course where
dept_name='Comp. Sci.'));

+-------+-------+

| id | name |
+-------+-------+

| 12312 | Dhaya |

+-------+-------+

1 row in set (0.00 sec)

d. Find IDs of instructors who have never taught any course. If the result of
your query is empty, add appropriate data (and include corresponding insert
statements) to ensure the result is not empty.
mysql> select id from instructor where id not in (select id from teaches);

+-------+

| id |

+-------+

| 33456 |

| 58583 |

| 76543 |

+-------+

3 rows in set (0.00 sec)


e. Find the courses, which are offered in both 'Fall' and 'Spring' semester.
mysql> select course_id, title from course where course_id=(select course_id from
teaches as F where semester='Fall' and exists(select * from teaches as S where
semester='Spring' and F.course_id=S.course_id));

+-----------+----------------------------+

| course_id | title |

+-----------+----------------------------+

| CS-101 | Intro. to Computer Science |

+-----------+----------------------------+

1 row in set (0.00 sec)


f. Find the id and title of all courses, which do not require any prerequisites.
mysql> select C.course_id, C.title from course as C where not
exists(select P.course_id from prereq as P where
C.course_id=P.course_id);

+-----------+----------------------------+
| course_id | title |

+-----------+----------------------------+

| BIO-101 | Intro. to Biology |

| CS-101 | Intro. to Computer Science |

| FIN-201 | Investment Banking |

| HIS-351 | World History |

| MU-199 | Music Video Production |

| PHY-101 | Physical Principles |

| CS-555 | DSP |

+-----------+----------------------------+

7 rows in set (0.00 sec)


g. Find the names of students who have not taken any biology dept courses
5. Write SQL update queries to perform the following (queries 2 and 4 are pretty
meangless, but still fun to write):

a) Give a 10% hike to all instructors


update instructor set salary=salary+(salary*0.1);
b) Increase the tot_creds of all students who have taken the course
titled "Genetics" by the number of credits associated with that
course.
update student set tot_cred=(select credits from course where
title='Operatig
Systems')+student.tot_cred where student.id=(select
takes.id from takes
where takes.course_id=(select course.course_id
from course where
title='Operating Systems'))insert into takes values
('12345', 'CS-101',
'1', 'Fall', '2009', 'C');
c) For all instructors who are advisors of atleast 2 students,
increase their salary by 50000.
update instructor set salary=salary+50000 where (id, 't') in
(select i_id as I, count(s_id)>5 as C from advisor group by
i_id);
d) Set the total credits to 2 for all courses which have less than 5
students taking them (across all sections for the course, across
all years/semesters).
update course set credits=2 where ('f',course_id) in (select
count (id)>5, course_id from takes group by course_id,
sec_id, semester, year);
6. Each offering of a course (i.e. a section) can have many Teaching assistants; each
teaching assistant is a student. Extend the existing schema(Add/Alter tables) to
accommodate this requirement.
7. According to the existing schema, one student can have only one advisor.
1. Alter the schema to allow a student to have multiple advisors and make
sure that you are able to insert multiple advisors for a student.
alter table advisor drop constraint "advisor_pkey";

insert into advisor values ('12345', '45565');


2. Write SQL queries on the modified schema. You will need to insert
data to ensure the query results are not empty.
a) Find all students who have more than 3 advisors
select student.name from student where (student.id,'t') in
(select s_id,
count(s_id)>3 from advisor group by s_id);
b) Find all students who are co-advised by Prof. Srinivas and Prof.
Gold.
select distinct(s_id) from advisor where (i_id) in (select id
from instructor
where name ='Srinivasan' or name='Gold')
c) Find students advised by instructors from different
departments. Etc.
select s_id, i_id from advisor where (s_id,'t') in (select s_id,

count(s_id)>2
from advisor group by s_id);
8. Write SQL queries for the following:
1. Delete all information in the database, which is more than 10 years old.
Add data as necessary to verify your query.
delete from takes where year < (year(CURRENT_DATE) –
10);
delete from teaches where year < (year(CURRENT_DATE) – 10);
delete from section where year < (year(CURRENT_DATE) – 10);
2. Delete the course CS 101. All courses which have this as a prereq
should remove this from its prereq set. Create a cascade constraint and
verify.
DELETE FROM course WHERE course_id='CS-101'
3. Nested Queries & Join Queries
1. Find the maximum number of teachers for any single course section.
2. Find all departments that have the minimum number of
instructors, using a subquery; order the result by department
name in descending order.
select dept_name,count(*) as num from instructor group by
(dept_name) order by num desc ;
3. For each student, compute the total credits they have
successfully completed, i.e. total credits of courses they have
taken, for which they have a non-null grade other than 'F'. Do
NOT use the tot_creds attribute of student.
mysql> select student.id, name, sum(course.credits) from
takes, student, course where grade <> 'F' and grade is not
null and takes.course_id = course.course_id and
student.id=takes.id group by id, name;
4. Find the number of students who have been taught (at any
time) by an instructor named 'Srinivasan'. Make sure you
count a student only once even if the student has taken more
than one course from Srinivasan.
mysql> select count(distinct takes.ID) from
instructor,takes,teaches,section where
takes.course_id=teaches.course_id and takes.sec_id =
teaches.sec_id and takes.semester = teaches.semester and
takes.year = teaches.year and
section.course_id=teaches.course_id and
section.semester=teaches.semester and
section.sec_id=teaches.sec_id and section.year=teaches.year
and teaches.ID=instructor.ID and
instructor.name='Srinivasan';
5. Find the name of all instructors who get the highest salary in
their department.
mysql> select name from instructor I1 , (select max(salary) as
maxsal,dept_name from instructor I2 group by dept_name ) I2
where (I2.maxsal = I1.salary and I1.dept_name =
I2.dept_name);
6. Find all students who have taken all courses taken by
instructor 'Srinivasan'. (This is the division operation of
relational algebra.) You can implement it by counting the
number of courses taught by Srinivasan, and for each student
(i.e. group by student), find the number of courses taken by
that student, which were taught by Srinivasan. Make sure to
count each course ID only once.

1. Find the IDs of all students who were taught by an instructor named Einstein; make 
    sure there are no duplicates in the result.
2. Find the highest salary of any instructor.
3. Find all instructors earning the highest salary (there may be more than one with the same 
salary).
4. Find the enrollment of each section that was offered in Autumn 2009. 
5. Find the maximum enrollment, across all sections, in Autumn 2009. 
6. Find the sections that had the maximum enrollment in Autumn 2009.
7. Suppose you are given a relation grade points(grade, points), which provides a conversion 
from letter grades in the takes relation to numeric scores; for example an “A” grade could be 
specified to correspond to 4 points, an “A−” to 3.7 points, a “B+” to 3.3 points, a “B” to 3 
points, and so on. The grade points earned by a student for a course offering (section) is 
defined as the number of credits for the course multiplied by the numeric points for the grade 
that the student received.
Given the above relation, and our university schema, write each of the following 
queries in SQL. You can assume for simplicity that no takes tuple has the null value for grade.

a. Find the total grade­points earned by the student with ID 12345, across all courses   
               taken by the student.
b. Find the grade­point average (GPA) for the above student, that is, the total 
               grade­points divided by the total credits for the associated courses.
      c.     Find the ID and the grade­point average of every student.
9. Find the maximum number of teachers for any single course section.
10. Find all departments that have the minimum number of instructors, using a
subquery; order the result by department name in descending order.
11. For each student, compute the total credits they have successfully completed, i.e.
total credits of courses they have taken, for which they have a non-null grade
other than 'F'. Do NOT use the tot_creds attribute of student.
12. Find the number of students who have been taught (at any time) by an instructor
named 'Srinivasan'. Make sure you count a student only once even if the student
has taken more than one course from Srinivasan.
13. Find the name of all instructors who get the highest salary in their department.
14. Find all students who have taken all courses taken by instructor 'Srinivasan'. (This
is the division operation of relational algebra.) You can implement it by counting
the number of courses taught by Srinivasan, and for each student (i.e. group by
student), find the number of courses taken by that student, which were taught by
Srinivasan. Make sure to count each course ID only once.
15. Display a list of all instructors, showing their ID, name, and the number of sections 
that they have taught. Make sure to show the number of sections as 0 for instructors who 
have not taught any section. Your query should use an outerjoin, and should not use 
scalar subqueries.
16. Write the same query as above, but using a scalar subquery, with­ out outerjoin.
17. Display the list of all course sections offered in Spring 2010, along with the names of 
the instructors teaching the section. If a section has more than one instructor, it should 
appear as many times in the result as it has instructors. If it does not have any instructor, 
it should still appear in the result with the instructor name set to “ —”.
18. Display the list of all departments, with the total number of in­ structors in each 
department, without using scalar subqueries. Make sure to correctly handle departments 
with no instructors.
5. High level programming language extensions

1. Write a SQL procedure, which accepts a name and prints welcome message for
the given name.
2. Write a SQL procedure to accept the details of a student and print in on the screen
using procedure.
3. Write a SQL procedure to demonstrate swapping of two numbers.
4. Write a SQL procedure that will accept an integer and find out its factorial value
and also print its Fibonacci series.
5. Write a SQL function, which returns the highest paid staff
6. Write a SQL function, which returns the average salary of given department.
7. Write a SQL function, which returns number of student who got above 60 marks
in a given subject.
8. Write a SQL function to find whether the given string is palindrome or not.

You might also like