0% found this document useful (0 votes)
68 views

Dbms Practical

The document provides details about various SQL commands used in database management systems. It discusses commands for creating tables, inserting data, accessing data, updating and deleting records, modifying table structures, and using clauses like WHERE, GROUP BY, HAVING. It also covers concepts like null values, primary keys, foreign keys and constraints. Examples are given for each command and concept to demonstrate their proper syntax and usage.

Uploaded by

gautamchauhan566
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)
68 views

Dbms Practical

The document provides details about various SQL commands used in database management systems. It discusses commands for creating tables, inserting data, accessing data, updating and deleting records, modifying table structures, and using clauses like WHERE, GROUP BY, HAVING. It also covers concepts like null values, primary keys, foreign keys and constraints. Examples are given for each command and concept to demonstrate their proper syntax and usage.

Uploaded by

gautamchauhan566
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/ 31

Practical File

Database Management System Lab


KCA252

Master of Computer Application (2nd Sem)


Session- 2022-23

Submitted To Submitted By
Mr.Pradeep Sharma Vineet Kumar
Associate Professor Roll No- 2200160140035

RBMI Group of Institutions, Bareilly


(Affiliated to A.P.J. Abdul Kalam University, Lucknow)

Vineet Kumar 2200160140035 Page 1


Index

Sr. Query Name Page no. Sign.


No.
1 Creating the table.
2 Insertion of data into table.
3 Accessing the data from the table.
4 Deletion of a record in the table and deletion of a
table.
5 Operation of Updating in a table.
6 Modifying the Structure of a table.
7 The Where Clause.
8 Null value concept.
9 Constraints.
10 Group by Clause.
11 Having clause.
12 Primary key concept.
13 Foreign key concept.
14 Joining multiple tables.
15 Intersect clause.
16 Views.
17 Procedural Language/Structure language.
18 Cursor.
19 Triggers.

Vineet Kumar 2200160140035 Page 2


Creating the Table
Command: Create Table
Syntax: CREATE TABLE client_vineet(column name datatype(size),coloumn
name datatype(size));
The Create table command includes a single clause for the column definition.
Each column is a parameter for the clause and thus, it is separated by comma.
Finally, the SQL statement is terminated with a semicolon.

Example 1:
Create a client_ vineet table whose structure is:

Column name datatype size


-------------------- ------------------- ----------------
Client_no varchar2 6
Name varchar2 20
Address 1 varchar2 30
Address 2 varchar2 30
City varchar2 15
State varchar2 15
Pincode number 6
Remarks varchar2 60
Bal_due number 10,2

Vineet Kumar 2200160140035 Page 3


Vineet Kumar 2200160140035 Page 4
Insertion of data into table
Command: INSERT INTO
Syntax: INSERT INTO table name(column name, column name)
Values(expression, expression);
Once a table is create the most natural thing to do is load this table with data to
be manipulated later. When inserting a single row of data into the table, the
insert operation is done for:
1. Create a new row in the database table.
2. Load the values passed into all the column specified.

Example 1:Insert the following values into the client_master table:


Column name Values
-------------------- ----------------
Client_no C02000
Name vineet kumar
Address1 A-5,Miranpur katra
Address2 Devnagar Road Spn
City Shahjahanpur
State U.P
Pincode 242301

Accessing the data from the table


Command: SELECT
Syntax: SELECT * from table name;

Vineet Kumar 2200160140035 Page 5


The select command is use to access the data row from the table. User can access
the data from the table by giving the field name using this command. If the user
want to see the data of one or more particular row instead of all rows then he
have to specify the name of the associated field in place of *.
Select command is use for two ways:
1. Select * from table name.
2. Select field name from table name.

Example 1: If we want to access the data of all fields of client_vineet table then
we have to write the following syntax:
Select * from client_vineet;

Vineet Kumar 2200160140035 Page 6


Deletion of a record in a table & deletion of table
Command: DELETE
Syntax: DELETE * from table name;

