You are on page 1of 17

PRACTICAL FILE

Subject: Database Management System Lab


Subject Code: KCS-551
Course: B. Tech Brach: Semester: 5th
Session Odd Sem 2023-24

Submitted To: Mr Mukesh Bhardwaj Submitted By: Anuj Negi


(Ass. Professor) (University Roll): 2102301640006
Index
Sr. No Practical Date Signature
1 Write SQL queries to create customer1, order1and
display the structures of the tables.

2 Write SQL queries to add new column pin code,


primary key (C_id) in customer1 and primary key
(order_no), and foreign key C_id reference to
customer1 using alter command.
3 Write SQL queries to insert and display the
records in customer1 and oder1 using the insert
command.

4 Write SQL queries to show Customer1 details


order by name.

5 Write SQL queries to show join operations


between Customer1and order1tables.

6 Write SQL queries to view using Customer1.

7 Write SQL queries to demonstrate subquery using


the Customer1 table.
8 Write PL queries to Check whether the given
number is an even or odd number
9 Write PL queries to Display 1 to 10 numbers using
PL.
10 Write PL queries to Check the greatest number
among three number
Program-1

Write SQL queries to create customer1, order1and display the structures of the
tables.

Create Table Customer 1

create table customer1(c_id number(5), name varchar2(25),age number(3),income


number(7),city varchar2(20),country varchar2(25));

Display the structure of customer1

Desc customer1.

Create Table Oder1

create table order1(order_no number(5), item_name varchar2(25),price


number(5,2), quantity number(5), c_id number(5));

Display the structure of customer1

desc order1;
Program-2

Write SQL queries to add new column pincode,primary key (C_id) in customer1
and primary key (order_no) and foreign key C_id reference to customer1 using
alter command.

Primary key: A primary key is used to ensure that data in the specific column is
unique. A column cannot have NULL values.It is either an existing table column or
a column that is specifically generated by the database according to a defined
sequence.

Foreign key: A foreign key is a column or group of columns in a relational


database table that provides a link between data in two tables. It is a column (or
columns) that references a column (most often the primary key) of another table.

Add columns using alter command

alter table customer1 add pincode number(6);

Add constraints primary key, not null

alter table customer1 add primary key (c_id);


ALTER TABLE order1 add primary key (order_no);

ALTER TABLE customer1 MODIFY income number(7,2);

ALTER TABLE order1 add foreign key (c_id) references customer1;


ALTER TABLE order1 MODIFY c_id not null;
Program-3

Write SQL queries to insert and display the records in customer1 and oder1 using
insert command.

Insertion of Records in customer1 table

insert into customer1 values (1000,'Rajesh Kumar


Gupta',19,56000.25,'varanasi','india',221007);

insert into customer1 values(1003,'Dinesh',25,66000,'ghaziabad','india',201001),


(1005,'Raghu',20,6000,'ghaziabadGr Noida','india',201001),
(1006,'Sweta',21,16000,'ghaziabad','India,201004);

Display the records of customer 1 table

select * from customer1;

Insertion of Records in order1 table

insert into order1 values (1206,'keyboard', 330.0,7,1000);

Display the records of order 1 table

select * from order1;


select name,city from customer1;
Program-4

Write SQL queries to show Customer1 details order by name.

Ascending order

select * from customer1 order by (name);

Descending Order

select * from customer1 order by (name) desc;


Program-5

Write SQL queries to show join operations between Customer1and order1tables.

The SQL Join clause is used to combine data from two or more tables in a
database. When the related data is stored across multiple tables, joins help you to
retrieve records combining the fields from these tables using their foreign keys.

Natural Join

select name,city, item_name from customer1 c,order1 od where c.c_id=od.c_id;

Outer Join

An Outer Join retrieves all the records in two tables even if there is no counterpart
row of one table in another table, unlike Inner Join. Outer join is further divided
into three subtypes - Left Join, Right Join and Full Join.

Left Outer Join

Retrieves all the records from the first table, Matching records from the second
table and NULL values in the unmatched rows.

select * from customer1 left join order1 on customer1.c_id=order1.c_id;


Right Outer Join

Retrieves all the records from the second table, Matching records from the first
table and NULL values in the unmatched rows.

select * from customer1 Right join order1 on customer1.c_id=order1.c_id;

Full Outer Join

Retrieves records from both the tables and fills the unmatched values with NULL.

select * from customer1 full outer join order1 on customer1.c_id=order1.c_id;


Inner Join

An INNER JOIN is the default join which retrieves the intersection of two tables.
It compares each row of the first table with each row of the second table. If the
pairs of these rows satisfy the join-predicate, they are joined together.

select * from customer1 inner join order1 on customer1.c_id=order1.c_id;


Program-6

Write SQL queries to view using Customer1.

A view in SQL is a virtual table that is stored in the database with an associated name.
It is actually a composition of a table in the form of a predefined SQL query. A view
can contain rows from an existing table (all or selected). A view can be created from
one or many tables. Unless indexed, a view does not exist in a database.

Create view c1 as

select name, age, income from customer1;

desc c1;
Program-7

Write SQL queries to demonstrate subquery using the Customer1 table.

An SQL Subquery, is a SELECT query within another query. It is also known


as Inner query or Nested query and the query containing it is the outer query.

The outer query can contain the SELECT, INSERT, UPDATE, and DELETE
statements. We can use the subquery as a column expression, as a condition in
SQL clauses, and with operators like =, >, <, >=, <=, IN, BETWEEN, etc.

Subquery to display city and average salary from customer1.

SELECT City, income,

(SELECT AVG(income) FROM customer1)

AS Avgincome FROM customer1;


Program-8

Write PL queries to Check whether the given number is even or odd number.

PL: PL/SQL is a block structured language that enables developers to combine the
power of SQL with procedural statements. All the statements of a block are passed
to oracle engine all at once which increases processing speed and decreases the
traffic.

declare
v1 number ;

begin

v1 :=:v1;

if mod(v1,2)=0 then
dbms_output.put_line('given number is even : ' || v1);
else
dbms_output.put_line('given number is odd : ' || v1);
end if;
end;
output:
Program-9

Write PL queries to Display 1 to 10 numbers using PL.

declare

x number;

begin

for x in reverse 1..10


loop

dbms_output.put_line(x);

end loop;

end;
Output
Program-10

Write PL queries to Check the greatest number among three number

declare

a number(4);

b number(4);

c number(4);

begin

a:=:a;

b:=:b;

c:=:c;

if (a>b) and (a>c) then

dbms_output.put_line(' a is greatest');

elsif (b>c) then

dbms_output.put_line('b is greatest');

else

dbms_output.put_line('c is greatest'); END IF;

end;

Output

You might also like