3.
VIEWS, SEQUENCES, SYNONYMS
AIM:
To create database and apply Views, Sequences, Synonyms.
SQL - Using Views
A view is nothing more than a SQL statement that is stored in the database
with an associated name. A view is actually a composition of a table in the form of
a predefined SQL query.
A view can contain all rows of a table or select rows from a table. A view
can be created from one or many tables which depends on the written SQL query
to create a view.
i) Creating Views
syntax:
CREATE VIEW view_name AS SELECT
column1, column2... FROM
table_name WHERE [condition];
ii) Updating a View
A view can be updated under certain conditions which are given below −
The SELECT clause may not contain the keyword DISTINCT.
The SELECT clause may not contain summary functions.
The SELECT clause may not contain set functions.
The SELECT clause may not contain set operators.
The SELECT clause may not contain an ORDER BY clause.
The FROM clause may not contain multiple tables.
The WHERE clause may not contain subqueries.
The query may not contain GROUP BY or HAVING.
Calculated columns may not be updated.
All NOT NULL columns from the base table must be included in the
view in order for the INSERT query to function
Syntax:
VIEW_NAME SET CONDITION WHERE
1
iii) Inserting Rows into a View
Rows of data can be inserted into a view. The same rules that apply to the
UPDATE command also apply to the INSERT command.
Here, we cannot insert rows in the CUSTOMERS_VIEW because we have not
included all the NOT NULL columns in this view, otherwise you can insert rows
in a view in a similar way as you insert them in a table.
iv) Deleting Rows into a View
Rows of data can be deleted from a view. The same rules that apply to the
UPDATE and INSERT commands apply to the DELETE command.
Syntax:
DELETE FROM view_name WHERE condition;
v) Dropping Views
View will be deleted from the database.
Syntax:
DROP VIEW VIEW_NAME;
List of Qureries:-
1. Create a table employees with fields Emp_Id, Emp_name, Emp_age, Emp_address,
Emp_salary.
2. Create a view that contains fields Emp_Id, Emp_name, Emp_age, Emp_address,
Emp_salary.
3. Display the details of view.
4. Update the employee age in view of particular name.
5. Delete the particular employee id in the view of particular age.
6. Drop the view name of particular table.
SQL | SEQUENCES
Sequence is a set of integers 1, 2, 3, … that are generated and supported by some
database systems to produce unique values on demand.
A sequence is a user defined schema bound object that generates a
sequence 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 provides 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 exceeds max_value.
In My SQL it is done by using auto increment.
2
Syntax:
CREATE SEQUENCE sequence_name START WITH initial_value
INCREMENT BY increment_value MINVALUE minimum value MAXVALUE
maximum value CYCLE|NOCYCLE ;
Synonyms
Description
A synonym is an alternative name for objects such as tables, views, sequences,
stored procedures, and other database objects.
You generally use synonyms when you are granting access to an object from
another schema and you don't want the users to have to worry about knowing
which schema owns the object.
Create Synonym (or Replace)
You may wish to create a synonym so that users do not have to prefix the table
name with the schema name when using the table in a query.
Syntax
The syntax to create a synonym:
CREATE [OR REPLACE] [PUBLIC] SYNONYM
[schema .] synonym_name FOR [schema .]
object_name [@ dblink];
Drop synonym
Syntax
DROP[PUBLIC] SYNONYMS[schema.] synonym_name [force];
RESULT
The database queries for views, sequence, synonym are executed successfully.
3
The following will be written as left side.
views:-
1,2,3:
4:
5:
4
6: