DBMS Practicals-Compressed

Download as pdf or txt
Download as pdf or txt
You are on page 1of 65

YASHWANTRAO CHAVAN MAHARASHTRA OPEN UNIVERSITY

SCHOOL OF COMPUTER SCIENCE

Certificate

This is to certify Mr. / Ms. MORE RIDDHI RAGHUNATH(PRN: _201901700344355_)

has successfully completed ____ / _____ practicals for the course code CMP-509 having

course name DATABASE MANAGEMENT SYSTEM of Semester IIIRD of BACHELOR

OF COMPUTER APPLICATIONS programme (programme code:P131) , during the

academic year 2020-2021.

_____________________________ __________________________
Name of the Counsellor Name of the SC Co-ordinator
Signature: ___________________ Signature: __________________
Date: ______________ Date: ______________

Seal of the Study Centre

_____________________________ __________________________
Name of the Internal Examiner Name of the External Examiner
Signature: ___________________ Signature: __________________
Date: ______________ Date: ______________

Stamp of the Exam Centre

YASHWANTRAO CHAVAN MAHARASHTRA OPEN UNIVERSITY


SCHOOL OF COMPUTER SCIENCE

Index
No. Practical Date of Page Signature of the
Activities/Programs/Assignments practical No. Counsellor and
performed date
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
SYBCA PRACTICAL NO.1 ROLL NO.25

Aim: To draw ER Model and Relational Model for a given database a) Case study 1:List the
data requirements for the database of the company which keeps track of the company
employee, department and projects. The database designers provide the following
description.

1. The company is organized into departments. Each department has unique name, unique
number, and particular employee to manage the department. We keep track of the start
date and the employee begins managing the department. The department has several
locations.

Answer:

DEPT_ID

MANAGE_DEPT
DEPT_NAME

DEPARTMENT

START_DATE
D_LOCAT

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.1 ROLL NO.25

2. The department controls a number of projects each of which has a unique name, unique
number and a single location.

Answer:

P_NAME

PROJECT P_ID
P_LOCAT

CONTROLLING_DEPT

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.1 ROLL NO.25

3. We store each employee names social security number , address , salary, sex and dob. An
employee is assigned one department but may work on several projects which are not
necessarily controlled by the same department. We keep track of the department of each
employee works on each project and for insurance purpose. We keep each dependents
first name, sex, dob and relation.

Answer:

SEX SSN
DOB

EMPLOYEE
DEPT_NAME ADDRESS

NAME
SALARY WORK_ON

F_NAME

L_NAME
PROJECT
HOURS_NEED

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.1 ROLL NO.25

b) Case study 2: Construct an E-R diagram for a hospital with a set of patients and a set of
medical doctors. Associate with each patient a log of the various tests and examinations
conducted. Also Construct appropriate tables for the ER Diagram.

Answer:

H_NAME SPECIALI D_NAME


H_ADD TY
R

HOSPITAL CONTAIN
S DOCTORS

H_PHO
H_ID START_
NE SAL
DATE
TREAT

ADMIT SUGGEST
T
D_NAME
P_NAM P_AGE T_NAM
E E

TEST
PATIENT DO

P_GEN P_WEI DATE_OF_TE TESET_AM


DER GHT ST OUNT

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.2 ROLL NO.25

Aim: Create one-to-many Relationship between Manager and Employee Relations Create
following Relations with the given fields

a) EMPLOYEE EmpId (PK), EmpName (Should be in the upper case), Department(Should


be Finance, Purchase or Sales) Salary, Mgrid

b) MANAGER Mgrid, MgrName, No. of Employees controlled Using above table solve the
following queries:

a. Display details of all those employees whose salary is higher than Rs.50

b. Display the details of employees who are working in Purchase department.

Code:

a).CREATE TABLE Employee1

emp_id int primary key,

empname char(10),

department char(15),

salary int,

manager_id references manager1(mgrid)

);

INSERT INTO Employee1

values(100,'Avni','Sales',50,14);

INSERT INTO Employee1

values(101,'Neil','Purchase',100,16);

INSERT INTO Employee1

values(102,'Shweta','Sales',70,13);

INSERT INTO Employee1

values(103,'Rajesh','Purchase',45,16);

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.2 ROLL NO.25

INSERT INTO Employee1

values(104,'Rita','Purchase',35,14);

SELECT * FROM Employee1;

Output:

b).CREATE TABLE manager1

mgrid int primary key,

mgrname char(15),

emp_controlled int

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.2 ROLL NO.25

);

INSERT INTO manager1

values(12,'Shrushti',10);

INSERT INTO manager1

values(13,'Pravin',12);

INSERT INTO manager1

values(14,'Anuj',16);

INSERT INTO manager1

values(15,'Pranali',7);

INSERT INTO manager1

values(16,'Amita',13);

SELECT * FROM manager1;

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.2 ROLL NO.25

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.2 ROLL NO.25

a. SELECT * FROM Employee1

WHERE salary > 50;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.2 ROLL NO.25

b. SELECT *FROM Employee1

WHERE department = 'Purchase';

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.2 ROLL NO.25

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.3 ROLL NO.25

Aim: Determine the functional dependencies. Remove partial dependency and transitive
dependencies in given table. ( i.e. convert it into 3NF).

Student = (RollNo, Name, Course_Code, Course_Name, Fees)

ROLL NO NAME COURSE_CODE COURSE_NAME FEES


123 Ravi C102 C 2500
123 Ravi C103 C++ 1200
123 Ravi C104 OOP’s 3200
124 Sumit C102 C 2500
124 Sumit C103 C++ 1200
125 Trupta C102 C 2500
125 Trupta C103 C++ 1200
125 Trupta C104 OOP’s 3200

Answer: 3rd Normal Form :

COURSE_CODE COURSE_NAME FEES


C102 C 2500
C103 C++ 1200
C104 OOP’s 3200

ROLL NO NAME COURSE_CODE


123 Ravi C102
123 Ravi C103
123 Ravi C104
124 Sumit C102
124 Sumit C103
125 Trupta C102
125 Trupta C103
125 Trupta C104

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.4 ROLL NO.25

Aim: Creation of Database and table-DDL COMMAND .Create a table called EMP with the
following structure.

Name Type

EMPNO NUMBER(6)

ENAME VARCHAR2(20)

JOB VARCHAR2(10)

DEPTNO NUMBER(3)

SAL NUMBER(7,2)

Allow NULL for all columns except ENAME and JOB.

a) -Add a column experience to the emp table. experience numeric null allowed.

b) Modify the column width of the job field of emp table.

Create dept table with the following structure.

Name Type

DEPTNO NUMBER(2)

DNAME VARCHAR2(10)

LOC VARCHAR2(10)

DEPTNO as the primary key

a) Create the emp1 table with ename and empno, add constraints to check the empnovalue
while entering (i.e) empno> 100.

b) Drop a column experience to the emp table.

Code:

CREATE TABLE Emp2

Empno number(6),

Ename varchar2(20) NOT NULL,

Jobname varchar2(10) NOT NULL,

Deptno number(3),

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.4 ROLL NO.25

Salary number(7,2)

);

DESC Emp2;

a). ALTER TABLE Emp2

ADD experience number(6);

DESC Emp2;

INSERT INTO Emp2 values(101,'AAA','Lecturer',01,45000.00,5);

INSERT INTO Emp2 values(11,'BBB','Sales',02,10000.00,2);

INSERT INTO Emp2 values(20,'CCC','ASP',03,20000.00,7);

INSERT INTO Emp2 values(300,'DDD','Lecturer',04,65000.00,10);

INSERT INTO Emp2 values(65,'EEE','ASP',05,78000.00,3);

DESC Emp2;

SELECT * FROM Emp2;

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.4 ROLL NO.25

Output:

b). ALTER TABLE Emp2 MODIFY(Jobname varchar2(20));

DESC Emp2;

CREATE TABLE Dept1

Deptno number(2) primary key,

Dname varchar2(10),

Loc varchar2(10)

);

DESC Dept1;

INSERT INTO Dept1 values(01,'HR','Borivali');

INSERT INTO Dept1 values(02,'Admin','Goregaon');

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.4 ROLL NO.25

INSERT INTO Dept1 values(03,'Manager','Dadar');

INSERT INTO Dept1 values(04,'Project_Mg','Matunga');

INSERT INTO Dept1 values(05,'Finance','CST');

SELECT * FROM Dept1;

Ouput:

a). CREATE TABLE Emp01

Deptno number(2),

Ename varchar2(10),

Empno number(6),

CHECK (Empno > 100)

);

INSERT INTO Emp01 values(01,'AAA',101);

INSERT INTO Emp01 values(02,'BBB',117);

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.4 ROLL NO.25

INSERT INTO Emp01 values(03,'CCC',201);

INSERT INTO Emp01 values(04,'DDD',300);

