0% found this document useful (0 votes)
2 views9 pages

Create the Following Tables for University Database

The document outlines the creation of a university database with tables for Department, Course, Instructor, Section, and Teaches, including their respective fields and relationships. It includes SQL commands for creating these tables, inserting data, and performing various queries to retrieve information about instructors, courses, and salaries. Additionally, it demonstrates data manipulation, such as deleting instructors based on salary criteria.

Uploaded by

kesavat0001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Create the Following Tables for University Database

The document outlines the creation of a university database with tables for Department, Course, Instructor, Section, and Teaches, including their respective fields and relationships. It includes SQL commands for creating these tables, inserting data, and performing various queries to retrieve information about instructors, courses, and salaries. Additionally, it demonstrates data manipulation, such as deleting instructors based on salary criteria.

Uploaded by

kesavat0001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Create the following tables for university database

Department(deptname,building,budget,primarykeydeptname))

Course (courseid, title, deptname, credits, primary key (course id))

Instructor(ID,name,deptname,salary,primarykey(ID),foreignkey(deptname)references department);

Section (courseid, sec id, semester, year, building, room number, time slot id, primary key (courseid, sec
id, semester, year), foreign key (course id) references course);

Teaches (ID, course id, sec id, semester, year numeric, primary key (ID, course id, sec id, semester,year),
foreign key (course id, sec id, semester, year) references section, foreign key (ID) references instructor);

SQL> CREATE TABLE Department (

deptname VARCHAR(10) PRIMARY KEY,

building VARCHAR(10),

budget DECIMAL(10, 2)

);

Table created.

SQL> CREATE TABLE Course (

courseid INT PRIMARY KEY,

title VARCHAR(10),

deptname VARCHAR(10),

credits INT,

FOREIGN KEY (deptname) REFERENCES Department(deptname)

);

Table created.

SQL> CREATE TABLE Instructor (

ID INT PRIMARY KEY,


name VARCHAR(10),

deptname VARCHAR(10),

salary DECIMAL(10, 2),

FOREIGN KEY (deptname) REFERENCES Department(deptname)

);

Table created.

SQL> CREATE TABLE Section (

courseid INT,

sec_id INT,

semester VARCHAR(10),

year INT,

building VARCHAR(10),

room_number VARCHAR(10),

time_slot_id INT,

PRIMARY KEY (courseid, sec_id, semester, year),

FOREIGN KEY (courseid) REFERENCES Course(courseid)

);

Table created.

SQL> CREATE TABLE Teaches (

ID INT,

courseid INT,

sec_id INT,

semester VARCHAR(10),

year INT,

PRIMARY KEY (ID, courseid, sec_id, semester, year),


FOREIGN KEY (courseid, sec_id, semester, year) REFERENCES Section(courseid, sec_id, semester,
year),

FOREIGN KEY (ID) REFERENCES Instructor(ID)

);

Table created.

SQL> INSERT INTO Department (deptname, building, budget) VALUES ('CSE', 'Engineer', 500000);

1 row created.

SQL> INSERT INTO Department (deptname, building, budget) VALUES ('ECE', 'TechCenter', 300000);

1 row created.

SQL> INSERT INTO Department (deptname, building, budget) VALUEs ('ME', 'Industrial', 250000);

1 row created.

SQL> select * from department;

DEPTNAME BUILDING BUDGET

---------- ---------- ----------

CSE Engineer 500000

ECE TechCenter 300000

ME Industrial 250000

SQL> INSERT INTO Course (courseid, title, deptname, credits) VALUES (101, 'DataStruct', 'CSE', 3);

1 row created.

SQL> INSERT INTO Course (courseid, title, deptname, credits) VALUES (102, 'Circuits', 'ECE', 4);

1 row created.

SQL> INSERT INTO Course (courseid, title, deptname, credits) VALUES (103, 'Tdynamics', 'ME', 3);

1 row created.

SQL> INSERT INTO Course (courseid, title, deptname, credits) VALUES (104, 'Algorithms', 'CSE', 3);

1 row created.

SQL> select * from course;


COURSEID TITLE DEPTNAME CREDITS

---------- ---------- ---------- ----------

101 DataStruct CSE 3

102 Circuits ECE 4

103 Tdynamics ME 3

104 Algorithms CSE 3

SQL> INSERT INTO Instructor (ID, name, deptname, salary) VALUES (1, 'Gowtham', 'CSE', 75000.00);

1 row created.

SQL> INSERT INTO Instructor (ID, name, deptname, salary) VALUES (2, 'John', 'ECE', 68000.00);

1 row created.

SQL> INSERT INTO Instructor (ID, name, deptname, salary) VALUES (3, 'William', 'ME', 72000.00);

