You are on page 1of 7

UNIT IV

Joins– Types of joins-Set operators-. Views-Creating, removing and altering views

Sequences-Creation, dropping sequence. Table Indexes.

JOINS:

When the required data are in more than one tables are joined using
the join function .The joined condition combines a row in one table
in another table based on the same values in the common column

Cartesian product :

 A Cartesian product results from a multitable query that


does not have a WHERE clause.

 The product normally results in output with a large number


of rows & is not very useful

 Whenever retrieving data from more than one table it uses


one or more valid join condition to avoid certain Cartesian

TYPES OF JOINS:

There are four types of joins


(1) Equijoin
(2) Non equijoin
(3) Outer join
(4) Self join
(1)Equijoin
The equijoin is a join with a join condition involving common
columns from 2 tables

The general syntax is

SELECT columnnames includes both table separated by


columns
FROM tablenames name of the 2 tables
WHERE join condition;

the condition that includes common columns

(2) Non Equijoin


 Here the columns are not joined in equal
 Without any combinations
(3) Outer join
 If a row in one table does not have a
matching value in other table

 The table that does not contain the


matching is called as deficient table

The general syntax is


SELECT tablename1, columnname, tablename2, columnname
FROM tablename1, tablename2
WHERE tablename1, columnname (+) = tablename2. columnname
Ex:
WHERE tablename1.columnname = tablename2. columnname (+)
(4) Self join
 Self join is joining a table to itself
 Two copies of tables are created
SET OPERATORS:

The set operators are:


UNION
UNION ALL
INTERSECT
MINUS

(1) UNION

 It returns all rows from both queries but duplicated rows are not repeated
 It takes the output from the two queries
 Combining the both the values

(2) Union all

 The UNION ALL operations is similar to the UNION operation


 The only difference is that UNION ALL operation also displays duplicate rows

(3) Intersect:

 The INTERSECT operations works on output from 2 separate queries &


returns
 Rows that appear in both outputs
(4) Minus:

 The MINUS operation is same as the DIFFERENCE operation


 When MINUS operation is performed on output from 2 queries the result is the
rows in the first query result that are not in the second query result.

VIEWS:
In SQL, a view is a virtual table based on the result-set of an
SQL statement. A view contains rows and columns, just like a real table. The fields in
a view are fields from one or more real tables in the database. You can add SQL
statements and functions to a view and present the data as if the data were coming
from one single table.

CREATING A VIEW:

A view is created with the CREATE VIEW statement.

CREATE VIEW view_name AS


SELECT column1, column2, ...
FROM table_name
WHERE condition;

Example:

CREATE VIEW [Brazil Customers] AS


SELECT CustomerName, ContactName
FROM Customers
WHERE Country = 'Brazil';

SELECT * FROM [Brazil Customers];


REMOVING A VIEW:

A view is deleted with the DROP VIEW statement.


DROP VIEW view_name;
DROP VIEW [Brazil Customers];

ALTERING A VIEW:

When you alter an underlying table the view becomes invalid. You need to
recompile that view to make it valid again. The Alter view statement is used for the
recompilation of a view.

Syntax

ALTER VIEW VIEW_NAME COMPILE;

ALTER VIEW Brazil Customers COMPILE;

SEQUENCES:

 A sequence is a user-defined schema-bound object that generates a series of numeric


values.
 Sequences are frequently used in many 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 sequence of numeric values is generated in an ascending or descending order at
defined intervals and can be configured to restart when it exceeds max_value.

Different Features of Sequences

 A sequence is a database object that generates and produces integer values in


sequential order.
 It automatically generates the primary key and unique key values.
 It may be in ascending or descending order.
 It can be used for multiple tables.
 Sequence numbers are stored and generated independently of tables.
 It saves time by reducing application code.
 It is used to generate unique integers.
 It is used to create an auto number field.
 Useful when you need to create a unique number to act as a primary key.
 Oracle provides an object called a Sequence that can generate numeric
values. The value generated can have maximum of 38 digits.
 Provide intervals between numbers.

Creating Sequences:

Syntax:

CREATE SEQUENCE sequence_name

START WITH initial_value


INCREMENT BY increment_value
MINVALUE minimum value
MAXVALUE maximum value
CYCLE|NOCYCLE ;
CREATE SEQUENCE sequence_1
start with 1
increment by 1
minvalue 0
maxvalue 100
cycle;

CREATE TABLE students


(
ID number(10),
NAME char(20)
);
INSERT into students VALUES
(sequence_1.nextval,'Shubham');
INSERT into students VALUES
(sequence_1.nextval,'Aman');
Dropping Sequences:
Use the DROP SEQUENCE statement to remove a sequence from the database.

DROP SEQUENCE TABLENAME.SEQUENCENAME;

DROP SEQUENCE CUSTOMER. sequence_1;

TABLE INDEXES:

The CREATE INDEX statement is used to create indexes in tables. Indexes are used to
retrieve data from the database more quickly than otherwise. The users cannot see the
indexes, they are just used to speed up searches/queries.

Syntax
CREATE INDEX index_name
ON table_name (column1, column2, ...);

EXAMPLE

CREATE INDEX idx_lastname


ON Persons (LastName);

CREATE INDEX idx_pname


ON Persons (LastName, FirstName);

The DROP INDEX statement is used to delete an index in a table.

DROP INDEX index_name ON table_name;

You might also like