Vous êtes sur la page 1sur 24

Q1.

Create following two tables including integrity constraints using SQL Command:
Table Name : Employee
Field Name
Data Type
Field Size
Constraints
Employee ID
Number
8
Primary Key
Employee Name
Text
20
Not Null
Street
Text
15
City
Text
3
Del,Bom,Cal
Table Name : Works
Field Name
Data Type
Employee ID
Number

Field Size
8

Company Name
Salary

20
10,2

Text
Number

Constraints
Foreign Key,
Primary Key
Primary Key
>10,000 and < 25,000

Perform the following operations using SQL Commands


(i)
(ii)

List all the employees names working in FBC


Find the number of employees earning a salary > average salary of all employees.

Ans:- create table employee(


employeeid number(8) PRIMARY KEY,
employeename varchar2(20) NOT NULL,
street varchar2(15),
city varchar2(3) CHECK (city IN ( 'del','bom','cal')));
Output:-

select * from employee;


Output:-

create table works


( empid number(8) ,
FOREIGN KEY(empid) REFERENCES employee(employeeid),
companyname varchar2(20),
salary number(10,2) CHECK (10000<SALARY AND SALARY <250000));
Output:-

alter table works add constraint my18primarykey PRIMARY KEY(empid,companyname);


Output:-

select * from works;


Output:-

select employeename from employee,works


where companyname='FBC' and employeeid=empid;
Output:-

select count(employeename) from employee,works where salary > (select AVG(salary) from
works) and employeeid=empid ;
Output:-

Q2.Create following tables :


Student (StudentID, Student_name)
Registered (StudentID, CoursedID, dt_of_Join)
Add the following constraints to the table using alter command:
(i) Primary Key and respective foreign keys for all tables.
(ii) age > 20 and < 60
List the student id and name of each student along with the total number of courses that the
student is registered for.
Ans:create table student
(studentid number(8),
stdname varchar2(20));
Output:-

create table registered


(studid number(8) ,
courseid number(8),
date_of_join date);
Output:-

alter table student add constraint myprimarykey PRIMARY KEY(studentid);


Output:-

alter table registered add constraint pk_primarykey PRIMARY KEY(studid,courseid);

Output:-

alter table registered add constraint fk_studid FOREIGN KEY(studid) REFERENCES


student(studentid);
Output:-

alter table student add age number(8);


Output:-

alter table student ADD CONSTRAINT check_age CHECK ( 20 < AGE AND AGE < 60);
Output:-

select studentid,stdname,courseid from student,registered where studentid=studid;


Output:-

Q3. Create following two tables including integrity constraints using SQL Command:
Table Name : Part
Field Name
Data Type
Part id
Text
Name
Text

Field Size
8
20

Constraints
Primary Key
Not Null

Table Name : Subpart


Field Name
Data Type
Part id
Text

Field Size
8

Sub Part id
Count
Cost

20
5
10,2

Constraints
Foreign Key,
Primary Key
Primary Key
>= 0 and < 100
Default 0

Text
Number
Number

Perform the following operations using SQL Commands


(i)
(ii)

List all the name of all the subparts of the part id P-100.
Find the total cost of part P-100 including subparts.

Ans:create table part


(partid varchar2(8) primary key,
name varchar2(20) not null);
Output:-

10

create table subpart


(partid varchar2(8),
subpartid varchar2(20) primary key,
count number(5) check(count between 0 and 100),
cost number(10,2) default(0),
foreign key(partid) references part);

Output:-

select * from part;


Output:-

11

Select * from subpart;


Output:-

select * from subpart where partid='P001';


Output:-

12

select sum(cost) from subpart where partid='P001';


Output:-

13

Q4.Create following two tables including integrity constraints using SQL Command:
Table Name : Student
Field Name
Data Type
Roll No
Number
Name
Text
Address
Text

Field Size
8
20
50

Constraints
Primary Key
Not Null

Table Name : Enrollment


Field Name
Data Type
Roll No
Number

Field Size
8

Course
Grade

20
1

Constraints
Foreign Key,
Primary Key
Primary Key
A,B,C,D,E

Text
Text

Perform the following operations using SQL Commands


(i)
(ii)

List all the Students enrolled for BBA and have grade A
Find the number of students enrolled in each course.

Ans:create table student2


(rollno number(8) ,
name varchar2(20) NOT NULL,
address varchar2(50));
Output:-

14

create table enrollment


(rollnumber number(8) ,
course varchar2(20),
grade varchar2(1) CHECK(grade IN('A','B','C','D','E')));
Output:-

alter table student2 add constraint pk1_primarykey PRIMARY KEY(rollno);


Output:-

15

alter table enrollment add constraint pk2_primarykey PRIMARY KEY(rollnumber,course);


Output:-

alter table enrollment add constraint fk1_rollnumber FOREIGN KEY(rollnumber)


REFERENCES student2(rollno);
Output:-

16

select * from student2,enrollment where course='bba' and grade='A' and rollno=rollnumber;


Output:-

select count(course) from enrollment GROUP by course;

17

Q5.Create following tables :


Employee (empno, name, office, age)
Books (isbn, title, author, publisher)
Loan (empno,isbn,date)
Add the following constraints to the table using alter command:
(i) Primary Key and respective foreign key for all tables.
(ii) age > 20 and < 60
List names of employees having any book published by TMH
Ans:create table employee2
(empno number(8),
name varchar2(20),
office varchar2(20),
age number(8));
Output:-

18

create table books


(isbn number(20),
title varchar2(20),
author varchar2(40),
publisher varchar2(18));
Output:-

create table loan


(employee__no number(20),
isbn1 number(20),
loandate date);
Output:-

19

alter table employee2 add constraint pk8_primarykey PRIMARY KEY(empno);


Output:-

alter table books add constraint pk4_primarykey PRIMARY KEY(isbn);


Output:-

20

alter table loan add constraint pk6_primarykey PRIMARY KEY(employee__no,isbn1);


Output:-

alter table loan add constraint fk2_isbn1 FOREIGN KEY(isbn1) REFERENCES books(isbn);
Output:-

21

alter table employee2 add CHECK(20 < AGE AND AGE < 60);
Output:-

select name from employee2,books,loan where publisher='TMH' and empno=employee__no


and isbn=isbn1;
Output:-

22

Q6. Create following two tables including integrity constraints using SQL Command:
Table Name : Student
Field Name
Data Type
CSTID
Number
CSTNAME
Text
CSTCITY
Text

Field Size
4
25
3

CSTDEP

8,2

Number

Constraints
Primary Key
Not Null
Must beUDP,MNG,BNG,PJM,MAR

Perform the following operations using SQL Commands


(i)
(ii)
(iii)

Provide constraint names while creating table


Drop the check constraint on CSTCITY
Create a new constraint chkdep to check that the deposit is within the range 1500
to 300000.

Ans:create table student4


(cstid number(4) PRIMARY KEY,
cstname varchar2(25) NOT NULL,
cstcity varchar2(3) CONSTRAINT check_student CHECK(cstcity
IN('UDP','MNG','BNG','PJM','MAR')),
cstdep number(8,2));
Output:-

23

alter table student4


drop constraint check_student;
Output;-

alter table student4 add constraint chkdep CHECK(1500<cstdep and cstdep<300000);


Output;-

24