0% found this document useful (0 votes)
57 views92 pages

432 - DBMS - Kishankumar Goud

This document discusses creating and managing tables in a database including constraints. It defines constraints as limits on data inserted, updated or deleted from tables to

Uploaded by

Kajal Goud
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)
57 views92 pages

432 - DBMS - Kishankumar Goud

This document discusses creating and managing tables in a database including constraints. It defines constraints as limits on data inserted, updated or deleted from tables to

Uploaded by

Kajal Goud
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/ 92

Name.

Kishan Kumar Goud


Roll no. 432 SYBSC.IT

PRACTICAL NO .01
Manipulating Data

AIM: a) Using INSERT statement


b) Using DELETE statement
c) Using UPDATE statement

1. CREATING TABLE:

 Syntax:
create table tablename
(column1 datatype(size),
column2 datatype(size),
…….
columnN datatype(size)
);
OR
create table tablename(column1 datatype(size), column2 datatype(size),
…..columnN datatype(size));

2. DESCRIBING TABLE:

 Syntax:
describe tablename;
OR
desc tablename;

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

3.INSERTING RECORDS INTO TABLE:

 Syntax:
insert into tablename values(data1, data2,…..dataN);

OR
insert into tablename values(data1,
data2,
……,
dataN);

4. DISPLAYING DATA OF TABLE:

 Syntax:
a) For entire table:
select * from tablename;
b) For particular column:
select column from tablename;
c) For multiple columns:
select columnX, columnY,…..from tablename;
d) For particular record with all columns:
select * from tablename where column=’data’;
e) For particular record with particular column:
select column, column from tablename where columnM=’data’;

5. INSERTING RECORDS WITH SELECTIVE COLUMN VALUES:

 Syntax:
a) For one column:
insert into tablename (columnX) values (data);
b) For multiple columns:
insert into tablename (columnX, columnY) values (data1, data2);

6. DELETING A PARTICULAR RECORD:

 Syntax:
delete from tablename
where column=’data’;

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

7. DELETE ALL RECORDS OF TABLE:

 Syntax:
delete from tablename;

8. UPDATING VALUE OF A COLUMN USING SAME COLUMN

(REPLACE OR SET NEW DATA):


 Syntax:
update tablename
set columnA=’newdata’
where columnA=’olddata’;

9.UPDATING VALUE OF COLUMN USING ANOTHER COLUMN:

 Syntax:

update tablename
set columnA=’newdata’
where column=’data’;

10. TO ADD A COLUMN IN TABLE (STRUCTURE):

 Syntax:
a) One column:
alter table tablename
add (column datatype(size));
b) Multiple column:
alter table tablename
add (column datatype (size)
column datatype (size)
….);

11. TO MODIFY A COLUMN IN TABLE (STRUCTURE):

 Syntax:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

alter table tablename


modify (column datatype(size));
12. TO CHANGE THE COLUMN NAME (STRUCTURE):

 Syntax:
alter table tablename
rename column oldcolumnname to newcolumnname;

13. TO CHANGE THE TABLENAME (STRUCTURE):

 Syntax:
rename oldtablename to newtablename;

14. DELETE/ DROP A COLUMN (STRUCTURE):

 Syntax:
alter table tablename
drop column columnname;

15. DELETE/ DROP THE TABLE (STRUCTURE):

 Syntax:
drop table tablename;

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Question Queries (Practical 1):


Q1) Create table dept_(rollno) with following columns:
dept_id, dept_name, dept_addr
Query:
create table dept_53(
dept_id int,
dept_name char(15),
dept_addr varchar(30)
);

Output:

Q2) Insert 5 records


Query:
insert into dept_53 values
(101,'Administration','Malad(W)');
insert into dept_53 values
(102,'Marketing','Kandivali(E)');
insert into dept_53 values
(103, 'Shipping','Goregaon(W)');
insert into dept_53 values

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

(104, 'Finanace','Borivali(W)');
insert into dept_53 values
(105, 'Sales','Bandra(E)');

Output:

Q3) Add column named d_manager


Query:
alter table dept_53
add(d_manager char(15));

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q4) Change the name of column dept_name and set it as d_name


Query:
alter table dept_53
rename column dept_name to d_name;

Output:

Q5) Delete the column having name as dept_addr


Query:
alter table dept_53
drop column dept_addr;

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q6) Delete the table dept_(rollno) from the database


Query:
drop table dept_53;

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .02
Creating and Managing Tables
AIM: a) Creating and Managing Tables
b) Including Constraints

THEORY: CONSTRAINTS

What are Constraints?

 Constraints enforces limits to the data or the type of data that can be
inserted into, updated or deleted from a table.
 The purpose of constraints is to maintain data integrity in insertion,
updation or deletion of a table.

TYPES OF CONSTRAINTS:
1. Not Null
2. Unique
3. Default
4. Check
5. Primary Key
6. Foreign Key

1. NOT NULL:
 It makes sure that a column does not hold a null value.
 When we do not provide values for a particular column while inserting a
record it takes a null value.
 By defining not null constraints, we can make sure that a particular
column will have values for all records.
 Keyword: not null

2. UNIQUE:

 This constraint enforces a column or a set of column to have a unique