INSERT INTO Emp01 values(05,'EEE',650);

SELECT * FROM Emp01;

Output:

b) .ALTER TABLE Emp2 DROP COLUMN experience;

DESC Emp2;

SELECT * FROM Emp2;

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.4 ROLL NO.25

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.5 ROLL NO.25

Aim: Simple SQL Query-1-DML COMMAND

A] Write syntax of all DML command.

B] Create database where create following tables: Emp(EMPNO int, ENAME


VARCHAR(20),JOB VARCHAR(10),DEPTNO int,SAL numeric(7,2)) Allow NULL for all
columns except ename and job. Dept(DEPTNO int, DNAME VARCHAR(10),LOC
VARCHAR(10)) Deptno as the primarykey.

Insert at least 8 records into tables and solve the following given queries using Emp and Dept
table:

a) Create the emp1 table with ename and empno, add constraints to check the empno value
while entering (i.e) empno> 100.

b) Update the emp table to set the salary of all employees to Rs15000/- who are working as
ASP.

c) Delete only those who are working as lecturer.

d) List the records in the emp table orderby salary in ascending order.

e) Display total salary spent for each job category.

f) Add constraints to the emp table that empno as the primary key and deptno as the foreign
key.

g) Add columns DOB to the emp table.

Code:

A]. SELECT –Syntax: SELECT expressions


FROM tables
WHERE conditions;
INSERT – Syntax: INSERT INTO table_name
Values(‘’);
UPDATE – Syntax: UPDATE table_name
SET column_name = expression
WHERE conditions;
DELETE – Syntax: DELETE FROM table_name
WHERE conditions;

B]. CREATE TABLE Emp2

Empno number(6),

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.5 ROLL NO.25

Ename varchar2(20) NOT NULL,

Jobname varchar2(10) NOT NULL,

Deptno number(3),

Salary number(7,2)

);

INSERT INTO Emp2 values(101,'AAA','Lecturer',01,45000.00);

INSERT INTO Emp2 values(11,'BBB','Sales',02,10000.00);

INSERT INTO Emp2 values(20,'CCC','ASP',03,20000.00);

INSERT INTO Emp2 values(300,'DDD','Lecturer',04,65000.00);

INSERT INTO Emp2 values(65,'EEE','ASP',05,78000.00);

INSERT INTO Emp2 values(100,'Anil','ASP',06,11111.11);

INSERT INTO Emp2 values(106,'Heena','Marketing',07,44444.44);

INSERT INTO Emp2 values(107,'Iry','Purchase',08,11111.11);

Output:

CREATE TABLE Dept1

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.5 ROLL NO.25

Deptno number(2) primary key,

Dname varchar2(10),

Loc varchar2(10)

);

INSERT INTO Dept1 values(01,'HR','Borivali');

INSERT INTO Dept1 values(02,'Admin','Goregaon');

INSERT INTO Dept1 values(03,'Manager','Dadar');

INSERT INTO Dept1 values(04,'Project_Mg','Matunga');

INSERT INTO Dept1 values(05,'Finance','CST');

INSERT INTO Dept1 values(06,'Accounts','Andheri');

INSERT INTO Dept1 values(07,'General','Malad');

INSERT INTO Dept1 values(08,'Manufact','Sion');

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.5 ROLL NO.25

a). CREATE TABLE Emp01

(
Deptno number(2),
Ename varchar2(10),
Empno number(6),
CHECK (Empno > 100)
);

INSERT INTO Emp01 values(01,'AAA',101);


INSERT INTO Emp01 values(02,'BBB',117);
INSERT INTO Emp01 values(03,'CCC',201);
INSERT INTO Emp01 values(04,'DDD',30);
INSERT INTO Emp01 values(05,'EEE',650);

SELECT * FROM Emp01;


Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.5 ROLL NO.25

b). UPDATE Emp2 SET Salary = 15000 WHERE Jobname = 'ASP';

SELECT *FROM Emp2;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.5 ROLL NO.25

c). DELETE FROM Emp2 WHERE Jobname = 'Lecturer';

SELECT *FROM Emp2;

Output:

d). SELECT * FROM Emp2 ORDER BY Salary;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.5 ROLL NO.25

e). SELECT Jobname,SUM(Salary) "TOTAL_SALARY" FROM Emp2 GROUP BY


Jobname;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.5 ROLL NO.25

f) . ALTER TABLE Emp2 ADD PRIMARY KEY(Empno);

