You are on page 1of 19

SQL

Internal Data Types:

1) Number
The Number data type can store positive, negative, zeroes, fixed point
numbers and floating point numbers with a precision of 38.
a) column_name number { p = 38, s = 0 }
b) column_name number(p) { fixed point }
c) column_name number(p,s) { float point }

Where p is the precision, which refers to the total number of digits and s is the scale,
which refers to the number of digits to the right of the decimal point.

2) Char
This data types is used when fixed length character string is required. It can
store alphanumeric values. The column length for this data type varies between 1-2000 bytes. By
default it is One (1) byte.

● If user enters a value shorter than the specified length the database blank-pads to the
fixed length.
● If the user enters a value larger than the specified length the database would return an
error.

3) Varchar2
This data types supports variable length character string. It also stores
alphanumeric values. The size of this data types is 1-4000 bytes. Varchar2 saves disk space
compared to Char. Consider a column assigned with Varchar2 data type of size 30 bytes, if user
enters 10 bytes of character, than the column length in that row would only be 10 bytes and not 30
bytes. In case of Char it would still occupy 30 bytes because the remaining would be blank padded
by oracle.

4) Long
This is used to store variable character length. Maximum size is 2 GB. It also
stores alphanumeric values. The following restriction needs to be fulfilled when a long data type is
used for a column in a table.
a. Only one column in a table have long data type.
b. They should not contain unique or primary key constraints.
c. The column cannot be indexed
d. Procedure or Stored Procedure can not accept long data type as arguments.

5) Date
Date data type is used to date and time in a column of a table. Oracle uses its
own format to store date in a fixed length of 7 bytes for century, month, day, year, hour, minute, and
second. Default format of date data type is “dd-mon-yy”.

6) Raw
Raw data type is used to store byte oriented data like binary data or byte
strings and the maximum size of this data type is 2000 bytes. While using this data type the size
should be mentioned because by default it doesn’t speciy any size. Only storage and retrieval of this
data type are possible, manipulation of data cannot be done. Raw data type can be indexed.
7) Long Raw
Long raw data type is used to store binary data of variable length, which can
have a maximum size of 2 GB. This data type cannot be indexed. All limitations faced by long data
type also holds good for long row data type.

8) LOB (Large OBjects)


This can store unstructured information such as sound clips, video files etc.
up to 4 GB in size. The LOB data type store values, which are known as locators. These locators
store the location of the large objects. The DBMS_LOB package can be used to manipulate LOB’s.
Internal LOB’ are stored in the database tablespace. External LOBs are also referred as BFILE.
These are stored in operating system files outside the database table space.
There are two different two LOB’s are given below.
1. CLOB:
A column with its data type is CLOB stores character objects with single byte
characters. It cannot contain character sets of varying strings. A table can
have multiple column with CLOB data type.
2. BLOB:
A column with its data type as BLOB can store large binary objects such as
graphics, video clips and sound files. A table can have multiple column with
CLOB data type.
3. BFILE:
BFILE stores file pointes managed by file system external to the database. A
BFILE column may contain file names for photos stores on a CD-ROM.

Data Definition Language (DDL)

The Data Definition Language is used to create object (e.g table, view etc.),
alter the structure of the object and also drop or destroy the object created. A table is a unit in oracle
that holds data in the form of rows and columns. The Data Definition Language used for table
definition as following commands.

1. Create table command.


2. Alter table command.
3. Truncate table command.
4. Drop table command.

1. Create table command –

Create table command is used to create a table. General syntax is given below.

Create table <table_name> (column_name1 data_type, column_name2 data_type, …,);

Example

SQL > Create table student1 (rollno number, name varchar2(20), address
varchar2(20), city varchar2(20));

We can also define a unique column_name to the table.

SQL > Create table student2 (rollno number unique, name varchar2(20), address
varchar2(20), city varchar2(20));
We can also define a primary key to a column_name for the table.

SQL > Create table student3 (rollno number primary key, name varchar2(20),
address varchar2(20), city varchar2(20));

2. Alter table command.

When you want to change the structure of table alter table command is used. For
example…