value.

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

 If a column has this constraint, then that column will not have duplicate
values.
 Keyword: unique

3. DEFAULT:
 This constraint provides the default value to the column. When no value is
provided for a particular record, the default value is set.
 Ex. For a particular table consisting column name city, has a default value
set as Mumbai. So when no value is entered for city for a particular record
while inserting, the default value Mumbai will be set as city for those
particular records.
 Keyword: default

4. CHECK:
 This constraint is used for specifying range of values for a particular
column of a table.
 Ex 10<=age<=20
 When this constraints is been set to a column, it ensures that the specific
column must have the values fitting into that range.
 Keyword: check

5. PRIMARY KEY:
 It uniquely identifies a particular record.
 The constraints associated with primary key are:

1. not null

2. unique

6. FOREIGN KEY:
 The primary key of one table when it is referenced into another table it is
known as Foreign key.
 The keyword is references which is used to refer the primary key of
another table.
 Keyword: references

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

A) TO ADD CONSTRAINTS (WITH TABLE CREATION):


 Syntax:

create table tablename


(columnA datatype(size) not null,
columnB datatype(size) unique,
columnC datatype(size) default ‘data’,
columnD datatype(size) check (condition)
);

B) TO ADD CONSTRAINTS AFTER TABLE CREATION):


 Syntax:

alter table tablename


modify (columnP datatype(size) constraint);

OR
alter table tablename
add constraint cn1 constraint (columnP);

C) DEMONSTRATING NOT NULL, UNIQUE, DEFAULT & CHECK


CONSTRAINTS:
 For not null: try inserting a null value.
 For unique: try inserting duplicate value.
 For default: use default keyword instead of value/data while inserting into
a record.
 For check: try inserting a value/data which is out of range.
D) TO ADD PRIMARY AND FOREIGN KEY CONSTRAINTS:
 Syntax: (for Primary Key)
(Table 1-Parent table-Primary key)

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

create table tablename1(


columnA datatype(size) primary key,
columnB datatype(size),
columnC datatype(size),
…….
); 

 Syntax: (for Foreign Key)


(Table 2-Child table-Foreign key)
create table tablename2(
columnP datatype(size),
columnQ datatype(size) references tablename1(columnA),
columnR datatype(size),
…..
); 

E) DEMONSTRATING PRIMARY KEY & FOREIGN KEY CONSTRAINTS:


 For PK: try inserting duplicate value/data for column set as PK.
 For FK: try inserting a value/data for column set as FK in child table
which has no match in PK column of parent table.
F) DELETE / DROP A CONSTRAINTS:
 Syntax: (for not null)

alter table tablename


modify (columnname datatype(size) null);

 Syntax: (for default)

alter table tablename


modify (columnname datatype(size) default null);

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

 Syntax: (for unique)

alter table tablename


drop unique (columnname);

Question Queries (Practical 2):


Q1) Create table student_roll
Query:
create table student_53(
Roll_no int not null,

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

S_name char(15) unique,


S_location varchar(20) default 'Busan',
S_age int check(S_age<=20)
);
Output:

Q2) Insert records and demonstrate/check the constraints.


Query:
To check not null constraints.
It will not allow you to insert null values.
insert into student_53 values(‘’,’Taehyung’,’Seoul’,20);
Output:

To check unique constraints.


It will not allow you to insert same/duplicate values.
insert into student_53 values(53,’Taehyung’,’Seoul’,20);
insert into student_53 values(52,’Taehyung’,’Seoul’,20);

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

 To check default constraints.


It will show you the default value that is set.
insert into student_53 values(51,'Jungkook',default,18);

Output:

 To check ‘check’ constraints.


It will not allow you to insert out of range values.
insert into student_53 values(50,'Suga','Incheon',21);

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q3) Create 2 tables: stream_b2 and student_b2


Query:
To create table:
For table stream_b2
create table stream_b2(
stream_id number(2) primary key,
stream_name char(10));
For table student_b3
create table student_b2(
Stream_id int primary key,
Stream_name varchar(12),
Str_id int references stream_b2(stream_id)
);

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q4) Insert records and demonstrate/checks the PK and FK constraints


Query:
To insert records:
For stream_b2 table.
insert into stream_b2 values(1,'IT');
insert into stream_b2 values(2,'CS');
For student_b2 table.
insert into student_b2 values(1001,'Alice','1');
insert into student_b2 values(1002,'Bob','2');
insert into student_b2 values(1003,'John','1');

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

To check primary key and foreign key constraint:


To check primary key constraint insert values in child table which has no
reference in parent table.
Query:
insert into student_b2 values(1004,'Joy','4');
Output:

To check foreign key constraint, try to delete a column from parent table that
has reference in child table.
Query:
Insert a new record in parent table.
insert into stream_b2 values(3,’BA’);
Insert a new record in child table.
insert into student_b2 values(1004,’Joy’,’3’)
Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Delete a record which has reference in parent table


Query:
delete from stream_b2
where stream_name=’BA’;

Output:

PRACTICAL NO .03

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

SQL Statements - I
AIM: a) Writing Basic SQL Select Statement
b) Restricting and Sorting data
c) Single Row Functions

