Assign Rosy
-
Upload
simar-preet -
Category
Documents
-
view
182 -
download
12
Transcript of Assign Rosy
Assignment of RDBMS
Submitted to Submitted byMrs. Jaspreet Kaur Rosy Walia
Batch-5Reg no.-7450070126Group-1
Assignment 1: To understand ER Diagram.
Problem Description:
Draw an ER Diagram for the following scenario:
There are multiple banks and each bank has many branches. Each branch has multiple customers
Customers have various types of accounts Some Customers also had taken different types of loans from these bank branches One customer can have multiple accounts and Loans
ER Diagram-:
N
1
N 1 M N
M
N
Assignment 2: To convert ER diagram to relational schema
Branch
Customer Loan
Account
Borrower
Detail
Depositor
Loan-Branch
`Relational Schema for it is-:Author (Name, Address)Book (Title, ISBN, Price, Year)Publisher (E-mail ID, Telephone#, Name, Address)Warehouse (Code, Phone, Address)Writes (name, ISBN)
Stored (Code, ISBN);
Assignment 3: Normalization
Objective: To determine the highest Normal Form
Problem Description: 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
Step 1: Determining primary key: From above stated functional dependencies, it is evident that a,e determines c and d . d determines e and hence a,e determines all the non-key attributes (b,c,d) of the relation. Thus a,e is the primary key for relation R.
Step 2: Highest normal form: 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.
Assignment 4: Normalization
Objective: Normalize the given relation to 3NF.
Problem Description: 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-:Functional Dependency-:Student Id -> Student NameStudent Id -> AddressStudent Id -> Course IdCourse Name -> Course Id Date of Exam -> Course Id Marks -> Course Id Grade -> Marks
3 NF Tables are-: The tables after 3 normalized form are as follows:
Student Details Table
Student Id StudentName
Address Course Id
1001Lawrence
21, Lion Blvd, St Peter
101
1002 Jagan 29, Tiger Blvd, Burbank 1021003 Bobby 213,Parliament Blvd, Petas 1031004 Ramsey 401,Gazipura Blvd,Gulbarga 1041005 Chang Hu 101,Marshal Blvd,Kingston 105
Course Details Table
Course Id Course Name
Date Of Exam Marks Grade
101 CHSSC 06-Mar-08 83 A102 PF 15-Mar-08 72 B103 RDBMS 22-Mar-08 89 A104 UNIX 02-Apr-08 63 C105 IWT 10-Apr-08 95 A
Assignment 5: Sql Queries
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 nullEmailId Varchar2(30) email Id of the applicant Should be UniqueAddress Varchar2(50) Complete address of Applicant City Varchar2(15)
Using SQL, table is created as-:
Create table ApplicantTable (ApplicantId Varchar2 (4) Constraint app_id PRIMARY KEY,ApplicantName Varchar2 (30) Constraint app_nn NOT NULL,EmailId Varchar2 (30) Constraint id_uq UNIQUE,Address Varchar2 (50),City Varchar2 (15));
Branchtable
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
Using SQL, table is created as-:
Create table BranchTable (BranchId Varchar2 (2) Constraint br_id PRIMARY KEYConstraint br_chk CHECK (BranchId LIKE ‘B %’),BranchName Varchar2 (30)Constraint app_nn NOT NULL);
Using SQL, table is created as-:
Create table CourseTable (CourseId Varchar2 (4) Constraint cid_pk PRIMARY KEY,CourseName varchar2 (30) Constraint cn_nn NOT NULL,Semester Number (1),BranchId Varchar2 (2) Constraint bid_fk FOREIGN KEY (BranchId) REFERENCES BranchTable (BranchId),Elective char (1) Constraint el_chk CHECK (Elective IN ('Y','N')),ProjectMarks Number (3),AssignmentMarks Number (3),InternalMarks Number (3),SemesterExamMarks Number (3));
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
Student Table
Column 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
Using SQL, table is created as-:
Create table StudentTable(StudentId Varchar2 (4) Constraint pk_sid PRIMARY KEY,ApplicantId Varchar2 (4) Constraint aid_fk FOREIGN KEY (ApplicantId) REFERENCES ApplicantTable (ApplicantId),CurrentSemester Number (1),UserId Varchar2 (15) Constraint uid_uq UNIQUE,Password Varchar2 (15) Constraint ps_nn NOT NULL);
Registration Table
Column name Data type Description Constraints
StudentIdVarchar2(4)
Student Id of the student who has taken up the course
Foreign key to Student table
CourseIdVarchar2(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) Overall grade of the student in the course
Using SQL, table is created as-:
Create table RegistrationTable(StudentId Varchar2 (4) Constraint sid_fk FOREIGN KEY (StudentId) REFERENCES StudentTable (StudentId),CourseId Varchar2 (4) Constraint cid_fk FOREIGN KEY (CourseId) REFERENCES CourseTable (CourseId),DateOfExam Date,ProjectMarks Number (3),AssignmentMarks Number (3),InternalMarks Number (3),SemesterMarks Number (3),Grade Char (1));
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
Using SQL, table is created as-:
Create table DepartmentTable(DepartmentId Number (2) Constraint pk_did PRIMARY KEY,DepartmentName Varchar2 (30) Constraint dn_nn NOT NULL,HeadOfDepartment Varchar2 (4));
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 NullDateOfJoining Date Date when instructor joins the department
DepartmentId Number(2)Department id of the department where Instructor works
Foreign key to Department table
Using SQL, table is created as-:
Create table InstructorTable(InstructorId Varchar2 (4)Constraint iid_pk PRIMARY KEYConstraint iid_chk CHECK (InstructorId LIKE ‘I %'),InstructorName Varchar2 (30) Constraint in_nn NOT NULL,DateOfJoining Date,DepartmentId Number (2) Constraint did_fk FOREIGN KEY (DepartmentId) REFERENCES DepartmentTable (DepartmentId));
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
CourseIdVarchar2(4)
Course id of the course for which allocation is done
Foreign key to Course table
InstructorIdVarchar2(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
Using SQL, table is created as-:
Create table CourseAllocationTable(AllocationId Number (4) Constraint aid_pk PRIMARY KEY,CourseId Varchar2 (4) Constraint cid_fk FOREIGN KEY (CourseId) REFERENCES CourseTable (CourseId),InstructorId Varchar2 (4) Constraint iid_fk FOREIGN KEY (InstructorId) REFERENCES InstructorTable (InstructorId),StartDate Date,EndDate Date);
Hostel 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 hostelGreater than 0
Using SQL, table is created as-:
Create table HostelTable(HostelId Varchar2 (10),Roomno Number (3),Constraint hr_pk PRIMARY KEY (HostelId, Roomno),StudentId Varchar2 (4) Constraint sid_fk FOREIGN KEY (StudentId) REFERENCES StudentTable (StudentId),HostelFee Number (6)Constraint hf_chk CHECK (HostelFee >0));
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
Using SQL, table is created as-:
Create table AttendanceTable(StudentId Varchar2 (4) Constraint sid_fk FOREIGN KEY (StudentId) REFERENCES StudentTable (StudentId),CourseId Varchar2 (4) Constraint cid_fk FOREIGN KEY (CourseId) REFERENCES CourseTable (CourseId),TotalLecturedays Number (3)Constraint tot_chk CHECK (TotalLecturedays>0),NoOfDaysPresent Number (3));
Insertion of data in Tables
Applicant Table Data Insertion
Insert into ApplicantTable values (‘A001’, ‘Raj’, ‘[email protected]’, ‘2nd Street, Hebbal’, ‘Mysore’);Insert into ApplicantTable values (‘A002’, ‘Vadi’, ‘[email protected]’, ‘4th Street, Vijayangar’, ‘Bangalore’);Insert into ApplicantTable values (‘A003’, ‘Sam’, ‘[email protected]’, ‘5thStreet, Edappalli’, ‘Cochin’);Insert into ApplicantTable values (‘A004’, ‘Suraj’, ‘[email protected]’, ‘1st Main, Rabindra Nagar’, ‘Kolkata’);Insert into ApplicantTable (ApplicantId, ApplicantName, Address, City) values (‘A005’, ‘Lakshmi’, ‘2nd Main, Allipuram’, ‘Vizag’);Insert into ApplicantTable values (‘A006’, ‘Sandra’, ‘[email protected]’, ‘3rd Corss,RS Puram’, ‘Coimbatore’);
Insert into ApplicantTable values (‘A007’, ‘Vivek’, ‘[email protected]’, ‘5th Main,Coimbed’, ‘Chennai’);Insert into ApplicantTable values (‘A008’, ‘Vikas’, ‘[email protected]’, ‘2nd Main, Jayalakhmipuram’, ‘Bangalore’);Insert into ApplicantTable (ApplicantId, ApplicantName, Address, City) values (‘A009’, ‘Bipin’, ‘2nd Cross, Siddarth Nagar’, ‘Mysore’);Insert into ApplicantTable values (‘A010’, ‘Gopi’, ‘[email protected] ’, ‘2nd Street, Green Market’, ‘Delhi’);Insert into ApplicantTable values (‘A011’, ‘Sandy’, ‘[email protected]’, ‘9th Cross,Seshadripuram’, ‘Bangalore’);Insert into ApplicantTable values (‘A012’, ‘Lilly’, ‘[email protected]’, ‘9th Main, Banjara Hills’, ‘Hyderabad’);Insert into ApplicantTable (ApplicantId, ApplicantName, Address, City) values (‘A013’, ‘Rose’, ‘11th Cross, Kadavanthara’, ‘Cochin’);Insert into ApplicantTable values (‘A014’, ‘Megha’, ‘[email protected]’, ‘10th Lane,Vashi’, ‘Mumbai’);Insert into ApplicantTable values (‘A015’, ‘Henry’, ‘[email protected]’, ‘2nd street,Silk Board’, ‘Bangalore’);Insert into ApplicantTable values (‘A016’, ‘Joel’, ‘[email protected]’, ‘4th street,Vijayanagar’, ‘Mysore’);
Branch Table Data Insertion
Insert into BranchTable values (‘B1’, ‘Information Science’);Insert into BranchTable values (‘B2’, ‘Computer Science’);Insert into BranchTable values (‘B3’, ‘Electronics’);Insert into BranchTable values (‘B4’, ‘Electrical’);Insert into BranchTable values (‘B5’, ‘Mechanical’);Insert into BranchTable values (‘B6’, ‘Civil’);
Course Table Data Insertion
Insert into CourseTable values (‘C001’, ‘Programming Fundamentals’, 1, ‘B1’, ‘N’,20,10,10,60);Insert into CourseTable values (‘C002’, ‘Data Structures’, 2, ‘B1’, ‘N’, 20,10,10,60);Insert into CourseTable values (‘C003’, ‘Basics of RDBMS’, 2, ‘B1’, ‘N’, 20,10,10,60);Insert into CourseTable values (‘C004’, ‘System Software’, 3, ‘B1’, ‘N’, 20, 10, 10, 60);Insert into CourseTable values (‘C005’, ‘Computer Hardware’, 3, ‘B2’, ‘N’, 20, 10, 10, 60);Insert into CourseTable values (‘C006’, ‘File Structures’, 4, ‘B1’, ‘N’, 60,10,10,20);Insert into CourseTable values (‘C007’, ‘Network Computing’, 4, ‘B3’, ‘N’, 20, 10, 10, 60);Insert into CourseTable values (‘C008’, ‘Data Warehousing’, 5, ‘B1’, ‘Y’, 20, 10, 10, 60);Insert into CourseTable values (‘C010’, ‘Analysis of Algorithms’, 5, ‘B1’, ‘N’, 20,10,10,60);
Insert into CourseTable (CourseId, CourseName, Semester, BranchId, Elective, Assignment Marks, InternalMarks, SemesterExamMarks) Values (‘C009’, ‘Neural Networks’, 6, ‘B2’, ‘Y’, 10, 10, 80);
Student Table Data Insertion
Insert into StudentTable values (‘S001’, ‘A001’, 2, ‘raj’, ‘Tiger’);Insert into StudentTable values (‘S002’, ‘A003’, 3, ‘sam’, ‘Tiger’);Insert into StudentTable values (‘S003’, ‘A004’, 5, ‘suraj’, ‘Tiger’);Insert into StudentTable values (‘S004’, ‘A006’, 3, ‘sandra’, ‘Tiger’);Insert into StudentTable values (‘S005’, ‘A007’, 4, ‘vivek’, ‘Tiger’);Insert into StudentTable values (‘S006’, ‘A008’, 7, ‘vikas’, ‘Tiger’);Insert into StudentTable values (‘S007’, ‘A009’, 8, ‘bipin’, ‘Tiger’);Insert into StudentTable values (‘S008’, ‘A010’, 3, ‘gopi’, ‘Tiger’);Insert into StudentTable values (‘S009’, ‘A012’, 1, ‘lilly’, ‘Tiger’);Insert into StudentTable values (‘S010’, ‘A013’, 4, ‘rose’, ‘Tiger’);Insert into StudentTable values (‘S011’, ‘A014’, 7, ‘megha’, ‘Tiger’);Insert into StudentTable values (‘S012’, ‘A015’, 6, ‘henry’, ‘Tiger’);Insert into StudentTable values (‘S013’, ‘A016’, 2, ‘jeol’, ‘Tiger’);
Registration Table Data Insertion
Insert into RegistrationTable values (‘S001’, ‘C001’, ‘5-Jun-08’, 20, 5, 5, 50, ‘A’);Insert into RegistrationTable values (‘S002’, ‘C001’, ‘24-Aug-08’, 15, 10, 8, 32, ‘B’);Insert into RegistrationTable values (‘S003’, ‘C002’, ‘15-Mar-08’, 20, 9, 9, 55, ‘A’);Insert into RegistrationTable values (‘S004’, ‘C003’, ‘13-Aug-08’, 20, 10, 10, 60, ‘A’);Insert into RegistrationTable values (‘S005’, ‘C004’, ‘8-Sep-08’, 15, 10, 10, 50, ‘A’);Insert into RegistrationTable values (‘S006’, ‘C005’, ‘22-Nov-08’, 10, 5, 5, 30, ‘D’);Insert into RegistrationTable values (‘S007’, ‘C006’, ‘6-Feb-08’, 45, 5, 5, 15, ‘B’);Insert into RegistrationTable values (‘S010’, ‘C004’, ‘8-Sep-08’, 20, 10, 10, 40, ‘A’);Insert into RegistrationTable values (‘S009’, ‘C003’, ‘29-Dec-08’, 18, 8, 10, 46, ‘A’);Insert into RegistrationTable values (‘S011’, ‘C007’, ‘13-Sep-08’, 20, 7, 8, 55, ‘A’);Insert into RegistrationTable values (‘S012’, ‘C008’, ‘16-Jan-08’, 35, 5, 5, 20, ‘B’);Insert into RegistrationTable values (‘S013’, ‘C006’, ‘6-Feb-08’, 47, 8, 7, 18, ‘A’);
Department Table Data Insertion
Insert into DepartmentTable values (10, ‘Information Science’, ‘I105’);Insert into DepartmentTable values (20, ‘Computer Science’, ‘I102’);Insert into DepartmentTable values (30, ‘Electronics’, ‘I104’);Insert into DepartmentTable values (40, ‘Electrical’, ‘I107’);Insert into DepartmentTable values (50, ‘Mechanical’, ‘I109’);Insert into DepartmentTable values (60, ‘Civil’, ‘I106’);
Instructor Table Data Insertion
Insert into InstructorTable values (‘I101’, ‘Bob Hockins’, ‘12-Jan-00’, 10);Insert into InstructorTable values (‘I102’, ‘Suguru Zikovich’, ‘21-Feb-01’, 20);Insert into InstructorTable values (‘I103’, ‘Ritivoi’, ‘13-Jan-03’, 10);Insert into InstructorTable values (‘I104’, ‘David Field’, ‘30-Mar-99’, 30);Insert into InstructorTable values (‘I105’, ‘Emillie Norton’, ‘4-Jun-98’, 30);Insert into InstructorTable values (‘I106’, ‘Ron Hardman’, ‘1-Jan-05’, 60);Insert into InstructorTable values (‘I107’, ‘Scott Urman’, ‘8-Apr-08’, 40);Insert into InstructorTable values (‘I108’, ‘Daisy Samson’, ‘9-Dec-04’, 50);Insert into InstructorTable values (‘I109’, ‘Ford Bravo’, ‘10-May-08’, 50);Insert into InstructorTable values (‘I110’, ‘Rebecca Brown’, ‘2-Jan-09’, 10);Insert into InstructorTable values (‘I111’, ‘Antario’, ‘23-Jun-04’, 10);Insert into InstructorTable values (‘I112’, ‘Samuel’, ‘25-Jan-02’, 60);Insert into InstructorTable values (‘I113’, ‘Justin’, ‘14-Apr-03’, 40);Insert into InstructorTable values (‘I114’, ‘Nissar’, ‘16-Mar-09’, 10);Insert into InstructorTable values (‘I115’, ‘Avinash Naren’, ‘27-May-07’, 20);Insert into InstructorTable values (‘I116’, ‘Micheal Rose’, ‘5-Jan-08’, 40);
Course Allocation Table Data Insertion
Insert into CourseAllocationTable values (‘1001’, ‘C001’, ‘I101’, ‘1-Jun-08’, ‘4-Jun-08’);Insert into CourseAllocationTable values (‘1002’, ‘C002’, ‘I102’, ‘7-Mar-08’, ‘14-Mar-08’);Insert into CourseAllocationTable values (‘1003’, ‘C001’, ‘I103’, ‘20-Aug-08’, ‘23-Aug-08’);Insert into CourseAllocationTable values (‘1004’, ‘C001’, ‘I101’, ‘1-Sep-08’, ‘4-Sep-08’);Insert into CourseAllocationTable values (‘1005’, ‘C003’, ‘I104’, ‘10-Aug-08’, ‘12-Aug-08’);Insert into CourseAllocationTable values (‘1006’, ‘C004’, ‘I103’, ‘4-Sep-08’, ‘7-Sep-08’);Insert into CourseAllocationTable values (‘1007’, ‘C003’, ‘I104’, ‘26-Aug-08’, ‘28-Aug-08’);Insert into CourseAllocationTable values (‘1008’, ‘C002’, ‘I107’, ‘21-Jul-08’, ‘27-Jul-08’);Insert into CourseAllocationTable values (‘1009’, ‘C005’, ‘I105’, ‘12-Nov-08’, ‘21-Nov-08’);Insert into CourseAllocationTable values (‘1010’, ‘C006’, ‘I106’, ‘1-Feb-08’, ‘5-Feb-08’);Insert into CourseAllocationTable values (‘1011’, ‘C007’, ‘I108’, ‘7-Sep-08’, ‘12-Sep-08’);Insert into CourseAllocationTable values (‘1012’, ‘C005’, ‘I109’, ‘12-Nov-08’, ‘21-Nov-08’);Insert into CourseAllocationTable values (‘1013’, ‘C008’, ‘I110’, ‘15-Jan-08’, ‘15-Jan-08’);
Attendance Table Data Insertion
Insert into AttendanceTable values (‘S001’, ‘C001’, 4, 3);Insert into AttendanceTable values (‘S002’, ‘C001’, 4, 4);Insert into AttendanceTable values (‘S003’, ‘C002’, 8, 5);Insert into AttendanceTable values (‘S004’, ‘C003’, 3, 3);Insert into AttendanceTable values (‘S005’, ‘C004’, 4, 2);Insert into AttendanceTable values (‘S006’, ‘C005’, 10, 9);Insert into AttendanceTable values (‘S007’, ‘C006’, 5, 5);Insert into AttendanceTable values (‘S010’, ‘C004’, 4, 4);
Insert into AttendanceTable values (‘S009’, ‘C003’, 3, 1);Insert into AttendanceTable values (‘S011’, ‘C007’, 6, 5);Insert into AttendanceTable values (‘S012’, ‘C008’, 1, 1);
Hostel Table Data Insertion
Insert into HostelTable values (‘Violet’, 101, ‘S001’, 3000);Insert into HostelTable values (‘Violet’, 201, ‘S003’, 4000);Insert into HostelTable values (‘Violet’, 301, ‘S004’, 5000);Insert into HostelTable values (‘Indigo’, 100, ‘S002’, 3000);Insert into HostelTable values (‘Indigo’, 200, ‘S005’, 4000);Insert into HostelTable values (‘Indigo’, 300, ‘S006’, 5000);Insert into HostelTable values (‘Blue’, 102, ‘S007’, 3000);Insert into HostelTable values (‘Blue’, 202, ‘S008’, 4000);Insert into HostelTable values (‘Blue’, 302, ‘S009’, 5000);
Assignment 6: Sql Queries
1. List the name of different cities from where applicants belong to. Select City from ApplicantTable;
2. List the different semesters in which students have enrolled.Select Semester from CourseTable;
3. List the distinct InstructorId and courseid from CourseAllocation table.Select DISTINCT InstructorId, CourseId from CourseAllocation table;
4. List the name of course which are not electives.Select CourseName from CourseTable ORDER BY CourseName Having (Elective = = ‘N’);
5. List the name of instructor who has joined before 01-jan-2002Select InstructorName from InstructorTable ORDER BY DateOfJoining < 01-jan-2002;
6. List Id of courses whose ending date is before the current system date.Select CourseId from CourseAllocationTable where EndDate < Current SYSDATE;
7. List the name of applicant from Mysore and Bangalore.Select ApplicantName from ApplicantTable where City IN (‘Mysore’, ‘Bangalore’);
8. List the name of applicants who do not belong to Mysore.Select ApplicantName from ApplicantTable where City NOT IN (‘Mysore’);
9. List the name of semester 1 and semester 2 courses which belongs to branch B1.
Select CourseName from CourseTable where CourseId=BranchId AND Semester IN (‘1’, ‘2’);
10. List the name of courses which are elective and project marks is equal to 20.Select CourseName from CourseTable ORDER BY CourseName Having (Elective = = ‘Y’ AND ProjectMarks=20);
11. List the name of the course in which project marks is greater than the semester marks.Select CourseName from CourseTable where (ProjectMarks > SemesterExamMarks);
12. List the details of all the applicant who belong to Bangalore, Mysore or HyderabadSelect * from ApplicantTable where City IN (‘Mysore’, ‘Bangalore’, ‘Hyderabad’);
13. List the details of courses which are taught in semester 1, 2 or 3.Select * from CourseTable where Semester IN (‘1’, ‘2’, ‘3’);
14. List the StudentId who have secured marks between 80 to 100.Select StudentId from RegistrationTable where Marks BETWEEN 80 AND 100;
15. List the student details in ascending order of their ApplicantId.Select * from StudentTable ORDER BY ApplicantId;
16. List the details of instructor on ascending order of their date of joining.Select * from InstructorTable ORDER BY DateOfJoining;
17. List the details of instructor on descending order of department no and ascending order of joining date.Select * from InstructorTable ORDER BY DepartmentId DESC, DateOfJoining;
18. List the name of instructors starting with ‘R’ and ending with ‘n’Select InstructorName from InstructorTable where InstructorName LIKE (‘R% %n’);
19. List the details of instructors whose date of joining is in the month of March. Select * from InstructorTable GROUP BY DateOfJoining Having Month=‘Mar’;
20. List the details of applicant whose email id contains a character ‘_’Select * from ApplicantTable where EmailId LIKE (‘_’);
21. List the applicant who do not have email id in their applicant detail information.Select * from ApplicantTable where EmailId IS NULL;
22. List the course Id which has project Marks in its course detail information. Select CourseId from CourseTable where ProjectMarks IS NOT NULL;
Assignment 7: Sql Queries
Objective: Understanding a simple query
Problem Description: 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)
The solution is-:
SELECT ename FROM emp WHERE deptno NOT IN (30, 40, 10);
Let us explore how this query is working.
Step 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.
Step 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
Objective: To visualize how group by and having works
Problem Description: 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)
The solution is:
SELECT job, avg (sal) from emp where deptno=20 group by job having avg (sal) > 1000 order by job;Let us explore how this query is working.
Step 1: First thing first we have to select (filter) data only for dept 20. For this we have to put
WHERE deptno=20
Step 2: Now, we have to find average salary of the employee’s job wise. Therefore we have to group our selected (filtered) data using group by clause.
GROUP BY job
Step 3: Now, we have to display only those rows having an average salary > 1000. Therefore we have to put
HAVING avg (sal) > 1000
Step 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
Objective: To solve simple SQL queries
Problem Description:
Perform the following queries:
1. List the Branch Id and the number of courses offered by each Branch2. List the number of courses which do not have Project Marks3. List the Branch Id and the number of electives offered in each branch4. List the Course Id, average total marks obtained in each Course
Hint: Total marks= project marks + assignment marks + internal marks + semester marks
5. List the number of courses for which exam was conducted between1-Jan-2008 and 30-May-2008
6. List the Instructor Id, number of different Courses offered by an Instructor7. List the Course Id which is offered by more than one instructor8. List the Instructor Ids who have finished handling at least two courses during the year
2008
9. List the total strength of students staying in each Hostel10. List the total marks in each Semester of a Branch
Hint: total marks= project marks + assignment marks + internal marks + semester marks11. List the Student Id, Student Name if the student has an Email Id12. List the names of students currently studying in 5th semester13. List the names of students along with the course names for which they have registered14. List the name of the course and the number of ‘A’ grades obtained in that course15. 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.16. List the name of course, name of instructor, start date, end date if that course is allotted to
the instructor17. 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 hostel18. List the names of applicants and their User Id and Password. Display NULL for
User Id and Password if they don’t get admission19. List the instructor name, branch name, number of courses taught by the instructor in that
branch20. List the student name and course name in which they have scored ‘D’ grade21. List the course names which have duration less than or equal to 4 days22. List the name of the instructor and the course names they are teaching on ’27-Aug-2008’23. List the names of students who are not staying in Hostel24. List the student name, course name and the number of days present for each course
conducted25. List the names of courses handled by the instructors who are also head of departments26. List the names of applicants who got admission into Computer Science branch27. List the names of instructors who are handling elective courses
Assignment 10 : Sql queries
Objective: To visualize how an inner query works.
Problem Description:
Consider the following “Suppliers” table
Suppliers TableSNo SName Status CityS1 Smith 20 LondonS2 Jones 10 ParisS3 Blake 30 ParisS4 Clark 20 LondonS5 Adams 30 Athens
Let us consider the following query:
“Get supplier numbers for suppliers with a status lower than that of supplier S1”
The solution is:
SELECT SNo FROM Suppliers WHERE Status < (SELECT Status FROM Suppliers WHERE SNo=’S1’);
Let us explore how this query is working.
Step 1: The inner query is executed first which selects the status of supplier S1 from suppliers table.
Step 2: Now the query looks like this:
SELECT SNo FROM Suppliers WHERE Status < 20
Step 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.
Assignment 11: Sql Queries
Objective: To learn about the DCL statements.
Problem Description:
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.
Step 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;
Step 2: User_A grants Select privilege to User_C on DEPARTMENT table
GRANT SELECTON DepartmentTO User_CWITH GRANT OPTION;
Step 3: User_B grants Select, Update privileges to User_D on EMPLOYEE table
GRANT SELECT, UPDATEON EmployeeTO User_D;
Step 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 tableYes, User_B able to Update the DEPARTMENT table because of GRANT OPTION.
2. Whether User_D able to Update the EMPLOYEE tableYes, User_D able to Update the EMPLOYEE table because of GRANT OPTION.
3. Whether User_C able to Select the data from DEPARTMENT tableYes, User_C able to Select the data from DEPARTMENT table because of GRANT OPTION.
4. Whether User_E able to Select the data from DEPARTMENT tableYes, User_E able to Select the data from DEPARTMENT table because of GRANT OPTION.
5. Whether User_D able to grant Select, Update privileges to any UserNo, User_D 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
No, User_D not able to Select data from EMPLOYEE table after privileges are revoked by User_A
Assignment 12 : Sql Queries
Objective: To visualize how updating of data is possible on Views.
Problem Description: Perform the following steps and understand the possibilities and limitations in updating views.Step 1:Create EMPLOYEE table with the following description.
EMPLOYEE
EmpId Number(4) Primary KeyEmpName Varchar2(15) Cannot be NULLSalary Number(7,2)DateOfJoin Date
Step 2: Insert the below data into EMPLOYEE table
EmpId EmpName Salary DateOfJoin
1001 Suresh 50000 01-Jun-021002 Kumar 40000 25-Mar-031003 Vijay 70000 15-Aug-01
Step 3: Create a View EMPLOYEE_VIEW as below:
CREATE VIEW EMPLOYEE_VIEWAS SELECT *
FROMEMPLOYEE
Step 4: Execute the below query and see the data in view:SELECT *FROM EMPLOYEE_VIEW
Solution-: Step 1: Creating EMPLOYEE tableCreate table Employee (EmpId Number (4)Constraint pk_cid PRIMARY KEY,EmpName varchar2 (15)Constraint en_nn NOT NULL,Salary Number (7, 2),DateOfJoin Date);
Step 2: Insertion of data into EMPLOYEE table
Insert into Employee values (‘1001’,’Suresh’,’50000’,’01-Jun-02’);Insert into Employee values (‘1002’,’Kumar’,’4000’,’25-Mar-03’);Insert into Employee values (‘1003’,’Vijay’,’70000’,’15-Aug-01’);
Step 3: Create a View EMPLOYEE_VIEW as below:
CREATE VIEW EMPLOYEE_VIEWAS SELECT * FROM EMPLOYEE
EMPLOYEE_VIEW
Step 4: Execute the below query and see the data in view:SELECT *FROM EMPLOYEE_VIEW
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, data insertion is possible into the view without a column having PRIMARY KEY constraint.
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, data insertion is possible 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-: When we Drop EMPLOYEE table, it will automatically delete the view of the table.
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-: Recreating Employee Table-:Create table Employee (Emp_No Number (4),E_Name varchar2 (15),Salary_Emp Number (7, 2),DateOfJoin_Emp (Date));
Data Insertion-:Insert into Employee values (‘1001’,’Suresh’,’50000’,’01-Jun-02’);Insert into Employee values (‘1002’,’Kumar’,’4000’,’25-Mar-03’);Insert into Employee values (‘1003’,’Vijay’,’70000’,’15-Aug-01’);
SELECT * FROM EMPLOYEE_VIEW;
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-: DROP TABLE EMPLOYEE;
Creating EMPLOYEE tableCreate table Employee (EmpId Number (4)Constraint pk_cid PRIMARY KEY,EmpName varchar2 (15)Constraint en_nn NOT NULL,Salary Number (7, 2),DateOfJoin Date);
Insertion of data into EMPLOYEE table-:
Insert into Employee values (‘1001’,’Suresh’,’50000’,’01-Jun-02’);Insert into Employee values (‘1002’,’Kumar’,’4000’,’25-Mar-03’);Insert into Employee values (‘1003’,’Vijay’,’70000’,’15-Aug-01’);
Creating View-:
CREATE VIEW EMPLOYEE_VIEWAS SELECT * FROM EMPLOYEE
EMPLOYEE_VIEW
Selecting View-:
SELECT *FROM EMPLOYEE_VIEW