1. When a user wants to add a new column.


2. When a user wants to change the size of a fields or data type itself.
3. To drop the integrity constraints that are created.

General syntax is …

Alter table <table_name> add (column_name data_type,…);


Altar table <table_name> modify (column_name data_type,…);

Example

SQL > Alter table student3 add (dob date);


SQL > Alter table student3 modify (address varchar2(30));

3) Truncate table command.

The truncate table command deletes all rows from the specified table but the
structure of table doesn’t change and it is retained by oracle. General syntax is..

Truncate table <table_name>;

Example

SQL > Truncate table student1;

By adding ‘reuse storage’ clause to the above command the space that is used by the
rows of the above table is reused.

Truncate table student1 reuse storage;

4) Drop table command.

In order to drop a table, we can use drop table command.

drop table <table name>;

Example:

SQL > drop table student3;


Command to view the structure of a table.

Desc <table name>

Example:

SQL > Desc student3;


SQL > Desc student2;

Data Manipulation Language (DML)

Data manipulation commands are used to query and manipulate existing objects like
tables. The DML commands are as follows.

1. Insert
2. Select
3. Update
4. Delete

1) Insert command

When the table is created using create table command, there is need to insert a rows
(records) into the table. The insert command is used to add one or more rows to a table. While using
this command the values are separated by commas and the data types varchar2, char, raw long and
date are enclosed in single quotes. The values must be entered in the same order as they are defined
in the table.

Insert into <table name> values <a list of data values>;

Example: If you have a studetn1 table then…

SQL > insert into student1 values (1,’Ramesh’, ‘abc society’, ‘wadhwan’);
SQL > insert into student1 values (2,’suresh’, ‘xyz society’, ‘wadhwan’);
SQL > insert into student1 (rollno, name) values (1,’Mahesh’);
SQL > insert into student1 values (&a,’&b’, ‘&c’, ‘&d’);
SQL > /

2) Select command

When we want to retrieve data stored in the table, we have to use select command.
This is generally referred to as ‘querying’ to the table. ‘*’ is used to select all columns for a
particular table.

Select column_name….. from table_name;

Example:

SQL > select * from student1;


SQL > select rollno from student1;
SQL > select rollno, name from student1;
SQL > select rollno, name, city from student1;
Selecting distinct rows

To prevent the selection of duplicate rows from the table, we have to use distinct caluse in
the select statement. Suppose there are duplicate values for rollno in student1 table, the following
query would exclude them from being displayed.

SQL > select distinct rollno from student1;

3) Update command

When we want to change or modify data into the table we have to use update command.
With the update command we can update the rows in the table. A single column or more than one
column can be updated. The update command consists of a ‘set’ clause and an optional ‘where’
clause.

Update table_name set field_name = values … where condition.

Example
SQL > update student1 set name = ‘suresh’;
SQL > update student1 set rollno = 5 where name = ‘mahesh’;

4) Delete Command

One or more rows could be deleted using the Delete command. All rows of a table
could also be deleted while structure remains unchanged. To delete specific rows from a table the
‘where’ clause can be used.

Delete from table_name where condition;

Example:

SQL > Delete from student1 where city = ‘wadhwan’;


SQL > Delete from student1 where name = ‘mahesh’;
SQL > Delete from student1 where age = 20;
SQL > Delete from student1;

Clauses in SQL

Following clauses are used with SELECT statement.

1. WHERE
2. STARTING WITH (% and _ Clause)
3. ORDER BY
4. GROUP BY
5. HAVING

Suppose we have a table CHECKS with following data.

Check_no Payer amount location


-------------- ------------------------ ----------- ----------
101 Ramesh Patel 1000.00 Wadhwan
102 Suresh shah 500.00 Ahmedabad
103 Nilesh Trivedi 1200.00 Wadhwan
104 Paresh Shukla 1500.00 Surendranagar
105 Kalpesh Patel 2000.00 Rajkot
106 Jingnesh Joshi 1700.00 Surendranagar

1) WHERE Clause