1 row created.

SQL> INSERT INTO Instructor (ID, name, deptname, salary) VALUES (4,'Madhuri','ECE', 80000.00);

1 row created.

SQL> INSERT INTO Instructor (ID, name, deptname, salary) VALUES (5,'Murali','ME', 30000.00);

1 row created.

SQL> select * from instructor;

ID NAME DEPTNAME SALARY

---------- ---------- ---------- ----------

1 Gowtham CSE 75000

2 John ECE 68000

3 William ME 72000

4 Madhu ECE 80000

5 Murali ME 30000
SQL> INSERT INTO Section (courseid, sec_id, semester, year, building, room_number, time_slot_id)
VALUES

(101, 1, 'odd', 2023, 'Engineer', '101', 1);

1 row created.

SQL> INSERT INTO Section (courseid, sec_id, semester, year, building, room_number, time_slot_id)
VALUES

(102, 1, 'even', 2023, 'Engineer', '102', 2);

1 row created.

SQL> INSERT INTO Section (courseid, sec_id, semester, year, building, room_number, time_slot_id)
VALUES

(103, 1, 'even', 2024, 'Industrial', '103', 3);

1 row created.

SQL> INSERT INTO Section (courseid, sec_id, semester, year, building, room_number, time_slot_id)
VALUES

(103, 1, 'odd', 2023, 'Techcenter', '102', 2);

1 row created.

SQL> select * from section;

COURSEID SEC_ID SEMESTER YEAR BUILDING ROOM_NUMBE TIME_SLOT_ID

---------- ---------- ---------- ---------- ---------- ---------- ------------

101 1 odd 2023 Engineer 101 1

102 1 even 2023 Engineer 102 2

103 1 even 2024 Industrial 103 3

103 1 odd 2023 Techcenter 102 2

SQL> INSERT INTO Teaches (ID, courseid, sec_id, semester, year) VALUES

(1, 101, 1, 'odd', 2023);

1 row created.
SQL> INSERT INTO Teaches (ID, courseid, sec_id, semester, year) VALUES

(2, 102, 1, 'even', 2023);

1 row created.

SQL> INSERT INTO Teaches (ID, courseid, sec_id, semester, year) VALUES

(3, 103, 1, 'even', 2024);

1 row created.

SQL> INSERT INTO Teaches (ID, courseid, sec_id, semester, year) VALUES

(1, 103, 1, 'odd', 2023);

1 row created.

SQL> select * from teaches;

ID COURSEID SEC_ID SEMESTER YEAR

---------- ---------- ---------- ---------- ----------

1 101 1 odd 2023

2 102 1 even 2023

3 103 1 even 2024

1 103 1 odd 2023

a.List the names of instructors along with the titles of courses that they teach

SQL> SELECT I.name, C.title

FROM Instructor I

JOIN Teaches T ON I.ID = T.ID

JOIN Course C ON T.courseid = C.courseid;

NAME TITLE

---------- ----------
Gowtham DataStruct

John Circuits

Gowtham Tdynamics

William Tdynamics

b.Find the names of all instructors whose salary is greater than atleast one instructor in the CSE
department

SQL> SELECT DISTINCT I1.name

FROM Instructor I1

WHERE I1.salary > (SELECT MAX(I2.salary) FROM Instructor I2 WHERE I2.deptname = 'CSE');

NAME

----------

Madhuri

c.To find the set of all courses taught in the Even as well as in odd semester

SQL> SELECT DISTINCT C.title

FROM Course C

JOIN Section S ON C.courseid = S.courseid

WHERE S.semester IN ('even', 'odd');

TITLE

----------

DataStruct

Circuits

Tdynamics

d.Find the average salary of instructors in the Computer Science department


SQL> SELECT AVG(salary) AS average_salary

FROM Instructor

WHERE deptname = 'CSE';

AVERAGE_SALARY

--------------

75000

e.Find the average salary in each department

SQL> SELECT deptname, AVG(salary) AS average_salary

FROM Instructor

GROUP BY deptname;

DEPTNAME AVERAGE_SALARY

---------- --------------

CSE 75000

ME 51000

ECE 74000

f. To find all the courses taught in the Even semester in current year but not in previous year in the same
semester

g. Delete all instructors with a salary between15,000 and 35,000.

SQL> DELETE FROM Instructor

WHERE salary BETWEEN 15000 AND 35000;


1 row deleted.

SQL> select * from instructor;

ID NAME DEPTNAME SALARY

---------- ---------- ---------- ----------

1 Gowtham CSE 75000

2 John ECE 68000

3 William ME 72000

4 Madhu ECE 80000

You might also like