mca practiles of 5th sem
description
Transcript of mca practiles of 5th sem
Q1.Create following two tables including integrity constraints using SQL Command: Table Name : EmployeeField Name Data Type Field Size ConstraintsEmployee ID Number 8 Primary KeyEmployee Name Text 20 Not NullStreet Text 15City Text 3 ‘Del’,’Bom’,’Cal’
Table Name : WorksField Name Data Type Field Size ConstraintsEmployee ID Number 8 Foreign Key,
Primary KeyCompany Name Text 20 Primary KeySalary Number 10,2 >10,000 and < 25,000
Perform the following operations using SQL Commands
(i) List all the employees names working in ‘FBC’(ii) 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:-
1
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:-
2
alter table works add constraint my18primarykey PRIMARY KEY(empid,companyname);
Output:-
select * from works;
Output:-
3
select employeename from employee,workswhere companyname='FBC' and employeeid=empid;
Output:-
select count(employeename) from employee,works where salary > (select AVG(salary) from works) and employeeid=empid ;
Output:-
4
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:-
5
create table registered(studid number(8) ,courseid number(8),date_of_join date);
Output:-
alter table student add constraint myprimarykey PRIMARY KEY(studentid);
Output:-
6
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:-
7
alter table student add age number(8);
Output:-
alter table student ADD CONSTRAINT check_age CHECK ( 20 < AGE AND AGE < 60);
Output:-
8
Q3. Create following two tables including integrity constraints using SQL Command:
Table Name : PartField Name Data Type Field Size ConstraintsPart id Text 8 Primary KeyName Text 20 Not Null
Table Name : SubpartField Name Data Type Field Size ConstraintsPart id Text 8 Foreign Key,
Primary KeySub Part id Text 20 Primary KeyCount Number 5 >= 0 and < 100 Cost Number 10,2 Default 0
Perform the following operations using SQL Commands
(i) List all the name of all the subparts of the part id “P-100”.(ii) 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
Q4.Create following two tables including integrity constraints using SQL Command:
Table Name : StudentField Name Data Type Field Size ConstraintsRoll No Number 8 Primary KeyName Text 20 Not NullAddress Text 50
Table Name : EnrollmentField Name Data Type Field Size ConstraintsRoll No Number 8 Foreign Key,
Primary KeyCourse Text 20 Primary KeyGrade Text 1 ‘A’,’B’,’C’,’D’,’E’
Perform the following operations using SQL Commands
(i) List all the Students enrolled for ‘BBA’ and have grade ‘A’(ii) 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 : StudentField Name Data Type Field Size ConstraintsCSTID Number 4 Primary KeyCSTNAME Text 25 Not NullCSTCITY Text 3 Must be-
UDP,MNG,BNG,PJM,MARCSTDEP Number 8,2
Perform the following operations using SQL Commands
(i) Provide constraint names while creating table(ii) Drop the check constraint on CSTCITY(iii) 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