To select specific rows from a table we include ‘where’ clause in the select statement. We
can retrieve only the rows which satisfy the ‘where’ the condition.

Select column_name….. from table_name where conditions;

Example:

SQL> select * from checks where amount > 1500;

CHECK_NO PAYEE AMOUNT LOCATION


--------- -------------------- --------- ---------------
105 kalpesh patel 2000 rajkot
106 jignesh joshi 1700 surendranagar

SQL> select payee from checks where location = 'rajkot' and amount > 1000;

PAYEE
-------------------
kalpesh patel

SQL> select payee from checks where location = 'rajkot' or amount > 1500;

PAYEE
--------------------
kalpesh patel
jignesh joshi

2) STARTING WITH (%) Clause

Starting with clause is used search the string starts with a particular character or
string. % sign is used for multiple character and _ (Underscore) is used for one character.

Example
SQL> select payee from checks where payee like 's_’;
SQL> select payee from checks where payee like 's%';

PAYEE
--------------------
suresh shah

3) ORDER BY Clause

To arrange the rows or records according to some predefined order we can


use order by clause. It is used to arrange the selected rows in ascending or descending order.
The order by clause ends with the particular column. Default order is ascending. Desc
keyword is used to arrange the records in descending order.

Example

SQL> select amount from checks order by amount;

AMOUNT
---------
500
1000
1200
1500
1700
2000

SQL> select amount from checks order by amount desc;

AMOUNT
---------
2000
1700
1500
1200
1000
500

SQL> select payee,amount from checks order by payee asc, amount desc;

PAYEE AMOUNT
-------------------- ---------
jignesh joshi 1700
kalpesh patel 2000
nilesh trivedi 1200
paresh shukla 1500
ramesh patel 1000
suresh shah 500

4) GROUP BY Clause

In order to find the records with particular group, group by clause is used.

Example

SQL> select location, sum(amount) from checks group by location;

LOCATION SUM(AMOUNT)
--------------- --------------------
ahmedabad 500
rajkot 2000
surendranagar 3200
wadhwan 2200
SQL> select location, count(payee) "Total" from checks group by location;

LOCATION Total
--------------- ---------
ahmedabad 1
rajkot 1
surendranagar 2
wadhwan 2

SQL> select max(amount) "Maximum", min(amount) "Minimum" from checks group by


location;

Maximum Minimum
--------- ---------
500 500
2000 2000
1700 1500
1200 1000

5) HAVING Clause

Having clause is used with group by clause in order to find the records, which
satisfies the given condition.

Example

SQL> select max(amount) "Maximum", min(amount) "Minimum" from checks group by


location having location = 'wadhwan';

Maximum Minimum
--------- ---------
1200 1000

SQL> select location, sum(amount) "Total" from checks group by location having location =
'surendranagar';

LOCATION Total
--------------- ---------
surendranagar 3200

## SQL> select check_no, payee from checks where amount = (select max(amount) from
checks group by location having location = 'wadhwan');

CHECK_NO PAYEE
-------------- --------------------
103 nilesh trivedi

Transaction Control commands

1. Commit 2) Savepoint
3) Rollback
All changes made to the database can be referred to as a transaction. Transaction
changes can be made permanent to a database only if they are committed.

Commit:
Commit command is used to end a transaction. With the help of this
command, transaction changes can be made permanent to the database.

SQL > Commit work; OR


SQL > commit;

Savepoint:

Save points are like markers to divide a very lengthy transaction to smaller
ones. They are used to identify a point in a transaction to when we can later rollback. Thus
savepoint is used in conjunction with rollback, to rollback portions of the current
transaction.

SQL > Savepoint <savepoint_name>;


SQL > Savepoint s1;

Rollback:

A rollback command is used to undo the work done in the current transaction.
We can either rollback the entire transaction so that all changes made by SQL statements are
undo or rollback a transaction to a savepoint so that the SQL statements after the save point
are rolled back.

SQL > rollback work; OR


SQL > rollback;

To rollback to a particular transaction i.e. a savepoint, we say

SQL> Rollback to savepoint <savepoint_name>


SQL> Rollback to savepoint s1;