The delete command is used to delete the record in a table. The user can delete a
particular record or all the record in the table by using this command. The delete
command is used in two ways.
1. Delete * from table name;
2. Delete field name from table name;

The first command delete all the record in a particular table immediately after
giving the command on SQL prompt, Where the second command delete only the
record of specified row of the given table.

Deleting a table
Command:DROP TABLE
Syntax:drop table from table name;
This command is use to remove the particular table in the database. It removes
the table in the database immediately after giving this command on the SQL
prompt.

Operation of updation in a table


Command:UPDATE

Vineet Kumar 2200160140035 Page 7


Syntax:UPDATE table from table name;
The update command is used to change or modify data values in a table.
This command is apply for updating
1.All the rows from a table
2. A select of rows from a table.

Updating all rows:


Syntax:UPDATE table name
Set column name=expression,column name-expression;

EXAMPLE 1:Update client_vineet


Set net sal=net-sal+basic-sal*0.15;

Updating records conditionally:


Syntax:UPDATE table name
Set column name=expression, column name = Expression
Where column name= Expression;

EXAMPLE 2:UPDATE CLIENT_vineet


SET NAME = ‘vineet yadav’,ADDRESS1=’SCI katra’
WHERE CLIENT_N0=’C02000’;

Vineet Kumar 2200160140035 Page 8


Modifying the structure of table
Command: ALTER TABLE
Syntax: ALTER table tablename ;
This command is used to modifying the structure of the table. A user can add new
column or change the size of any data type or change the data type.
The alter table command is use to following ways:
Adding New Column :
Syntax :Alter table name ADD (new column name data type (size),new
column name data type (size)……..);
Example 1:
ALTER TABLE client_vineet
ADD (client_tel number(8), client_fax number(15));

Vineet Kumar 2200160140035 Page 9


Modiying Existing columns:
Syntax: ALTER TABLE table name
MODIFY(column name new data type (new size));

Example 2:
ALTER TABLE client_master
MODIFY(client_fax varchar2(25));

Vineet Kumar 2200160140035 Page 10


The where clause
The where clause is an important clause in the sql quary statement. The user can
apply a particular criteria in the sql statement . If a user want to see the details of a
particular person or department or any other or any other particular thing then he
can do it by applying the where clause in the sql quary statement. The user not
only access the record from the database by using the where clause but also update,
modify and delete the specific record with use of where clause.
The syntax of where clause statement with different sql command are as following:
1. Accessing the specific record:
Select field name , field name from tablename
Where field name =’expression’ ;
Example 1:Select the client _name from client_master whose city
isKANPUR
SELECT name,city from client_master
Where city = ‘KANPUR’;

Vineet Kumar 2200160140035 Page 11


2. Updating the specific record:
Update table name
Set field name=”
where field name =’expression’;

Example2:Update the client_name corresponding the client_no of C02001 in


the client_master table.
Update client_master
Set client_name=’rajesh’
Where client_ no=’C02001’

3. Deleting the specific record:


Delete from tablename
Where clicnt_no=’C02001

NULL VALUE CONCEPT


If the user do not enter any value in a participation field then oracle will place a
null value in the column in the absence of user defined values. A null is different
from a blank or zero null values are treated by oracle. A null value can be inserted
into the column of any data types. When a columns defined as not null value then
that column become a mandatory column. The not null constraints only apply at
column level.

Syntax for setting null value in the field:


Create table t2(roll_no number(10) not null, name value(20),amount
number(10,2) not null);

Principle of null values:


1. Setting a null value is appropriate when the actual value is unknown, Or
when a value would not be meaningfully.

Vineet Kumar 2200160140035 Page 12


2. A null value is not equivalent to a value of zero if the data type is number
and space if the data type is character.
3. A null value will evaluate to null in any expression eg null*10=NULL.
4. Null value can be inserted into column of any data type.
5. If the column has a null value oracle ignores the UNIQUE, FORIGN KEY,
CHECK constraints that may be attached to the column.

CONSTRAINTS
The value which are enforced on data being entered, and prevent the user from
entering invalid data into table are called constraints. With the help of constraints
are as a part of global table destination by the Oracle engine in its system table.

Constraints are of two types:


1. Column level constraints
2. Table level constraints

Column level constraints:


Column level constraints are defined along with the column definition, when
creating or altering table structure, they are column level constraints. Column
level constraints are applied to the current column is the immediately proceeds
the constraints.

Example 1:Crete table t1(roll_no number(10) primary key, name var char2(20));
Table level constraints:
If the data constraints defined after defining all the table columns when creating
or altering the table structure it is the table level constraints.

Example 2:Create table t1(roll_no number(10), name varchar2(20), address


varchar2(20), primary key(roll_no));

Vineet Kumar 2200160140035 Page 13


Name of constraints
Unique constraints:
The purpose of unique key is to ensure that information is in the column is
unique.

Column level:
Create table t1 (roll_no number (10),UNIQUE);

Table level:
Create table t1(rill_no number(10),name varchar2(20)
UNIQUE(roll_no));

Check constraints:
Check constraints are used to satisfy the condition when you entering the data in
the column .check constraints must be avoided if the constraints can be defined
using not null, primary key or foreign key.

Syntax:
At column level:
Create table t1(roll_no number(10),name varchar2(20)check(name like ‘c
%’),address varchar2(20));

At table level:
Create table (roll_number(10),name varchar2(20),address
varchar2(20)check(name like’c%’));

To removing a constraints from a table:


Syntax:Alter table tablename DROP PRIMARY KEY;
DEFAULT

Vineet Kumar 2200160140035 Page 14


Default value concept is used when a user is loading a record with values and
leave the column empty. Then the oracle engine will automatically load this
column with the default value specified.

Syntax:
Create tablet1(roll_no number(10),name varchar2(20),default ‘F’,address
varchar2(20);

DISTINGUISH VALUES
It cannot show you duplicated values. The DISTINCT keyword eliminates rows
from the result of a select command.
Select distinct*from tablename;
Select DISTINCT*from t1;
In column
Select DISTINCTcolumn name from table name;

Vineet Kumar 2200160140035 Page 15


GROUP BY CLAUSE
(THE CONCEPT OF GROUPING)
The concept of grouping are as follows:
1. Retrieve all the rows from database table
2. Retrieve selected rows from tables with the use of where clause, which
return only those that need condition specified.
3. Retrieve unique rows from the table with the use of distinguished clause.
4. Retrieve rows in the stored order ascending or descending clause.
5. Retrieve rows in the stored order ascending or descending order as
specified use of GROUP BY CLAUSE.

SYNTAX:
Create table sales_order (order_no number (20),product_no
number (20),Qnt_no number(20) ,Qnt_dispatch number(20),amount
number(10));

EXAMPLE 1: Apply the group by clause in calculating the qty_ordered in the


following table.
ORDER PRODUCT _NO QTY_ORDERED QTY_DISP
------------ -------------------------- --------------------------------------
1 1001 10 10
2 1002 10 8
3 1001 10 10
4 1003 5 5
5 1001 5 5

Vineet Kumar 2200160140035 Page 16


Select product_no ,sum(qty-ordered)”total” from sales_order Group by
product_no;
PRODUCT_NO TOTAL
------------------------- ---------------------
1001 25
1002 10
1003 5

HAVING CONDITION
Having condition is applied in the sql query statements only with Group by Clause.
When the user want to see the details of any particular person or product or any
other thing while using Group By Clause, then having condition is applied in the
sql query statement.

EXAMPLE 1: Let the example of sales_order table .The table is given below:
ORDER PRODUCT-NO QTY-ORDERED QTY-DISP
-------------------------------- -------------------------- ------------------
1 1001 10 10
2 1002 10 8
3 1001 10 10
4 1003 5 5
5 1001 5 5

If we want to see only the details corresponding the product no of 1001 then we
can do it by applying the having condition in the query as following.
Select product _no ,sum(qty _ordered)”total qty ordered “ from sales _order
GROUP BY product _no HAVING product _no =(1001)

Vineet Kumar 2200160140035 Page 17


PRODUCT_NO TOTAL QTY ORDERED
---------------------- -------------------------------------
1001 25

PRIMARY KEY CONCEPT


A primary key is one or more column in a table used to uniquely identify each row
in the table. A primary key column in a table has special attributes.
It defines the columns cannot as a mandatory column that is the column cannot
be left as blank. The NOT NULL attributes is active.

The data held access the column must be UNIQUE:


The only function of a primary key in a table is to uniquely identify arrow. Only
when a record cannot be uniquely identified using the value in a single column,
will a composite primary key be defined.

PRIMARY KEY Constraint defined at column level:


Syntax: column name data type(size) PRIMARY KEY
Example 1: Create a table sales_ order where the columns order_ no is it
primary key. Define constraint at column level.
CREATE TABLE sales_order (order_no varchar2(6) PRIMARY KEY, order_date,
client_no varchar2(6), dely_addr varchar2(25), salesman_no varcha2(26),
dely_type char(1), billed|_yr char(10),dely_date date, order_status varchar2(10));

Primary key constraint at the level:


Syntax:PRIMARY KEY (Column name t, column name,………..J)

Vineet Kumar 2200160140035 Page 18


FOREIGN KEY CONCEPT
Foreign keys represent relationship between tables. A foreign key is a column (or
group of columns) whose values are derived from the primary or unique key of
some other table.

Foreign table(detail table)


This table in which foreign key is defined and table that defines the primary or
unique and referenced by the foreign key is called master or primary table.

Foreign key constraint defined at column level:

Syntax:column name data type(size) REFERENCES table name[(column


name)][ON DELETE]

Example 1:Primary key is dateorder_no and product_no, foreign key is


dateorder_no and product_no, foreign key is dateorder_no referencing column
order_no in sales_oredr table.
CREATE TABLE sales_order detail(dateorder_no varchar2(6) REFERENCE
sales_order, product_no varchar2(6), qnt_ordered number(8), qnt_disp
number(8), product_rate number(8,2) PRIMARY KEY(dateorder_no, product_no));

Vineet Kumar 2200160140035 Page 19


JOINING MULTIPLE TABLE
Sometimes we require to treat multiple tables as through they were a single
entity. Then a single sql sentence can manipulate data from all the tables. To
achieve this, we have to join tables. Tables are joined on columns that have the
same data type and data width in the table.

Example 1:
Retrieve the order numbers, client names and their order dates from the
client_master and sales_order tables. The order date should be displayed in
‘DD//MM//YY’ format and sorted in ascending order.
Here the data required is in two tables sales_order and client_master. These
tables have to be accessed as through as through they were one entity.

Table name: Sales_order

Order No Client No Order Date


019001 C0006 12-apr-97
019002 C0002 25-dec-97
019003 C0001 03-oct-97
019004 C0005 18-june-97

Table: Client_master
Client No Name Bal Due
C0006 Ashok Mehra 500
C0002 Vishal Parikh !000
C0001 Ajay Mehta 0
C0005 Rohit Roy 0

SELECTorder_no, name, to_char(order_date, ‘DD/MM/YY’)” oredrdate”


Vineet Kumar 2200160140035 Page 20
FROM sales_order, client_master
WHERE client_master.client_no=sales_order.client_no
ORDER BY to_char (order_date, ‘DD/MM/YY’);
UNION, INTERSECT AND MINUS CLAUSE

Union clause:
Multiple queries can be put together and their output combinedusing the union
clause. The Union clause merge the output of two or more queries into a single
set of rows and columns.

Records Only in Records Only


Common Records In
Query one both Queries in Query

second

Output of Union Clause

Example 1: Retrieve the names of all the clients and salesname in the city of
‘Mumbai’ from the table client_master and salesman_master.

Vineet Kumar 2200160140035 Page 21


Table name: client_master

Client No Name City


C0001 Ashok Mehra Mumbai
C0002 Vishal Parikh Delhi
C0003 Ajay Mehta Mumbai
C0004 Rohit Roy Calcutta

Table name: salesman_master

Salesman_no Name City


S00001 Manish Patel Mumbai
S00002 Kiran Dixit Delhi
S00003 Nitlesh Kanna Mumbai
S00004 Mahesh Patil Calcutta

Select SALESMAN_NO “ID”, name


From salesman_master
Where city=’Mumbai’;

UNION
Select client_no “ID”,name
From client_master
Where city=’Mumbai’;

Oracle executes the queries as follows


Select client_no “ID”, name

Vineet Kumar 2200160140035 Page 22


From client_master
Where city=’Mumbai’;

The target will be as follows:


ID Name
C00001 Ashok Mehra
C00003 Ajay Mehta
C00005 Nalini Deewan

Then
Select SALESMAN_NO “ID”, NAME
From salesman_master
Where city=’Mumbai’;

The target will be as follows:


ID Name
S00001 Manish Patel
S00003 Nitesh Khanna

The output of union clause is


ID Name
------------ ---------------
C00001 Ashok Mehra
C00003 Ajay Mehta
C00005 Nalini Patel
S00001 Manish Patel
S00003 Nitlesh

Vineet Kumar 2200160140035 Page 23


INTERSECT CLAUSE
Multiple queries can be put together and their output combined using the intersect
clause . The intersect clause output only rows produced by both the queries
intersect i.e the output in an intersect clause will include only those rows that are
retrieved by both the queries.

Common
Records in
Both queries

Output of the interect clause

Example 1:
Retrieve the salesman name in ‘Mumbai’ whose efforts have resulted into atleast
one sales tranjection.

Table name: salesman_master


Salesman _no Name City
S00001 Manish Patel Mumbai
S00002 Kiran Dixit Delhi
S00003 Nitlesh Kanna Mumbai
S00004 Mahesh Patel Calcutta

Table name: sales_order


Vineet Kumar 2200160140035 Page 24
Order no Order Date Salesman No
O19001 12-apr-97 S00001
O19002 25-dec-97 S00003
019003 03-oct-97 S00001
019004 18-jun-97 S00004

SELECT salesman_no, From salesman_master


Where city=’Mumbai’ ;
INTERSECT
Select salesman-master.salesman_no,name
FROM salesman_master,sales-order_order
WHERE salesman_master.salsman_no=sales_order.salesman_no.

The first query in the intersect example is as follows

Salesman_no Name
S00001 Manish Patel
S00003 Nitesh Khanna

The second query in the intersect example is as follows:


SELECT salesman_master.salesman_no,name
FROM salesman_master,salesman_order
WHERE salesman_master.salesman _no=sales_order.salesman_no;

The target table will be as follows:

Salesman_no Name

Vineet Kumar 2200160140035 Page 25


S00001 Manish Patel
S00003 Nitesh Khanna
S00001 Manish Patel
S00004 Manish Patel

The INSERSECT clause picks up records that are comman in both queries.
Thus, the output after applying the INTERSECT clause will be.

OUTPUT:

Salsman_no Name
----------------- ----------------------
S00001 Manish Patel
S00003 Nitesh Khanna

VIEWS
To reduce redundant data to the minimum possible, oracle allows the creation of
an object called a view. A view is map to a select sentence the table on which the
view is based is described in the form clause of select statement. Thus a view is
map to a table will in effect have a subset of the actual column of the table from it
is built. This technical offered a simple effective way of hiding column of a table.
An inserting effect above the view is that it is stored only as the definition in
oracle system catalog.

The Reason:
1. When data security is require.
2. When data redundancy is to be kept to the minimum while maintaining
data security.

Syntax:

Vineet Kumar 2200160140035 Page 26


Create view a as
Select column1,column2 from table name where column name=expression list;
group by grouping criteria having predicate

READ ONLY VIEWS:


Create view a, as

Select Roll_no,name from tablename;

For Rename(Read only view):


Create View a,as
Select name,address from tablename;

UPDATETABLE VIEW:
Create view a2 from roll_no,name,address from tablename;

PROCEDURE LANGUAGE/STRUCTURE LANGUAGE


This language provide conditional checking, branching & looping. In this language
work on entire block, reduce network traffic on the oracle engine. This language
display user friendly message also. All short calculations done quickly without the
use of oracle engine.

PL/SQL Block:
PL/SQL permits the creation of structured logical block of code that describes
process which have to applied to data. A single PL/SQL block consists of a set of
sql statement together and pass to the oracle engine. The PL/SQL block contain
the Begin & End section that contain the expression section.

Example 1: Write a PL/SQL block to calculate the area of a circle.


DECLARE:
pi constant number(4,2):=3.14
r number(5);

Vineet Kumar 2200160140035 Page 27


i number(2):=0;
area number(5);
n number(2):==0;

BEGIN:
n:=8 n;
while i<n
loop
r:=&r
area:=pi*power(r,2);
insert into rad values(r,area);
i:i+1;
end loop;

END;

Vineet Kumar 2200160140035 Page 28


CURSOR
The oracle engine user a work area for its internal processing in order to execute
an sql statement. This work are provide to sql’s operations and is called a cursor.
The data that is stored in the cursor is called a active data set-cursor are two type.
1. Implicit cursor
2. Explicit cursor
If the oracle engine for its internal has opened a cursor there are known as
implicit cursor, but user defined cursor which is open and used processing data as
required is known as explicit cursor.

Example 1: The HRD manager decided to raise the salary of all emp in dept no
20 by .05. The record also is being entered in emp raise table. It include emp_no,
date etc. Write the PL / SQL Block for it.

DECLARE:Cursor c_emp is selected emp_code, salary from employee where


deptno=20; Deptno=20;
Str_emp_code employee.emp_code%type;
Num_salary employee.salary%type;

BEGIN: Open c_emp;


IF c_emp% IS OPEN THEN
LOOP
FETCH c_emp INTO str_emp code num _salary;
Exist when c_emp% NOT FOUND;
UPDATE employee SET salary= num_salary+(num_salary .05)
Where emp_code=str_emp_code;

INSERT INTO emp_raise VALUES (str_emp_code,


sysdate,num_salary*.05)END LOOP
COMMIT;CLOSE c_emp;
ELSEDbms_output.putline(‘unable to open cursor’);
End if;
End;

Vineet Kumar 2200160140035 Page 29


TRIGGERS
Database triggers are database object created by the sql tools stored the client
and server in the oracle’s system table. PL/SQL can used to write database
triggers. Triggers are used to defined code that is executed /fired when certain
actions or even occres. At the database level trigger can be defined for events
such as inserting a record, delecting a record,updating a record. The major issue
that make these trigger standlone is that they are fired inmplicit(interval) by the
oracle, engine itself not explicit call be the user.

Example 1:Write a PL/SQL code block that will accept an account number from
the user and debit an account number from the user and debit an account of Rs
2000 from the account if the account has a minimum balance of 500 after amount
is debited. The process is to be fired on the account table.

Table name: Account


Account_id Name Bal
------------------ ------------- ------------
AC001 Anuj 5000
AC002 Robert 10000
AC003 Mita 5000
AC004 Sunita 15000
AC005 Melba 10000

Vineet Kumar 2200160140035 Page 30


DECLARE:
Acct_balance number(11,2);
Acct_no varchar2(2,6);
Debit_amt number(5):=2000;
Min_bal constant number(5,2)=500.00;

BEGIN:
Acct_no := &acct_no;
SELECT bal INTO acct_balance
FROM accounts
WHERE account_id=acct_no;
Acct_balance := aact_balance_debit_amit;
If acct_balance>=MIN_BAL then
UPDATE ACCOUNT_ id=acct_no;
END IF;
END;

OUTPUT:
Account_id Name Bal
---------------- ----------- ---------
AC001 Anuj 5000
AC002 Robert 10000
AC003 Mita 5000
AC004 Sunita 15000
AC005 Melba 10000

Vineet Kumar 2200160140035 Page 31

You might also like