ALTER TABLE Emp2 ADD FOREIGN KEY(Deptno) REFERENCES Dept1(Deptno);

g). ALTER TABLE Emp2 ADD DOB date ;

INSERT INTO Emp2 values(101,'AAA','Lecturer',01,45000.00,’ 01-02-2000’);

INSERT INTO Emp2 values(11,'BBB','Sales',02,10000.00,’ 03-04-2001’);

INSERT INTO Emp2 values(20,'CCC','ASP',03,20000.00,’ 05-12-2002’);

INSERT INTO Emp2 values(300,'DDD','Lecturer',04,65000.00,’ 11-08-2003’);

INSERT INTO Emp2 values(65,'EEE','ASP',05,78000.00,’ 11-23-2004’);

INSERT INTO Emp2 values(100,'Anil','ASP',06,11111.11,’ 09-30-2000’);

INSERT INTO Emp2 values(106,'Heena','Marketing',07,44444.44,’ 03-15-2001’);

INSERT INTO Emp2 values(107,'Iry','Purchase',08,11111.11,’ 01-20-2002’);

SELECT * FROM Emp2;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.6 ROLL NO.25

Aim: Simple SQL Query 2: SQL Functions

a) List all the aggregate functions with example?

b) List all the string functions with example?

c) Using above Emptable solve the following queries:

1. Display all the details of the records whose employee name starts with ‘A’.

2. Display all the details of the records whose employee name does not start with ‘A’.

3. Calculate the total and average salary amount of the emp table.

4. Determine the max and min salary and rename the column as max_salary and min_salary.

5. Find how many job titles are available in employee table.

6. Count the total records in the emp table.

Code:

a).1. The SUM() function returns the total sum of a numeric column.

SELECT SUM(Salary) FROM Emp2;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.6 ROLL NO.25

2. The AVG() function returns the average value of a numeric column.

SELECT AVG(Salary) FROM Emp2;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.6 ROLL NO.25

3. The MAX() function returns the largest value of the selected column.

SELECT MAX(Salary) FROM Emp2;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.6 ROLL NO.25

4. The MIN() function returns the smallest value of the selected column.

SELECT MIN(Salary) FROM Emp2;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.6 ROLL NO.25

5. The COUNT() function returns the number of rows that matches a specified criterion.

SELECT COUNT(Salary) FROM Emp2;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.6 ROLL NO.25

b). 1. This function is used to convert the lower case string into upper case.

SELECT UPPER(Ename) FROM Emp2;

Output:

2. This function is used to convert the upper case string into lower case.

SELECT LOWER(Ename) FROM Emp2;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.6 ROLL NO.25

3. This function is used to add two words or strings.

SELECT CONCAT(Deptno,Ename) FROM Emp2;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.6 ROLL NO.25

4. The initcap function converts the first letter of each word in a string to uppercase, and
converts any remaining characters in each word to lowercase.

SELECT INITCAP(Ename) FROM Emp2;

Output:

c). 1 . SELECT * FROM Emp2

WHERE Ename LIKE 'a%';

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.6 ROLL NO.25

2. SELECT * FROM Emp2

WHERE Ename NOT LIKE 'a%';

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.6 ROLL NO.25

3. SELECT SUM(Salary) FROM Emp2;

Output:

SELECT AVG(Salary) FROM Emp2;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.6 ROLL NO.25

4. SELECT MIN(Salary) "min_salary",MAX(Salary) "max_salary"

FROM Emp2;

Output:

5. SELECT COUNT(DISTINCT Jobname)

FROM EMP2;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.6 ROLL NO.25

6. SELECT COUNT(Empno)"TOTAL RECORDS"

FROM EMP2;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.6 ROLL NO.25

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.7 ROLL NO.25

Aim : Advanced SQL queries using Set Operations.

a) List all the set operators?

b) Using above Emp table solve the following queries:

1. Display all the dept numbers available with the dept and emp tables avoiding duplicates.

2. Display all the dept numbers available with the dept and emp tables.

3. Display all the dept numbers available in emp and not in dept tables and vice versa.

Code:

a). 1. The UNION operator is used to combine the result-set of two or more SELECT
statements.

2. The UNION ALL operator selects only distinct values by default.

3. The MINUS set operator removes the second query's results from the output if they are
also found in the first query's results.

b).1.SELECT deptno FROM Emp2

UNION

SELECT deptno FROM Dept1;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.7 ROLL NO.25

2. SELECT deptno FROM Emp2

UNION ALL

SELECT deptno FROM Dept1;

Output:

