Professional Documents
Culture Documents
A Combined DBMS Lab Manual
A Combined DBMS Lab Manual
PORBANDAR
LABORATORY MANUAL
ENROLLMENT NO : _____________________________________
NAME : _____________________________________
DIVISION : _____________________________________
ADDRESS : _____________________________________
_____________________________________
_____________________________________
GOVERNMENT POLYTECHNIC
PORBANDAR
CERTIFICATE
Mr./Misss.__________________________________________________________________
Date of Submission____________________________
SR.
PRACTICAL PAGE DATE SIGN
NO.
DEMONSTRATE VARIOUS
7
NUMERIC FUNCTIONS.
DEMONSTRATE VARIOUS
8
CHARACTER FUNCTIONS.
DEMONSTRATE VARIOUS
9
CONVERSION FUNCTIONS.
PRACTICAL: 1
AIM: PERFORM QUERIES FOR DDL(CREATE) and DML(INSERT)
COMMANDS.
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.
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)
);
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
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.
This will prompt you for the values but for every insert you have to use
forward slash.
Example:
EXERCISE:-
Create the tables described below:-
PRACTICAL: 2
AIM: PERFORM QUERIES FOR RETRIVAL OF RECORDS FROM A
TABLE.
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:
DESCRIBE <TableName>;
Example:
DESCRIBE client_master;
EXERCISE:-
Exercise on retrieving records from a table.
3) Retrieve the list of name, city and state of all the clients.
7) Find the names of salesman who have a salary greater than 10000 rupees.
PRACTICAL: 3
AIM: PERFORM QUERIES FOR DML (UPDATE and DELETE)
COMMANDS.
UPDATE
The UPDATE command is used to change or modify data of a table
Update command used to update
a) Selected rows from table
b) 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
a) Selected rows from table
b) 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’;
EXERCISE:-
Exercise on updating records 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.
3) Delete from client_master where the column state holds the value ‘Tamilnadu’.
PRACTICAL: 4
AIM: PERFORM QUERIES FOR DML (ALTER, TRUNCATE, DROP
and RENAME) COMMANDS.
ALTER TABLE
Alter command used to modify structures of a table.
Alter command can be used to add ,modify, or drop columns in a table.
Syntax:
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;
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;
EXERCISE:-
Exercise on altering table structure, deleting table structure and renaming a table:-
Add a column called ‘Telephone’ of data type number and size=10 to the Client_master
table.
PRACTICAL: 5
AIM: PERFORM QUERIES USING 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
- Subtraction / Division
** Exponentiation () Enclosed Operation
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;
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:
Between operator is used to check between two values or specific range.
Example:
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.
Example:
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)
Example:
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 is similar to * asterisk wildcard
in DOS).
While _(underscore) is used to refer single character.
Example:
select * from client_master where name like ‘_ahul’;
EXERCISE:-
Create table described below:-
Exercise on altering table structure, deleting table structure and renaming a table:-
4) List all employees who stay in cities having ‘d’ as a last character.
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.
8) List eid, ename and city of those employees who lives in city ‘Ahmedabad’ or
‘Surat’ or ‘Vadodara’.
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)
EXERCISE:-
Write output of following queries:-
PRACTICAL: 7
AIM: DEMONSTRATE VARIOUS NUMERIC FUNCTIONS.
NUMERIC FUNCTIONS
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(n)
COSH()
It returns hyperbolic cosine of a given value.
Syntax: COSH(n)
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)
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)
EXERCISE:-
Write output of following queries:-
PRACTICAL: 8
AIM: DEMONSTRATE VARIOUS CHARACTER FUNCTIONS.
CHARACTER FUNCTIONS
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)
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)
EXERCISE:-
Write output of following queries:-
PRACTICAL: 9
AIM: DEMONSTRATE VARIOUS CONVERSION FUNCTIONS.
CONVERSION FUNCTIONS
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 FUNCTIONS
UID
It returns an integer that uniquely identifies the session user.
8
USER
It returns the name by which the current user is known 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
Client Weight
Johnson tools 59
Inf Software 27
Peterson Industries NULL
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)
EXERCISE:-
Write output of following queries:-
PRACTICAL: 10
AIM: DEMONSTRATE VARIOUS GROUP FUNCTIONS.
GROUP FUNCTIONS
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)
EXERCISE:-
Write output of following queries:-
PRACTICAL: 11
AIM: PERFORM QUERIES USING GROUP BY, HAVING AND
ORDER BY CLAUSE.
GROUP BY
HAVING
ORDER BY
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;
EXERCISE:-
Create the tables described below:-
For above table solve following queries using group by, having and order by:
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.