Sunteți pe pagina 1din 30

EXPERIMENT 2 Creating a Table

Aim: To create a Student Table with the attributes RollNo, Name, Branch, Year, Date of Birth
and Total_Marks where RollNo is Primary Key and Name must not be NULL.

Apparatus: A Personal Computer loaded with Oracle 11g software.

Description: The data in RDBMS is stored in database objects called tables. The table is a
collection of related data entries and it consists of columns and rows. Every table is broken up
into smaller entities called fields i.e., Rows and Columns. Syntax for creating a table is:
Create table <table_name> (col1 datatype1, col2 datatype2 …coln datatypen);

Query:
create table student(
RollNo varchar2(10) Primary Key,
Name varchar2(20) Not Null,
Branch varchar2(10),
Year varchar2(3),
DOB date,
Total_Marks number(3));

Output:
Table created.

Result:
Student Table created successfully with the given constraints.

1
EXPERIMENT 3 Inserting Records into a Table

Aim: To insert 5 records into a table named Student with the attributes RollNo, Name, Branch,
Year, Date of Birth and Total_Marks where RollNo is primary key and Name must not be null.

Apparatus: A Personal Computer loaded with Oracle 11g software.

Description: The SQL INSERT Statement is used to add new rows of data into a table in the
database. We may not need to specify the column(s) name in the SQL query if you are adding
values for all the columns of the table. But make sure the order of the values is in the same order
as the columns in the table. The syntax for inserting a record into a table is:
insert into <table_name> values (value1, value2, value3 …. Valuen);

Queries:
 insert into student values(‘14P11F0001’, ‘Aravind’,’CSE’,’II’,’19-feb-1994’,350);
Output: 1 row created.
 insert into student values(‘14P11F0002’, ‘Bhanu’,’CSE’,’II’,’10-dec-1993’,385);
Output: 1 row created.
 insert into student values(‘14P11E0001’, ‘Mahesh’,’EEE’,’II’,’12-mar-1994’,420);
Output: 1 row created.
 insert into student values(‘14P11E0002’, ‘Kiran’,’EEE’,’II’,’06-feb-1994’,416);
Output: 1 row created.
 insert into student values(‘14P11F0003’, ‘Naresh’,’CSE’,’II’,’11-nov-1993’,464);
Output: 1 row created.

Result:
5 rows inserted into student table successfully.

2
EXPERIMENT 4 Updating Records of a Table

Aim: i) To update student table column Total_Marks as 453 whose RollNo is ‘14P11F0001’
ii) To update student table column Name as “Kiran Kumar” whose RollNo is ‘14P11E0002’

Apparatus: A Personal Computer loaded with Oracle 11g software.

Description: The SQL UPDATE Query is used to modify the existing records in a table. We can
use WHERE clause with UPDATE query to update selected rows otherwise all the rows would
be affected. The syntax for Update query is:
Update <table_name>
set <col1> = value1, <col2> = value2 , …
where <condition>;

Queries:
i) Update student
set Total_Marks = 453
where RollNo = ‘14P11F0001’;
Output: 1 row updated.

ii) Update student


set Name = ‘Kiran Kumar’
where RollNo = ‘14P11E0002’;
Output: 1 row updated.

Result:
Records of student table updated successfully with the specified values.

3
EXPERIMENT 5 Modifying Structure of the Table

Aim: i) To add new column Semester varchar2(2) into Student table.


ii) To modify the size of column Name varchar2(20) as varchar2(30) in Student table.
iii) To drop column Total_Marks in Student table.

Apparatus: A Personal Computer with loaded Oracle 11g software.

Description: The SQL ALTER TABLE command is used to modify the definition (structure) of
a table by modifying the definition of its columns.The ALTER command is used to perform Add,
drop, and modify table columns. The syntax for alter table command is :
 Adding Column Alter Table <Table_Name> Add <Col Datatype>;
 Modifying Column Alter Table <Table_Name> Modify <Col Datatype>;
 Dropping Column Alter Table <Table_Name> Drop column <col name>;