3. SELECT deptno FROM Emp2

MINUS

SELECT deptno FROM Dept1;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.7 ROLL NO.25

SELECT deptno FROM Dept1

MINUS

SELECT deptno FROM Emp2;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.8 ROLL NO.25

Aim: Advanced SQL queries using Sub query.

Using aboveEmp table solve the following queries:

1. Display all employee names and salary whose salary is greater than minimum salary of the
company and job title starts with ‘M’.

2. Issue a query to find all the employees who work in the same job as Arjun.

3. Issue a query to display information about employees who earn more than any employee in
dept.

Code:

1.SELECT Ename ,Salary ,Jobname FROM Employee WHERE Salary >

(SELECT MIN(Salary) FROM Employee) AND Jobname LIKE 'M%';

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.8 ROLL NO.25

2. SELECT Ename FROM Employee

WHERE Jobname = (SELECT Jobname FROM Employee WHERE Ename = 'Arjun');

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.8 ROLL NO.25

3. SELECT * FROM Employee

WHERE Salary >(SELECT Salary FROM Employee WHERE Ename = 'BBB');

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.9 ROLL NO.25

Aim: Advanced SQL queries using JOINS.

a) What is joins? List types of joins with syntax.

b) Using aboveEmp table solve the following queries:

1. Display the employee details, departments that the departments are same in both the
emp and dept.

2. Display all the employees and the departments implementing a left outer join.

3. Display the employee name and department name in which they are working
implementing a right outer join.

4. Display the employee name and department name in which they are working
implementing a full outer join.

Code:

a). A SQL Join statement is used to combine data or rows from two or more tables based on
a common field between them. Different types of Joins are:
• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN

1.INNER JOIN: The INNER JOIN keyword selects all rows from both the tables as long
as the condition satisfies. This keyword will create the result-set by combining all rows
from both the tables where the condition satisfies i.e value of the common field will be
same.

Syntax:

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables.

2. LEFT JOIN: This join returns all the rows of the table on the left side of the join and
matching rows for the table on the right side of join. The rows for which there is no

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.9 ROLL NO.25

matching row on right side, the result-set will contain null. LEFT JOIN is also known as
LEFT OUTER JOIN.

Syntax:

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables.

3. RIGHT JOIN: RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of
the table on the right side of the join and matching rows for the table on the left side of join.
The rows for which there is no matching row on left side, the result-set will contain null.
RIGHT JOIN is also known as RIGHT OUTER JOIN.

Syntax:

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.
table2: Second table
matching_column: Column common to both the tables.

4. FULL JOIN: FULL JOIN creates the result-set by combining result of both LEFT JOIN
and RIGHT JOIN. The result-set will contain all the rows from both the tables. The rows
for which there is no matching, the result-set will contain NULL values.

Syntax:

SELECT table1.column1,table1.column2,table2.column1,....

FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
table1: First table.

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.9 ROLL NO.25

table2: Second table


matching_column: Column common to both the tables.

5. SELF JOIN: As the name signifies, in SELF JOIN a table is joined to itself. That is,
each row of the table is joined with itself and all other rows depending on some conditions.
In other words we can say that it is a join between two copies of the same table.

Syntax:

SELECT a.coulmn1 , b.column2


FROM table_name a, table_name b
WHERE some_condition;
table_name: Name of the table.
some_condition: Condition for selecting the rows.

b). CREATE TABLE dept


(
deptno int primary key,
dname char(20),
loc char(20)
);

INSERT INTO dept values(01,'Sales','Matunga');


INSERT INTO dept values(02,'Marketing','Goregaon');
INSERT INTO dept values(03,'Lecturer','Dadar');
INSERT INTO dept values(04,'ASP','Bandra');
INSERT INTO dept values(05,'HR','Borivali');

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.9 ROLL NO.25

CREATE TABLE emp


(
eid int PRIMARY KEY,
ename char(20),
DeptNo int,
FOREIGN KEY (DeptNo) REFERENCES dept(deptno)
);

INSERT INTO emp values(100,'AAA',01);

INSERT INTO emp values(101,'BBB',05);

INSERT INTO emp values(102,'CCC',02);

INSERT INTO emp values(104,'EEE',03);

INSERT INTO emp values(105,'FFF',05);

INSERT INTO emp values(106,'GGG',02);

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.9 ROLL NO.25

1. SELECT eid,ename,dname
FROM emp
INNER JOIN dept
ON emp.DeptNo = dept.deptno;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.9 ROLL NO.25

