deepak jha
Post on 22-Feb-2015
404 Views
Preview:
TRANSCRIPT
ASSIGNMENT
OF
R D B M S
(Relational Database
Management System)
SUBMITTED TO:
Mr. DINESH KUMAR
SUBMITTED BY:
Deepa
k
REG.N
O. 10803590
SECTI
ON B6801A20
BATC
H: 3
ASSIGNMENT 1:
TO UNDERSTAND ER DIAGRAM.
QUES 1) Draw an ER Diagram for the following scenario:
1) There are multiple banks and each bank has many branches. Each branch has
multiple customers
2) Customers have various types of accounts
3) Some Customers also had taken different types of loans from these bank branches
4) One customer can have multiple accounts and Loans
ANS: 1) Identify the Entities
i) Bank
ii) Branches
iii) Customers
iv) Accounts
v) Loans
2) Find the Relationships
i) Bank has many branches.
The Cardinality between Bank and Branch is one to many
1 M
ii) Each branch has multiple customers
Bank Branches Has
The Cardinality between Bank and Branch is one to many
1 M
iii) One customer can have multiple accounts
The Cardinality between Bank and Branch is one to many
1 M
iv) One customer can have multiple and Loans
The Cardinality between Bank and Branch is one to many
1 M
3) Identify the Key Attributes Bank Name is the key attribute of Entity Bank which uniquely identifies the Bank
Location is the key attribute of Entity Branch which uniquely identifies the Branch
Customer# (Customer Id) is the key attribute of Entity Customer which uniquely
Identifies the Customer
Loan Term is the key attribute of Entity Loan
Account Title is the key attribute of Entity Account
4) Identify the Other Key Attributes
Branch Customers Has
Customer Accounts Has
Customer Loans Has
For Bank Entity, the relevant attribute is Location
For Branch Entity, the relevant attribute is Name
For Customer Entity, the relevant attribute is Name
For Loan Entity, the relevant attribute is Loan amount
For Account Entity, the relevant attribute is Account type
5) Draw the E R Diagram
1 M
1
M
1 1
M M
Type
Title Term
Loans AccountsAmount
Has
Has
Name
C-Id
BranchesHas
Location Name
Bank
Location
Name
Has
Customers
ASSIGNMENT 2:
TO CONVERT ER DIAGRAM TO RELATIONAL SCHEMA
ANS: The relational schema is:
BOOK(ISBN,title,price,year)
AUTHOR(name,address)
PUBLISHER(emailid,name,telephone#,address)
WAREHOUSE (code, phone, and address)
AUTHOR BOOK
PUBLISHER
WAREHOUSE
ISBN PRICE YEARE-MAILID TELEPHONE# NAME ADDRESS
CODE PHONE ADDRESS
ASSIGNMENT 3:
NORMALIZATION
QUES 1: Let us find out the Primary Key and highest normal form for this relation:
R{a,b,c,d,e,} a,e c, d e, a,eb, a,ed
ANS: The Primary Key is ( a,e )
All other attributes are functionally dependent on both a and e.
All the attributes are atomic in nature therefore Relation R is in 1NF.
All non-key attributes (b, c, d) are fully functionally dependent on the primary key (a, e).
Therefore the relation in is 2NF.
All the non-key attributes (b, c, d) are non-transitively depending on primary key (a, e).
Therefore the relation is in 3NF.
QUES 2: Find the primary key and the highest normal form of following relations
a. R{a,b,c} a,bc and ca
b. R{a,b,c,d} ab, ad and bc
c. R{a,b,c,d,e} a,bc, a,bd and de
ANS: a) R {a, b, c} a, bc and ca
The Primary Key is (a,b)
All the attributes are in atomic level or are atomic in nature therefore Relation R is in 1NF.
All non-key attributes are fully functionally dependent on the primary key. Therefore the relation in is 2NF.
The relation is also in 3NF
b) R {a, b, c, d} ab, ad and bc
The Primary Key is a
All the attributes are in atomic level or are atomic in nature therefore Relation R is in 1NF.
All non-key attributes are fully functionally dependent on the primary key . Therefore the relation in is 2NF.
The relation is also in 3NF because all non key attributes are not transitively dependent on
key attribute.
c) R {a, b, c, d, e} a, bc, a, bd and de
The Primary Key is a,b
All the attributes are in atomic level or are atomic in nature therefore Relation R is in 1NF.
All non-key attributes are fully functionally dependent on the primary key. Therefore the relation in is 2NF.
The relation is also in 3NF because all non key attributes are not transitively dependent on key attribute.
ASSIGNMENT 4:
NORMALIZATION
QUES 1: Convert following table to 3NF table. Please add appropriate columns in normalized tables to make 3NF compliant.
Note: Consider address column as simple attribute
Student Id
StudentName
Address Course Id
Course Name
Date Of Exam Marks Grade
1001Lawrence
21, Lion Blvd, St Peter
101 CHSSC 06-Mar-08 83 A
1002Jagan
29, Tiger Blvd, Burbank
102 PF 15-Mar-08 72 B
1003Bobby
213,Parliament Blvd, Petas
103 RDBMS 22-Mar-08 89 A
1004Ramsey
401,Gazipura Blvd,Gulbarga
104 UNIX 02-Apr-08 63 C
1005 Chang Hu
101,Marshal Blvd,Kingston
105 IWT 10-Apr-08 95 A
ANS: All the attributes are in atomic level or are atomic in nature.
Therefore the above table is in 1NF
For 2NF, we have to remove the partial dependencies.
Functional Dependencies of the above table. Let us suppose the table name is Student
details
Studentdetails (Student Id, Course Id, StudentName, Address, CourseName, Date of
Exam, Marks, Grade)
Student Id -> StudentName, Address, Marks, Grade {Partially dependent on key attribute}
Course Id -> CourseName, Date of Exam {Partially dependent on key attribute}
Student Id, Course Id -> Marks, Grade {Fully dependent on key attribute}
Marks -> Grade {No dependency on key attribute}
After removing the partially dependency, we get the following tables which are in 2NF
Student Info
Course Info
Marks Info
Student Id Student Name Address
1001Lawrence
21, Lion Blvd, St Peter
1002Jagan
29, Tiger Blvd, Burbank
1003Bobby
213,Parliament Blvd, Petas
1004Ramsey
401,Gazipura Blvd,Gulbarga
1005Chang Hu
101,Marshal Blvd,Kingston
Course Id Course Name Date of Exam
101 CHSSC 06-Mar-08
102 PF 15-Mar-08
103 RDBMS 22-Mar-08
104 UNIX 02-Apr-08
105 IWT 10-Apr-08
Student Id Course Id Marks Grade
1001 101 83 A
1002 102 72 B
1003 103 89 A
1004 104 63 C
1005 105 95 A
For 3NF, Non key attributes are not transitively dependent on key attribute.
Course Id -> Marks -> Grade
The above dependency is a transitive dependency.
After removing the transitive dependency, we get the following tables which are in 3NF
Marks Info
Marks Grade Info
Student Info
Student Id Course Id Marks
1001 101 83
1002 102 72
1003 103 89
1004 104 63
1005 105 95
Marks Grade
83 A
72 B
89 A
63 C
95 A
Course Info
Student Id Student Name Address
1001Lawrence
21, Lion Blvd, St Peter
1002Jagan
29, Tiger Blvd, Burbank
1003Bobby
213,Parliament Blvd, Petas
1004Ramsey
401,Gazipura Blvd,Gulbarga
1005Chang Hu
101,Marshal Blvd,Kingston
Course Id Course Name Date of Exam
101 CHSSC 06-Mar-08
102 PF 15-Mar-08
103 RDBMS 22-Mar-08
104 UNIX 02-Apr-08
105 IWT 10-Apr-08
ASSIGNMENT 5:
SQL QUERIES
QUES 1: Create the following tables with appropriate constraints and insert the given
sample data.
i)
Applicant Table
Column name Data type Description Constraints
ApplicantId Varchar2(4)
Unique Id generated for an applicant when he or she applies for admission in the college. Primary Key
ApplicantName Varchar2(30) Name of the applicant Not null
EmailId Varchar2(30) email Id of the applicantShould be Unique
Address Varchar2(50) Complete address of Applicant City Varchar2(15)
ANS: The query for creation of this table is:
CREATE TABLE Applicant
(ApplicantId varchar2 (4) CONSTRAINT applicant_pk PRIMARY KEY,
ApplicantName varchar2 (30) CONSTRAINT applicant_applicantname_notnull NOT
NULL,
EmailId varchar2 (30) CONSTRAINT applicant_emailid_uq UNIQUE,
Address varchar2 (50), city varchar2 (15));
ii) Branch Table
Column name Data type Description Constraints
BranchId Varchar2(2) Unique Id generated for a branch
Primary Key, Should start with ‘B’
BranchName Varchar2(30) Name of the branch Not Null
ANS: The query for creation of this table is:
CREATE TABLE Branch
(BranchId varchar2 (2) CONSTRAINT branch_pk PRIMARY KEY CONSTRAINT
branch_bid CHECK (BranchId like’ b %’),
BranchName varchar2 (30) CONSTRAINT branch_branchName_notnull NOTNULL);
iii) Course Table
Column name Data type Description ConstraintsCourseId Varchar2(4) Unique Id given to each course Primary KeyCourseName varchar2(30) Name of the course Not Null
Semester Number(1)Name of the semester in which the course is taught
BranchId Varchar2(2)Id of the branch where the course is taught
Foreign key to Branch Table
Elective char(1) Whether it is elective paper or notIts value can be 'Y' or 'N' only
ProjectMarks Number(3)Marks of the project in the course (if any)
AssignmentMarks Number(3)Assignment Marks for the course (if any)
InternalMarks Number(3)Internal Marks for the course (if any)
SemesterExamMarks Number(3)Marks of the semester exam of the course
ANS: The query for creation of this table is:
CREATE TABLE Course
(CourseId varchar2 (4) CONSTRAINT course_pk PRIMARY KEY,
CourseName varchar2 (30) CONSTRAINT course_coursename_notnull NOT NULL,
Semester number (1),
BranchId varchar2 (2) CONSTRAINT course_fk REFERENCES Branch (BranchId),
Elective char (1) IN (‘Y’,’N’),
ProjectMarks number (3),AssignmentMarks number (3),
InternalMarks number (3),
SemesterExamMarks number (3));
iv) Student TableColumn name Data type Description Constraints
StudentId Varchar2(4)
Unique id generated for each student when he or she gets admitted in to the college Primary Key
ApplicantId Varchar2(4)
Applicant Id when the student applied for the admission in the college
Foreign key to Applicant table
CurrentSemester number(1)Semester in which the student is currently studying
UserId Varchar2(15)Decided at the time of selection in the college
It should be unique
Password Varchar2(15)Decided at the time of selection in the college Not Null
ANS: The query for creation of this table is:
CREATE TABLE Student
(StudentId varchar2 (4) CONSTRAINT student_pk PRIMARY KEY,
ApplicantId varchar2 (4) CONSTRAINT student_fk REFERENCES Applicant
(ApplicantId),
CurrentSemester number (1),
UserId varchar2 (15) CONSTRAINT student_userid_uq UNIQUE,
Password varchar2 (15) CONSTRAINT student_password_notnull NOT NULL);
v) Registration Table
Column name Data type Description Constraints
StudentId Varchar2(4)Student Id of the student who has taken up the course
Foreign key to Student table
CourseId Varchar2(4)Course Id of the course in which the student has appeared
Foreign key to Course table
DateOfExam DateDate on which the semester exam was conducted
ProjectMarks Number(3)Marks obtained by student in the project of the course (if any)
AssignmentMarks Number(3)Assignment Marks obtained by the student for the course (if any)
InternalMarks Number(3)Internal Marks obtained by the student for the course (if any)
SemesterMarks Number(3)Marks of the semester exam obtained by the student for the course
Grade Char(1)Over all grade of the student in the course
ANS: The query for creation of this table is:
CREATE TABLE Registration
(StudentId varchar2 (4) CONSTRAINT registration_student_fk REFERENCES Student
(StudentId),
CourseId varchar2 (4) CONSTRAINT registration_course_fk REFERENCES Course
(CourseId),
DateOfExam date,
ProjectMarks number (3),
AssignmentMarks number (3),
InternalMarks number (3),
SemesterMarks number (3),
Grade char (1));
vi) Department Table
Column name Data type Description Constraints
DepartmentId Number(2)Unique Id generated for Each Department Primary Key
DepartmentName Varchar2(30) Name of the Department Not Null
HeadOfDepartment Varchar2(4)Id of Instructor who is managing the department
ANS: The query for creation of this table is:
CREATE TABLE Department
(DepartmentId number (2) CONSTRAINT department_pk PRIMARY KEY,
Departmentname varchar2 (30) CONSTRAINT department_departmentname-notnull NOT
NULL,
HeadOfDepartment varchar2 (4));
vii) Instructor Table
Column name Data type Description Constraints
InstructorId Varchar2(4) Unique Id generated for Instructors
Primary Key, Should start with ‘I’
InstructorName Varchar2(30) Name of the instructor Not Null
DateOfJoining DateDate when instructor joins the department
DepartmentId Number(2)Department id of the department where Instructor works
Foreign key to Department table
ANS: The query for creation of this table is:
CREATE TABLE Instructor
(InstructorId varchar2 (4) CONSTRAINT instructor_pk PRIMARY KEY CONSTRAINT
instructor_iid CHECK (InstructorId like ‘I %’),
InstructorName varchar2 (30) CONSTRAINT instructor_instructorname_notnull NOT
NULL,
DateOfJoining date,
DepartmentId number (2) CONSTRAINT instructor_fk REFERENCES Department
(DepartmentId));
viii) Course Allocation Table
Column name Data type Description Constraints
AllocationId Number(4)Unique Id generated whenever ever a course is allocated to a instructor Primary key
CourseId Varchar2(4)Course id of the course for which allocation is done
Foreign key to Course table
InstructorId Varchar2(4)Instructor Id of the instructor who has been allocated the course
Foreign key to Instructor table
StartDate Date
Start date when a course is taken up by the instructor( A course can be allocated to the instructor in the middle of the semester also)
EndDate Date
Date till which the course is allocated to the instructor( the instructor can be de allocated from the course in the middle of the semester)
Can't be less than Start Date
ANS: The query for creation of this table is:
CREATE TABLE CourseAllocation
(AllocationId number (4) CONSTRAINT allocation_pk PRIMARY KEY,
CourseId varchar2 (4) CONSTRAINT coursealloction_course_fk REFERENCES Course
(CourseId),
InstructorId varchar2 (4) CONSTRAINT courseallocation_instructor_fk REFERENCES
Instructor (InstructorId),
StartDate date,
EndDate date CONSTRAINT coursealloction_enddate CHECK (NOT
EndDate<StartDate));
ix) Hotel Table
Column name Datatype Description Constraints
HostelId Varchar2(10)Unique Id Given to each hostel in the college
Composite Primary KeyRoomno Number(3)
Unique no given to each room in a particular hostel
StudentId Varchar2(4)
Student id of the student who reside in a particular hostel and in a particular room
Foreign key to Student Table
HostelFee Number(6) Monthly fee of the hostel Greater than 0
ANS: The query for creation of this table is:
CREATE TABLE Hostel
(HostelId varchar2 (10),
Roomno number (3),
StudentId varchar2 (4) hotel_fk REFERENCES Student (StudentId),
HostelFee number (6) CONSTRAINT hotel_fee CHECK (HostelFee>0),
CONSTRAINT hotel_pk PRIMARY KEY (HostelId, Roomno));
x) Attendance Table
Colum name Datatype Description Constraints
StudentId Varchar2(4)Student id of the student who’s attendance is recorded
Foreign key to Student table
CourseId Varchar2(4)Course Id of the course in which attendance is recorded
Foreign key to Course table
TotalLecturedays Number(3) Total no of attendance in the course Greater than 0
NoOfDaysPresent Number(3)No of classes the student has attended in the course
ANS: The query for creation of this table is:
CREATE TABLE Attendance
(StudentId varchar2 (4) CONSTRAINT attendance_student_fk REFERENCES Student
(StudentId),
CourseId varchar2 (4) CONSTRAINT attendance_course_fk REFERENCES Course
(CourseId),
TotalLecturedays number (3) CONSTRAINT totallecture_days CHECK (TotalLecturedays>0),
NoOfDaysPresent number (3));
a) Applicant Data
ApplicantIdApplicant Name EmailId Address City
A001Raj raj@abc.co
m2nd Street,Hebbal
Mysore
A002
Vadi vadi@abc.com
4th Street,Vijayangar
Bangalore
A003
Sam Sam_Sundar@abc.com
5th Street,Edappalli
Cochin
A004 Suraj suraj@abc.com
1st Main,Rabindra Nagar
Kolkata
A005 Lakshmi 2nd Vizag
Main,Allipuram
A006 Sandra sandra@abc.com
3rd Corss,RS Puram
Coimbatore
A007 Vivek Vivek_kumar@abc.com
5th Main,Coimbed
Chennai
A008 Vikas vikas@abc.com
2nd Main,Jayalakhmipuram
Bangalore
A009 Bipin 2nd Cross,Siddarth Nagar
Mysore
A010 Gopi gopi@abc.com
2nd Street,Green Market
Delhi
A011 Sandy sandy@abc.com
9th Cross,Seshadripuram
Bangalore
A012 Lilly lilly@abc.com
9th Main,Banjara Hills
Hyderabad
A013 Rose 11th Cross,Kadavanthara
Cochin
A014 Megha megha@abc.com
10th Lane,Vashi
Mumbai
A015 Henry henry@abc.com
2nd street,Silk Board
Bangalore
A016 Joel jeol@abc.com
4th street,Vijayanagar
Mysore
ANS: INSERT INTO Applicant (ApplicantId, ApplicantName, EmailId, Address, City)
VALUES
('A001','Raj','raj@abc.com','2nd Street,Hebbal','Mysore'),
('A002','Vadi','vadi@abc.com','4th Street,Vijaynagar','Bangalore'),
('A003','Sam','sam_sundar@abc.com','5th Street,Edappalli','Cochin'),
('A004','Suraj','suraj@abc.com','1st Main,Rabindra nagar','Kolkata'),
('A005','Lakshmi','2nd main,Allipuram','Vizag'),
('A006','Sandra','Sandra@abc.com''3rd Cross,RS Puram','Coimbatore'),
('A007','Vivek','vivek_kumar@abc.com''5th main,Coimbed','Chennai'),
('A008','Vikas','vikas@abc.com''2nd Main,Jayalakhmipuram','Bangalore'),
('A009','bipin','2nd Cross,Siddarth nagar',city='Mysore'),
('A010','Gopi','gopi@abc.com''2nd Street Green Market','delhi'),
('A011','Sandy','sandy@abc.com''9th Cross,Seshadripuram','Bangalore'),
('A012','Lilly','lilly@abc.com''9th Main,Banjara Hills','Hyderabad'),
('A013','Rose','11th Cross,kadavanthara','Cochin'),
('A014','Megha','megha@abc.com''10th Lane,Vashi','Mumbai'),
('A015',='Henry',='henry@abc.com''2nd Street,Silk Road','Bangalore'),
('A016','Joel','jeol@abc.com''4th Street,Vijayanagar','Mysore');
b) Branch Data
BranchId BranchNameB1 Information Science
B2 Computer ScienceB3 ElectronicsB4 ElectricalB5 MechanicalB6 Civil
ANS: INSERT INTO Branch (BranchId,Branchname)VALUES('B1','Information Science'),
('B2','computer Science'),
('B3','Electronics'),
('B4','Electrical'),
('B5','Mechanical'),
('B6','Civil');
c) Course Data
CourseId
CourseName Semester
BranchId Elective
Project Marks
Assign-ment Marks
InternalMarks
Semester ExamMarks
C001Programming Fundamentals
1 B1 N 20 10 10 60
C002 Data Structures 2 B1 N 20 10 10 60
C003Basics of RDBMS
2 B1 N 20 10 10 60
C004 System Software 3 B1 N 20 10 10 60
C005Computer Hardware
3 B2 N 20 10 10 60
C006 File Structures 4 B1 N 60 10 10 20
C007Network Computing
4 B3 N 20 10 10 60
C008Data Warehousing
5 B1 Y 20 10 10 60
C010Analysis of Algorithms
5 B1 N 20 10 10 60
C009 Neural Networks 6 B2 Y 10 10 80
ANS: INSERT INTO
Course VALUES (‘&CourseId’,’&CourseName’, &Semester,’&BranchId’,’&Elective’,
&ProjectMarks, &AssignmentMarks, &InternalMarks, &SemesterExamMarks);
d) Student Data
StudentId ApplicantIdCurrent Semester UserId Password
S001 A001 2 raj Tiger
S002 A003 3 sam Tiger
S003 A004 5 suraj Tiger
S004 A006 3 sandra TigerS005 A007 4 vivek TigerS006 A008 7 vikas TigerS007 A009 8 bipin TigerS008 A010 3 gopi TigerS009 A012 1 lilly TigerS010 A013 4 rose TigerS011 A014 7 megha Tiger
S012 A015 6 henry TigerS013 A016 2 jeol Tiger
ANS: INSERT INTO Student VALUES (&StudentId,’&ApplicantId’,
&CurrentSemester,’&UserId’,’&Password’);
e) Registration Data
StudentId
CourseId
DateOfExam
ProjectMarks
AssignmentMarks
Semester
GradeInternal Exam Marks Marks
S001 C001 5-Jun-08 20 5 5 50 AS002 C001 24-Aug-08 15 10 8 32 BS003 C002 15-Mar-08 20 9 9 55 AS004 C003 13-Aug-08 20 10 10 60 AS005 C004 8-Sep-08 15 10 10 50 AS006 C005 22-Nov-08 10 5 5 30 DS007 C006 6-Feb-08 45 5 5 15 BS010 C004 8-Sep-08 20 10 10 40 AS009 C003 29-Dec-08 18 8 10 46 AS011 C007 13-Sep-08 20 7 8 55 AS012 C008 16-Jan-08 35 5 5 20 BS013 C006 6-Feb-08 47 8 7 18 A
ANS: INSERT INTO Registration VALUES (‘&StudentId’,’&CourseId’, ‘&DateOfExam’,
‘&ProjectMarks’, ‘&AssignmentMarks’, &InternalMarks,
‘&SemesterExamMarks’,’&Grade’);
f) Department Data
DepartmentId DepartmentName HeadOfDepartment
10 Information Science I10520 Computer Science I10230 Electronics I10440 Electrical I10750 Mechanical I10960 Civil I106
ANS: INSERT INTO department VALUES (&DepartmentId,
&DepartmentName’,’&HeadOfDepartment’);
g) Instructor Table
InstructorId InstructorName DateOfJoining DepartmentId
I101 Bob Hockins 12-Jan-00 10
I102 Suguru Zikovich 21-Feb-01 20
I103 Ritivoi 13-Jan-03 10I104 David Field 30-Mar-99 30I105 Emillie Norton 4-Jun-98 30I106 Ron Hardman 1-Jan-05 60I107 Scott Urman 8-Apr-08 40I108 Daisy Samson 9-Dec-04 50I109 Ford Bravo 10-May-08 50I110 Rebecca Brown 2-Jan-09 10I111 Antario 23-Jun-04 10I112 Samuel 25-Jan-02 60I113 Justin 14-Apr-03 40I114 Nissar 16-Mar-09 10I115 Avinash Naren 27-May-07 20I116 Micheal Rose 5-Jan-08 40
ANS: INSERT INTO instructor VALUES (‘&InstructorId’,’&InstructorName’,
&DateOfJoining, &DepartmentId);
h) Course Allocation Table
AllocationId CourseId InstructorId StartDate EndDate1001 C001 I101 1-Jun-08 4-Jun-08
1002 C002 I102 7-Mar-08 14-Mar-08
1003 C001 I103 20-Aug-08 23-Aug-08
1004 C001 I101 1-Sep-08 4-Sep-081005 C003 I104 10-Aug-08 12-Aug-081006 C004 I103 4-Sep-08 7-Sep-081007 C003 I104 26-Aug-08 28-Aug-081008 C002 I107 21-Jul-08 27-Jul-081009 C005 I105 12-Nov-08 21-Nov-081010 C006 I106 1-Feb-08 5-Feb-08
1011 C007 I108 7-Sep-08 12-Sep-081012 C005 I109 12-Nov-08 21-Nov-081013 C008 I110 15-Jan-08 15-Jan-08
ANS: INSERT INTO CoursAllocation VALUES
(&AllocationId,’&CourseId’,’&InstructorId’, &StartDate, &EndDate);
i) Attendance Data
StudentId CourseId TotalLectureDays NoOfDaysPresent
S001 C001 4 3S002 C001 4 4S003 C002 8 5S004 C003 3 3S005 C004 4 2S006 C005 10 9S007 C006 5 5S010 C004 4 4S009 C003 3 1S011 C007 6 5S012 C008 1 1
ANS: INSERT INTO Attendance VALUES (‘&StudentId’,’&CourseId’,
&TotalLectureDays, &NoOfDaysPresent);
j) Hotel Data
HostelId RoomNo StudentId HostelFeeViolet 101 S001 3000Violet 201 S003 4000Violet 301 S004 5000Indigo 100 S002 3000Indigo 200 S005 4000Indigo 300 S006 5000Blue 102 S007 3000Blue 202 S008 4000Blue 302 S009 5000
ANS: INSERT INTO Hostel VALUES
(‘&HostelId’,’&Roomno’,’&StudentId’,’&FostelFee’);
The format of inserting the values is same for all the cases as explained in part a and
part b.
ASSIGNMENT 6:
SQL QUERIES
1) List the name of different cities from where applicants belong to
ANS: SELECT ApplicantName,City FROM Applicant
2) List the different semesters in which students have enrolled
ANS: SELECT Semester FROM Student
3) List the distinct InstructorId and CourseId from CourseAllocation table.
ANS: SELECT DISTINCT InstructorId, CourseId FROM CourseAllocation
4) List the name of course which are not electives
ANS: SELECT CourseName FROM Course WHERE Elective=’N’.
5) List the name of instructor who has joined before 01-jan-2002
ANS: SELECT InstructorName FROM Instructor WHERE DateOfJoining < System date
6) List Id of courses whose ending date is before the current system date.
ANS: SELECT CourseId FROM CourseAllocation WHERE EndDate < System date
7) List the name of applicant from Mysore and Bangalore.
ANS: SELECT ApplicantName FROM Applicant WHERE City
IN(‘Mysore’,’Bangalore’);
8) List the name of applicants who do not belong to Mysore.
ANS: SELECT ApplicantName FROM Applicant WHERE City NOT(‘Mysore’);
9) List the name of semester 1 and semester 2 courses which belongs to branch B1.
ANS: SELECT CourseName FROM Course WHERE Semester IN (‘1’,’2’) AND
BranchId = B1;
10) List the name of courses which are elective and project marks is equal to 20.
ANS: SELECT CourseName FROM Course WHERE Elective =’Y’ AND
ProjectMarks = 20;
11) List the name of the course in which project marks is greater than the semester
marks.
ANS: SELECT CourseName FROM Course WHERE ProjectMarks > SemesterMarks;
12) List the details of all the applicants who belong to Bangalore, Mysore or Hyderabad.
ANS: SELECT * FROM Applicant WHERE City IN(‘bangalore’,’mysore’,’hyderabad’);
13) List the details of courses which are taught in semester 1, 2 or 3.
ANS: SELECT * FROM Course WHERE Semester IN(‘1’,’2’,’3’);
14) List the studentId who have secured marks between 80 to 100.
ANS: SELECT StudentId FROM Student WHERE SemesterExam BETWEEN 80 AND
100;
15) List the student details in ascending order of their applicantId.
ANS: SELECT * FROM Student ORDER BY ApplicantId;
16) List the details of instructor on ascending order of their date of joining.
ANS: SELECT * FROM Instructor ORDER BY DateOf Joining;
17) List the details of instructor on descending order of department no and ascending
order of joining date.
ANS:SELECT * FROM Instructor ORDER BY DepartmentName DESC,ORDER BY
DateOf Joining;
18) List the name of instructors starting with ‘R’ and ending with ‘n’
ANS: SELECT InstructorName FROM Instructor WHERE InstructorName (LIKE ‘r%n’);
19) List the details of instructors whose date of joining is in the month of March.
ANS: SELECT * FROM Instructor WHERE DateOf Joining BETWEEN ‘1-March’ AND
’31-March’;
20) List the details of applicant whose email id contains a character ‘_’
ANS: SELECT * FROM Applicant WHERE EmailId (LIKE ‘%_%’);
21) List the applicant who do not have email id in their applicant detail information.
ANS: SELECT EmailId FROM Applicant WHERE EmailId IS NULL;
22) List the course Id which has project Marks in its course detail information.
ANS: SELECT CourseId FROM Course WHERE ProjectMarks(CourseAllocation);
ASSIGNMENT 7:
SQL QUERIES
QUES: To understand a simple query
Let us try to understand how the following query works:
“List employees not belonging to department 30, 40, or 10”
Assume that the Emp, Dept tables contains the following description:
Emp (EmpId, EmpName, sal, job, hiredate, deptno)
Dept (Deptno, DeptName)
ANS: SELECT ename FROM emp WHERE deptno NOT IN (30, 40, 10);
Working of Query
1) The query filters those tuples which do not belong to deptno 30, 40 or 50, because of the operator NOT IN, from the emp table.
2) Finally it displays only the employee names of the filtered tuples because we specify only ename in the SELECT clause.
ASSIGNMENT 8:
SQL QUERIES
QUES: Let us try to understand how the following query works:
“List the total salary, maximum and minimum salary and average salary of the employees job wise, for department 20 and display only those rows having an average salary > 1000”.
Assume that the Emp table contains the following description:
Emp (EmpId, EmpName, sal, job, hiredate, deptno)
ANS: SELECT job, avg (sal) FROM emp WHERE deptno=20 GROUP BY job HAVING avg (sal) > 1000 ORDER BY job;
Working of Query
1) First thing first we have to select (filter) data only for dept 20. For this we have to putWHERE deptno=20
2) Now, we have to find average salary of the employees job wise. Therefore we have to group our selected (filtered) data using group by clause.
GROUP BY job
3) Now, we have to display only those rows having an average salary > 1000. Therefore we have to put
HAVING avg (sal) > 1000
4) For getting the final output in ascending order of job we have to put order by clause.
ORDER BY job
ASSIGNMENT 9:
SQL QUERIES
Perform the following queries:
1) List the Branch Id and the number of courses offered by each Branch
ANS: SELECT BranchId, COUNT (NoOfCourses) FROM Branch Data GROUP BY
BranchId;
2) List the number of courses which do not have Project Marks
ANS: SELECT COUNT (no of courses) FROM Course Data WHERE project marks IS
NULL;
3) List the Branch Id and the number of electives offered in each branch
ANS: SELECT BranchId, COUNT (no of electives) FROM Branch Data GROUP BY
BranchId;
4) List the Course Id, average total marks obtained in each Course.
Hint: Total marks= project marks + assignment marks + internal marks + semester
marks
ANS: SELECT CourseId, AVG (total marks) FROM Course GROUP BY CourseId;
5) List the number of courses for which exam was conducted between
1-Jan-2008 and 30-May-2008
ANS: SELECT COUNT (no of courses) FROM tablename WHERE ExamDate
BETWEEN 1-Jan-2008 AND 30-May-2008
6) List the Instructor Id, number of different Courses offered by an Instructor
ANS: SELECT InstructorId, COUNT (Courses) FROM tablename GROUP BY
InstructorId;
7) List the Course Id which is offered by more than one instructor
ANS: SELECT CourseId, COUNT (Courses) FROM tablename GROUP BY CourseId
HAVING COUNT (Instructor)>1;
8) List the Instructor Ids who have finished handling at least two courses during the
year 2008
ANS: SELECT InstuctorId FROM Instuctor WHERE COUNT (Courses) >=2;
9) List the total strength of students staying in each Hostel
ANS: SELECT Hostel, COUNT (Strength) FROM tablename GROUP BY Hostel;
10) List the total marks in each Semester of a Branch
Hint: total marks= project marks + assignment marks + internal marks + semester
marks
ANS: SELECT Semester, SUM (total marks) FROM tablename GROUPBY Semester;
11) List the Student Id, Student Name if the student has an Email Id
ANS: SELECT StudentId, StudentName FROM table GROUP BY StudentId, and
StudentName HAVING EmailId IS NOT NULL;
12) List the names of students currently studying in 5th semester
ANS: SELECT StudentName FROM tablenameWHERE Semester=5;
13) List the names of students along with the course names for which they have
registered
ANS: SELECT StudentName, CourseName FROM tablename GROUP BY StudentName;
14) List the name of the course and the number of ‘A’ grades obtained in that course
ANS: SELECT CourseName, COUNT (Grade) FROM Tablename GROUP BY
CourseName HAVING Grade=A;
15) List the name of department along with the instructor name who is heading that
department if he has minimum two years of experience in the College.
ANS: SELECT DepartmentName, InstructorName FROM tablename GROUP BY
DepartmentName HAVING Experience>2;
16) List the name of course, name of instructor, start date, end date if that course is
allotted to the instructor
ANS: SELECT CourseName, InstructorName, StartDate, EndDate FROM table GROUP
BY CourseName;
17) List the student id and hostel id if they are staying in the hostel. Also display the
student ids who are not staying in the hostel
ANS: SELECT StudentId, HostelId FROM tablename WHERE regd IS NOT NULL
UNION
SELECT StudentId, HostelId FROM tablename WHERE regd IS NULL;
18) List the names of applicants and their User Id and Password. Display NULL for
User Id and Password if they don’t get admission
ANS: SELECT Name, UserId, Password FROM tablename WHERE Admission IS NOT NULL
UNION
UPDATE Tablename SET UserId=NULL, Password=NULL WHERE Admission IS NULL;
19) List the instructor name, branch name, number of courses taught by the instructor
in that branch
ANS: SELECT InstructorName, BranchName, COUNT (Courses) FROM table GROUP
BY InstructorName, BranchName;
20) List the student name and course name in which they have scored ‘D’ grade
ANS: SELECT StudentName, CourseName FROM table GROUP BY StudentName
HAVING Grade=D;
21) List the course names which have duration less than or equal to 4 days
ANS: .SELECT CourseName FROM table WHERE Duration<=4;
22) List the name of the instructor and the course names they are teaching on ’27-Aug-
2008’
ANS: SELECT InstructorName, CourseName FROM tablename GROUP BY
InstructorName, CourseName HAVING date=27-aug-2008;
23) List the names of students who are not staying in Hostel
ANS: SELECT StudentName FROM tablename WHERE Hostel IS NOT NULL;
24) List the student name, course name and the number of days present for each course
conducted
ANS: SELECT StudentName, CourseName, COUNT (NoOfDays) FROM tablename
GROUP BY StudentName, CourseName;
25) List the names of courses handled by the instructors who are also head of
departments
ANS: SELECT CourseName, InstructorName FROM tablename WHERE
Instructor=HeadOfDepartment;
26) List the names of applicants who got admission into Computer Science branch
ANS: SELECT ApplicantName, CSEBranch FROM tablename GROUP BY CSEBranch;
27) List the names of instructors who are handling elective courses
ANS: SELECT InstructorName FROM Tablename WHERE Courses =Electives;
ASSIGNMENT 10:
SQL QUERIES
QUES: Consider the following “Suppliers” table
Suppliers Table
Let us consider the following query:
“Get supplier numbers for suppliers with a status lower than that of supplier S1”
ANS: SELECT SNo FROM Suppliers WHERE Status < (SELECT Status FROM
Suppliers WHERE SNo=’S1’);
Working of Query
1) The inner query is executed first which selects the status of supplier S1 from suppliers table.
2) Now the query looks like this:
SELECT SNo FROM Suppliers WHERE Status < 20
3) Above query is again executed as a simple query i.e. tuples are selected for those suppliers
whose status is less than 20 and finally there SNo is being displayed.
SNo SName Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens
ASSIGNMENT 11:
SQL QUERIES
QUES: Assume that User_A has created two tables: EMPLOYEE and DEPARTMENT.
He/She is granting/ revoking the privileges to other users on these two tables. Different
data control mechanisms have been addressed.
1) User_A grants Select, Insert and Update privileges to User_B on EMPLOYEE table
with grant option.
GRANT SELECT, INSERT, UPDATEON EmployeeTO User_B
WITH GRANT OPTION;
2) User_A grants Select privilege to User_C on DEPARTMENT table
GRANT SELECTON DepartmentTO User_CWITH GRANT OPTION;
3) User_B grants Select, Update privileges to User_D on EMPLOYEE table
GRANT SELECT, UPDATEON EmployeeTO User_D;
4) User_C grants Select privilege to User_E on DEPARTMENT table
GRANT SELECT ON Department TO User_E;
Check the following:
1) Whether User_B able to Update the DEPARTMENT table
ANS: No, User_B not able to UPDATE the Department Table, because User_B has no
GRANT Permission to update the Department Table
2) Whether User_D able to Update the EMPLOYEE table
ANS: Yes, User_D is able to UPDATE the Employee Table, because User_B has GRANT
Permission of UPDATE to User_D and User_B also has GRANT permission from
User_A
3) Whether User_C able to Select the data from DEPARTMENT table
ANS: Yes, User_C able to SELECT the data from DEPARTMENT table, because User_C
has GRANT permission from User_A
4) Whether User_E able to Select the data from EMPLOYEE table
ANS: No, User_E can’t able to SELECT the data from EMPLOYEE table
5) Whether User_D able to grant Select, Update privileges to any User
ANS: No, User_D can’t able to grant Select, Update privileges to any User
6) Whether User_D able to Select data from EMPLOYEE table after privileges are
revoked by User_A as below:
REVOKE SELECT, INSERT, UPDATEON EmployeeFROM User_B
ANS: NO, User_D can’t able to SELECT data from EMPLOYEE
ASSIGNMENT 12:
SQL QUERIES
QUES: Perform the following steps and understand the possibilities and limitations in updating views.
1) Create EMPLOYEE table with the following description.
EMPLOYEE
2) Insert the below data into EMPLOYEE table
3) Create a View EMPLOYEE_VIEW as below:
CREATE VIEW EMPLOYEE_VIEWAS SELECT * FROMEMPLOYEE
4) Execute the below query and see the data in view:
SELECT *FROM EMPLOYEE_VIEW
EmpId Number(4) Primary KeyEmpName Varchar2(15
)Cannot be NULL
Salary Number(7,2)DateOfJoin Date
EmpId
EmpName Salary DateOfJoin
1001 Suresh 50000 01-Jun-021002 Kumar 40000 25-Mar-031003 Vijay 70000 15-Aug-01
Check the following:
1) Whether data insertion is possible into the view without a column having PRIMARY KEY constraint
INSERT INTO EMPLOYEE_VIEW (EmpName, Salary, DateOfJoin) VALUES
('Kiran', 2500,'01-Jan-2002');
ANS: Yes, we can insert the data into the view having PRIMARY KEY
2) Whether data insertion is possible into the view without a column having NOT NULL constraint
INSERT INTO EMPLOYEE_VIEW (EmpId, Salary, DateOfJoin) VALUES
(1004, 60000,'01-Jan-2004');
ANS: Yes, we can insert into the view without a column having NOT NULL constraint
3) Whether data insertion is possible into the view without a column having any
constraint on it.
INSERT INTO EMPLOYEE_VIEW (EmpId, EmpName, DateOfJoin) VALUES
(1004,’Arun’,'01-Jan-2004');
ANS: Yes, data insertion is possible into the view without a column having any constraint on
it.
4) Drop table EMPLOYEE and select the data from view.
DROP TABLE EMPLOYEE;
SELECT * FROM EMPLOYEE_VIEW;
ANS: Nothing is displayed because if Table is DROP then view is also dropped
5) Recreate the table EMPLOYEE with different names for all columns of it, insert few
rows and select data from the view
SELECT * FROM EMPLOYEE_VIEW;
ANS: 1) Create Table:
CREATE TABLE EMPLOYEE (Emp_contactnum number (10), Emp_DOB date)
2) Insertion
INSERT INTO EMPLOYEE VALUES (&Emp_contactnum, &Emp_DOB);
3) View
CREATE VIEW EMPLOYEE_VIEW AS SELECT * FROM EMPLOYEE;
INSERT INTO EMPLOYEE_VIEW VALUES (‘9465012362’, ‘06-April-90’);
SELECT * FROM EMPLOYEE_VIEW;
Emp_Contactnum Emp_DOB9465012362 06-April-90
6) Drop table EMPLOYEE, Recreate the it with same old names for all columns, insert few rows and select data from the view
SELECT * FROM EMPLOYEE_VIEW;
ANS: CREATE TABLE EMPLOYEE
(Empid number (4) CONSTRAINT Employee_pk PRIMARY KEY,
EmpName varchar2 (15) CONSTRAINT EmployeeName_notnull NOT NULL,
Salary number (7, 2)
DateOfJoin date);
INSERT INTO EMPLOYEE VALUES (&Empid, ’&EmpName’, ‘& Salary, &DateOfJoin);
top related