Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

KKW Unit 3 Interactive SQL and Advanced SQL (Part B)

Uploaded by

thakkarparth793
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

KKW Unit 3 Interactive SQL and Advanced SQL (Part B)

Uploaded by

thakkarparth793
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Data Base Management System 2020

Unit 03- Interactive SQL and Advanced SQL


Course Outcomes (COs) / Unit Outcomes (UOs):
Course Outcome (CO 304.1):Design Normalized database on given data
(CO 304.2): Create & manage database using SQL command
Unit Outcomes:
3a)Write the given queries using relevant functions
3b)Write query to combine the given multiple table using Join
3c)Design SQL queries to implement VIEWS on the given table
3d)Apply & drop INDEXES & SYNONYM on the given table
4a)Write simple PL/SQL code using control structure & handle various exceptions in the given situation
4b)Create cursor for retrieving multiple records in the given situation
4c)Create & execute stored procedures & functions in the given situation
4d)Create & apply database trigger using PL/SQL in the given situation

 Views
 View is Logical representation of subset of data from one or more tables.
 View is Virtual table and its data derived from Base table.
 All operation performed on View affect the Base table.
 A view is a virtual Table, through which a selective portion of the data from one or more table can
be seen.
 A view takes output of Query and treats as Table. Therefore a view can be called as “stored query”
OR “Virtual table”.
 View allow user to do following :
1) Structure data As per user Need
2) Restrict access the data i.e. User can see data only
3) Summarize data from various tables.

Use of View

 View provides powerful and flexible security mechanism by hiding parts of database from certain
user.
 View allows users to access data in a customized way.
 View can simplify complex operation on base relation.

Advantages of Views

 To restrict Data Access.


 To make Complex Queries Easy.
 To provide data Independence.
 To Present different view of same data.

Types of Views

1) Read Only View


In this view user can only see the data, user cannot make any changes in it.
2) Updatable View
In this type of view user can insert, update and delete the data.

Prepared By: Ms. Y. U. Kadam, Lecturer, KKWP, Nashik Page 1


Data Base Management System 2020

Syntax for Creating View:


SQL > CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW <view name> AS <query>
[WITH CHECK OPTION] [WITH READ ONLY];
Parameter:

 OR REPLACE: - The OR REPLACE option is used to recreate a view that already exists.

 FORCE - The FORCE option allows a view to be created even if a base table that the view
references does not already exist.

 NOFORCE- NOFORCE allows a system user to create a view if and only if BASE TABLE
is Exist .This is the default option.

 WITH CHECK OPTION -The WITH CHECK OPTION clause allows rows that can be
selected through the view to be updated.

 Query – select statement.

 WITH READ ONLY - The WITH READ ONLY option allows creation of a view that is
read-only. You cannot use the DELETE, INSERT, or UPDATE commands to modify data for
the view.
Example
Consider Table “student”

SID FNAME LNAME


---------- ---------- ----------
1 Sachin patil
2 Pooja pande
3 nirmal das
4 Navin Rao
5 Ajay Bose
6 Raj Dey

SQL> create or Replace view stud as select sid, fname from student;
Output: View created.

SQL> select * from stud;


SID FNAME
---------- ----------

1 Sachin
2 Pooja
3 nirmal
4 Navin
5 Ajay
6 Raj

Prepared By: Ms. Y. U. Kadam, Lecturer, KKWP, Nashik Page 2


Data Base Management System 2020

Creating View Using the WITH CHECK OPTION Clause


 You can ensure that DML operations performed on the view stay in the domain of the view by
using the WITH CHECK OPTION clause:

SQL > create or replace view stud as as select * from student where Sid=1 with check option
Constraint sid_ck;

Output: View created


Any attempt to change the sid number for any row in the view fails because it violates the WITH
CHECKOPTIONconstraint

Creating View WITH READ ONLY Option


 You can ensure that no DML operations occur by adding the WITH READ ONLY option to your
view definition.

 Any attempt to perform a DML operation on any row in the view results in an error.

 With read only not allow DML Operations


SQL>CREATE OR REPLACE VIEW studAS SELECT sid, fname, lname FROM student
WHERE sid = 10 WITH READ ONLY;

Output: View created.

Insertion of data into view


User can insert the data into view using insert command. If row is inserted into view, then
Base table also affected.
Syntax:

SQL> insert into <view_name> values(value1, value2, value3,…,value n);

Example:

SQL> Insert into stud values (7,‟ABC‟); 1 row inserted

Deletion of data from view


User can delete the data from view using delete command. If row is deleted from view, then
Base table also affected.

Syntax:

Delete from <view_name > where <columnname=condition>;

Example:

Delete from stud where sid=7;

1 row deleted
Prepared By: Ms. Y. U. Kadam, Lecturer, KKWP, Nashik Page 3
Data Base Management System 2020

Updating Data from View


 All update to Base table immediately reflect in all view that reference to base table.
 IF view is updated, then Base table also get update.
 View is not update if it contain keyword DISTINCT, Aggregate Function ,set Function