2. SELECT eid,ename,dname,loc
FROM emp
LEFT OUTER JOIN dept
ON emp.Deptno = dept.deptno;

Output:

3. SELECT eid,ename,dname,loc
FROM emp
RIGHT OUTER JOIN dept
ON emp.DeptNo = dept.deptno;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.9 ROLL NO.25

4. SELECT eid,ename,dname,loc
FROM emp
FULL OUTER JOIN dept
ON emp.DeptNo = dept.deptno;

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.9 ROLL NO.25

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.10 ROLL NO.25

Aim: Advanced SQL queries using PL-SQL. Solve the following PL-SQL programs.

A) Write a pl/sql program to swap two numbers.

B) Write a pl/sql program to find the largest of two numbers.

Code:

A]. delcare

a number;

b number;

temp number;

begin

a:= 50;

b:= 60;

dbms_output.put_line('Before swapping: ');

dbms_output.put_line('a=' || a || 'b=' || b);

temp:= a;

a:= b;

b:= temp;

dbms_output.put_line('After swapping: ');

dbms_output.put_line('a=' || a || 'b=' || b);

end;

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.10 ROLL NO.25

B]. declare

a number;

b number;

begin

a:= 50;

b:= 60;

dbms_output.put_line('a=' || a || 'b=' || b);

IF a>b THEN

dbms_output.put_line('a is greater than b');

ELSE

dbms_output.put_line('b is greater than a');

END IF;

end;

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.10 ROLL NO.25

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.11 ROLL NO.25

Aim: Advanced SQL queries using PROCEDURE AND FUNCTION .

A] Write syntax of Procedure and Function.

B] Create a procedure to print the odd numbers from 1 to 10.

Code:

A]. Syntax of Procedure :

CREATE [OR REPLACE] PROCEDURE procedure_name

[(parameter_name [IN | OUT | IN OUT]type [,…])]

{IS | AS}

BEGIN

<procedure_body>

END;

Syntax of Function :

CREATE [OR REPLACE] PROCEDURE procedure_name

[(parameter_name [IN | OUT | IN OUT]type [,…])]

RETURN return_datatype

{IS | AS}

BEGIN

<function_body>

END function_name;

B]. declare

a number;

begin

a:= 25;

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.11 ROLL NO.25

if mod(a,2) = 0 then

dbms_output.put_line('Value of a is even');

else

dbms_output.put_line('Value of a is odd');

end if;

end;

Output:

declare

a number;

begin

a:= 24;

if mod(a,2) = 0 then

dbms_output.put_line('Value of a is even');

else

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.11 ROLL NO.25

dbms_output.put_line('Value of a is odd');

end if;

end;

Output:

declare

a number;

begin

a:= 1;

while a <= 10

loop

dbms_output.put_line('odd numbers are: ' || a);

a:= a+2;

end loop;

end;

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.11 ROLL NO.25

Output:

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.12 ROLL NO.25

Aim: Write a pl/sql program to find the total and average of 6 subjects and display the grade.

Code:

declare

js number(10);

dbms number(10);

cn number(10);

ds number(10);

c number(10);

html number(10);

total number(10);

per number(10);

begin

js:= 77;

dbms_output.put_line('Marks of js:' || js);

dbms:= 70;

dbms_output.put_line('Marks of dbms:' || dbms);

cn:= 68;

dbms_output.put_line('Marks of cn:' || cn);

ds:= 80;

dbms_output.put_line('Marks of ds:' || ds);

c:= 90;

dbms_output.put_line('Marks of c:' || c);

html:= 85;

dbms_output.put_line('Marks of html:' || html);

total:= js+dbms+cn+ds+c+html;

dbms_output.put_line('Total marks are:' || total);

per:= (total/600) * 100;

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.12 ROLL NO.25

dbms_output.put_line('Percentage :' || per);

if per<=45 then

dbms_output.put_line('Grade F');

else if per<=60 and per>=45 then

dbms_output.put_line('Grade 'D);

else if per<=70 and per>=60 then

dbms_output.put_line('Grade C');

else if per<=80 and per>=70then

dbms_output.put_line('Grade B');

else if per>=90 then

dbms_output.put_line('Grade A');

else

dbms_output.put_line('Invalid marks');

end if;

end if;

end if;

end if;

end if;

end;

DATABASE MANAGEMENT SYSTEM


SYBCA PRACTICAL NO.12 ROLL NO.25

Output:

DATABASE MANAGEMENT SYSTEM

You might also like