DBMS Lab Manual
DBMS Lab Manual
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)
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
a) By value method
b) By address method
Syntax:
Example:
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.
Syntax:
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> /
4
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
EXERCISE
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
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
9
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
10
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
PRACTICAL: 2
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
Syntax:
SQL> describe client_master;
1
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
EXERCISE
2
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
3) Retrieve the list of name, city and state of all the clients.
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
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
2
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
2) Change the cost_price of ‘Trousers’ to 999.50 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)….);
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.
3
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
3) Change the size of salary column in salesman_master to 10.
4
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
PRACTICAL: 5
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 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;
1
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
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
Exercise on altering table structure, deleting table structure and renaming a table:-
3
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
2) List all employees having salary either 12000 or 20000.
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
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
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
Output:
Output:
Output:
Output:
Output:
Output:
Output:
2
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
PRACTICAL: 7
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
Output:
Output:
Output:
Output:
Output:
Output:
Output:
Output:
3
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
Output:
Output:
Output:
Output:
Output:
Output:
Output:
4
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
PRACTICAL: 8
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
Output:
Output:
Output:
Output:
Output:
Output:
Output:
Output:
3
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
PRACTICAL: 9
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
1
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
Client Weight
Johnson tools 59
Inf Software 27
Peterson Industries NULL
Client Weight
Johnson tools 59
Inf Software 27
Peterson Industries 43
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
Output:
Output:
Output:
Output:
Output:
Output:
3
GOVERNMENT POLYTECHNIC, DAHOD
COMPUTER ENGINEERING DEPARTMENT
DBMS (3330703), Semester – 3rd
PRACTICAL: 10
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
Output:
Output:
Output:
Output:
Output:
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 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>;
Example:
SELECT branch_no“branch_no”,COUNT(emp_no)”No Employees”
From emp_master GROUP BY branch_no;
HAVING Clause
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
For above table solve following queries using group by, having and order by:
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.