Data Control Language (DCL)

Data control language provides users with privilege commands. The owner can give rights
of the database objects, say tables to the others users. Granting privileges (insert, select..) to others
allow them to perform operations within their (privileges) purview.

1. Grant
2. Revoke

Grant privilege:

Grant command is used to giving permission (rights) to use database objects (tables
etc…) to other users. In case the user wants to share an objects with others, the appropriate
privileges can be granted on that particular object others. Objects are logical data storage,
structures like tables, views, sequences etc.
SQL> grant privileges on <object_name> to <user_name>
SQL> grant select, update on student1 to scott;

The privilege to update values in the rollno, name columns of the student1 table is to
be granted to scott using following syntax.

SQL> grant select, update (rollno, name) on student1 to scott;

When object privileges are granted and WITH GRANT OPTION is used, these
privileges can be passed to others.
SQL> grant select, update on student1 to scott with grant option;

Revoke privilege:

To withdraw(revoke) the privilege that has been granted to user, we use the revoke
command. The syntax is given below.

SQL> Revoke privileges on <object_name> from <user_name>


SQL> Revoke select, update on student1 from scott;

Set Operators:

Set operators combine the results of two queries into single one. The following set
operators are available in SQL to joining queries.

1. Union
2. Union all
3. Intersect
4. Minus

The columns in the select statements joined using the set operators should strictly follow the
following rules.
● The queries which are related by a set operator should have the same number
of columns and the corresponding columns must be of the same data type.
● Such a query should not contain any column of type long.
● The label under which the rows are displayed are those from the first select
statement.

Suppose, we have to table Student and Teacher with following data.

STUDENT TEACHER

ROLLNO NAME ADDRESS RNO TNO TNAME


------ ------------------------------ --------------------- ------- ------- ----------------------
77 Jayesh Patel abc society 1 1 Jatin Zinzuvadiya
111 Jani Mahesh pqr society 2 2 Jayanti Khimsuria
2 Dhaval Mehta xyz society 5 3 Dharmendra Gadhvi
5 Suresh Mehta abc society
55 Dinesh Shukla mnp society
1. Union

The Union operator returns all distinct rows selected by both queries. The following
example combines the result of two queries with the union operator which eliminates
duplicate rows.

SQL> select rollno from student union select rno from teacher;

The output of the following query will given below.

ROLLNO
-----------
1
2
5
55
77
111

2) Union all
The ‘union all’ operator returns all rows selected by either query
including duplicates. The following example combine the result with the use of ‘union all’ operator
which does not eliminates duplicate rows.

SQL> select rollno from student union all select rno from teacher;

ROLLNO
---------
77
111
2
5
55
1
2
5

3) Intersect
Intersect operator returns only rows that are common to both the
queries.

SQL> select rollno from student intersect select rno from teacher;

ROLLNO
---------
2
5
4) Minus
Minus operator returns all distinct rows selected only by the first
query and not by second.

SQL> select rollno from student minus select rno from teacher;

ROLLNO
---------
55
77
111

SQL> select rno from teacher minus select rollno from student;

RNO
---------
1

Relating data to JOIN Concept:

The purpose of join is to combine the data spread across the tables. A join is actually
performed by the ‘where’ clause which combines the specified rows of tables. If a join involves
over two tables then oracle joins the first two based on the condition and then compares the result
with the next table and so on.

There are basically three different types of joins. They are :

1. Simple join
a. Equi-join
b. Non equi-join
2. Self join
3. Outer join

1) Simple Join : It is the most common type of join. It retrieves rows from two tables having a
common column and is further classified into equi-join and non equi-join.

A) Equi-join : A join, which is based on equalities, is called an equi-join. The equi-join


combines rows that have equivalent values for the specified columns.

SQL> select student.rollno, student.name, teacher.tname from student, teacher


where student.rollno = teacher.rno;

ROLLNO NAME TNAME


--------- -------------------- -------------------------
2 dhaval mehta jayanti khimsuria
5 suresh mehta dharmendra gadhvi

Table Aliases: Table aliases are used to make multiple table queries shorter and more
readable. As a result, we give an alias to the table in the ‘from’ clause. The alias can be used
instead of the table name throughout the query.
SQL> select s.rollno, s.name, t.tname from student s, teacher t
where s.rollno = t.rno;

ROLLNO NAME TNAME


--------- -------------------- -------------------------
2 dhaval mehta jayanti khimsuria
5 suresh mehta dharmendra gadhvi

B) Non-equi Join : A non-equi join specifies the relationship between columns belonging to
different table by making use of the relational operators (<,>,<=,>=,<>) other than =.

SQL> select s.rollno, s.name, t.tname from student s, teacher t


where t.rno > s.rollno;

ROLLNO NAME TNAME


--------- -------------------- -------------------------
2 dhaval mehta dharmendra gadhvi

SQL> select s.rollno, s.name, t.tname from student s, teacher t where t.rno >= s.rollno;

ROLLNO NAME TNAME


--------- -------------------- -------------------------
2 dhaval mehta jayanti khimsuria
2 dhaval mehta dharmendra gadhvi
5 suresh mehta dharmendra gadhvi

2) Self Join : joining of table to itself is known as a self join. i.e. It joins one row in a
table to another. The join is performed by mirroring the table using the ‘where’
clause. It can compare each row of the table to itself and also with other rows of the
same table.

SQL> select a.rno, b.tname from teacher a, teacher b where a.rno = b.rno;

RNO TNAME
--------- -------------------------
1 jatin zinzuvadiya
2 jayanti khimsuria
5 dharmendra gadhvi

SQL> select a.rno, b.tname from teacher a, teacher b where a.rno = b.tno;

RNO TNAME
--------- -------------------------
1 jatin zinzuvadiya
2 jayanti khimsuria
SQL> select a.rno, b.tname from teacher a, teacher b where a.rno <> b.rno

RNO TNAME
--------- -------------------------
2 jatin zinzuvadiya
5 jatin zinzuvadiya
1 jayanti khimsuria
5 jayanti khimsuria
1 dharmendra gadhvi
2 dharmendra gadhvi

SQL> select a.rollno, b.name from student a, student b where a.rollno > b.rollno;

ROLLNO NAME
--------- --------------------
111 jayesh patel
77 dhaval mehta
111 dhaval mehta
5 dhaval mehta
55 dhaval mehta
77 suresh mehta
111 suresh mehta
55 suresh mehta
77 dinesh shukla
111 dinesh shukla

3) Outer Join : outer join extends the result of a simple join. An outer join returns all
the rows returned by simple join as well as those rows from one table that do not
match any row from the other table. The symbol (+) represents the outer join.

SQL> select s.rollno, s.name, t.tname from student s, teacher t where s.rollno(+) = t.rno;

ROLLNO NAME TNAME


--------- -------------------- -------------------------
jatin zinzuvadiya
2 dhaval mehta jayanti khimsuria
5 suresh mehta dharmendra gadhvi

The above example will also retrieve rows from teacher which do not have any
matching records in the student table.

SQL> select s.rollno, s.name, t.tname from student s, teacher t where s.rollno = t.rno(+);

ROLLNO NAME TNAME


--------- -------------------- -------------------------
2 dhaval mehta jayanti khimsuria
5 suresh mehta dharmendra gadhvi
55 dinesh shukla
77 jayesh patel
111 jani mahesh

The above example will also retrieve rows from student which do not have any
matching records in the teacher table.

SQL> select s.rollno, s.name, t.tname from student s, teacher t where s.rollno(+) = t.tno;

ROLLNO NAME TNAME


--------- -------------------- -------------------------
jatin zinzuvadiya
2 dhaval mehta jayanti khimsuria
dharmendra gadhvi

SQL> select s.rollno, s.name, t.tname from student s, teacher t where s.rollno = t.tno(+);

ROLLNO NAME TNAME


--------- -------------------- -------------------------
2 dhaval mehta jayanti khimsuria
5 suresh mehta
55 dinesh shukla
77 jayesh patel
111 jani mahesh

Functions

