Sie sind auf Seite 1von 48

DIGITAL ASSIGNMENT

DATABASE TECHNOLOGIES
Name-Shaswata Ganguly
Register Number 18MCA0021
Lab Cycle I
Consider the following relational database schema for teaching-learning process in a university.
(Source: Database Systems – Coronel & Morris)
PROFESSOR(Prof_id, Prof_name, Email, Mobile, Specialty, Dept_id)
SCHOOL(SCode, Scl_name, Prof_id, Location)
DEPARTMENT(Dept_id, Dname, SCode, Prof_id)
COURSE(Crs_code, Crs_name, Description, Credits, Hours)
CLASS(Cls_code, Slot, Stime, Etime, Crs_code, Prof_id, Room_no, Sem_code, Day_of_week)
SEMESTER(Sem_code, Term, Year, Sdate, Edate)
STUDENT(Reg_no, Sname, Address, DoB, Email, Mobile, Dept_id, Prof_id)
ENROLL(Cls_code, Reg_no, Enroll_time, Grade)
STUDENT_VISA(Reg_no, Visa_status)
PROGRAMME(Prog_code, Prog_name, Prog_preamble, Scode, Dept_id)
The primary keys are underlined and foreign keys are self-explanatory. The Dept_id column in professor
table stands for the department the professor belongs to and Prof_id column in the school table stands
for the professor who chairs the school, the same column in the department table stands for the
professor who heads the department, the domain of Term column in semester table is {Winter, Fall}.
1. Create the above tables with the following integrity constraints assigning name to integrity constraint.
(i) Prof_id must have exactly five characters and their email and mobile number are unique. The email
address must have @ as one of the characters and mobile number must have exactly ten characters.

(ii) Use timestamp data type without fractional parts of seconds for start time and end time column of
class table.
(iii) The Sem_code should start with either ‘Win’ or ‘Fall’ and Term column can assume only one of two
values {Winter, Fall}.
(iv) Email and mobile column in student table should have same characteristics as those in professor
table.
(v) The enroll_time in the enroll table should be of timestamp data type without fractional parts of
seconds. The grade may assume one of the values in {‘S’, ‘A’, ‘B’, ‘C’, ‘D’, ‘C’, ‘F’}. The grade F
indicates
Failed.

(vi) Use ‘on delete cascade’ or ‘on delete set null’ clause as requirements. Use deferrable constraint, if
required.
(vii) Additional (innovative) integrity constraints, if any, may be specified by you.

Answer

SQL>

create table professor(prof_id char(5) constraint prof_pk primary key,

prof_name varchar(20),

Email varchar(30) unique,

Mobile number(12) unique,

Speciality varchar(10),

Dept_id varchar(10));

Output:

SQL> create table school(SCode varchar(10) constraints scl_pk primary key,

Scl_name varchar(20),
Prof_id char(5),

Location varchar(10));

Output:

SQL> create table department(Dept_id varchar(10) constraint dept_pk primary


key,

Dname varchar(10),

SCode varchar(10),

Prof_id char(5));

Output:
SQL> create table course(Crs_Code varchar(10) constraint crs_pk primary key,

name varchar(10),

Description varchar(20),

Credits number(2),

Hours number(2));

Output:
SQL>

create table class(cls_code varchar(10) constraint cls_pk primary key,

Slot char(5),Stime timestamp(0),

Etime timestamp(0),

Crs_code varchar(10),

Prof_id char(5) ,

Room_no number,

em_code varchar(10),

Day_of_week varchar(10));

Output:
SQL>

create table semester(Sem_code varchar(10) check(Sem_code in


('WINTER','FALL')),

Term varchar(10),

Year date,

Sdate timestamp(0),

Edate timestamp(0),

constraint sem_pk primary key(Sem_code));


Output:

SQL> create table student(Reg_no varchar(10) constraint std_pk primary key,

Sname varchar(10),

Address varchar(30),

DoB date,

Email varchar(20),

Mobile number(10),

Dept_id varchar(10),

Prof_id char(5));

Output:
SQL> create table enroll(Cls_code varchar(6),

Reg_no varchar(10),

Enroll_time timestamp(0),

Grade char(1),

constraint enr_pk primary


key(cls_code,Reg_no));

