RDBMS Topic 4 Adv. SQL, MSBTE Questions and Answers ( 12 ...RDBMS Topic 4 Adv. SQL, MSBTE Questions...
Transcript of RDBMS Topic 4 Adv. SQL, MSBTE Questions and Answers ( 12 ...RDBMS Topic 4 Adv. SQL, MSBTE Questions...
RDBMS Topic 4 Adv. SQL, MSBTE Questions and Answers ( 12 Marks)
2017
2016
Q. What is view? Definition of view: 2 marks)
Ans :
View: A view is a logical extract of a physical relation i.e. it is derived from any
base
relation.
OR
View: Views are virtual relations mainly used for security purpose, and can be
provided
on request by a particular user.
Q. Consider following schema. ( 4 Marks)
Employee (empname, empid, dob, salary, job)
Create a view on employee having attribute (empname, empid, dob, salary,
job) where salary is greater than 20,000.
Ans:
Create view EMPVIEW as select empname, empid, dob, salary, job from
employee where salary>20000;
OR
CREATE VIEW EMPVIEW AS SELECT * FROM EMPLOYEE WHERE
SALARY >20000;
Q. What are views? Give its syntax and explain its advantages.
(Views - 2 marks; Syntax - 1 mark; Advantages (Any two) - 1 mark)
Views are created for security reasons. Instead of coping same table multiple times
for different requirements, views can be created. View is a logical copy of physical
table.
It doesn‘t exist physically. With the help of view, we can give restricted access to
users. When view is used, underlying table is invisible, thus increasing security.
Views can be used to see, insert, update and delete data from base table.
Syntax for creating view:-
Create [OR Replace][Force /Noforce] view <viewname>[alias name ….] As
subquery [with CHECK OPTION[CONSTRAINT]] [with READ ONLY];
Advantages of Views:
Views restrict access to the data because the view can display selective
columns from the table.
Views can be used to make simple queries to retrieve the results of
complicated queries. For example, views can be used to query information
from multiple tables without the user knowing how to write a join statement.
Views provide data independence for adhoc users and application programs.
One view can be used to retrieve data from several tables.
Views provide groups of users to access to data according to their particular
criteria. Thus implements security and authorization.
2015
Explain views with example.
(Explanation of view with syntax – 3 Marks, Example - 1 Mark)
Views are created for security reasons. Instead of coping same table multiple times
for different requirements, views can be created.
View is a logical copy of physical table. It doesn’t exist physically. With the help
of view, we can give restricted access to users.
When view is used ,underlying table is invisible, thus increasing security. Views
can be used to see, insert, update and delete data from base table.
Syntax for creating view:-
Create [OR Replace][Force /Noforce] view
<viewname>[alias name ….]
As subquery
[with CHECK OPTION[CONSTRAINT]]
[with READ ONLY];
Example :
Create view emp_info as select Emp_no, Emp_name from Employee where
salary>12000;
To describe content of view
Select * from emp_info;
To describe structure of view
describe emp_info;
Q. How to create view? (Syntax OR Example of view - 2 Marks)
Ans:
Syntax for creating view.
Create view <viewname> as select <query>;
OR
Example :
Create viewemp_info as select Emp_no, Emp_name from Employee;
2014
Q. Write the syntax for creating a view. (Syntax- 2 Marks)
Ans: Syntax for creating view. Create view <viewname> as select <query>
2016
Q. What are sequence? Why it is used? Create sequence for STUDENT table.
(Definition - 1 mark; Use - 1 mark; creating valid sequence example/pattern - 2
marks)
Ans:
Definition: A sequence refers to a database object that is capable of generating
unique and sequential integer values.
Use:
1. It saves a time by reducing application code.
2. It is used to generate unique sequential integers.
3. It is used to create an auto number fields.
4. Sequence can be use for many tables/relations
Sequence for ‘student’ table: Create sequence student_seq increment by 1 start
with 1 maxvalue 60 nocycle;
2015
Q Explain sequences with example.
(Definition of sequence - 1 Mark, syntax – 2 Marks, Example - 1 Mark)
Ans:
Definition
A sequence refers to a database object that is capable of generating unique and
sequential integer values.
Syntax:
Create sequence <seq_name> [increment by num] [start with num] [maxvalue
num] [minvaluenum][cycle/nocycle][cache num/nocache];
Where,
Increment by num: Used to specify the interval between sequence numbers.
Start with num: States the first sequence numbers that needs to be generated.
Minvalue num: This is used to state the minimum value of the sequence.
Maxvalue num :It states the maximum value generated by sequence.
Cycle: Cycle indicates that the sequence will be continued for generating the
values from thestarting after reaching either its maximum value or minimum value.
Cache: The cache option is used to pre-allocates a set of sequence numbers and
keep these numbers in the memory so that they can be accessed.
No Cache: This states that there is no pre-allocation for the values of sequence.
Example:
Create sequence student_seq increment by 1 start with 1 maxvalue 60 nocycle;
Q. What is sequence? What are the various operations with respect to
sequences? (Sequence explanation – 2 Marks, any two operations – 2 Marks)
Ans:
Sequence :
exists a column which can uniquely identify the record
from a table. For that purpose, that column has to be a primary key which can
never contain duplicate or null value.
minimized by creating sequence for automatic entry of certain values which are in
sequence.
be inserted into a table.
numeric values.
The value generated can have maximum of 38 digits.
Operations allowed on sequence:
1) Create a sequence :
Create sequence < sequence name >
[incremented by <integer value> start with <integer value>Maxvalue<integer
value>/nomaxvalue
minvalue<integer value>/nominvalue cycle/ nocycle]
cache<integer value>/Nocache
order/noorder
INCREMENT BY:Specifies the interval between sequence number. It can be any
positive or negative value but not zero. If this clause is omitted the default value is
1.
MINIVALUE: Specifies the sequence minimum value.
NOMINVALUE: Specifies the maximum value of 1 for an ascending sequence
and –(10)^26 for a descending sequence.
MAXVALUE: Specifies the maximum value that a sequence can generate.
NOMAXVALUE: Specifies a maximum of 10^27 for an ascending sequence or -1
for a descending sequence is the sequence minimum value(1) and for a descending
sequence, it is the maximum value(-1).
CYCLE: Specifies that the sequence continues to generate repeat values after
reaching either its maximum.
NOCYCLE: Specifies that a sequence cannot generate more values after reaching
the maximum value.
Example
Create sequence addr_sqe increment by 1 start with 1 minivalue 1 maxvalue 999
cycle;
2) Referncing a sequence
Once a sequence is created SQL can be used to view the values held in its cache.
To simply view sequence value use a SELECT sentence as describe below.
SELECT<SequenceName>.NextVal FROM DUAL;
Every time nextval reference a sequence its output is automatically increment from
the old values to the values to the new value ready for use.
INSERT INTO
ADDR_DTLS(ADDR_NO,CODE_NO,ADDR_TYPE,ADDR1,ADDR2,CITY,STATE
,PINCODE)VALUES(ADDR_SEQ.NextVal,‟B5‟,‟B‟,‟Vertex Plaza,Shop
4,‟,‟Western Express Highway,Dahisar(East),‟,‟Mumbai‟,‟400078‟);
To reference the current value of a sequence:
SELECT<SequenceName>.CURRVAL FROM DUAL;
3) Altering a sequence
A sequence once created can be altered. This is achieved by using the ALTER
SEQUENCE statement .
SYNTAX:
ALTER SEQUENCE<SequenceName>
[Increment by <IntegerValue> Maximum<IntegerValue>/NOMAXVALUE
MINVALUE<IntegerValue>/NOMINVALUE CYCLE/NOCYCLE
CACHE<IntegerValue>/NOCACHE ORDER/NOORDER]
Change the cache value of the sequence ADDR_SEQ to 30 and interval between
two numbers as 2.
Example - ALTER SEQUENCEADDR_SEQ INCREMENT BY 2 CACHE 30;
4) Dropping a Sequence
The DROP SEQUENCE command is used to remove the sequence from database.
Syntax:
DROP SEQUENCE<SequenceName>;
2014
What are sequences? Write syntax for creating sequence. (Explanation -2
Marks, Syntax-2 Marks) Ans: Sequence:
It is database object that generate/produce integer values in sequential order.
It automatically generates primary key and unique key values.
It may be ascending or descending order.
It can be used for multiple tables.
Sequence numbers are stored and generated independently of tables.
Syntax:
Create sequence <seq_name>
Start with [initial value]
Increment by [value]
Minvalue [minimum value]
Maxvalue [maximum value]
[cycle/nocycle]
[{cache value / No cache}]
[{order / No order}];
2017
Q. Define index. ( 2 Marks)
Ans:
Index: An index is a schema object that can speed up the retrieval of rows by
using pointer. An Index provides direct and fast access to rows in a table.
Q. Describe unique index and composite index with example.(4 marks)
Ans:
Unique Index:
Unique indexes are used not only for performance, but also for data integrity. An
unique index does not allow any duplicate values to be inserted into the table.
The basic syntax is as follows:
CREATE UNIQUE INDEX index_name on table_name (column_name);
Example:
CREATE UNIQUE INDEX ename_idx on emp (ename);
Composite Index:
A composite index is an index created on two or more columns of a table.
The basic syntax is as follows:
CREATE INDEX index_name on table_name (column1, column2);
Example:
CREATE INDEX en_idx on emp (ename,job);
2016
Q. What is index ? Explain types of index. (Index Definition - 2 marks; Any
Two Types - 1 mark each)
Ans:
Index: An index is a schema object that can speed up the retrieval of rows by
using pointer. An index provides direct and fast access to rows in a table. Indexes
are created explicitly Or automatically. Indexes are used to speed up
searches/queries.
Types of Index:
Simple index (Single column): An index created on single column of a table is
called a Simple Index. Unique indexes 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.
Composite (concatenated): Indexes that contain two or more columns from the
same table which are useful for enforcing uniqueness in a table column where
there‘s no single column that can uniquely identify a row.
Q. Explain the following terms with syntax and example: ( 4Marks)
a) Simple Index
b) Composite Index
c) Unique Index.
a) Simple Index:
A simple index is one that is created based on only one column of a table.
The basic syntax is as follows:
CREATE INDEX index_name ON table_name (column_name);
Example:
CREATE INDEX ename_idx on emp (ename);
b) Composite Index:
A composite index is an index created on two or more columns of a table.
The basic syntax is as follows:
CREATE INDEX index_name on table_name (column1, column2);
Example:
CREATE INDEX en_idx
on emp (ename,job);
c) Unique Indexes:
Unique indexes 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. The
basic syntax is as follows:
CREATE UNIQUE INDEX index_name on table_name (column_name);
Example:
CREATE UNIQUE INDEX ename_idx on emp (ename);
2015
Q. Explain the concept of indexing with neat diagram.
(Explanation - 3 Marks, Any relevant Diagram - 1 Mark)
Ans:
Index: An index is a schema object that can speed up the retrieval of rows by
using pointer. An index provides direct and fast access to rows in a table. Indexes
are created explicitly Or automatically. Indexes are used to speed up
searches/queries.
Types of Index:
Simple index(Single column): An index created on single column of a table is
called a Simple Index.
Unique indexes 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.
Composite (concatenated): Indexes that contain two or more columns from the
same table which are useful for enforcing uniqueness in a table column where
there’s no single column that can uniquely identify a row.
Q Explain with example simple and composite index.
(Simple Index Explanation & Example-2 Marks, Composite Index Explanation
&Example- 2 Marks)
Ans:
Simple index: An index created on single column of a table is called a Simple
Index.
Syntax: Create index on <tablename><column name>;
E.g.: Create index idx on employee (empno);
Composite Index:An Index that contain two or more columns from the same table
is called as Composite Index.
Syntax: Create index on <tablename><column name1, Column name 2>;
E.g.: Create index idx on employee (ename, empno);
2014
Q. Describe following terms: i) unique indexes ii) composite indexes. (Each
index – 2 Marks)
i) Unique indexes 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.
The basic syntax is as follows:
Create unique index index_name on table_name(column_name);
The example is as follows:
Create unique index index_empno on emp(empno);
ii) 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.
The basic syntax is as follows:
Create index index_name on table_name(column1,column2);
The example is as follows:
Create index index_empdept on emp(empno,deptno);
2017
Q. Explain snapshot with example. (4 Marks)
Snapshot:
It is also known as materialized view.
It is a copy of either an entire single table or set of its rows or collection of
tables, views or rows using join, grouping and
selection criteria.
Useful in distributed environment
It has two types:
Simple snapshot and complex snapshot. Simple snapshot related to single table and
complex snapshot related to joined tables.
Example :
Operations on snapshot:
i) Creating Snapshot:
Create snapshot command is used to create the snapshot.
Syntax:-
CREATE SNAPSHOT [schema.] <snapshot name>AS subquery;
Example:-
Create snapshot emp_snapas select * from emp where deptno=6;
ii) Altering snapshot:
Snapshot can be altered by using ALTER SNAPSHOT command.
The only parts ofa snapshot that can be altered are its storage parameters,
refresh type and refresh start,and next interval.
The select for the snapshot, base tables, and other data related items cannot
be changed without dropping and recreating the snapshot.
Syntax:-
ALTER SNAPSHOT <snapshotname>
[[PCTFREE <integer>]
[PCTUSED <integer>]
[REFRESH [FAST/COMPLETE/FORCE]];
Example:-
To change the automatic refresh mode for the emp_data snapshot to
fast:
ALTER SNAPSHOT emp_data REFRESH FAST;
iii) Dropping a snapshot
To remove the snapshot DROP SNAPSHOT Command .When
snapshot is dropped which a snapshot log had associated with it, only
the rows required for maintainingthat snapshot are dropped.
Syntax:-
Drop snapshot <snapshot name>;
Example:-
Drop snapshot emp_snap;
2016
What are snapshots? Give its uses. How to create a snapshot?
(Definition - 1 mark; any one Use - 1 mark, Syntax/Example - 2 marks) Ans:
Snapshots:
It is also known as materialized view.
It is a copy of either an entire single table or set of its rows or collection of
tables, Views or either rows using join, grouping and selection criteria.
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:
Create snapshot snapshot_name refresh with rowid as <select query>;
Example:
Create snapshot emp_snap refresh with rowid as select * from emp;
Q. Explain the following terms with syntax and example. (4 Marks)
i) Creating snapshot
ii) Altering snapshot
iii) Dropping a snapshot.
i) Creating Snapshot:
Create snapshot command is used to create the snapshot.
Syntax:-
CREATE SNAPSHOT [schema.] <snapshot name>
AS subquery;
Example:-
Create snapshot emp_snap
as select * from emp where deptno=6;
ii) Altering snapshot
Snapshot can be altered by using ALTER SNAPSHOT command. The only
parts of a snapshot that can be altered are its storage parameters, refresh type
and refresh start, and next interval.
The select for the snapshot, base tables, and other data related items cannot
be changed without dropping and recreating the snapshot.
Syntax:-
ALTER SNAPSHOT <snapshotname>
[[PCTFREE <integer>]
[PCTUSED <integer>]
[REFRESH [FAST/COMPLETE/FORCE]];
Example:-
To change the automatic refresh mode for the emp_data snapshot to fast:
ALTERSNAPSHOT emp_data REFRESH FAST;
iii) Dropping a snapshot
To remove the snapshot DROP SNAPSHOT Command .When snapshot is dropped
which a snapshot log had associated with it, only the rows required for maintaining
that snapshot are dropped.
Syntax:-
Drop snapshot <snapshot name>;
Example:- Drop snapshot emp_snap;
2015
Explain snapshot with example. (Explanation of snapshot – 3 Marks, example –
1 Mark)
Ans:
Snapshot:
• It is also known as materialized view.
• It is a copy of either an entire single table or set of its rows or collection of tables,
views or rows using join, grouping and selection criteria.
• Useful in distributed environment
It has two types:
Simple snapshot and complex snapshot. Simple snapshot related to single table and
complex snapshot related to joined tables.
Example:
Snapshot for emp table:
Create snapshot emp_data refresh with rowid as select * from emp;
Q. Write a PL/SQL program to create any snapshot.
(Creation of snapshot – 2 Marks, PL/SQL code – 2Marks)
[Note: Any other relevant example of PL/SQL code can be considered]
Ans:
1) To create a snapshot :
Create snapshot emp_snap refresh with rowid as select * from emp;
2) To use snapshot from PL/SQL :
declare
cnt as number(2):=0;
begin
Select count(*) into cnt from emp_snap;
dbms_output.put_line („Number of rows from snapshot :‟ || cnt);
end;
2014
Q. What is snapshot? Write the syntax for creating a snapshot. (Explanation-
2 Marks, Syntax- 2 Marks)
Ans:
Snapshot:
It is also known as materialized view.
It is a copy of either an entire single table or set of its rows or collection of
tables,
Views or either rows using join, grouping and selection criteria.
Useful in distributed environment
It has two types: Simple snapshot and complex snapshot. Simple snapshot related
to single table and complex snapshot related to joined tables.
Syntax:
Create snapshot snapshot_name Pctfree n Pctused n Tablespace tablespace_name
Storage clause Refresh Star with date As select statement;
OR
Create snapshot snapshot_name As select statement;
2014
Q What are synonyms? Write a syntax for creating a synonym. (Synonym
definition -2 Marks, Correct syntax -2 Marks)
Ans:
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.
Syntax:
CREATE [OR REPLACE] [PUBLIC] SYNONYM [schema.] synonym_name
FOR [schema.] object_name;
OR
create synonym synonym_name for object_name;
DROP SYNONYM
Once a synonym has been created in Oracle, you might at some point need to drop
the synonym.
Syntax
The syntax to drop a synonym in Oracle is:
DROP [PUBLIC] SYNONYM [schema .] synonym_name [force];
PUBLIC
Allows you to drop a public synonym. If you have specified PUBLIC, then you
don't specify a schema.
force
It will force Oracle to drop the synonym even if it has dependencies. It is probably
not a good idea to use force as it can cause invalidation of Oracle objects.
Example
Let's look at an example of how to drop a synonym in Oracle.
For example:
DROP PUBLIC SYNONYM suppliers;
This DROP statement would drop the synonym called suppliers that we defined
earlier.