A) Aggregate functions
1) Sum ( ) 2) Avg ( ) 3) Min ( ) 4) Max ( )
5) Variance ( ) 6) Stddev( )

B) Date and Time functinons


1) Add_months ( ) 2) Last_day ( ) 3) Months_between ( )
4) next_day ( ) 5) new_time ( )

C) Arithmetic functions
1) abs ( ) 2) ceil ( ) 3) floor ( ) 4) exp
5) ln ( ) 6) log ( ) 7) power ( ) 8) sign ( )
9) sqrt ( ) 10) mod( ) 11) trunc ( ) 12) sin ( )
13) sinh ( ) 14) cos ( ) 15) cosh ( ) 16)
round ( )

D) Character functions
1) chr ( ) 2) concat ( ) 3) initcap ( ) 4) lower ( )
5) upper ( ) 6) lpad ( ) 7) rpad ( ) 8) ltrim ( )
9) rtrim ( ) 10) replace ( ) 11) substr ( ) 12) translate()
13) instr ( ) 14) length ( )

E) Conversion Functions
1) to_char ( ) 2) to_number( ) 3) to_date ( )

F) Miscellaneous Functions
1) greatest ( ) 2) least ( ) 3) nvl ( ) 4)
count ( )
4. vsize ( )
Constraints

A constraint is an object in a relational database that places rules on data inserted into a
column of a table. Constraints are used to ensure accuracy and consistency of data in a relational
database. Data integrity is this assurance of accurate and consistent data in the database.

Why use constraints ?

Components of data integrity (data accuracy and consistency) enforce rules (constraints) on
the database. Accuracy of data involves placing rules (constraints) on table columns so that the
database only allows certain types of accurate data to be inserted into these columns. Database
normalization are used to provide data consistency. Implementation of referential integrity
constraints (foreign key) will help maintain consistent data in a normalized database.

Types of Constraints :

1. Domain integrity constraint


2. Entity integrity constraint
3. Referential integrity constraint

1) Domain integrity constraint

There are basically two types of domain integrity constraints.

a. Not Null constraint


b. Check constraint

a) Not Null constraint : By default the tables can contain null values. The enforcement
of not null constraint in a table ensures that the table contains values. Oracle doesn’t
insert the record until this is satisfied.

Example:

SQL > create table emp (emp_id number(3) not null, emp_name varchar2(10) not
null, address varchar2(20), city varchar2(10));

b) Check constraint : This can be defined to allow only a particular range of values.
When the values not inserted in the specified range oracle rejects the record.

Example:

SQL> create table emp_pay (emp_id number(3), designation varchar2(10),


pay_scale number(5) constraint chk_pay_scale check ((pay_scale) >0 and
(pay_scale) < 10000));

2) Entity integrity constraint

There are basically two types of domain integrity constraints.

a. Unique constraint
b. Primary key constraint
a) Unique constraint : The unique constraint designates a column or a group of
columns as a unique key. This constraint allows only unique values to be stored in
the column. Oracle rejects duplication of records when unique key constraint is used.

Example

SQL> create table emp (emp_id number(3) not null unique, emp_name varchar2(10)
not null, address varchar2(20), city varchar2(10));
OR
SQL> alter table emp add constraint emp_id_unique unique (emp_id);

b) Primary key constraint : The primary key constraint avoids duplication of rows
and does not allow null values, when enforced in a column or set of columns. If a
primary key constraint is assigned to a combination of columns it is said to be a
composite primary key.

Example

SQL> create table emp (emp_id number(3) primary key, emp_name varchar2(10)
not null, address varchar2(20), city varchar2(10));
OR
SQL> alter table emp add constraint pk_emp_id primary key (emp_id);

3) Referential integrity constraint (Foregin key) constraint.

To establish a ‘parent-child’ or a ‘master-detail’ relationship between two tables


having a common column, we make use of referential integrity constraint. This is an
example of one to many relationships. To implement this we should define the column in the
parent table as a primary key and the same column in the child table as a foreign key
referring to the corresponding parent entry.

(Parent Table) (Child table)


CUSTOMER
cust_id (PK)
cust_name
cust_address
….
ITEM
item_id (PK)
cust_id (FK)
item_desc
….
depends on

SQL> create table customer (cust_id number(3) primary key, cust_name varchar2(10),
cust_address varchar2(20), cust_city varchar2(10));
SQL> create table Item (item_id number(3) primary key, cust_id number(3) constraint
fk_cust_id references customer (cust_id), item_desc varchar2(20), item_price number(5,2));
OR
SQL> alter table item add constraint fk_cust_id foreign key (cust_id) references customer
(cust_id);

On Cascade Clause

This clause specifies that oracle maintains referential integrity by automatically


removing dependent foreign key values if a referenced primary key is removed.
Example

SQL> alter table item add constraint fk_cust_id foreign key (cust_id) references customer
(cust_id) on delete cascade;

Database Objects

1. View
2. Index
3. Synonym
4. Sequences

1) View : A view is often referred as a virtual table. A view is defined by a query on one or
more database tables. A view takes a output of a query and treats it as a table. Advantages of
views are : 1) They simplify commands for the user because they allow them to select
information from multiple tables without actually knowing how to perform a join. 2) They hide
data complexity because when view is defined by a join which is a collection of related columns
or rows in multiple tables therefore view hides the fact that this information actually originates
from several tables.

Example

SQL> Create view student_info as select * from student;


SQL> Create view teacher_info as select tname from teacher;
SQL> Create view mix_info as select student.rollno, student.name, teacher.tname from student,
teacher where student.rollno = teacher.rno;
SQL> update teacher_info set tname = 'a k parikh' where tname = 'jatin zinzuvadiya';
SQL> drop view teacher_info;

2) Index : An index is a way of presenting data differently then the way it appears on the disk. We
can create indexes explicitly to speed up SQL statement execution on a table. Similar to the
indexes in books that help us to locate information faster, an oracle index provides a faster
access path to table data. The index points directly to the location of the rows containing the
value.

Example

SQL> create index ind_rollno on student(rollno);

Unique index : Indexes can be unique or non-unique. Unique indexes guarantee that no two
rows of a table have duplicate values in the column that define the index. Non-unique index do
not impose this restriction on the column values.
SQL> create unique index ind_tno on teacher(tno);

Composite index : A composite index (also called a concatenated index) is an index created on a
multiple column of a table.

SQL> create index ind_rnotno on teacher(rno,tno);

Reverse key indexes : You can also create a index in reverse order. Under some circumstances
using a reverse-key index can make application run faster.
SQL> create index ind_rnotno on teacher(rno,tno) reverse;

3) Synonym : A Synonym is a database object which is used as an alias (alternative name) for a
table. They are used to Simply SQL statements, hide the name and owner of an object and
provide public access to an object.

Example

SQL> create synonym syn_student for student;


SQL> drop synonym syn_student;
SQL> create public synonym syn_student for student;

4) Sequences : A sequence is a database object which can generate unique, sequential integer
values. It can be used to automatically generate primary keyor unique key values. A sequence
can be either in ascending or descending order.

Syntax : create sequence seq_name [increment by n] [start with n] [ maxvalue n] [minvalue n]


[cycle/nocycle] [ cache/nocache]

Cycle : specifies that the sequence continues to generate values from the beginning after
reaching either its max or min value. The default values is no cycle.

Cache : The cache option pre-allocates a set of sequence number an retains them in memory so
that sequence number can be accessed faster. The default value is no cache.

SQL> Create sequence seq_tno increment by 1 start with 4 maxvalue 10;


SQL> insert into teacher (tno) values (seq_tno.nextval);

Alter Sequence : The sequence can be altered when we want to perform the follwing :
● Set or eliminate minvalue or maxvalue
● Change the increment value
● Change the number of cached sequence numbers.

SQL> alter sequence seq_tno maxvalue 15;


SQL> alter sequence seq_tno increment by 2;
SQL> drop sequence seq_tno;

N.B. : seq_tno.currval gives the current value of seq_tno sequenece.


seq_tno.nextval gives the next value of seq_tno sequenece.

You might also like