Output:
SQL> create table student_visa(Reg_no varchar(9),Visa_status varchar(10),

constraint stdvsa_pk primary key


(Reg_no));

Output:
SQL> create table programme(Prog_code varchar(10), Prog_name varchar(20),
Prog_preamble varchar(30), SCode varchar(10), Dept_id varchar(10));

Output:

Adding Constraints:

SQL> alter table School add constraint schl_fk1 foreign key(prof_id) references
Professor;

Output:
SQL> alter table Department add constraint dpt_fk1 foreign key(prof_id) references
Professor;

Output:

SQL> alter table Class add constraint cls_fk1 foreign key(prof_id) references
Professor;

Output:

SQL> alter table Student add constraint std_fk1 foreign key(prof_id)


references Professor;

Output:
SQL> alter table School add constraint schl_fk2 foreign key(Scode) references
School;

Output:

SQL> alter table Department add constraint dpt_fk2 foreign key(Scode)


references School;

Output:

SQL> alter table Programme add constraint prg_fk1 foreign key(Scode)


references School;

Output:

SQL> alter table Programme add constraint prg_fk2 foreign key(Dept_id)


references Department;

Output:
SQL> alter table Student add constraint std_fk2 foreign key(Dept_id)
references Department;

Output:

SQL> alter table Professor add constraint prf_fk1 foreign key(Dept_id)


references Department;

Output:

SQL> alter table Class add constraint crs_fk foreign key(Crs_code) references
Course;

Output:

SQL> alter table enroll add constraint enr_fk foreign key(Cls_code)


references Class;

Output:
SQL> alter table enroll add constraint enr_fk2 foreign key(Reg_no) references
Student;

Output:

SQL> alter table student_visa add constraint std_vsa_fk1 foreign key(Reg_no)


references Student;

Output:

SQL>alter table semester add constraint sem_ck1 check(Sem_code like 'Win%' or


Sem_code like 'Fall%');

Output:
SQL>alter table Student add constraint std_unq1 UNIQUE(Email,Mobile);

Output:

SQL> alter table Student add constraint std_ck1 check(Email like '%@%');

Output:

SQL> alter table semester add constraint sem_chk2 check(term='Winter' or term='Fall');

Output:

SQL> alter table enroll add constraint enr_chk1 check(grade='S' or grade='A'


or grade= 'B' or grade = 'C' or grade= 'D' or grade = 'C' or grade = 'F');

Output:

2. Enter data into the above tables. (Learn also how to enter data interactively.). Display the content of
each table. Use column formatting while displaying data.

SQL> insert into PROFESSOR values(&Prof_id, &Prof_name, &Email, &Mobile,


&Specialty, &Dept_id);
Output:

SQL>insert into SCHOOL values(&Scode,&Scl_name,&Prof_id,&Location);

Output:

SQL> insert into DEPARTMENT values(&Dept_id, &Dname, &SCode, &Prof_id);

Output:
SQL>insert into COURSE values(&Crs_code, &Crs_name, &Description, &Credits,
&Hours);

Output:

SQL>insert into semester values('Win18-19','Winter',TO_DATE('11-DEC-18','DD-


MON-YY'),

TO_TIMESTAMP('01-JUL-18:00:00','DD-MON-YY:HH24:MI'),

TO_TIMESTAMP('01-MAY-19:00:00','DD-MON-YY:HH24:MI'));

SQL>insert into semester values('Fall18-19','Fall',TO_DATE('01-JUL-18','DD-


MON-YY'),

TO_TIMESTAMP('01-JUL-18:00:00','DD-MON-YY:HH24:MI'),

TO_TIMESTAMP('01-DEC-18:00:00','DD-MON-YY:HH24:MI'));

SQL>insert into CLASS values(&Cls_code, &Slot, &Stime, &Etime, &Crs_code,


&Prof_id, &Room_no, &Sem_code, &Day_of_week);
Output:

SQL>insert into PROGRAMME values(&Prog_code, &Prog_name, &Prog_preamble,


&Scode, &Dept_id)

Output:
3. (i) Display name, email address and address for those students who live in Katpadi area and whose
name has an l as the third character.

SQL>select Sname,Address,Email from student where Address like '%Katpadi%'


and Sname like '__l%';

Output:
(ii) Display name, email address and address for those students who are not from Tamil Nadu.

SQL>select Sname,Address,Email from student where Address not like '%Tamil Nadu%';

Output:
(iii) Display name, email address and address of foreign students only.

SQL>select Sname,Address from student,Student_visa where


Student.Reg_no=STUDENT_VISA.Reg_no and Visa_status='foreign';

Output:

(iv) List the name of professors along with their specialty who belong to School of Medicine.

SQL>

Output:
(v) Display name of the school and name of professor who chairs the school.

SQL> select Scl_name,Prof_name from School,Professor where


SCHOOL.Prof_id=Professor.Prof_id;

Output:

(vi) List course code, course name and course description in alphabetic order of course code.

SQL> select Crs_code,name,Description from Course order by Crs_code;

Output:
(vii) Change the mobile number of a student interactively.

SQL> update Student set Mobile=&Mobile where Reg_no=&Reg_no;

Output:
(viii) Remove enrollment information of a student from a particular course interactively. How would you
recover the data?

SQL> delete from Enroll where Cls_code=&Cls_code;

SQL>rollback;

Output:
(ix) Create a duplicate of course table.

SQL> create table course_dup as select * from course;

Output:

(x) Create a view for list of students (Reg_no, Sname) and the courses they have registered along with
name of professors teaching the course.

SQL> create view V1 as

select Student.Reg_no,Sname, name,Prof_name from


student,Class,course,enroll,Professor where student.Reg_no=enroll.Reg_no and
enroll.Cls_code=CLASS.Cls_code and course.crs_code=class.Crs_code and
PROFESSOR.prof_id=class.Prof_id;

select * from v1;


Output:

(xi) List the room number, slot, start time, end time and duration of every class held on Wednesdays in
descending order of room number.

SQL> select Room_no,slot,Stime,Etime,Hours from class,course where


course.Crs_code=class.Crs_code and Day_of_week='Wednesday'

order by Room_no desc;


Output:

(xii) Display the name and grade of a student in different courses underwent in fall semester 2017 – 18
(Fall 2017).

SQL> select Sname,Grade from student,enroll where


student.Reg_no=enroll.Reg_no;
Output:

(xiii) Find out name of students who have taken Database Systems course as well as Operating Systems
course in fall semester 2016 – 17 (Fall 2016).

SQL>

select Sname from Student,enroll,course,class where


Student.Reg_no=enroll.Reg_no and Enroll.Cls_code=Class.Cls_code
and Course.Description like 'Operating Systems' and class.Sem_code='Fall
2016'

union

select Sname from Student,enroll,course,class where


Student.Reg_no=enroll.Reg_no and Enroll.Cls_code=Class.Cls_code

and Course.Description like 'Database Systems' and class.Sem_code='Fall


2016';

Output:
(xiv) Find out name of students who have taken Database Systems course but have not taken Operating
Systems course in winter semester 2017 – 18 (Winter 2018).

SQL>

select Sname,Course.Description from Student,enroll,course,class where


Enroll.Cls_code=Class.Cls_code

and class.Crs_code=course.crs_code and Course.Description like


'%Database%' and class.Sem_code='Winter 2016'

minus

select Sname,Course.Description from Student,enroll,course,class where


Enroll.Cls_code=Class.Cls_code

and class.Crs_code=course.crs_code and Course.Description like


'%Operating Systems%' and class.Sem_code='Winter 2016';

Output:

(xv) List the registration number and name of the students who have registered for maximum number of
credits in Winter 17-18 (Winter 2018) semester. (Assume that the maximum number of credits = 26).

SQL>select e.reg_no,sname from student s,enroll e,class c,course cr,semester


s where

s.reg_no=e.reg_no and e.cls_code=c.cls_code and c.crs_code=cr.crs_code


and c.sem_code=s.sem_code and year=to_date('2018','yyyy') and term like
'winter' group by e.reg_no,

sname having sum(credits)=26;

Output:

(xvi) List the name of the course and the number of students registered in each slot for course under
different faculty members.

SQL> select crs_name,count(reg_no) no_of_stu,slot,c.prof_id from course cr,

class c,enroll e,professor p where cr.crs_code=c.crs_code and


c.cls_code=e.cls_code

and c.prof_id=p.prof_id group by slot,crs_name,c.prof_id;


Output:

(xvii) Find out the name of the students who have registered in all the courses being taught by Prof.
O’Brien in Winter 17-18 (Winter 2018).

SQL> select sname from student where not exists(

select cr.crs_code from class c,course cr,professor p,semester s where

c.crs_code=cr.crs_code and c.prof_id=p.prof_id and c.sem_code=s.sem_code and

prof_name like 'O"Brien' and term like 'winter' and


year=to_date('2018','yyyy')

minus

select cr.crs_code from class c,course cr,semester s,enroll e,student st


where

c.crs_code=cr.crs_code and c.sem_code=s.sem_code and c.cls_code=e.cls_code


and

e.reg_no=st.reg_no and term like 'winter' and year=to_date('2018','yyyy'));

Output:
(xviii) List the registration number of the students who registered in Database Systems course on
November 17, 2017

SQL> select e.reg_no from student s,enroll e,class c,course cr where

s.reg_no=e.reg_no and e.cls_code=c.cls_code and c.crs_code=cr.crs_code

and crs_name like 'dbms' and enroll_time like '17-nov-17';

Output:
(xix) Write a query to display the grade of a student given his/her registration number and the course
name for Fall semester 17–18 (Fall 2017).

SQL> select grade from enroll e,class c,semester s,course cr where

e.cls_code=c.cls_code and c.sem_code=s.sem_code and c.crs_code=cr.crs_code

and reg_no='&reg_no' and crs_name='&crs_name' and year=2017 and term like

'fall';

Output:

(xx) List the name of departments and the name professors who is in charge of the department.
SQL> select dname,prof_name from professor p,department d where

p.dept_id=d.dept_id;

Output:

(xxi) List the name of schools with students strength higher than 7000.

SQL>

Output: select scl_name,e.reg_no from school s,programme pr,student st,enroll


e

where s.scode=pr.scode and pr.dept_id=st.dept_id and st.reg_no=e.reg_no

group by e.reg_no,scl_name having count(e.reg_no)>7000;


(xxii) List the name of the department(s) under school of medicine with student strength higher than the
average students of all the departments in the school.

SQL> select dname,e.reg_no from department d,student s,enroll e,school


sc,programme p

where d.dept_id=s.dept_id and s.reg_no=e.reg_no and sc.scode=p.scode and

p.dept_id=d.dept_id and scl_name like 'School of medicine'

group by e.reg_no,dname having count(e.reg_no)>avg(e.reg_no);


Output:

(xxiii) Given the registration number of a student, display the total credits registered by him/her in
Winter 17–18 (Winter 2018).

SQL> select e.reg_no,sum(credits) from enroll e,class c,course cr,semester s

where e.cls_code=c.cls_code and c.crs_code=cr.crs_code and


c.sem_code=s.sem_code

and e.reg_no='&reg_no' and year=TO_DATE('2018','YYYY') and term like 'winter'


group by e.reg_no;

Output:
(xxiv) Given the registration number of a student, display her/his grade in the course she/he registered
in Fall 17–18 (Fall 2017).

SQL> select grade from enroll e,class c,course cr,semester s where

e.cls_code=c.cls_code and c.crs_code=cr.crs_code and c.sem_code=s.sem_code

and e.reg_no='&reg_no' and year=TO_DATE('2017','YYYY') and term like


'%Fall%';
Output:

(xxv) Display the name of the courses that are not being offered in Winter 17–18 (Winter 2018).

SQL> select distinct crs_name from course cr,class c,semester s where


cr.crs_code=c.crs_code

and c.sem_code=s.sem_code and year<> TO_DATE('2018','YYYY') and term not


like 'winter';

Output:
(xxvi) Write necessary SQL statement to advance the start time and end time of every class by ten
minutes in Fall 18–19 (Fall 2017).

SQL> update class set stime=stime+to_dsinterval('000 00:10:00'),etime=

etime+to_dsinterval('000 00:10:00') where cls_code in(select cls_code from

class,semester where semester.sem_code=class.sem_code and term='fall'

and year= TO_DATE('2017','YYYY'));

Output:
(xxvii) Write necessary SQL statement to advance the start date and end date of Fall 18–19 semester by
one week with respect to Fall semester of 17 – 18 (Fall 2017).

SQL> update semester set sdate=sdate+to_dsinterval('007 00:00:00'),

edate=edate+to_dsinterval('007 00:00:00') where term like 'Fall' and


YEAR=TO_DATE('2017','YYYY');

Output:
(xxviii) Find out the name list of students who had secured ‘S’ grade in at least 50% of the courses
cleared by her/him.

SQL>

Output:
(xxix) Given the registration number of a student, find out his/her free slots.

SQL> select slot from class c,enroll e where c.cls_code=e.cls_code

minus

(select slot from class c,enroll e where c.cls_code=e.cls_code and

e.reg_no='&e.reg_no');

Output:
(xxx) Find out the name list of students who have classes in the afternoon session only on a specific day
of the week.

SQL> select sname from student s,enroll e,class c where s.reg_no=e.reg_no and

e.cls_code=c.cls_code and stime>to_timestamp('14:00','hh24:mi');

Output:

(xxxi) Add a column named ‘Duration’ (to indicate duration of a class) with appropriate data type to the
CLASS table and populate the column from values of start time and end time columns.
SQL> alter table class add duration interval day to second;

update class set duration=etime-stime where cls_code='cl01';

Output:

(xxxii) Add a column named ‘SemesterDuration’ (indicating duration of a semester) with appropriate
data type to the SEMESTER table and populate the column from values of start date and end date
columns.

SQL> alter table semester add duration interval DAY to SECOND;

update semester set duration=TO_DSINTERVAL(edate-sdate) where


sem_code='win2016';

Output:
(xxxiii) Find out the list of students who are undergoing MCA program.

SQL> select reg_no,sname from student s,programme p where s.dept_id=p.dept_id

and prog_name like 'MCA';

Output:

(xxxiv) Display the name of programs and the name of school offering the program.
SQL> select prog_name,scl_name from programme p,school s where
p.scode=s.scode;

Output:

(xxxv) Display the name of the departments and the name of the program controlled by the department.

SQL> select dname,prog_name from department d,programme p where

d.dept_id=p.dept_id;

Output:
4. (i) Test the string manipulation functions – UPPER, LOWER, INITCAP, LENGTH, LPAD, RPAD,
LTRIM,
RTRIM and TRIM, using select queries on data present in the tables. Use one query each for
demonstration of one function.

select upper(Sname) Name_in_Upper_Case from student;

select lower(Sname) Name_in_Lowercase from student;

select INITCAP(sname) Cap_INIT from student;

select sname,Length(sname) name_length from student;

SELECT RPAD(Sname,30,'*.') "LPAD example"

FROM Student;

SELECT RPAD(Sname,30,'*.') "RPAD example"

FROM Student;

SELECT Reg_no, LTRIM(Reg_no, '18MCA00') "Short Roll number"

FROM Student;

where Visa_status not like 'native';


(ii) Write query to illustrate usage of NVL function and NULLIF function.
SELECT Reg_no, NVL(TO_CHAR(Visa_status), 'foreign') from STUDENT_VISA

(iii) Display the name of the students who were born on a specified month.

SQL>select sname from student extract(month from dob)=&x;


(iv) Display the name of the students with a specified date of birth.

SQL>select sname from student where dob='&dob';


(v) Display the date of birth of a specified student in the format ‘Day of week, Month dd, yyyy’.

SQL>select TO_CHAR(dob,'month dd-yyyy') from student where reg_no='&Reg_no';


(vi) Display the hour and minutes of the start time and end time of a specified slot.

SQL>select extract (hour from Stime),extract(minute from Stime),extract(hour


from Etime),extract(minute from Etime)

from class where slot='&slot';


(vii) Display the day of week of the start date and end date of Winter semester 17–18 (Winter 2018).

SQL>select to_char(Sdate,'day'),to_char(Edate,'day') from semester where


year=TO_DATE('2018','yyyy');
(viii) Display the duration of Winter semester 17–18 (Winter 2018) in terms of number of weeks.

SQL>select (etime-stime)/7 from semester where Sem_code like "%winter 17%";


5. Create a sequence that starts with 1000 and is incremented by 1. Use this sequence in the following
table for entering information about at least three customers.
CUSTOMER(Cus_code, Cus_name, Cus_address, Cus_mobile)

CREATE SEQUENCE customers_seq


START WITH 1000
INCREMENT BY 1
NOCACHE
NOCYCLE;

Output is on the next page:

Das könnte Ihnen auch gefallen