You are on page 1of 8

MTL710 ASSIGNMENT

GURRAM LOKESH (2012EE10455)

A basic university database is created. It has four relations: students, courses, departments and
registrations. Commands used to create these relations and the generated schema are given below.

CREATE TABLE students


(
id
INT NOT NULL,
firstname VARCHAR(20) NOT NULL,
lastname VARCHAR(20) NOT NULL,
age
INT NOT NULL,
address
CHAR(25),
phone
DECIMAL(10, 0),
PRIMARY KEY (id)
);

CREATE TABLE courses


(
id
INT NOT NULL,
NAME
VARCHAR (20) NOT NULL,
code
VARCHAR (10) NOT NULL,
slot
CHAR (1) NOT NULL,
departmentid INT NOT NULL,
credits
INT NOT NULL,
PRIMARY KEY (id)
)

CREATE TABLE department


(
id
INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
code VARCHAR (4) NOT NULL,
PRIMARY KEY (id)
)
;
CREATE TABLE registrations
(
id
INT NOT NULL,
studentid INT NOT NULL,
courseid INT NOT NULL,
PRIMARY KEY (id)
);

1
MTL710 : Course Assignment

DESC command is used to describe the relation


DESC students
NAME
NULL
--------- -------id
NOT NULL
firstname NOT NULL
lastname NOT NULL
age
NOT NULL
address
phone

type
-----------number(38)
varchar2(20)
varchar2(20)
number(38)
char(25)
number(10)

DESC courses
Name
NULL?
type
----------------------------------------- -------- --------------------------id
NOT NULL number(38)
NAME
NOT NULL varchar2(20)
code
NOT NULL varchar2(10)
slot
NOT NULL char(1)
departmentid
NOT NULL number(38)
credits
NOT NULL number(38)
DESC
NAME
---id
NAME
code

department
NULL
type
-------- -----------NOT NULL number(38)
NOT NULL varchar2(20)
NOT NULL varchar2(4)

DESC registrations
NAME
NULL
type
--------- -------- ---------id
NOT NULL number(38)
studentid NOT NULL number(38)
courseid NOT NULL number(38)

2
MTL710 : Course Assignment

INSERT command is used to enter the data into these relations.

3
MTL710 : Course Assignment

Final Database:
SELECT command is used to get all the rows in a relation.

4
MTL710 : Course Assignment

1. Nested SQL Query


Query to print first name of students registered in Course -6

SELECT firstname
FROM
students
WHERE id IN (SELECT studentid
FROM
registrations
WHERE courseid = 6);

Output:

5
MTL710 : Course Assignment

2. Update Command
A Query is written to update the address of student named Aditya to CTP-India
UPDATE students
SET
address = 'CTP-INDIA'
WHERE firstname = 'ADITYA';

Output:

3. Create a View
A Query is written to create a View containing students contact numbers
CREATE VIEW contacts_view
AS
SELECT firstname,
lastname,
phone
FROM
students;

Result:

6
MTL710 : Course Assignment

4. PL/SQL Program to illustrate Exception


A PL/SQL program that displays name and mobile number of a student on online entry of STUDENT
ID. If no student with that ID exists an exception is thrown
DECLARE
s_fname students.firstname%TYPE;
s_lname students.lastname%TYPE;
s_phone students.phone%TYPE;
BEGIN
SELECT firstname,
lastname,
phone
INTO
s_fname, s_lname, s_phone
FROM
students
WHERE id = &id;
dbms_output.Put_line ('Name: '
|| s_fname
|| ' '
|| s_lname);
dbms_output.Put_line ('Phone No: '
|| s_phone);
EXCEPTION
WHEN no_data_found THEN
dbms_output.Put_line('No such Student!');
WHEN OTHERS THEN
dbms_output.Put_line('Error!');
END;

Output:
Case 1: No exception

Case 2: Exception

7
MTL710 : Course Assignment

5. PL/SQL program to compute the total


A PL/SQL program is written to calculate total number of credits a student is registered for.
DECLARE
student_id students.id%TYPE;
cr
courses.credits%TYPE;
crtotal
courses.credits%TYPE;
CURSOR s_courses IS
SELECT credits
FROM
courses
WHERE id IN (SELECT courseid
FROM
registrations
WHERE studentid = &id);
BEGIN
dbms_output.ENABLE;
crtotal := 0;
OPEN s_courses;
LOOP
FETCH s_courses INTO cr;
EXIT WHEN s_courses%NOTFOUND;
crtotal := cr + crtotal;
END LOOP;
dbms_output.Put_line('registered credits : '
|| ' '
|| crtotal);
CLOSE s_courses;
END;

Output:

8
MTL710 : Course Assignment

You might also like