THEORY: 1. SELECT QUERY

It is used to retrieve the data from the database.


It never makes any change in the database.
 Syntax:

select columnA,.. from tablename;


If we want to retrieve data from all columns of the table then we can use *
instead of column names.
The * symbol represents all the columns of the table.

2. WHERE CLAUSE:

It is used to specify condition in select statement while fetching records from


the database.
The rows satisfying the condition given by the where clause are retrieved.
 Syntax:

select * from tablename where condition;


{condition → columnname=value}

3. DISTINCT CLAUSE:

It is used to avoid selection of duplicate value rows.

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Ex. Consider a situation where duplicate values are present for department name
column in Employee table. We want to know the department names for an
organization. We will not want to have the names repeated multiple times. In
such case distinct clause is very helpful.
 Keyword: distinct
Syntax:

select distinct columnP from tablename;

4. ORDER BY CLAUSE:

To arrange the displayed rows in ascending or descending order of given


column, order by clause is used
 Keyword: order by, desc
Syntax:

select * from tablename


order by columnA;

Ascending is the default sorting method. For displaying in descending order the
keyword desc is used.
Syntax:

select * from tablename


order by columnA desc;

5. CASE CONVERSION FUNCTIONS:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Accepts character input and returns character value.


Functions under this category are:
upper(): converts a string to uppercase
lower(): converts a string to lowercase
initcap(): converts only the initial alphabets of a string to uppercase
Syntax:

select upper(columnA), lower(columnB), initcap(columnC) from


tablename;

6. CHARACTER FUNCTIONS:

Accepts character input & returns character value


Functions under this category are:-
A] concat(): concatenates 2 or more string values.
Syntax: select concat(columnP,columnQ) from tablename;

B] length(): returns length of input column.


Syntax: select length(columnX) from tablename;

C] instr(): returns numeric position of a character/string


Syntax: select instr(columnA,’character’) from tablename;

7. NUMBER FUNCTION:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Accepts the numeric input & return numeric value.


Functions under this category are:
A] round(), trunc(): used to round and truncate the values.
B] mod(): returns the reminder of division operation of 2 numbers.
Dual: dummy table

8. SUBSTRING:

It displays a part of string


 Keyword: substr()

select empno, substr(fname,3) Name from empb1/2/3/4;


Start position

With limited characters:

Start position
n
Select empno,substr(fname,1,3) Name from emp1/2/3/4;
length
h

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Question Queries (Practical 3):


Q1) Display all the records of Emp_b1 table
Query:
create table Emp_b1
(Emp_no int,
F_name char(15),
L_name varchar(20),
E_city char(10),
E_sal int
);
Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q2) Display the firstname of all employees along with their ids.
Query:
select F_name,Emp_no from Emp_b1;

Output:

Q3) Display all records from employee table having salary equal to 22000.
Query:
select * from Emp_b1 where E_sal=22000;

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q4) Display unique salary values from the employees table.


Query:
select distinct E_sal from Emp_b1;

Output:

Q5) Display the names of all the employees in descending order


Query:
select * from Emp_b1
rder by F_name desc;

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q6) Display the salaries of all employees in lower to highest order.


Query:
select E_sal from Emp_b1
order by E_sal;

Output:

Q7) Display the firstname of all employees in upper case.


Query:
select upper(F_name) from Emp_b1;

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q8) Display the lastname of all employees in lower case.


Query:
select lower(L_name) from Emp_b1;

Output:

Q9) Display the employees city with initial alphabet capital.


Query:
select initcap(E_city) from Emp_b1;

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q10) Combine the firstname and lastname of all the employees and display as
“Full name”.
Query:
select concat(F_name,L_name)"Full Name" from Emp_b1;

Output:

Q11) Find the length of the Ecity column.


Query:
select length(trim(E_city)) from Emp_b1;

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q12) Find the position of ‘sty’ in employees names.


Query:
select instr(F_name,'sty') from Emp_b1;

Output:

Q13) Display new salary of employee with 1.15% increment.


Query:
select Emp_no,concat(F_name,L_name)"Full Name",E_sal,
(E_sal*1.15/100)"Increment" from Emp_b1;

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q14) Display new salary of employee with 1.15% increment and also show the
increased amount.
Query:
select Emp_no,concat(F_name,L_name)"Full Name",E_sal,
(E_sal*1.15/100)"Increment" ,
(E_sal+(E_sal*1.15/100))"New Salary" from Emp_b1;

Output:

For annual salary:


Query:
select E_sal "Monthly Salary",(E_sal*12)"Annual Salary" from Emp_b1;

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Substring
Query:
select substr(F_name,2,4) from Emp_b1;

Output:

Query:
select substr(F_name,1,3) from Emp_b1;

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Round off
Query:
select round(8.454,1)from dual;

Output:

Query:
select round(8.454,-1)from dual;

Output:

Query:
select round(4.6444,2)from dual;

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Query:
select round(4.6444,0)from dual;

Output:

Truncation
Query:
select trunc(4.6444,1)from dual;

Output:

Query:
select trunc(4.6444,-1)from dual;

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Query:
select trunc(4.6444,0)from dual;

Output:

Modulus
Query:
select mod(15,6) from dual;

Output:

Query:
select mod(20,7) from dual;
Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .04
SQL Statements - II
AIM: a) Displaying data from multiple tables
b) Aggregating data using group functions
c) Subqueries

THEORY: 1. AGGREGATE FUNCTIONS

It allows you to perform calculation on set of values to return a single scalar


value.

It is often used with the group by clause of the select statement most commonly
used aggregate functions are as follows:

1. Min()

2. Max()

3. Sum()

4. Avg()

5. Count()

1A. GROUP BY CLAUSE:


 Group by clause is used in collaboration with select statement. 
 It is used to arrange identical data, group set of rows that have the same
value.
 It is used in conjunction with aggregate function to produce summary
report.
 Queries containing group by clause only return a single row from every
grouped item

 Syntax:

select column P from tablename group by column P;

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

2. COUNT():

It returns total number of values of specified column of a table.


 Syntax:

select count(column P) from tablename;


 With where clause
select count(column P) from tablename where column P=‘data’;
 With group by clause
select count(*) from tablename group by column P;

3. SUM()
 Returns addition of all values of specified column of a table
Syntax:

select sum(column P) from tablename;


 With where clause
select sum(column P) from tablename where column Q=‘data’;
 With group by clause
select sum(column P) from tablename group by column Q;

4. MIN():

Returns minimum value of specified column of a table.


Syntax:

select min(column P) from tablename;


 With where clause

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

select min(column P) from tablename where column Q=‘data’;


 With group by clause
select min(column P) from tablename
group by column Q;

5. MAX():

Returns maximum value of specified column of a table.


Syntax:

select max(column P) from tablename;


 With where clause
select max(column P) from tablename where column Q=‘data’;
 With group by clause
select min(column P) from tablename group by column Q;

6. AVG():

Returns average value of specified column of a table.


Syntax:

select avg(column P) from tablename;


 With where clause
select avg(column P) from tablename where column Q=‘data’;
 With group by clause
select avg(column P) from tablename group by column Q

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

7. SUBQUERY:
Writing a query inside another query is known as subquery or nested query
Syntax:

select column A
from tablename1
where columnP operator (select column P 
from tablename2 
[where(condition)]);
 Operators:  = / < / > / in / like / …

The LIKE operator is used in a WHERE clause to search for a specified


pattern in a column.

There are two wildcards often used in conjunction with the LIKE operator:

-The percent sign (%) represents zero, one, or multiple characters


-The underscore sign (_) represents one, single character

WHERE CustomerName LIKE 'a%'


Finds any values that start with "a"

WHERE CustomerName LIKE '%a'


Finds any values that end with "a"

WHERE CustomerName LIKE '%or%'


Finds any values that have "or" in any position

WHERE CustomerName LIKE '_r%'


Finds any values that have "r" in the second position

WHERE CustomerName LIKE 'a_%'


Finds any values that start with "a" and are at least 2 characters in length

7A. INSERTING USING SUBQUERIES:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

 Inserting records of one table into another using subquery.


 Q) Create a backup copy of emp table as copy_emp1/2/3/4 and insert the
records of emp table
insert into copy_empb1/2/3/4 (select * from empb1/2/3/4
[where condition]);
 Updating and deleting using subquery is also possible 

8. JOINS:
 It combines the matching and unmatching rows from two tables
 Types
 Inner join and Outer join

8A. INNER JOINS:

 It displays matching records from two tables which satisfies given


condition
Syntax:

select table1.columnA, column P, ….


from table1 INNER JOIN table2
ON table1.columnA = table2.columnA;

8B. NATURAL JOINS:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

 It displays matching records from two tables without specifying condition,


based on matching/common column.
Syntax:

select table1.columnA, column P, …./*


from table1 NATURAL JOIN table2;

8C. OUTER JOINS (CONT.):

1) Left outer join: It displays unmatching records from left table and has null
value for right table columns, along with matching records from both tables.
 Syntax:

select table1.columnA, column P, ….


from table1 LEFT OUTER JOIN table2
ON table1.columnA = table2.columnA;

2) Right outer join: It displays unmatching records from right table and has null
values for left table columns, along with matching records from both tables.
 Syntax:

select table1.columnA, column P, ….


from table1 RIGHT OUTER JOIN table2
ON table1.columnA = table2.columnA;

3) It displays all the matching rows from both left and right table along with the
unmatching records if present in either tables with null values. 
 Syntax:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

select table1.columnA, column P, ….


from table1 FULL OUTER JOIN table2
ON table1.columnA = table2.columnA;

Question Queries (Practical 4):

Part – A
Displaying data from multiple tables.

Display all the records of empb1 table


Query:
create table empb1
(Eno int,
Fname char(15),
Lname varchar(20),
Esal int,
Eage int

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

);

Output:

Query:
insert into empb1 values(1001,'alice','cena',28000,35);
insert into empb1 values(1002,'bob','jose',22000,38);
insert into empb1 values (1003,'john','william',25000,35);
insert into empb1 values(1004,'alice','william',22000,38);
insert into empb1 values(1005,'jesty','jose',20000,38);
Output:

Q1) Retrieve the count of employees whose firstname is “Alice”.

Query:
select count(Fname) from empb1 where Fname='alice';

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Output:

Q2) Retrieve the count of employees as per same age group.


Query:
select count(Eage),Eage from empb1 group by Eage;

Output:

Q3) Find the total salary of all employees.


Query:
select sum(Esal) from empb1;

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q4) Find the total salary of all employees having same firstname.
Query:
select sum(Esal), Fname from empb1 group by Fname;

Output:

Q5) Retrieve the total salary of employees based on same age group.
Query:
select sum (Esal),Eage from empb1 group by Eage;

Output:

Q6) Find minimum age of all employees.


Query:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

select min(Eage) from empb1;

Output:

Q7) Find minimum salary of all employees having same last name.
Query:
select Lname,min(Esal) from empb1 group by Lname;

Output:

Q8) Retrieve minimum age of employees based on same salary range.


Query:
select min(Eage) from empb1 group by Esal;

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q9) Find the maximum age of all employees.


Query:
select max (Eage) from empb1;

Output:

Q10) Find the maximum age of all employees having same firstname.
Query:
select Fname,max(Eage) from empb1 group by Fname;

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q11) Retrieve the maximum age of employees having lastname as “Jose”.


Query:
select max (Eage) from empb1 where Lname='jose';

Output:

Q12) Find average salary of all employees.


Query:
select avg (Esal) from empb1;

Output:

13) Find the average age of all employees having same salary.
Query:
select avg (Eage) from empb1 group by Esal;
Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q14) Retrieve the average salary of employees having firstname as “Alice”.


Query:
select avg (Esal) from empb1 where Fname='alice';
Output:

Part -B
Aggregating data using group functions.

Query:
create table empb_1
(Empid int,
Fname char(15),
Lname varchar(20),
Esal int,
m_id int,
d_id int
);

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Query:
insert into empb_1values (123,'Deep','Shah',50000,720,1401);
insert into empb_1values (250,'Bhavini','Shah',50000,123,1401);
insert into empb_1values (723,'Kreena','Shah',50000,450,1402);
insert into empb_1values (752,'Meena','Gala',20000,723,1402);
insert into empb_1values (562,'Teena','Bajaj',12000,456,1450);
insert into empb_1values (633,'Meena','Shah',18000,463,1450);

Query:
create table deptb_1
(d_id number(5),
d_name char(10),
m_id int,
loc_id int
);

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Query:
insert into deptb_1 values(1401,'IT',123,501);
insert into deptb_1 values(1402,'Marketing',850,502);
insert into deptb_1 values(1403,'Shipping',752,503);
insert into deptb_1 values(1404,'sales',462,504);

Output:

Q15) Display full name of all employees who are having salary more than
“Meena Shah”.
Query:
select Empid,Esal,concat(Fname,Lname)"Full Name" from empb_1
where Esal>(select Esal from empb_1 where Fname='Meena' and
Lname='Shah');

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q16) Display names of all employees who are having salary same as “Deep”.

Query:
select concat(Fname,Lname)"Full name",Empid,Esal from empb_1
where Esal=(select Esal from empb_1 where Fname='Deep');

Output:

Q17) Display id and names of all employees with minimum salary.


Query:
select Empid,Esal,concat(Fname,Lname)"Full name" from empb_1 where
Esal=(select min(Esal) from empb_1);

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q18) Display details of those employees who are having ‘a’ in their last name
and who have salary above average.
Query:
select concat(Fname,Lname) "Full Name",Esal,Empid from empb_1 where
Empid in (select Empid from empb_1 where Lname like '%a%') and Esal >
(select avg (Esal) from empb_1);

Output:

Q19) Display maximum, minimum, sum, average salary of all employees.


Query:
select max(Esal),min(Esal),sum(Esal),avg(Esal) from empb_1;

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q20) Display total employees per department.


Query:
select d_id,count(fname) from empb_1 group by d_id;

Output:

Part – C
Subqueries:
Q21) Display first name of all employees and their department names from
Employee and Department table.
Query:
select empb_1.d_id,fname,d_name from empb_1 left outer join deptb_1 on
empb_1.d_id=deptb_1.d_id;
Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q22) Display first name of employees and their department names from
Employees and Department table for employee named “Meena”.
Query:
select fname,d_name from empb_1,deptb_1 where
empb_1.d_id=deptb_1.d_id and fname='Meena';

Q23) Display department information with employees present in them if any.


Query:
select deptb_1.d_id, d_name,empid,fname,lname from empb_1
right outer join deptb_1 on empb_1.d_id=deptb_1.d_id;
Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q24) Display department details of those departments that are specifically


having employees.
Query:
select deptb_1.d_id, d_name,empid,fname,lname from empb_1 inner join
deptb_1 on empb_1.d_id=deptb_1.d_id;
Output:

Q25) Display the all department details and all employee details.
Query:
select deptb_1.d_id, d_name, empid, fname, lname from empb_1
full outer join deptb_1 on empb_1.d_id=deptb_1.d_id;

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .05
Creating & Managing Other Database Objects
AIM: a) Creating views
b) Other database objects
c) Controlling user access

THEORY: 1. VIEWS:
 Views are virtual tables which stores data as output of query expression.
 It is similar to tables but a table has set of definition in which it actually
stores data whereas a view also has set of definition which is built on top
of table or other views.
 Syntax:
create view viewname as (query expression);
1. VIEWS CONT.
 Updating views: As views are virtual copy of table, they are replaced
when we say updated
 Syntax: create or replace view viewname as (query expression);

 Dropping View:
 Syntax: drop view viewname;

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

 Read Only views:


 Syntax: create view viewname as (query expression) with read only;
2. SYNONYMS:
 It is used to give alternate names to a table
 Syntax:
create synonym syn_name for tablename;

3. SEQUENCE (OTHER DATABASE OBJECTS):

 It is a database object from which the user may generate unique integers.
 It can be used to automatically generate primary key values.
 These are generated independently so can be used for one or more tables.
 Syntax:
create sequence seq_name
start with value
increment by value
maxvalue value
nocycle
nocache;

 How to obtain / check sequence values?


a) Next value: gives next values that is generated by sequence
Syntax:
select seq_name.nextval from dual;
b) Current value: displays current value that is generated by sequence
Syntax:
select seq_name.currval from dual;
 Inserting rows into table using sequence value

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

insert into tablename values(seq_name.nextval, data2,data3,..)

 Inserting rows into table using sequence value

insert into tablename values(seq_name.nextval, data2,data3,..)

4.1. CONTROLLING USER ACCESS: CREATE USER:

 It is used to create a new user account.


 Syntax:
create user username1
identified by password;
 We can also change the password if required
 Syntax:
alter user username1
identified by newpassword;

4.2 THE GRANT STATEMENT:

 Once new user account is created, we can give/ add privileges to account
by using the grant statement.
 It has many options hence it is a powerful statement.
 Its core functionality is to manage the privilege of both user and role
throughout the database.

4.3 ROLES AND PRIVILEGES:

 Every user can be granted a no. of privileges i.e. permissions.


 These privileges allow a particular user to do certain things, some
examples of privileges are:

o Connect to a database

o Create table

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

o Select, insert, update, delete, etc.

 A user can receive privilege in 2 different ways


1. Grant privilege to user explicitly
2. Grant privilege to a role and then grant role to one or more use

ROLES AND PRIVILEGES:

 Syntax:
A) System privilege: grant privilege to username;
B) Object privilege:  grant privilege on object to username;
C) Creating role: create role rolename;
D) Role privilege: grant privilege on object to rolename;
E) Revoking privileges:
I – revoke privilege on object from username;
ii – revoke privilege on object from rolename;
Adding user to role
Syntax:
grant rolename to username;

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Question Queries (Practical 5):

Q1) Create a view “empb_1_v1” of employee table.


Query:
create view empb_1_v1 as (select * from empb_1);

Output:

Q2) Create a view “deptb_1_v1” of department table with only two departments
1401 and 1402.
Query:
create or replace view deptb_1_v1 as (select * from deptb_1 where d_id in
(1401,1402));

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q3) Create a view “empb_1_v1” of employee table with some selective


columns and annual salary.
Query:
create or replace view empb_1_v1 as (select Empid, concat(Fname, Lname)
"Full Name", Esal, (Esal*12)"Annual Salary" from empb_1);

Output:

Q4) Create a view “empb_1_v1” of employee and department table.


Query:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

select empid,empb_1.d_id,concat(fname,lname)"Full Name",d_name,Esal from


empb_1 full outer join deptb_1 on empb_1.d_id=deptb_1.d_id;
Output:

Q5) Update a view empb_1_v1 and set the d_id values as 1403 for d_id 1450.
Query:
update empb_1 set d_id=1403 where d_id=1450;
Output:

Q6) Create a read only view of employee table as empb_1_RO.


Query:
create view empb_1_RO as (select * from empb_1) with read only;

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Output:

Q7) Create a sequence for empid.


Query:
create sequence emp_b1_seq1 start with 100
increment by 50 maxvalue 800
nocycle nocache;
Output:

Q8) Check the current value and next value of empid sequence.
Query:
select emp_b1_seq1.nextval from dual;
Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Query:
select emp_b1_seq1.currval from dual;
Output:

Q9) Insert 2 records into employee table using sequence.


Query:
insert into empb_1
values(emp_b1_seq1.nextval,'Taehyung','Kim',25000,545,1490);
insert into empb_1
values(emp_b1_seq1.nextval,'Jungkook','Jeon',35000,560,1490);

Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q10) Create a synonym as table empb_1_synb1 and department as


deptb_1_synb1.
Query:
create synonym empb_1_synb1 for empb_1;
create synonym empb_1_synb1 for empb_1;
Output:

Q11) Create a synonym as table empb_1_synb1 and department as


deptb_1_synb1.
Query:
create user newb_1
identified by system  PASSWORD
grant connect to newb_1;
Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q12) Revoke the privilege of new_b1/2/3/4 to view the table emp_b1/2/3/4 (of
the system user)
Query:
GRANT SELECT ON empb_1 TO newb_1;
select * from system.empb_1;
Output:

Q13) Change the password of new_b1/2/3/4.


Query:
alter user new_b1

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

identified by shipra;  PASSWORD


Output:

PRACTICAL NO .06
Using Set Operators, Date/ time Functions, group By Clause
(Advanced features) & Advanced Subqueries

AIM: a) Using Set Operators

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

b) date/time Functions
c) Enhancements to the Group By clause
d) Advanced Subqueries

THEORY: 1. SET OPERATORS:


 Different Set Operators are as follows:
1. Union: returns distinct values from either or both queries
2. Union all: returns values from either or both queries including duplicate.
3. Intersect: returns only those values which are common to both queries.
4. Minus: returns values which are in first query but not in second query.
Syntax:
select columnA from query1
UNION / UNION ALL / INTERSECT / MINUS
select columnA from query2; 

2. DATE / TIME FUNCTIONS:

 sysdate / current_date : used to see current system date.


 last_day() : used to see the last day of the month of specified date.
select last_day (sysdate) from dual;
 next_day() : used to see when a particular ‘day’ is coming next.
select next_day(sysdate,’day’) from dual;
 add_months() : used to add ‘N’ number of months to a date specified.
select add_months (sysdate,N) from dual;

 systimestamp / current_timestamp : used to see current date time with


fractional seconds and time zone.
 extract() : it extracts and returns the value of specified date time field from
given date.
select extract(year/ month/ day from sysdate) from dual;

3. ENHANCEMENT TO GROUP BY CLAUSE:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

 Group By clause is used in collaboration with select statement.


 It helps to arrange similar data into groups and can be used with SQL
functions to group the results.
Syntax:
select columnA
from tablename
where condition
group by columnA;

3A. ROLL UP AND CUBE:

 Roll up enables an SQL statement to calculate multiple levels of subtotal


across a specified group of dimensions. It also calculates grand total.
 Cube enables a select statement to calculate subtotal for all possible
combinations of group of dimensions. This is the set of information which
is typically needed for cross tabular report. It also calculates grand total.
 Syntax:
select columns
from tablename
where condition
group by rollup / cube (columns);

4. ADVANCED SUBQUERIES:

1. Nested subqueries: queries inside where clause.


2. Inline Views: queries inside the from clause (usually referenced with
alias)
3. Co-related subqueries: If inner query returns one or more records, the
outer query is executed, else row fetched by parent query is not displayed.
4. Scalar subquery: A subquery that returns only a single row is scalar
subquery and can be used in those places where an expression is normally
used.
NOTE: Subqueries can also be used in Having clause.

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Question Queries (Practical 6):


Employee Table

Department Table

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

SET OPERATORS:
Q1) Write a query to retrieved d_id’s present in employee and department table.
Query:
select D_Id from empb_1 union all select D_ID from deptb_1;
Output:

Q2) Write a query to retrieve unique d_id’s present in employee and department
table.
Query:
select D_ID from empb_1 union select D_ID from deptb_1;
Output:

Q3) Write a query to retrieve all d_id’s who are having employees.

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Query:
select D_ID from empb_1 intersect select D_ID from deptb_1;
Output:

Q4) Write a query to retrieve all m_id’s which are present in employee table but
not in department table.
Query:
select M_ID from empb_1 minus select M_ID from deptb_1;

Output:

Q5) Demonstrate the use of various date/ time functions.


Query: (DATE/TIME FUNCTIONS)
select sysdate from dual;
Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Query:
select current_date from dual;
Output:

Query:
select last_day(sysdate) from dual;
Output:

Query:
select next_day(sysdate,'Monday') from dual;
Output:

Query:
select add_months(sysdate,7) from dual;
Output:

Query:
select systimestamp from dual;

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Output:

Query:
select current_timestamp from dual;
Output:

Query:
select extract(year from sysdate) from dual;
Output:

Query:
select extract(month from sysdate) from dual;
Output:

Query:
select extract(day from sysdate)"Today's day" from dual;
Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

GROUP BY ROLL UP
Q6) Display subtotal values of salaries by department id’s and by manager id’s
of depts. 1401, 1402, 1403 using rollup operator.
Query:
select D_ID,M_Id,sum(Esal)
from empb_1
where D_ID in(1401,1402,1403)
group by rollup(D_ID,M_ID);
Output:

GROUP BY CUBE
Q7) Display subtotal values of salaries by department id’s and by manager id’s
of depts. 1401, 1402, 1403 using cube operator.
Query:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

select D_ID,M_Id,sum(Esal)
from empb_1
where D_ID in(1401,1402,1403)
group by cube(D_ID,M_ID);

Output:

Q8) Display details of those employees who earn less than average salary of
their department using subquery.
Query:
select empid,fname,lname,d_id from empb_1 E
where esal<(select avg(esal) from empb_1 where d_id=E.d_id);
Output:

Q9) Display count of all employees whose last name begin with character ‘s’.
Query:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

select count(*) from(select * from empb_1 where lname like 'S%');


Output:

Q10) Display details like name and salary of all managers.


Query:
select empid,fname,esal,d_id from empb_1 E
where exists(select * from empb_1 where m_id=E.empid);
Output:

Q11) Retrieve the lowest paid employees of their department using co related
subquery.
Query:
select fname,esal,d_id from empb_1 E1 where
E1.esal in(select min(esal) from empb_1 E2 where E1.d_id=E2.d_id);
Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Q12) Retrieve the lowest paid employees using scalar subquery.


Query:
select fname,lname,esal,d_id from empb_1 where esal in
(select min(esal) from empb_1);
Output:

Q13) Retrieve the d_id's where the average salary of each department less than
the average salary for all employees.
Query:
select d_id ,avg(esal) from empb_1 group by d_id
having avg(esal) < (select avg(esal) from empb_1);
Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .07
Creating Database Triggers

AIM: a) Creating Database Trigger

THEORY: 1. TRIGGERS:
 A trigger is a PL/SQL block structure which is fired when a DML statement
like Insert, Update and Delete or a database operation is executed on a
database table.
 A trigger is triggered automatically when the statement is executed.

1A. TRIGGERS CONT.

 Syntax:
CREATE [OR REPLACE] TRIGGER trigger_name
{BEFORE/AFTER/INSTEAD OF} event
[OF col_name]
ON table_name
[FOR EACH ROW/FOR EACH STATEMENT]
[WHEN (condition)]
DECLARE
declaration-statements;
BEGIN
statements;
END;
/

1B. TRIGGERS CONT.


 Types of PL-SQL triggers

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

1. Row Level Trigger- Action is taken for each row created, updated or
deleted.
2. Statement Level Trigger- Action is taken for each SQL Statement
executed.
 Hierarchy of Trigger execution
1. Before Statement Level
2. Before Row Level
3. After Row Level
4. After Statement Level 

Question Queries (Practical 6):


Employee Table

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Department Table

Q1) Create a trigger to update salary of employee and show the difference
between updated and existing salary.
Query:
> set serveroutput on;
create or replace trigger batch_b1
before update on empb_1
for each row
when(new.esal>0)
DECLARE
sd number;
BEGIN
sd := :new.esal - :old.esal;
dbms_output.put_line('Salary Difference=' || sd);
END;

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

/
Output:

 Updating salary of
employee to show
the difference
between updated
and existing salary.
Query:
update empb_1
set esal=’38000’
where esal=’30000’;
Output:

Q2) Create a trigger to increment salary of employee by 500 before inserting a


record in table.
Query:
create or replace trigger incr_b1
before insert on empb_1
for each row
BEGIN
:new.esal := :new.esal+500;
dbms_output.put_line('Incremented Salary =’ || :new.esal);
END;

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

/
Output:

Query:
insert into empb_1 values(850,'Taehyung','Kim',25000,545,1490);

Q3) Create a trigger to insert the deleted records of employee table in backup
table.
Query:
 First we will create the backup table to store the deleted records.
create table emp_backup_b1
(Empid int,
Fname char(15),
Lname varchar(20),
Esal int,
m_id int,
d_id int
);
Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

Query:
create or replace trigger trg_delb1
after delete
on empb_1
for each row
BEGIN
insert into emp_backup_b1 values(:old.empid,:old.fname,:old.lname,
:old.esal,:old.m_id,:old.d_id);
END;
/
Output:

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

delete from empb_1 where d_id = 1490;

select * from emp_backup_b1;

Q4) Create a trigger to insert the username and logindate in a table.


Query:
 First we will create the logindetails table.

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

create table logindetails_b1


(
username varchar2(10),
logicaldt date
);
Output:

create or replace trigger login_b1


after logon
on database
BEGIN
insert into logindetails_b1 values(user,sysdate);
END;
/
Output:

select * from logindetails_b1;

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

PRACTICAL NO .08
PL-SQL Basics

AIM: a) Declaring variables


b) Writing executable statements
c) Writing control structures

Question Queries (Practical 8):


Q1) Write a PL_SQl program to find addition of two numbers.
Query:
> set serveroutput on;
DECLARE
a number;
b number;
result number;
BEGIN
a :=&a;
b :=&b;

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

result :=a+b;
dbms_output.put_line('Addition of '|| a ||' and '||b ||' is ' ||result);
END;
/

Output:

Q2) Write a PL_SQl program to find largest of two numbers using simple if
statement.
Query:
DECLARE
a number;
b number;
BEGIN
a := &a;

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

b :=&b;
if (a>b) then
dbms_output.put_line('A is greater than B');
end if;
if(b>a) then
dbms_output.put_line('B is greater than A');
end if;
END;
/
Output:

Q3) Write a PL_SQl program to demonstrate while loop.


Query:
DECLARE
a number;
BEGIN

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

a :=1;
while(a<11)
LOOP
dbms_output.put_line(a);
a:= a+1;
END LOOP;
END;
/
Output:

Q4) Write a PL_SQl program to find factorial of entered number using loop.
Query:
DECLARE
n number;
i number;
fact number;
BEGIN
fact := 1;

DATABASE MANAGEMENT SYSTEM


Name. Kishan Kumar Goud
Roll no. 432 SYBSC.IT

n :=&n;
for i in 1..n
LOOP
fact := fact *i;
END LOOP;
dbms_output.put_line('Factorial for number '||n||' is '||fact);
END;
/
Output:

DATABASE MANAGEMENT SYSTEM

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy