0% found this document useful (0 votes)
874 views49 pages

DBMS Lab Manual

This document discusses SQL data types, commands, and queries used to create tables, insert data, and retrieve data from tables in an Oracle database. It provides examples of creating tables named Client_master, Product_master, and Salesman_master using various data types like VARCHAR2, NUMBER, and DATE. The document also demonstrates inserting sample data into these tables and retrieving data using SELECT queries, including using conditions. Exercises are provided to retrieve data from the Client_master table using SELECT queries.

Uploaded by

Paresh Patel
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)
874 views49 pages

DBMS Lab Manual

This document discusses SQL data types, commands, and queries used to create tables, insert data, and retrieve data from tables in an Oracle database. It provides examples of creating tables named Client_master, Product_master, and Salesman_master using various data types like VARCHAR2, NUMBER, and DATE. The document also demonstrates inserting sample data into these tables and retrieving data using SELECT queries, including using conditions. Exercises are provided to retrieve data from the Client_master table using SELECT queries.

Uploaded by

Paresh Patel
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/ 49

GOVERNMENT POLYTECHNIC, DAHOD

COMPUTER ENGINEERING DEPARTMENT


DBMS (3330703), Semester – 3rd

PRACTICAL: 1

AIM: PERFORM QUERIES FOR DDL (CREATE) and DML (INSERT) COMMANDS

Introduction

SQLData Types

 DATATYPE
 CHAR(size)
 VARCHAR(size)/VARCHAR2(size)
 DATE
 NUMBER(P,S)
 LONG
 RAW/LONG RAW

 CHAR(SIZE)

 This data type is used to store character strings values of fixed length.
 The size represents length of character should be stored
 This data can store maximum 255 characters.
 Example: Name CHAR(60)

 VARCHAR(size)/VARCHAR2(size)

 This data type used store variable length alphanumeric data.


 The maximum size of this data type is 4000 characters.

 DATE
 This data type is used to store date and time.
 The standard format is DD-MON-YY for example 21-JUN-09.
 Date time stores time in the 24-hours format.
 Range of this data type is January 1, 4712 B.C. to December 31, 4712 A.D.

1
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
 NUMBER(P,S)
 The NUMBER data type is used store numbers (both Integer and floating
point numbers).
 We can store number up to 38 characters long.
 The precision (P), determines the maximum length of number, and scale (s),
determines the point sign (.) right from a number(P).

 LONG
 This data type is used to store variable length character strings up to 2 GB.
 LONG data can be used to store binary data in ASCII format.

 RAW/LONG RAW
 The RAW/LONG RAW data type are used to store binary data, such as
digitized picture or image
 RAW data type can have a maximum length of 255 bytes.
 LONG RAW data type can contain up to 2 GB.

SQL Commands
 CREATE
 Create command used to create a table in database.

Syntax:
SQL> Create table <table name>
(
<columnname1> <datatype> (<size>),
<columnname2> <datatype> (<size>)
);
Example:
SQL> Create table client_master
(
client_no varchar2(6),
name varchar2(20),
city varchar2(15),
pincode number(8),
state varchar2(15)
);

2
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
 INSERT

 Used to insert data into a table or create a new row in table:

We have two methods to insert data in a table

a) By value method

b) By address method

a) USING VALUE METHOD

Syntax:

insert into <table_name> values (value1,value2, value3 …. Valuen);

Example:

SQL) insert into client_master


values(‘C00001’,’RAHUL’,’Mumbai’,400054,’Maharashtra’);

SQL) insert into client_master


values (‘C00002’,’MANISH’,’Bangalore’,560001,’Karnataka’);

To insert a new record again you have to type entire insert command, if
there are lot of records this will be difficult. This will be avoided by using
address method.

b) USING ADDRESS METHOD

Syntax:

insert into <table_name> values (&col1, &col2, &col3 …. &coln);

This will prompt you for the values but for every insert you have to use
forward slash.

3
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
Ex:

SQL> insert into client_master


values (‘&client_no’, '&name', ‘&city’,&pincode,’&state’);

Enter value for client_no: C0001


Enter value for name: Rahul
Enter value for city: Mumbai
Enter value for pincode: 400054
Enter value for state: Maharashtra

SQL> /

Enter value for client_no: C0002


Enter value for name: Manish
Enter value for city: Bangalore
Enter value for pincode: 560001
Enter value for state: Karnataka

4
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
EXERCISE

Create the tables described below:-

Table Name : Client_master

COLUMN NAME DATA TYPE SIZE


Client_no Varchar2 6
Name Varchar2 20
City Varchar2 15
Pincode Number 8
State Varchar2 15

Insert data for Client_master table:

Client_no Name City Pincode State


C00001 Ivan bayross Bombay 4000054 Maharashtra
C00002 Vandana Madras 7800001 Tamil Nadu
C00003 Pramala Bombay 4000057 Maharashtra
C00004 Chhaya Bangalore 560001 Karnataka
C00005 Hansel Bombay 400060 Maharashtra
C00006 Deepak Mangalore 560050 Karnataka

5
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

6
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
Table Name : Product_master

COLUMN NAME DATA TYPE SIZE


Product_no Varchar2 6
Description Varchar2 20
Quantity Number 8
Sell_price Number 8,2
Cost_price Number 8,2

Insert data for Product_master table

Product_no Description Quantity Sell_price Cost_price


P00001 1.44 floppies 100 525 500
P00002 Monitor 10 12000 11280
P00003 Mouse 20 1050 1000
P00004 Jeans 50 750 500
P07885 Trousers 60 850 550
P07975 Skirts 50 450 300

7
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

8
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
Table Name : Salesman_master

COLUMN NAME DATA TYPE SIZE


Salesman_no Varchar2 6
Salesman_name Varchar2 20
City Varchar2 20
Pincode Number 8
State Varchar2 20
Salary Number 8
Remarks Varchar2 20

Insert data for Salesman_master table

Salesman_no Salesman_name City Pincode State Salary Remarks


S00001 Kiran Bombay 400002 Maharashtra 35000 Good
S00002 Manish Bombay 400001 Maharashtra 5000 Good
S00003 Ravi Bombay 400003 Maharashtra 4000 Average
S00004 Prem Bombay 400002 Maharashtra 8000 Good
S00005 Vishal Bombay 400001 Maharashtra 12000 Good
S00006 Rahul Bombay 400003 Maharashtra 25000 Good

9
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

10
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

PRACTICAL: 2

AIM: PERFORM QUERIES FOR RETRIVAL OF RECORDS FROM A TABLE

Introduction
 SELECT
 The SELECT command is used to retrieve selected rows from one or more
tables

Syntax:
1). SELECT * FROM <TableName>;
2). SELECT <ColumnName1>,<ColumnName2>
From <TableName>;
3). SELECT * FROM <TableName> WHERE <Condition>;

Example:
1). SELECT * FROM client_master;
2). SELECT name, city FROM client_master;
3). SELECT * FROM client_master
Where city=’Bombay’;

 DESCRIBE

 The DESCRIBE command is used to display information of table structure.

Syntax:
SQL> describe client_master;

Name Null? Type


-------------------------------------------------------------------
Client_no Varchar2(6)
Name Varchar2(20)
City Varchar2(15)
Pincode Number(8)
State Varchar2(2)

1
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

EXERCISE

Exercise on retrieving records from a table.

1) Retrieve entire contents of Client_master table.

2) Find out the name of all the clients.

2
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
3) Retrieve the list of name, city and state of all the clients.

4) List the various products available from the Product_master table.

5) List all the clients who are located in Bombay.

3
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
6) List all the products whose sell_price is >= 1000 rupees.

7) Find the names of salesman who have a salary greater than 10000 rupees.

4
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
8) Display the structure of product_master, salesman_master tables.

5
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
9) Display list of all tables

6
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

PRACTICAL: 3

AIM: PERFORM QUERIES FOR DML (UPDATE, DELETE) COMMANDS


 UPDATE
 The UPDATE command is used to change or modify data of a table
 Update command used to update
 Selected rows from table
 All the rows from table
Syntax:
Update <table name>
Set <columnname1> = <expression1>,
<columnname2> = <expression2>;
Where condition;
Example:
Update client_master
Set city=’Bombay’
Where client_no=’C00002’;

 DELETE
 Delete command used to delete data or rows from a table
 delete command used to delete
 Selected rows from table
 All the rows from table
Syntax:
1) Delete from <table name>;
2) Delete from <table name> Where <Condition>;

Example:
1) Delete from client_master;
2) Delete from client_master Where client_no=’C00001’;

1
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
EXERCISE

Exercise on updating records in a table:-

1) Change the city of client_no ‘C00005’ to ‘Bangalore’ in Client_master table.

2
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
2) Change the cost_price of ‘Trousers’ to 999.50 rupees.

3) Change the salary of ‘S00002’ to 6500 rupees.

3
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
Exercise on deleting record in a table:-

1) Delete all salesman from salesman_master whose salaries are greater than
5000 rupees.

2) Delete all products from Product_master where the quantity is equal to 50.

4
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

3) Delete from client_master where the column state holds the value ‘Tamil Nadu’.

5
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

PRACTICAL: 4

AIM: PERFORM QUERIES FOR DML (ALTER, DROP and RENAME) COMMANDS

 ALTER
 Alter command used to modify structures of a table.
 Alter command can be used to add ,modify, or drop columns in a table

Syntax:
Adding New Columns
SQL> Alter table TableName
Add (NewColumnName Datatype(size),
NewColumnName Datatype(size)….);

Modifying Existing Columns


SQL> Alter table TableName
Modify (ColumnName NewDatatype(NewSize));

Dropping(deleting) Existing Columns


SQL> Alter table TableName
Drop column columname;

Example:
1).alter table client_master add(telephone_no number(10));
2) alter table product_master modify(sell_price number(10,2));
3) alter table emp drop column dept;

 TRUNCATE TABLE
 TRUNCATE TABLE used to delete all data from a table
 Logically, this is equivalent to DELETE statement that deletes all rows
 TRUNCATE command is faster than DELETE command
 The number of deleted rows are not returned

Syntax:
TRUNCATE TABLE <TableName>

Example:
SQL> TRUNCATE TABLE client_master;

1
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

 DROP TABLE
 DROP TABLE command is used to delete or destroy table from a database

Syntax:
DROP TABLE <TableName>

Example:
SQL> DROP TABLE client_master;

 RENAME TABLE
 RENAME TABLE command is used to change the name of existing table.

Syntax:
RENAME <TableName> TO <NewTableName>

Example:
SQL> RENAME Client_master TO Client;

2
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
EXERCISE

Exercise on altering table structure, deleting table structure and renaming a table:-

1) Add a column called ‘Telephone’ of data type number and size=10 to the
Client_master table.

2) Change the size of Sell_price column in product_master to (10,2)

3
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
3) Change the size of salary column in salesman_master to 10.

4) Destroy the table Client_master along with its data.

5) Change the name of the Salesman_master table to sman_master.

4
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

PRACTICAL: 5

AIM: PERFORM QUERIES USING OPERATORS

Operators

 Arithmetic Operators
 Oracle allows arithmetic operators to used while viewing records from table or
while performing data manipulation operations such as Insert, Update and Delete.
These are:
+ Addition * Multiplication
** Exponentiation () Enclosed operation
- Subtraction / Division

Example:
Select product_no, description, sell_price * 0.05 from product_master;

 Logical Operators
 The AND Operator
 The OR Operator
 Combining the AND and OR Operator
 The NOT Operator

 The AND Operator


 The AND Operator allows creating an SQL statement based on two or more
conditions being met.
 It can be used in any valid SQL statement such as select, insert, or delete.
 Example: Select product_no, description, sell_price * 0.05 from
product_master where sell_price between 500 and 1100;

 The OR Operator
 The OR condition allows creating an SQL statement where records are
returned when any one of the conditions are met
 It can be used in any valid SQL statement such as select, insert, or delete.
 Example: select client_no, name, city, pincode from client_master
Where pincode=4000054 or pincode=4000057;

 Combining the AND and OR Operator


 The AND and OR conditions can be combined in a single SQL statement
 It can be used in any valid SQL statement such as select, insert, or delete.

1
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

 The NOT Operator


 The Oracle engine will process all rows in a table and display only those
records that do not satisfy the condition specified
 Example: select * from client_master where not ( city=’Bombay’, or
city=’Delhi’ );

 Comparison Operators
 Comparison operators are used in condition to compare one expression with other.
The comparison operators are =, <, >, >=, <=, !=, between, like, is null and in
operators

 Between operator is used to check between two values or specific range


 Example:
SQL>select * from salesman_master where salary between 5000 and 8000;
 The above select statement will display only those rows where salary of
salesman is between 5000 and 8000.

 In operator:
 The IN operator can be used to select rows that match one of the values in a
list
 SQL> select * from client_master where client_no in(C00001, C00003);
 The above query will retrieve only those rows where client_no is either in
C00001 or C00003

 Like operator:
 Like operator is used to search character pattern,.
 The like operator is used with special character % and _(underscore)
 SQL> select * from client_master where city like ‘ b% ’;
 The above select statement will display only those rows where city is start
with ‘b’ followed by any number of any characters.
 % sign is used to refer number of character ( it similar to * asterisk wildcard in
DOS).
 While _(underscore) is used to refer single character.
 SQL> select * from client_master where name like ‘_ahul’;

2
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
EXERCISE

Create table described below:-

Table Name : Emp

COLUMN NAME DATA TYPE SIZE


Eid Char 4
Ename Varchar2 10
Birthdate Date --
Salary Number 7
City Char 10

Insert data for Emp table:

Eid Ename Birthdate Salary City


E01 Tulsi 26-JAN-82 12000 Ahmedabad
E02 Gopi 15-AUG-83 15000 Anand
E03 Rajshree 31-OCT-84 20000 Vadodara
E04 Vaishali 23-MAR-85 25000 Surat
E05 Laxmi 14-FEB-83 18000 Anand
E06 Shivali 05-SEP-84 20000 Surat

Exercise on altering table structure, deleting table structure and renaming a table:-

1) Display birthdate for employees having id ‘E01’ and ‘E03’.

3
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
2) List all employees having salary either 12000 or 20000.

3) List all employees who lives in city ‘Ahmedabad’ or ‘Surat’.

4) List all employees who stay in cities having ‘d’ as a last character.

4
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

5) List all employees whose name ends with ‘ali’.

6) List all employees whose name starts with ‘T’ or ‘V’ and ends with ‘i’.

7) List ename and salary of those employees having salary between 10000 and
20000.

5
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

8) List eid, ename and city of those employees who lives in city ‘Ahmedabad’ or
‘Surat’ or ‘Vadodara’.

6
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

PRACTICAL: 6

AIM: DEMONSTRATE VARIOUS DATE FUNCTIONS

 Date function

 ADD_MONTHS()
 It returns the date after adding the number of months specified
within the function
 Syntax: ADD_MONTHS(date,n)

 MONTHS_BETWEEN
 It returns the date the number of months between the specified
dates.
 Syntax: MONTHS_BETWEEN(date1,date2)

 Round()
 This function round a date in specified format
 Syntax: ROUND(date,format)

 NEXT_DAY
 It returns the date of the first weekday named by ‘char’ that is
after the date named by ‘date’. ‘char’ must be day of the week.
 Syntax: NEXT_DAY(date,’char’)

 TRUNC()
 This function trunc a date in specified format
 Syntax: TRUNC(date,format)

 GREATEST()
 It displays the latest date from a list of dates
 Syntax: GREATEST (date1, date2, date3 …)

 NEW_TIME()
 It display the date in the required time zone
 Syntax: NEW_TIME (date,currenttimezone,newtimezone)

1
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
EXERCISE

Write output of following queries

1) select ADD_MONTHS (’04-jun-2009’,2) from dual;

Output:

2) select MONTHS_BETWEEN (’02-FEB-09’,’02-JAN-09’) from dual;

Output:

3) select ROUND(sysdate,’year’) from dual;

Output:

4) select NEXT_DAY(‘04-FEB-09’,’FRIDAY’) from dual;

Output:

5) select trunc(sysdate,’year’) from dual;

Output:

6) select GREATEST(’02-FEB-09’, ’02-JAN-09’) from dual;

Output:

7) select NEW_TIME(sysdate,'EST','HST') from dual;

Output:

2
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

PRACTICAL: 7

AIM: DEMONSTRATE VARIOUS NUMERIC FUNCTIONS

 Numeric function

 ABS()
 It returns the absolute value.
 Syntax: ABS(n)

 CEIL()
 It returns the smallest integer that is greater than or equal to a
specific value
 Syntax: CEIL(n)

 COS()
 It returns the cosine of a given value.
 Syntax: COS

 COSH()
 It returns hyperbolic cosine of a given value.
 Syntax: COSH

 EXP()
 It returns e raised to the nth power, where e=2.71828183.
 Syntax: EXP(n)

 FLOOR()
 It returns the greatest integer that is less than or equal to a
specific value.
 Syntax: FLOOR(n)

 POWER()
 It returns the value raised to a given positive exponent.
 Syntax: POWER(value, exponent)

1
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
 MOD()
 It divides a value by a divisor and returns the remainder.
 Syntax: MOD(value, divisor)

 ROUND()
 It rounds a number to given number of digits of precision
 Syntax: ROUND(value, precision)

 TRUNC()
 It truncates digits of precision from a number
 Syntax: TRUNC(value, precision)

 SORT()
 It returns the square root given number
 Syntax: SQRT(n)

2
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
EXERCISE

Write output of following queries

1) select ABS(-15) from dual;

Output:

2) select CEIL(1.3) from dual;

Output:

select CEIL(2) from dual;

Output:

3) select COS(45) from dual;

Output:

4) select COSH(45) from dual;

Output:

5) select EXP(5) “Exponent” from dual;

Output:

6) select FLOOR(1.3) from dual;

Output:

select FLOOR(-2.3) from dual;

Output:

3
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

7) select POWER(3,2) from dual;

Output:

select POWER(64,0.5) from dual;

Output:

8) select MOD(100,10) from dual;

Output:

select MOD(10,3) from dual;

Output:

9) select ROUND(66.666,2) from dual;

Output:

10) select TRUNC (66.666,2) from dual;

Output:

11) select SQRT(64) from dual;

Output:

4
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

PRACTICAL: 8

AIM: DEMONSTRATE VARIOUS CHARACTER FUNCTIONS

 Character function

 INITCAP()
 Returns a string with the first letter of each word in UPPER
CASE
 Syntax: INITCAP(char)

 LOWER()
 It takes any string or column and converts it into lowercase
 Syntax: LOWER(string)

 UPPER()
 It takes any string or column and converts it into upper case
 Syntax: UPPER(string)

 LTRIM()
 It removes character from the left of char with initial characters
removed up to the first character not in set
 Syntax: LTRIM(char, set)

 RTRIM()
 It returns char with final character removed after the last
character not in the set. ’set’ is optional, it defaults to spaces.
 Syntax: RTRIM(char, set)

 TRANSLATE()
 Replace a sequence of character in a string with another set of
character.
 Syntax: TRANSLATE(string1,string to replace, replacement
string)

 REPLACE()
 It replaces a character in a string with zero or more character.
 Syntax: REPLACE(string1, character to be replaced, characters)

1
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
 SUBSTRING()
 It returns a substring from a given string
 It returns a portion of char, beginning at character m exceeding
up to n characters.
 Syntax: SUBSTR(string)

2
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
EXERCISE

Write output of following queries

1) select INITCAP('rahul kumar') from dual;

Output:

2) select LOWER(‘RAHUL’) from dual;

Output:

3) select UPPER(‘rahul’) from dual;

Output:

4) select LTRIM(‘RAHUL’,’R’) from dual;

Output:

5) select RTRIM(‘RAHUL’,’L’) from dual;

Output:

6) select TRANSLATE(‘1sct523’,’123’,’7a9’) from dual;

Output:

7) select REPLACE(‘Hello’, ’o’, ’rec’ ) from dual;

Output:

8) select SUBSTR(‘SECURE’,3,4) from dual;

Output:

3
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

PRACTICAL: 9

AIM: DEMONSTRATE VARIOUS CONVERSION FUNCTIONS

 Conversion function

 TO_CHAR()
 It converts a value of DATE data type to CHAR value. It accept
a date as well as the format in which the date has to appear.
The format must be a date format
 Syntax: TO_CHAR(date, format)

 TO_DATE()
 It converts a CHAR filed to a DATE filed.
 Syntax: TO_DATE (‘char’, format)

 TO_NUMBER()
 It converts a character value containing a number to a value of
NUMBER data type.
 Syntax: TO_NUMBER(’char’)

 Miscellaneous function

 UID
 It returns an integer that uniquely identifies the session user.

 USER
 It returns the name by which the current user is know to Oracle.

 NVL
 It stands for Null value Substitution.
 Syntax: NVL(value, substitute)
 If the value is NULL, this function is equal to substitute.
 If the value is not NULL, this unction is equal to value.
 Example

Table name: Shipping

1
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
Client Weight
Johnson tools 59
Inf Software 27
Peterson Industries NULL

Select NVL (weight, 43) from shipping;

Client Weight
Johnson tools 59
Inf Software 27
Peterson Industries 43

In the above output, the NVL function replaces the value


specified in wherever it encounters a NULL value. Hence, 43 is
inserted for client Peterson Industries.

 VSIZE
 It returns the number of bytes in the internal representation of
an expression.
 Syntax: VSIZE(expression)

2
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
EXERCISE

Write output of following queries

1) select TO_CHAR(SYSDATE,’DD-MM-YY’) from dual;

Output:

2) select TO_DATE(‘26/08/09’,’DD/MM/YY’) from dual;

Output:

3) select TO_NUMBER(‘100’) from dual;

Output:

4) select UID from dual;

Output:

5) select USER from dual;

Output:

6) select VSIZE(‘SCT on the net’) from dual;

Output:

3
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

PRACTICAL: 10

AIM: DEMONSTRATE VARIOUS GROUP FUNCTIONS

 Group function

 AVG()
 It returns average value of the specified column, ignoring NULL
values.
 Syntax: AVG(Column name)

 MIN()
 It returns the minimum value in the specified column.
 Syntax: MIN(column name)

 MAX()
 It returns the maximum value in the specified column.
 Syntax: MAX(column name)

 SUM()
 It returns the sum of values of the specified column.
 Syntax: SUM(column name)

 COUNT()
 It returns the number of rows in specified column or the total
number of rows in the table.
 Syntax: COUNT(column name)
 Syntax: COUNT(*)

 DECODE
 It is transformation function that does an orderly value-by-value
substitution.
 Syntax: DECODE(string,if1,then1,if2,then2,…..,else)

1
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

EXERCISE

Write output of following queries

1) select AVG(sell_price) from product_master;

Output:

2) select MIN(sell_price) from product_master;

Output:

3) select MAX(sell_price) from product_master;

Output:

4) select SUM(salary) from salesman_master;

Output:

5) select COUNT(salesman_no) from salesman_master;

Output:

select COUNT(*) from salesman_master;

Output:

6) select DECODE(‘SATUR’,’SUN’,’HOLIDAY’,’SATUR’,’HALFDAY’,
’FULL DAY’) from dual;

Output:

2
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

PRACTICAL: 11

AIM: PERFORM QUERIES USING GROUP BY, HAVING AND ORDER BY CLAUSE

 Group by, Having by and Order by clause

 GROUP BY Clause

 GROUP BY clause is another section of the select statement


 This optional clause tells Oracle to group rows based on distinct values that
exist for specified columns
 The GROUP BY clause creates a data set, containing several sets of records
grouped together based on a condition

 Syntax:
Select <column name1>,<column name2>, <column nameN>
AGGREGATE_FUNCTION (<Expression>)
From Table Name WHERE <condition>
GROUP BY<column name1>,<column name2>,<column nameN>;

 Example:
SELECT branch_no“branch_no”,COUNT(emp_no)”No Employees”
From emp_master GROUP BY branch_no;

 HAVING Clause

 HAVING Clause can be used in conjunction with the GROUP BY clause


HAVING imposed a condition on the GROUP BY clause.
 which further filter the group created by the groups by clause
 Syntax:
Select <column name1>,<column name2>, <column nameN>
AGGREGATE_FUNCTION (<Expression>)
From Table Name WHERE <condition>
GROUP BY<column name1>,<column name2>,<column nameN>;
HAVING <condition>
 Example:
Select product_no,sum (qty_ordered) “total QTY ordered”
From sales_order GROUP BY product_no
HAVING product_no=’P00001’ or product_no=’P00004’

1
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
 HAVING clause is specified row displayed on screen.

 ORDER BY Clause

 ORDER BY is use want the information it return sorted in the order specify.
 Syntax:
 Select <column name1>,<column name2>, <column nameN>
From Table Name Where <condition>
ORDER BY <column name1>;
 Example:
Select feature, section, page from NEWSPAPER
Where section=’F’
ORDER BY feature;

2
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
EXERCISE

Create the tables described below:-

Table Name : EMPLOYEE

COLUMN NAME DATA TYPE SIZE


Emp_no Number 4
Ename Varchar2 10
Salary Number 6
Join_date date
Deptno Number 2

Insert data for EMPLOYEE table:

Emp_no Ename Salary Join_date Deptno


101 Jiya 10000 12-DEC-90 10
102 Rohit 15000 15-NOV-94 20
103 Dinesh 7000 10-JAN-89 10
104 Chhaya 12000 25-OCT-98 30
105 Rima 14000 21-JUL-88 20
106 Payal 8000 17-MAR-90 10

For above table solve following queries using group by, having and order by:

1) Display department wise salary total.

3
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd

2) Display salary total of all employees who works in department number 10.

3) Sort the data of EMPLOYEE table based on salary column in descending order.

4) Display ename and salary of employees based on name in alphabetical order.

You might also like