Queries:
i) alter table student add Semester varchar2(2);
Output: Table altered.
ii) alter table student modify Name varchar2(30);
Output: Table altered.
iii) alter table student drop column Total_Marks;
Output: Table altered.

Result:
A new column Semester is added into Student table; size of Name attribute (column) is modified
that is increased to size 30 characters and Total_Marks column is dropped.

4
EXPERIMENT 6 Displaying Records from a Table

Aim: i) To display all records present in Student Table.


ii) To display RollNo, Name, DOB columns present in Student Table.

Apparatus: A Personal Computer with loaded Oracle 11g software.

Description: SELECT statement is used to fetch the data from a database table which returns
data in the form of result table. These result tables are called result-sets.
 If we want to fetch all the fields available in a table, syntax is:
Select * from <table_name>; -- here * indicates all columns
 If we want to fetch some of the fields available in a table, syntax is:
Select col1, col2, … coln from <table_name>;

Queries:
i) select * from student;
Output:
RollNo Name Branch Year DOB Semester
---------------- --------------------------- ----------------- --------- ----------------- ------------
14P11F0001 Aravind CSE II 19-FEB-1994
14P11F0002 Bhanu CSE II 10-DEC-1993
14P11E0001 Mahesh EEE II 12-MAR-1994
14P11E0002 Kiran Kumar EEE II 06-FEB-1994
14P11F0003 Naresh CSE II 11-NOV-1993

5 rows selected.

ii) select rollno, name, dob from student;


Output:
RollNo Name DOB
---------------- --------------------------- -----------------
14P11F0001 Aravind 19-FEB-1994
14P11F0002 Bhanu 10-DEC-1993
5
14P11E0001 Mahesh 12-MAR-1994
14P11E0002 Kiran Kumar 06-FEB-1994
14P11F0003 Naresh 11-NOV-1993

5 rows selected.

Result:
Hence records are displayed successfully from Student Table.

6
EXPERIMENT 7 Displaying Specific Records

Aim: To display the following details from Student Table:


i) Student record whose RollNo is ‘14P11E0002’
ii) Student records in ascending order based on their names.
iii) Student records whose RollNo are in the given list: (‘14P11E0001’, ‘14P11F0001’,
‘14P11F0003’ )
iv) Student records whose Name is either ‘Mahesh’ or ‘Naresh’
v) Student records whose Names are not in the given list: (‘Mahesh’, ‘Naresh’, ‘Kiran
Kumar’)
vi) Student record whose Name is ‘Kiran Kumar’ and DOB is ‘06-FEB-1994’.

Apparatus: A Personal Computer with loaded Oracle 11g software.

Description: SELECT statement is used to fetch the data from a database table which returns
data in the form of result table. These result tables are called result-sets.
 If we want to fetch records based on a condition, syntax is:
Select * from <table_name> where condition;
 If we want to fetch records in sorting order, syntax is:
Select * from <table_name> order by <column-name> [desc];
Hint: desc is optional. We can use desc, if we want to sort the records in descending order.

Queries:
i) Select * from student where RollNo = ‘14P11E0002’;
Output:
RollNo Name Branch Year DOB Semester
---------------- --------------------------- ----------------- --------- ----------------- ------------
14P11E0002 Kiran Kumar EEE II 06-FEB-1994

7
ii) Select * from student order by Name;
Output:
RollNo Name Branch Year DOB Semester
---------------- --------------------------- ----------------- --------- ----------------- ------------
14P11F0001 Aravind CSE II 19-FEB-1994
14P11F0002 Bhanu CSE II 10-DEC-1993
14P11E0002 Kiran Kumar EEE II 06-FEB-1994
14P11E0001 Mahesh EEE II 12-MAR-1994
14P11F0003 Naresh CSE II 11-NOV-1993

iii) Select * from student where RollNo in (‘14P11E0001’, ‘14P11F0001’,


‘14P11F0003’ );
Output:
RollNo Name Branch Year DOB Semester
---------------- --------------------------- ----------------- --------- ----------------- ------------
14P11E0001 Mahesh EEE II 12-MAR-1994
14P11F0001 Aravind CSE II 19-FEB-1994
14P11F0003 Naresh CSE II 11-NOV-1993