, ORDER by Clause, GROUP BY or having clause With SELECT Clause in Quer y.
Also where clause may not contain Sub queries .In all this case view is not update.

Syntax:

update <view_name> set <columname=expression> where <columname=expression>;

Example:

update stud set sid=8 where sid=1;

1 row updated

Removing a View
You can remove a view without losing data because a view is based on underlying tables in the
database.

Syntax :

DROP VIEW <view_name>;

Example:

DROP VIEW Stud;

View Dropped

Prepared By: Ms. Y. U. Kadam, Lecturer, KKWP, Nashik Page 4


Data Base Management System 2020

 Sequence
 The quickest way to retrieve the data from table is to have column in a database whose data
uniquely identifies a row.
 Just like tables, views, indexes, and synonyms, a sequence is a type of database object.
 Oracle provide a way to create a column whose value is generates automatically as a numeric
values.
 Oracle provides an object called sequence that generates numeric values.
 A value generated can have maximum 38 digits.
 Sequences are used to generate unique, sequential integer values that are used as primary key
values in database tables.
 A sequence can be defined to
1) Generate in either ascending or descending order.
2) Provide intervals between the numbers.
3) Automatically generate unique numbers.
4) Create a primary key value.
5) A sequence Speeds up the efficiency of accessing sequence values when cached in
memory.

Creation of sequence
Minimum information required for sequence is:
1. Maximum number
2. Start number
3. Increment value
Syntax:

SQL> CREATE SEQUENCE <sequence name> START WITH <start value number>

INCREMENT BY <number> MAXVALUE <MAXIMUM VLAUE NUMBER> | NOMAXVALUE

MINVALUE <minimum value number> | NOMINVALUE CYCLE | NOCYCLE

CACHE <number of sequence value to cache> |NOCACHE ORDE NOORDER;

• The default start_num is 1.


• The default number of integers to cache is 20.
• The minimum number of integers that may be cached

Keywords and parameters


1. Increment by specifies the interval between sequence number, default increment number is 1.
2. Minvalue specifies the minimum value of a sequence
3. Nominvalue specifies the minimum is 1 for an ascending sequence or -10^26 for a descending
sequence.
4. Maxvalue specifies the maximum value of a sequence
5. Nomaxvalue specifies the maximum is 10^27 for an ascending sequence or -1 for descending
order
6. Start with specifies the first sequence number to be generated
Prepared By: Ms. Y. U. Kadam, Lecturer, KKWP, Nashik Page 5
Data Base Management System 2020
7. CYCLE specifies the sequence generates integers even after reaching its maximum or minimum
value. When an ascending sequence reaches its maximum value, the next value generated is the
minimum and when a descending sequence reaches its minimum value, the next value generated
is the maximum.
8. NOCYCLE specifies the sequence cannot generate any more integers after reaching its
maximum or minimum value.
9. CACHE cache_num specifies the number of integers to keep in memory.
10. NOCACHE specifies no integers are to be stored.
11. ORDER guarantees the integers are generated in the order of the request. You typically use
ORDER when using Real Application Clusters.
12. NOORDER doesn't guarantee the integers are generated in the order of the request. NOORDER
is the default.

Example
Create a sequence named “sq_sal” whose initial value is 1, interval is 1 and the maximum value is 2,
Do not use the CYCLE option

SQL > CREATE SEQUENCE sq_sal START WITH 1 INCREMENT BY 1


MAXVALUE 9 NOCYCLE;

Output: Sequence created

Accessing Sequence Values


• NEXTVAL- returns the next available sequence value. It returns a unique value every time it is
referenced.
Syntax: SQL> Select <sequence_name>.nextval from dual;
Example: SQL>Select sq_sal .nextval from dual;
Output- 1
SQL>Select sq_sal .nextval from dual;
Output- 2

• CURRVAL - obtains the current sequence value.


SQL> Select <sequence_name>.currval from dual;
Example:
Select sq_sal .currval from dual;
Output - 2

Inserting a value through Sequence


User can insert the data into table using sequence

Example

SQL> create table sales (product_id number(10), pname varchar2(10));

Table created.

Prepared By: Ms. Y. U. Kadam, Lecturer, KKWP, Nashik Page 6


Data Base Management System 2020
The INSERT commands shown below insert three rows into the sale table.

The INSERT commands reference the sq_sal.nextval column.

SQL >INSERT INTO sales values (sq_sal.nextval, „CD‟ );


SQL> INSERT INTO sales values (sq_sal.nextval, „DVD‟);
SQL> INSERT INTO sales values (sq_sal.nextval, „SMPS‟);

SQL>SELECT * FROM sales;

PRODUCT_ID PNAME

---------- ----------

1 CD
2 DVD
3 SMPS

Modifying a Sequence
 Change the increment value, maximum value, minimum value, cycle option, or cache option.
 Only future sequence numbers are affected and increment by and max value can be changed
 We cannot change the minimum value once sequence gets created.

