Académique Documents
Professionnel Documents
Culture Documents
Table Definition:-
A table is a unit of storage, which holds data in the form
of rows and columns. The Data Definition used for table
definition can be classified into the following three
categories.
• Create table command
• Alter table command
• Drop table command
Create a Table:-
Create table<table name>(column definition 1, column
definition 2..);
In a table
• We should specify a unique column name.
• We should specify proper data type along with
its width.
• We can include ‘Not null’ condition when
needed; by default it is ‘Null’.
Alter a table:-
Alter table <table name>modify(column definition…);
Alter table <table name> add(column definition..);
Drop a table:-
To dropping a table
Drop table <table name>;
Truncate:-
This command is used to delete all the records from the
table.Command to truncate a table is Truncate
table<table name>;
Desc:-
It shows the view the structure of the table Desc help to
achieve the same. Command to view the table’s structure
is Desc <table name>
Advantages of partitions:-
Ex:-
SQL> create table Mite(empno number(7),empname
varchar2(20), empadd varchar2(30)) partition by range
(empname)(partition p1 values less than(‘h’),partition p2
value less than(‘m’));
Integrity Constraints:-
An integrity Constraint is a mechanism used by Oracle 8i
to prevent invalid data entry into the table. It is nothing
but enforcing rule for the columns in a table. The various
types of integrity constraints are explained below:
‘Not null’:-
We know that by default all columns in a table Null
values. When a ‘Not null’ constraint is enforced on a
columns or a set of columns in a table, It will not allow
Null values. ’Not Null’ integrity constraint can be defined
using alter table command ever when the table contains
rows. But the rows violating the constrain rule must be
sent exception Zero and Null are not equivalent.
SQL>create table IT(empno number(6) constraint eno Not
Null,
Empname varchar2(30));
Where eno is the constraint name. This command will
ensure that the user enters a value for the empno
column on the IT table, failing which it returns an error
message.
Check constraints:-
These are rules governed by logical expressions or Boolean
expressions. Check conditions can’t contain subquries.
The following example will help us to understand it much
better & creates a table IT_order with a check constraint
to restrict the values of ordid to be within 1000.
Example:-
SQL>create table IT_order (ordid number(4) constraint
checkit check (ordid>1000),orderdate date, commplan
character(l), custid number(6),shipdate , tatal
number(8,2));
Maintains uniqueness in a record:-
An entity is any data recorded in a database. Each entity
represents a table and each row of a table represents an
instance of that entity. This constraint is used to identity
each row in a table uniquely we need to use this
constraint.
Unique constraints:-
Usage of the unique key constraint is to prevent the
duplication of values within the rows of a specified column
or a set of columns in a table. Columns defined with this
constraint can also allow Null values. If unique key
constraint is defined in more than one column, It is said
to be composite unique key. In the above case
combination of columns can’t be duplicated. Maximum
combination of columns that’s composite unique key can
contain is 16.
Example:-
SQL>create table ITT(id number(6), stdprice
number(8,2),minprice number(8,2), startdate date,
enddate date constraint unidate unique);
The same constraint can be enforced using table level
syntax also
Example
SQL>create table IT(id number(6), stdprice number(8,2),
enddate date, startdate, constraint unitab
unique(enddate));
The above example prevents the duplication of values to
be entered in enddate column.
E.g.:-
SQL> create table MITE(empid number(6), empname
varchar2(30),
Constraint compuni unique(empid, empname));
In this case combination of columns custid and name
can’t be duplicated.
Primary key constraints:-
This constraint avoids duplication of rows does not Null
values, When enforced in a column or set of columns. As a
result it is used to identify a row. A table can have only
one primary key. Primary key constraint can’t be defined
in an alter table command when the table contains rows
having Null values.
Example:-
SQL> create table IT_customer (custid number(6)
constraint prim_con primary key, name varchar(45),
address varchar2(30), city char(30), repid
number(4),creditlimit number(8,2));
Example:-
SQL> create table ITT_item (item number(6), ordid
number(6),prodid number(6), actualprice number(8,2), qty
number(8,2), constraint prim_id
Primary key(itemid));
This constraint ensures that they don’t contain Null
values.
Example:-
SQL> create table IT_order (order number94), orderdate
date, commplan character(1), shipdate date, total
number(8,20, empid number(6) constraint fk_empid
references IT_customer(empid));
The above command creates a table IT_order and will
also enable a foreign key on empid column, which
would refer to the primary key on the empid foreign
key on
empid column, which would refer to the empid column of
the IT_customer table. Before enabling this constraint, we
ensure that empid column of the customer table has been
defined with either unique or primary key constraint. The
foreign key constraint ensures that all values in the
column empid in IT_order table have a corresponding
empid value in the IT_customer table.
Example:-
SQL>create table IT_order (order number(4), orderdate
date, commplan character(1), ship_date date, tatal
number(8,2), empid number(6),
Constraint fk_empid references IT_customer(empid) on
delete cascade);
Q5. Explain the join concept and explain the different types of
joins.
Ans:- The purpose of a join is to combine the data spread
across tables. A join is actually performed by the ‘where’
clause which combines the specified rows of tables. The
syntax for joining tables is as follows:
Equi-join:-
Non equi-join:-
Example:-
SQL>select * from IT_customer,order_info where
IT_customer.custid>orde_info.custid and
IT_customer.repid=10;
The above example joins the rows of customer table to the
of order_info table provided custid’s belonging to customer
table are greater than custid’s belonging to order_info
table. Yet, it retrieves rows only when the second
expression.
i.e.,repid=10 is true.
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 and use it instead of the
table name throughout the query. To prevent ambiguity in
a query we include table names in the select statements.
The following example is illustrative of the same.
E.g.,
SQL> select b.* ,o.*from IT_customer b.order_info o where
b.custid>o.custid and repid=10;
Where ‘o’ refers to order info table and ‘c’ refers to
customer table. The aliases, which are defined in, the
“from clauses is separated from the table name by spaces.
B.*,o.* will retrieve all columns from both tables provided
the conditions in the where clauses is satisfied.”
Self join:-
Joining of a table itself is known as a self join,i.e., it joins
one row in a table to another. It can compare each row of
the table to itself and also with other rows of the same
table.
Ex:-
SQL>select o.* from order_info 0,order_info u where
o.ship_date>=u.ord_date and o.custid=u.custid;
The above example will display only those rows from the
order_info table whose ship_date is greater than or equal
to the order_date provided they both have the ‘Ssame
custid’.
Outer join:-
The outer join extends the result of a simple join. An outer
join returns the rows returned by simple join as well as
those rows from one tables that do not match any row
from the other table. The symbol,(+), represents outer join.
Ex:-
SQL> select order_info.ordid,prodid,item_ordid form item,
order+info where order_info.ordid(+)=item Ordid;
The above example will retrieve rows from item table
which does not have any matching records in the
order_info table because of the presence of an outer join.
Function in view:-
Single row functions comprising number, chraracter, date,
group functions and express can also be used in views.
Consider the following example which illustrates the usage
expressions in a view.
E.x:
SQL>create view discount (quantity, ordid, price) as select
qty, ordid, actualprice/150 from item;
The above example finds the discount offered for a
product.
Partition View:-
The concept behind the partition views is entirely different
from that of the partition indexes. With partition views,
the data resides in separate tables. These tables are then
brought together at runtime using the relation operator
Union all. The syntax for a partition view given as
Create view<viewname> as
Select * from <tablename>
Union all
Select *from <tablename2>
Union all
Select * from <tablename3>;
Object View:-
Objects views allow the use relational data in object-
oriented applications. Hence we can define object views as
extensions of traditional relational views that allow us to
threat the relational date as object entities. Hence we can
select, insert, update and delete the relational data as if
they object entities.
Example:-
SQL> create table employees (eno integer primary key,
ename varchar2(25), add varchar2(25), street
varchar2(50), city varchar2(25), state varchar2(25), pin
varchar2(8), country varchar2(25));
To manipulate the above-created table, insert and update
statements are issued as follows:
Example:-
SQL> Insert into employees Values
(10,’shailza’,’sss’ ,’74NSK
salai’,Madras’,’Tamilnadu’,’600026’,’India’);
Example:-
SQL> select * from emp where deptno=(select deptno from
customer where sal=7500);
Example:-
Multiple Subqueries:-
Oracle 8i places no limit on the number of queries include
in a where clause. Consider the following example, which
illustrates multiple queries.
Example:
SQL> select * from emp where sal=any(select sal from emp
where deptno<20) and deptno=any(select deptno from emp
where ename like ‘m%’);
Ex:-
SQL>select * from emp where empno=(selct empno from
empwhere deptno=(select deptno from emp where
.sal<5000));
User1:-
Updates row 1 of product;
Commit;
User 2:-
Deletes row 1 of product;
Commit;
Since both the users are accessing the same row on the
same table at the same time it may end up in a situation
where user2 will delete the row that user 1 has updated.
To overcome this situation, Oracle 8i incorporates a
locking facility, which permits or denies access to other
users on a table or to certain rows in a table, when a user
is operating on them.
Types of locks:-
Locks are the mechanisms used to prevent destructive
between users accessing the same resource at the same
time. A resource can either be an entire table or a specific
row in a table. Thus provide a high degree of data
concurrency. Locks can be acquired at the different levels.
Example:
SQL> update order_info set commplan=’a’ where
ordid=200;
Share lock
A share lock locks the table allowing other user to
only query but not insert, update or delete rows in a
table. Multiple users can place share locks on the
same table at the same time, i.e., it allows resources
to be shared, and hence the name share lock. The
following example locks table item in share mode.
Example:
SQL>lock table customer in share update mode;
NOTE:-
Example:-
SQL>lock table price in exclusive mode;
Note:-
Locks can be released by issuing either rollback
or commit.
No wait:-
If a user has locked a table without a ‘No wait’ clause in
the lock table format and if another user tries to violate
the above restrictions by trying to lock the table, then, he
will be made to wait indefinitely until the former issues a
commit or rollback statement. This delay could be avoided
by appending a ‘no wait’ clause in the lock table
command. The following example is illustrative of this
feature.
Example:-
SQL> lock table customer in exclusive mode nowait;
Deadlock:-
A deadlock occurs when two users have a lock, each on a
separate object, and, each wants to acquire a lock on the
other user’s object. When this happens, the first user has
to wait for the second user to release the lock, but the
second user will not release until the lock on the first
user’s object is freed. At this point, both the users are at
an impasse and cannot proceed with their business. In
such a case, Oracle 8i detects the deadlock automatically
and solves the problem by aborting one of the two
transactions.
The following tabular column clearly explains the mode of
functioning of the different discussed above.
Ans:-
(a)Column commands:- SQL * PLUS uses column names
as default column headings while displaying query
results. We can display our own column heading with the
help of the following command
Column<col_name>heading<col_heading>;
The following example changes the column heading ‘qty’ to
‘quantity’.
Example:
SQL>column qty heading quantity,
SQL>select qty from it;
Note:
The new heading is effective until we exit from the current
SQL * Plus session.
Example:-
SQL>column total format $999,99;
SQL>Select total from ord.r_info;
Title commands:-
Title commands compressing ‘ttitle’ and ‘btitle’ are used to
place top title and bottom title on each page. The
command is given below:
Ttitle position_clause char_value position_clause;
Btitle position_clause char_value position_clause;
Example:-
SQL> ttitle center ‘introduction’;
SQL>btitle center ‘conclusion’;
SQL> select prodid from item;
INTRODUCTION
=============
Arithmetic operators:-
To perform calculations based the number values, we
include arithmetic expressions in SQL command. An
arithmetic expression consists of column names with
number data types and an arithmetic operator connecting
them. The arithmetic operators are
• + (Addition)
• - (Subtraction)
• * (Multiplication)
• / (Division)
Example:
SQL> select ordid, itemid, 100 * (actualprice + qty) from
IT_item Where prodid= 120;
Example:
SQL>select * from EMP where sal>5000;
The above example displays the rows for which the sal is
greater than 5000.An example discussed below is used to
check for negations, (i.e. check for NOT conditions).
Logical operators
A logical operator is used to combine the results of two
conditions to produce a single result.
The logical operators are
AND
NOT
OR
And operator:-
Example:-
SQL> select * from emp where sal.4000 AND deptno<2;
And operator will display all the columns from EMP table
provided both the conditions mentioned below are
satisfied.
Predefined exceptions
The exception is raised implicitly when a PL/SQL program
violates Oracle 8i rule. The following are the predefined
exceptions supported by PL/SQL.
Begin
Sequence_of_statements;
Exception
end;
Example:-
Declare
Price item.actualprice%type;
Begin
exception
end;
User-defined exception
A user-defined exception should be declared and raised
explicitly by a ‘raise’ statement. It can be declared only in
the declarative part of the PS/SQL block. The syntax is
<exception_name> exception;
raise<exception name>
Example:-
Declare
Zero_price exception;
Price number(8,2);
Begin
Select actualprice into price item where ordid=400;
If price=0 or price is null then
Raise zero-price;
End if;
Exception
when zero-price then
Dbms_output.put_line(‘raised zero-price exception’);
End;
Explicit Cursor
The set of rows returned by a query can contain zero or
multiple rows depending upon the query defined. These
rows are called active set. The cursor will point to the
current row in the active set.
After declaring a cursor, we can use the following
commands to controls the cursor.
• Open
• Fetch
• Close
Open< cursor_name>;
After processing the last row in the active set, the cursor is
disabled with the help of the ‘close’ command. The syntax
is as follows:
close<cursor_name>;
Notfound:-
After opening a cursor, a ‘fetch’ statement is used to fetch
rows from the active set, one at a time.
The attribute %notfound indicates whether fetch
statement returns row from the Active set. If the last
‘fetch’ fails to return a row, then %notfound evaluates to
true, else, it evaluates to false.
%found:-
The %found attributes is the logical opposite of
%notfound. It evaluates to true if the ‘fetch’ statement
succeeds. It would be evaluated to false if the last ‘fetch’
command fails because no more rows were available.
%Rowcount:-
The %rowcount attribute is returning the number of rows
fetched. Before the first fetch%rowcount is zero. When the
‘fetch’ statement returns a row, then the number is
incremented.
%Isopen:-
If the cursor is already open, then the attribute %isopen
evaluates to true, else it evaluates to false.
Implicit cursor
PL/SQL implicit declares cursors for all SQL data
manipulation statements, including queries that return
one row.
Implicit cursor attributes can be used to access
information about the most recently executed SQL
statement. The most recently executed SQL statement is
referred as ‘SQLCURSOR’.
%Notfound:-
%notfound attribute evaluates to true if DML statements
do not return any row, else it evaluates to false.
%Found:-
The %found attribute is the logical opposite of the
%notfound. The %found attribute is evaluated to true if
the SQL DML statement affects one or more rows, else it is
evaluated to false.
%Rowcount:-
The %rowcount attributes count the number of rows
returned by an SQL DML statement. Be %rowcount will
return zero if the DML statement does not affect any row.
%Isopen:-
Oracle 8i closes the cursor automatically after executing
its associated SQL statement. As a result, %isopen is
always evaluated to false.
Parts of a trigger:-
Trigger statement :-
Trigger body:-
It is a PL/SQL block that is executed when a triggering
statement is issued.
Trigger Restriction:-
Note:-
A sub query can’t be included in the WHEN clause.
Types of Triggers:-
• Before
• After
• For each row
• For each statement (default)
Before/after Options
The before/after options can be used to specify when the
body should be fired with respect to the triggering
statement. If the user includes a before option, then,
Oracle 8i fires the trigger before executing the triggering
statement. On the other hand, If AFTER is used, then,
Oracle 8i fires the trigger after executing the triggering
statement.
Note:-
Example:-
Example:-
SQL> alter trigger trig disable;
Tree structures:-
The notion of tree-structured or multi-level indices is
usually used for large indices. The reason for providing
an index in the first place is to remove the need for
physical sequential scanning of the indexed file.
However, physical sequential scanning is still needed in
the index. If the indexed file is very large, then the
index also can get quite sizeable and therefore scanning
the index may itself get quite time consuming.
Disadvantages of hashing:-
Front compression:-
Front compression is a differential compression technique
for compressing data by which characters at the front
each entry is the same as those in the previous entry are
by a corresponding count.
Rear compression:-
The rear compression technique is a which by data is
compressed, first by eliminating all trailing spaces, and
then by dropping all characters to the right of the one
required t distinguish the entry in question from it’s two
immediate neighbours. Second indicating the number of
characters recorded.