iv) Select * from student where Name=’Mahesh’ or Name=’Naresh’;


Output:
RollNo Name Branch Year DOB Semester
---------------- --------------------------- ----------------- --------- ----------------- ------------
14P11E0001 Mahesh EEE II 12-MAR-1994
14P11F0003 Naresh CSE II 11-NOV-1993

v) Select * from student where Name not in (‘Mahesh’, ‘Naresh’, ‘Kiran Kumar’);
Output:
RollNo Name Branch Year DOB Semester
---------------- --------------------------- ----------------- --------- ----------------- ------------
14P11F0001 Aravind CSE II 19-FEB-1994
14P11F0002 Bhanu CSE II 10-DEC-1993

8
vi) Select * from student where Name = ‘Kiran Kumar’ and DOB= ‘06-FEB-1994’.
Output:
RollNo Name Branch Year DOB Semester
---------------- --------------------------- ----------------- --------- ----------------- ------------
14P11E0002 Kiran Kumar EEE II 06-FEB-1994

Result:
Hence Records are displayed successfully based on given conditions.

9
EXPERIMENT 8 Creating & Dropping an Index

Aim: To create and delete an index on RollNo in Student Table.

Apparatus: A Personal Computer loaded with Oracle 11g software.

Description: Index in SQL is created on existing tables to retrieve the rows quickly. When there
are thousands of records in a table, retrieving information will take a long time. Therefore
indexes are created on columns which are accessed frequently, so that the information can be
retrieved quickly. Indexes can be created on a single column or a group of columns. When a
index is created, it first sorts the data and then it assigns a ROWID for each row. Syntax for
creating index is:
CREATE INDEX index_name ON table_name (column_name1, column_name2 ...);

Indexes can easily be removed with the DROP statement.


DROP INDEX index_name;

Queries:

i) Create index stuid on Student (RollNo);

Output: Index created Successfully.

ii) Drop index stuid;

Output: Index dropped Successfully.

Result: Index created and deleted on RollNo in Student Table Successfully.

10
EXPERIMENT 9 Exercises on Group Functions

Aim: To display the following details from Student Table:


i) Number of students present in CSE branch.
ii) Total number of branches present in Student Table.
iii) Maximum Marks of Students
iv) Minimum Marks of Students
v) Average Marks of Students
vi) Sum of marks of all Students

Apparatus: A Personal Computer loaded with Oracle 11g software.

Description: Group functions are built-in SQL functions that operate on groups of rows and
return one value for the entire group.
i) Count ( ): This function returns the number of rows in the table that satisfies the
condition specified in the WHERE condition.
ii) Distinct( ): This function is used to select the distinct rows.
iii) Max( ) : This function is used to get the maximum value from a column.
iv) Min( ) : This function is used to get the minimum value from a column.
v) Avg( ) : This function is used to get the average value of a numeric column.
vi) Sum( ) : This function is used to get the sum of a numeric column

Queries:
i) Select Count (*) from Student Where Branch = 'CSE';
Output: 3
ii) Select Distinct Branch from Student;
Output: 2
iii) Select Max (Total_Marks) from Student;
Output: 485
iv) Select Min (Total_Marks) from Student;
Output: 325
v) Select Avg (Total_Marks) from Student;
11
Output: 350

vi) Select Sum (Total_Marks) from Student;


Output: 1750

Result: Hence required outputs displayed successfully by using grouping functions.

12
EXPERIMENT 10 Exercises on Functions

Aim: To exercise on number functions, character functions, conversion functions and date
functions.

Apparatus: A Personal Computer loaded with Oracle 11g software.

Description: Function can be classifies into single row function and group functions. A single
row function or scalar function returns only one value for every row queries in table. Single row
function can appear in a select command and can also be included in a where clause. The single
row function can be broadly classified as: Date Function, Numeric Function, Character Function
and Conversion Function. The example that follows mostly uses the symbol table “dual”. It is a
table, which is automatically created by oracle along with the data dictionary.

Queries:
 Date Functions:
Command Query Output
Add_months(d,n); Select add_months(sysdate,2) from dual;
last_day (d); Select last_day (‘1-jun-2015’) from dual;
month_between (d1,d2); Select month_between (‘1-jun-2009’,’1-aug-
2009’) from dual;
next_day (d,day); Select next_day (sysdate,’wednesday’) from dual;
round (d,[fmt]); Select round (‘1-jun-2009’,’year’) from dual;

 Number Functions:
Command Query Output
Abs(n) Select abs(-15) from dual; 15
Ceil(n) Select ceil(55.67) from dual; 56
Exp(n) Select exp(4) from dual; 54.59
Floor(n) Select floor(100.2) from dual; 100
Power(m,n) Select power(4,2) from dual; 16
Mod(m,n) Select mod(10,3) from dual; 1
Round(m,n) Select round(100.256,2) from dual; 100.26
Trunc(m,n) Select trunc(100.256,2) from dual; 100.23
Sqrt(m,n) Select sqrt(16) from dual; 4

13
 Character Functions:
Command Query Output
initcap(char); select initcap(“hello”) from dual; Hello
lower (char); select lower (‘HELLO’) from dual; hello
upper (char); select upper (‘hello’) from dual; HELLO
ltrim (char,[set]); select ltrim (‘cseit’, ‘cse’) from dual; select it
rtrim (char,[set]); rtrim (‘cseit’, ‘it’) from dual; cse
replace (char,search select replace(‘jack and jue’,‘j’,‘bl’) from dual; black and
string, replace string); blue
substr (char,m,n); select substr (‘information’, 3, 4) from dual; Form

 CONVERSION FUNCTION
Command Query Output
to_char(d,[format]); select to_char (sysdate, ’dd-mm-yy’) from dual;
to_date(d,[format]); select to_date(‘aug 15 2009’,’mm-dd-yy’) from
dual;

Result:
Hence exercise on number functions, character functions, conversion functions and date
functions are successfully completed.

EXPERIMENT 11 Exercise on Set Operators

Aim: To create two employ tables with the fields empno, ename, salary with five records
inserted in each and to exercise on Set Operators using those tables.

Apparatus: A Personal Computer loaded with Oracle 11g software.

14
Description: Set operators are used to join the results of two (or more) SELECT statements.
The SET operators available in Oracle 11g are UNION, UNION ALL, INTERSECT and
MINUS. The UNION set operator returns the combined results of the two SELECT statements.
It removes duplicates from the results. The UNION ALL set operator which retains the
duplicates in the final result. INTERSECT lists only records that are common to both the
SELECT queries; the MINUS set operator removes the second query's results from the output if
they are also found in the first query's results. INTERSECT and MINUS set operations produce
unduplicated results.

Queries:
 Create table employ1( empno varchar2(6), ename varchar2(25),salary number(10,2));
Insert into employ1 values(‘E101’,’Hari’,2000);
Insert into employ1 values (‘E102’,’Ravi’,5000);
Insert into employ1 values (‘E103’,’Suresh’,3000);
Insert into employ1 values (‘E104’,’Ramesh’,4000);
Insert into employ1 values (‘E105’,’Alekhya’,1000);

 Create table employ2(empno varchar2(6), ename varchar2(25),salary number(10,2));


Insert into employ2 values (‘E106’,’Krishna’,2000);
Insert into employ2 values (‘E102’,’Ravi’,5000);
Insert into employ2 values (‘E107’,’Giri’,3000);
Insert into employ2 values (‘E104’,’Ramesh’,4000);
Insert into employ2 values (‘E108’,’Naresh’,1000);

 select * from employ1 union select * from employ2;


Output:
empno ename salary
--------- ------------------------- ---------
E101 Hari 2000
E102 Ravi 5000
E103 Suresh 3000
15
E104 Ramesh 4000
E105 Alekhya 1000
E106 Krishna 2000
E107 Giri 3000
E108 Naresh 1000

 select * from employ1 union all select * from employ2;


Output:
empno ename salary
--------- ------------------------- ---------
E101 Hari 2000
E102 Ravi 5000
E103 Suresh 3000
E104 Ramesh 4000
E105 Alekhya 1000
E106 Krishna 2000
E102 Ravi 5000
E107 Giri 3000
E104 Ramesh 4000
E108 Naresh 1000

 select * from employ1 intersect select * from employ2;


Output:
empno ename salary
--------- ------------------------- ---------
E102 Ravi 5000
E104 Ramesh 4000

16
 select * from employ1 minus select * from employ2;
Output:
empno ename salary
--------- ------------------------- ---------
E101 Hari 2000
E103 Suresh 3000
E105 Alekhya 1000

Result: Hence exercise on Set Operators is successfully completed.

EXPERIMENT 12 Exercise on Sub Queries

Aim: To create Employ table with the fields empno, empname, designation, hiredate, salary,
deptno and to display the following details using Subqueries.
i) Display the records of employees whose salaries are greater than the salary of empno
1777.

17
ii) Display the records of employees whose salaries are greater than any salaries of the
employees between 2000 and 3000.
iii) Display the records of the employees whose salaries are greater than all the salaries of
the employees between 2000 and 3000.

Apparatus: A Personal Computer loaded with Oracle 11g software.

Description: Nesting of queries, one within the other is termed as a subquery. A statement
containing a subquery is called a parent query. Subqueries are used to retrieve data from tables
that depend on the values in the table itself. In single row subquery, it will return one value. In
multi row subquery, it will return more than one value. In such cases we should include operators
like any, all, in or not in between the comparision operator and the subquery.

Queries:
 Create table employ( EmpNo number(4), EmpName varchar2(25), Designation
varchar2(15), HireDate date, Salary number(8,2), DeptNo number(2));
 Insert into employ values (1775, ‘Suresh’,’Manager’,’12-jan-2010’,5000.00,20);
Insert into employ values (1776, ‘Prasad’,’Clerk’,’19-jun-2008’,2700.00,10);
Insert into employ values (1777, ‘Kumar’,’Assistant’,’23-Aug-2009’,2300.00,12);
Insert into employ values (1778, ‘Ajay’,’Assistant’,’15-Sep-2007’,2500.00,16);
Insert into employ values (1779, ‘Hari’,’Attendar’,’06-jan-2014’,1900.00,14);

i) select * from employ where salary > (select salary from employ where empno =
1777);
Output:
EmpNo EmpName Designation HireDate Salary DeptNo
----------- ------------------- --------------- ------------------ ------------- -----------
1775 Suresh Manager 12-jan-2010 5000.00 20
1776 Prasad Clerk 19-jun-2008 2700.00 10
1778 Ajay Assistant 15-Sep-2007 2500.00 16

ii) select * from employ where salary > any (select salary from employ where salary
between 2000 and 3000);
18
Output:
EmpNo EmpName Designation HireDate Salary DeptNo
----------- ------------------- --------------- ------------------ ------------- -----------
1775 Suresh Manager 12-jan-2010 5000.00 20
1776 Prasad Clerk 19-jun-2008 2700.00 10
1777 Kumar Assistant 23-Aug-2009 2300.00 12
1778 Ajay Assistant 15-Sep-2007 2500.00 16

iii) select * from employ where salary > all (select salary from employ where salary
between 2000 and 3000);
Output:
EmpNo EmpName Designation HireDate Salary DeptNo
----------- ------------------- --------------- ------------------ ------------- -----------
1775 Suresh Manager 12-jan-2010 5000.00 20

Result: SubQueries are executed successfully and the specified records are displayed.

EXPERIMENT 13 Exercise on Joins

Aim: To create Employ table with the fields empno, empname, job, deptno and to create
department table with the fields deptno, deptname, location with some records inserted in each to
display the following details using joins.

19
i) Display empno, empname, salary, deptno, deptname, location of all the employees
ii) Display the records of the employees whose deptno in employ table is greater than
deptno in dept table.
iii) Display all the records from both the tables whose columns are in common.
iv) Display the cross product of both the tables
v) Display the all matching records and the records which are in employ table those that
are not in department table.
vi) Display the all matching records and the records which are in department table those
that are not in employ table.
vii) Display the all matching records and the non-matching records from both tables.
viii) Display all the records that have matched in both the tables using inner join.

Apparatus: A Personal Computer loaded with Oracle 11g software.

Description: The purpose of a join is to combine the data across tables. A join is actually
performed by the where clause which combines the specified rows of tables. If a join involves in
more than two tables then oracle joins first two tables based on the joins condition and then
compares the result with the next table and so on.
 EQUI JOIN: A join which contains an ‘=’ operator in the joins condition.
 NON-EQUI JOIN: A join which contains an operator other than ‘=’ in the joins
condition.
 NATURAL JOIN: Natural join compares all the common columns.
 CROSS JOIN: This will gives the cross product.
 OUTER JOIN: Outer join gives the non-matching records along with matching records.
 LEFT OUTER JOIN: This will display the all matching records and the records which
are in left hand side table those that are not in right hand side table.
 RIGHT OUTER JOIN: This will display the all matching records and the records which
are in right hand side table those that are not in left hand side table.
 FULL OUTER JOIN: This will display the all matching records and the non-matching
records from both tables.
 INNER JOIN: This will display all the records that have matched.

Queries:
20
 Create table emp( EMPNO number(4), ENAME varchar2(25), JOB varchar2(15),
DeptNo number(2));
 Insert into emp values (111, ‘saketh’,’analyst’,10);
Insert into emp values (222, ‘sudha’,’clerk’,20);
Insert into emp values (333, ‘jagan’,’manager’,10);
Insert into emp values (444, ‘madhu’,’engineer’,40);
 Create table dept( DEPTNO number(2), DNAME varchar2(25), LOC varchar2(15) );
 Insert into dept values (10, ‘mkt’,’hyd’);
Insert into dept values (20, ‘fin’,’bang’);
Insert into dept values (30, ‘hr’,’bombay’);

i) select empno,ename,job,dname,loc from emp e,dept d where e.deptno=d.deptno;


EMPNO ENAME JOB DNAME LOC
---------- ---------------- ---------- ------------ ----------
111 saketh analyst mkt hyd
333 jagan manager mkt hyd
222 sudha clerk fin bang

ii) select empno,ename,job,dname,loc from emp e,dept d where e.deptno > d.deptno;
EMPNO ENAME JOB DNAME LOC
---------- ---------- ---------- ---------- ----------
444 madhu engineer mkt hyd
444 madhu engineer fin bang
444 madhu engineer hr bombay

iii) select empno,ename,job,dname,loc from emp natural join dept;


EMPNO ENAME JOB DNAME LOC
---------- ------------- ---------- ---------- ----------
111 saketh analyst mkt hyd
333 jagan manager mkt hyd
21
222 sudha clerk fin bang

iv) select empno,ename,job,dname,loc from emp cross join dept;


EMPNO ENAME JOB DNAME LOC
------------ ----------------- ---------- ------------- ----------
111 saketh analyst mkt hyd
222 sudha clerk mkt hyd
333 jagan manager mkt hyd
444 madhu engineer mkt hyd
111 saketh analyst fin bang
222 sudha clerk fin bang
333 jagan manager fin bang
444 madhu engineer fin bang
111 saketh analyst hr bombay
222 sudha clerk hr bombay
333 jagan manager hr bombay
444 madhu engineer hr bombay

v) select empno,ename,job,dname,loc from emp e left outer join dept d


on(e.deptno=d.deptno);
EMPNO ENAME JOB DNAME LOC
---------- ---------- ---------- ---------- ----------
111 saketh analyst mkt hyd
333 jagan manager mkt hyd
222 sudha clerk fin bang
444 madhu engineer

vi) select empno,ename,job,dname,loc from emp e right outer join dept d


on(e.deptno=d.deptno);
EMPNO ENAME JOB DNAME LOC
---------- ---------- ---------- ---------- ----------
111 saketh analyst mkt hyd

22
333 jagan manager mkt hyd
222 sudha clerk fin bang
hr bombay

vii) select empno,ename,job,dname,loc from emp e full outer join dept d


on(e.deptno=d.deptno);
EMPNO ENAME JOB DNAME LOC
---------- ------------- ---------- ---------- ----------
333 jagan manager mkt hyd
111 saketh analyst mkt hyd
222 sudha clerk fin bang
444 madhu engineer hr bombay

viii) select empno,ename,job,dname,loc from emp inner join dept using(deptno);


EMPNO ENAME JOB DNAME LOC
---------- ------------- ---------- ---------- ----------
111 saketh analyst mkt hyd
333 jagan manager mkt hyd
222 sudha clerk fin bang

Result: By using joins specified records are displayed successfully.

EXPERIMENT 14 Exercise on various Date and Number Format Models

Aim: a) To format the HIRE_DATE and SALARY columns of EMPLOYEES table using
TO_CHAR function.

23
b) To format the given number into specified format containing decimal point and commas using
TO_NUMBER function.
c) To format the given date into specified format using TO_DATE function.

Description: SQL Conversion functions are single row functions which are capable of
typecasting column value, literal or an expression. TO_CHAR, TO_NUMBER and TO_DATE
are the three functions which perform cross modification of data types.
 TO_CHAR function: TO_CHAR function is used to typecast a numeric or date input to
character type with a format model optional.
Syntax: TO_CHAR(number1, [format], [nls_parameter])
 TO_NUMBER function: The TO_NUMBER function converts a character value to a
numeric datatype. If the string being converted contains nonnumeric characters, the function
returns an error.
Syntax: TO_NUMBER (string1, [format], [nls_parameter])
 The TO_DATE function: The TO_DATE function allows users to enter a date in any
format, and then it converts the entry into the default format
Syntax: TO_DATE( string1, [ format_mask ], [ nls_language ] )

Queries:
 Create table employees ( Empid number, first_name varchar2(20), hire_date date, salary
number);
 Insert into employees values(101, ‘steven’,’17-jun-2003’,24000);
 Insert into employees values(102, ‘Neena’,’21-sep-2005’,17000);
 Insert into employees values(103, ‘Lex’,’13-jan-2001’,17000);
 Insert into employees values(104, ‘Alexander’,’03-jan-2006’,9000);

a) SELECT first_name, TO_CHAR (hire_date, 'MONTH DD, YYYY'), TO_CHAR (salary,


'$99999.99') FROM employees ;
FIRST_NAME HIRE_DATE SALARY
------------------- ------------------------------- ---------------
Steven JUNE 17, 2003 $24000.00
Neena SEPTEMBER 21, 2005 $17000.00

24
Lex JANUARY 13, 2001 $17000.00
Alexander JANUARY 03, 2006 $9000.00

b) SELECT TO_NUMBER('1121.23', '9G999D99') FROM DUAL;


TO_NUMBER('1121.23','9G999D99')
----------------------------------------------
1,121.23

SELECT TO_NUMBER('1210.73', '9999.99') FROM DUAL;


TO_NUMBER('1210.73','9999.99')
--------------------------------------------
1210.73