SQL> ALTER SEQUENCE seq_sales INCREMENT BY 2 MAXVALUE 20 NOCAC NOCYCLE;

Output: Sequence altered.

To remove a sequence
Syntax: SQL> DROP SEQUENCE <sequence_name>;

Example: SQL> DROP SEQUENCE sq_sal;

Output: sequence dropped

Prepared By: Ms. Y. U. Kadam, Lecturer, KKWP, Nashik Page 7


Data Base Management System 2020

 INDEX
 Indexing table is an access strategy
 It is the way to search and sort the records in a table.
 An Oracle Server index is a schema object that can speed up the retrieval of rows by using a
pointer.
 Indexes can be created explicitly or automatically.
 If you do not have an index on the column you‟re selecting, then a full table scan occurs.
 An index provides direct and fast access to rows in a table.
 Its purpose is to reduce the necessity of disk I/O (input/output) by using an indexed path to
locate data quickly.
 The index is used and maintained automatically by the Oracle Server.
Types of Index

1) Simple index

2) Unique index

3) Composite index

1) Simple index or Single column Index :


 An index created on single column of a table is called a Simple Index.

Syntax: SQL> Create index <Index Name> on <table_name> (column name);


Example: Create index eid_ind on Emp ( EmpID);
Output: Index Created

2) Composite Index
 Composite index is an index on two or more columns of a table.
 To create a composite index, take into consideration the columns that you may use very
frequently in a query's WHERE clause as filter conditions.
 If there are two or more columns that are frequently used in the WHERE clause as filters, the
composite index would be the best choice.
Syntax : SQL> Create index index_name on table_name (column1,column2);
Example : SQL> Create index index_emp_dept on emp (empno,deptno);
Output: Index Created

3) Unique Indexes
 Unique index are used not only for performance, but also for data integrity.
 A unique index does not allow any duplicate values to be inserted into the table.
Syntax : SQL> Create unique index index_name on table_name (column_name);
Example: SQL > Create unique index ind_emp on emp (empno);

4) Reverse Indexes
User can also create reverse index
Syntax : SQL> Create index index_name on table_name (column1) reverse;
In Normal Index In reverse index
C1 1C
C2 2C
C3 3C

Prepared By: Ms. Y. U. Kadam, Lecturer, KKWP, Nashik Page 8


Data Base Management System 2020
Dropping Indexes.

 You cannot modify indexes.


 To change an index, you must drop it and then re-create it.
 Remove an index definition from the data dictionary by issuing the
 DROP INDEX <index_name >statement.
 If you drop a table, indexes and constraints are automatically dropped.
Syntax: SQL> DROP INDEX <index name>;

Example: SQL> DROP INDEX ind_emp;

 Synonyms
 It is another name given to the object.
 It simplifies the access to the object.
 A synonym is a form of short name used to simplify the task of referencing a database object.

Creating Synonyms
The general form of the CREATE SYNONYM command is:

SQL> CREATE SYNONYM synonym_name FOR object_name; Example

SQL> Create synonym stud for student;

Output: Synonym created

Removing Synonyms
If you own a synonym, you have the right to drop (delete) the synonym. The DROP SYNONYM
command is quite simple.

Syntax: SQL> DROP SYNONYM <synonym_name>;


Example: SQL>Drop synonym stud;

Output: synonym dropped.

Renaming Synonyms
 Synonyms can be renamed with the RENAME SYNONYM command.
 All existing references to the synonym are automatically updated.
 The syntax of the RENAME SYNONYM command is like that for the RENAME command
for any other database object such as a view or table.
Syntax: SQL> RENAME old_synonym_name TO new_synonym_name;

Example: SQL> rename stud to stud1;

Prepared By: Ms. Y. U. Kadam, Lecturer, KKWP, Nashik Page 9


Data Base Management System 2020

 Snapshots
 Snapshot is a recent copy of a table from database or set of rows and columns of a table.
 It is also known as materialized view.
 A snapshot is created on destination system with create snapshot command.
 A snapshot can be simple or complex
 A simple snapshot consists of either a single table.
 A complex snapshot consists of join tables, views or group by or complex select statement.
Uses:
 Useful in distributed environment.
 Response time of the queries gets minimized as the client has made a local copy of master
table.
 If the master table gets corrupted, then data can be restored using snapshot.

Creating Snapshots:
Create snapshot command is used to create the snapshot.
Syntax:
SQL> Create snapshot <snapshot_name> refresh with rowid as <select query>;
Example:
SQL> Create snapshot emp_snap refresh with rowid as select * from emp;
Output: Materialized View Created

Altering Snapshots:

SQL>Alter snapshot <snapshot_name> refresh with rowid;


Example:
SQL>Alter snapshot emp_snap refresh with rowid;

DROP snapshot

SQL>DROP snapshot <snapshot_name>;


Example:
SQL> Drop snapshot emp_snap;

Prepared By: Ms. Y. U. Kadam, Lecturer, KKWP, Nashik Page 10

You might also like