c) SELECT TO_DATE('January 15, 1989, 11:00 A.M.', 'Month dd, YYYY, HH:MI A.M.',
'NLS_DATE_LANGUAGE = American') FROM DUAL;
TO_DATE('
-------------
15-JAN-89

Result: By using date and number format models the given data is converted into respective
format types.

EXPERIMENT 15 Exercise on Sequences

25
Aim: To create a sequence that generates sequential numbers and to insert that numbers as
RollNo into student table.
[Hint: Student table contains RollNo, Name, Department and Address].

Description: A sequence is a set of integers 1, 2, 3, ... that are generated in order on demand.
Sequences are frequently used in databases because many applications require each row in a
table to contain a unique value, and sequences provide an easy way to generate them.
 The syntax for creating a sequence is :
CREATE SEQUENCE sequence_name
[ START WITH <constant> ]
[ INCREMENT BY <constant> ]
[ { MINVALUE [ <constant> ] } | { NO MINVALUE } ]
[ { MAXVALUE [ <constant> ] } | { NO MAXVALUE } ]
[ CYCLE | { NO CYCLE } ]
[ { CACHE [ <constant> ] } | { NO CACHE } ] [;]
 In Oracle PL/SQL, NEXTVAL and CURRVAL are the two pseudo columns in Oracle
which are used to access the sequence values. NEXTVAL generates the new value of the
sequence while CURRVAL displays the current value of the sequence.

Queries:
 CREATE SEQUENCE Stu_Seq
START WITH 101
INCREMENT BY 1
MINVALUE 101
MAXVALUE 1000
CYCLE
CACHE 3;

 insert into student values(Stu_Seq.CURRVAL, ’Ravi’, ’DCME’, ’SVEC,Tirupati’);


 insert into student values(Stu_Seq.NEXTVAL, ’Hari’, ’DCME’, ’SVEC,Tirupati’);
 insert into student values(Stu_Seq.NEXTVAL, ’Naresh’, ’DCME’, ’SVEC,Tirupati’);
 insert into student values(Stu_Seq.NEXTVAL, ’Kumar’, ’DCME’, ’SVEC,Tirupati’);
 insert into student values(Stu_Seq.NEXTVAL, ’Prasad’, ’DCME’, ’SVEC,Tirupati’);
26
 insert into student values(Stu_Seq.NEXTVAL, ’Kiran’, ’DCME’, ’SVEC,Tirupati’);

Result: Sequence created and sequence numbers are inserted as rollno’s into student table
successfully.

Exercise 16: Exercise on Synonyms

27
Aim: To create and drop a synonym.

Description: A synonym is a database object, which is used as an alias for a table, view or
sequence. There are two types of synonyms that is private and public. Private synonym is
available to the particular user who creates. Public synonym is created by DBA which is
available to all the users. Synonym hides the name and owner of the object and Synonym
provides location transparency for remote objects of a distributed database.
 Syntax for creating a synonym:
Create [public] synonym synonym_name for table/view/sequence;
 Syntax for dropping a synonym.
Drop synonym synonym_name;

Queries:
 create synonym s1 for emp;
Output: Synonym created.
 create public synonym s2 for student;
Output: Synonym created.
 drop synonym s1;
Output: Synonym dropped.

Result: Synonyms created and dropped successfully.

Exercise 17: Exercise on Views

Aim: To create a view with the columns empno, name, deptname, location and to display the
records in the view using the following employ & department tables.
Employ table:
28
Empno Name Sal Deptno
E101 Kiran 15000 10
E205 Prasad 17000 20
E307 Bhanu 9000 30

Department table:
Deptno Deptname Location
10 HR Bangalore
20 Marketing Chennai
30 Finance Hyderabad

Description: A view is a database object that is a logical representation of a table. It is delivered


from a table but has no storage of its own and often may be used in the same manner as a table.
A view takes the output of the query and treats it as a table, therefore a view can be thought of as
a stored query or a virtual table. Simple view can be created from one table where as complex
view can be created from multiple tables.
Syntax:
CREATE [OR REPLACE]
[FORCE | NOFORCE] view
alias [,alias] …)]
AS subquery
[WITH CHECK OPTION [CONSTRAINT constraint ]]
[WITH READ ONLY]

Queries:
 CREATE VIEW Emp_View
AS SELECT empno, name , deptname, location FROM employ e, department d
WHERE e.deptno =d.deptno;
Output: View created.

 Select * from Emp_view;


Output:
Empno Name Deptname Location
----------- ---------------- ----------------- ------------------

29
E101 Kiran HR Bangalore
E205 Prasad Marketing Chennai
E307 Bhanu Finance Hyderabad

Result: Hence view created and records in the view are displayed successfully.

30

S-ar putea să vă placă și