Csyr2 Advanced Database Lab

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 45

Cs Yr 2

ADVANCED DATABASE LAB


User defined data types.

ADT are the data types created by the programmer having sub­types.

There are several reasons why ADT's are useful within an object

database

Encapsulation - Because each user-defined data type exist as a


complete entity, including the data definitions, default values, and

value constraints, this entity insures uniformity and consistency.


 Reusability - As a hierarchy of common data structures are
assembled, these can be re-used within many definitions,
saving coding time and insuring uniformity.
 Flexibility - The ability to create real-world data
representations of data allows the database object designer to
model the real world as it exists.
 An ADT of address may contain may attributes such as street_no, street_name,
city, State.
Syntax for creating ADT is
Examples: create or replace type as object (attr1 datatype, attr2 datatype ...)
 create or replace type addr as object(street_no number(3), street_name
varchar2(20),city varchar2(10), state varchar2(10));
 create table emp( Essn number(6),Ename varchar(12),Salary number(7,2),age
integer, Address addr));

 insert into empl_addr


values(&id,'&na',&ag,&sal,addr(&stn,'&stna','&city','&st'));
Varrays

V arrays help in storing repeating attributes of a record in a single row.


V arrays have a defined lower value 0 and maximum could be any valid

number.
Collector such as varying array allows repetition of only those column

values that change potentially storage values.


collectors are used to accurately represent relationship between data type

in the database object


syntax

 create type type_name as varray(5) of datatype


Examples:
 create type item_code as varray(5) of varchar2(20);
 create table order1(order_no number(5), item item_code);
object Table.

 Each row within the object table has OID­an object identifier
value.

 This is system generated identifier which is being assigned at


the time of a new row insertion.

 create type type_name as object(list of attributes)

 create table table_name of type_name


To view OID of each row
select ref(a) from table_name a
Nested tables

 Varying array have a limited no. of entries, whereas nested tables


have no limit on the no. of entries per row.
 A nested table is a table within a table.
 create type type_name as object (attribute1,attribute2,... )
 create type tablename as table of type_name
 create table table_name(attribute data type, attribute1 tablename)
nested table attribute1 store as project
EXAMPLES

 Create type proj as object(proj_code varchar2(20), proj_name


varchar2(20),remarks varchar2(20));
 Create type pront as table of proj;
 create table dept(dept_no number(5),dept_name
varchar2(20),dept_loc varchar2(20),projects pront) nested table
projects as project1;
Define A TYPE

 CREATE OR REPLACE TYPE PersonObj AS OBJECT


( first_name VARCHAR2(50), last_name VARCHAR2(50),
date_of_birth DATE, MEMBER FUNCTION getAge
RETURN NUMBER ); /
 Define A TYPE BODY
 Next we define a TYPE BODY to add functionality to the

getAge member function.


 CREATE OR REPLACE TYPE BODY PersonObj AS
MEMBER FUNCTION getAge RETURN NUMBER AS
BEGIN RETURN Trunc(Months_Between(Sysdate,
date_of_birth)/12); END getAge; END; /
 Defining A Table
 Now the object type is defined we can use it as a
datatype in a table.
 CREATE TABLE people ( id NUMBER(10) NOT
NULL, person PersonObj );
 INSERT INTO people VALUES (1, PersonObj('John','Doe',
TO_DATE('01/01/1999','DD/MM/YYYY')));
COMMIT;
DECLARE
v_person PersonObj;
BEGIN
v_person := PersonObj('Jane','Doe',
TO_DATE('01/01/1999','DD/MM/YYYY'));

INSERT INTO people VALUES (2, v_person);

COMMIT;

END;

/
Data Access

 Once the data is loaded it can be accessed using the


dot notation.
 alias.column.attribute
 alias.column.function()
 SELECT p.id, p.person.first_name,
p.person.getAge() age FROM people p;
Member procedure

Creating a member procedure


create or replace typename as object (attribute datatype


member procedure procedure_name (attribute in datatype) ...);
Defining a member procedure
create or replace type body typename as member procedure
procedure_name(attribute in datatype) is
begin
Definition partitioning
end;
end;
step 1: create or replace type T1 as object(ssn number(5),name

varchar2(20), member procedure change_name(name1 in varchar2));


step 2: Create table tab of T1;
step 3: create or replace type body T1 as
member procedure change_name(name1 in varchar2) is
begin
name:=name1;
end;
end;
Member Function

 Syntax: create or replace objectname as object


(attribute1,attribute2...
member function functionname (parameter list) return datatype);
Create or replace function[schema.] Functionname (argument in
datatype,...) Return datatype {is,as}
variable declarations;
constant declarations;
begin
pl/sql subprogram body;
exception
exception pl/sql block;
end;
EXAMPLE
Create or replace type animal_by as object (breed varchar2(25),name

varchar2(25),birthdate date member function AGE(bithdate in date) return number);


Create or replace AGE(birthdate date)
return number is
begin
return round(sgs_date_bithdate);
end;
end;
Examples

 CREATE OR REPLACE TYPE address AS OBJECT


(house_no varchar2(10),

street varchar2(30),

city varchar2(20),

state varchar2(10),

pincode varchar2(10) );

/
Cont…
CREATE OR REPLACE TYPE customer AS OBJECT
(code number(5),
name varchar2(30),
contact_no varchar2(12),
addr address,
member procedure display );
/
DECLARE
residence address;
BEGIN
residence := address('103A', 'M.G.Road', 'Jaipur',
'Rajasthan','201301');
dbms_output.put_line('House No: '|| residence.house_no);
dbms_output.put_line('Street: '|| residence.street);
dbms_output.put_line('City: '|| residence.city);
dbms_output.put_line('State: '|| residence.state);
dbms_output.put_line('Pincode: '|| residence.pincode);
END;
/
Using Map method

CREATE OR REPLACE TYPE rectangle AS


OBJECT (length number,
width number,
member function enlarge( inc number)
return rectangle,
member procedure display,
map member function measure return number );
/
Creating the type body:
 CREATE OR REPLACE TYPE BODY rectangle AS MEMBER
FUNCTION enlarge(inc number) return rectangle IS
BEGIN return rectangle(self.length + inc, self.width + inc);
END
enlarge;
MEMBER PROCEDURE display
IS BEGIN
dbms_output.put_line('Length: '|| length); dbms_output.put_line('Width: '||
width);
END display;
MAP MEMBER FUNCTION measure return number IS BEGIN
return (sqrt(length*length + width*width));
END measure;
END; /
DECLARE
r1 rectangle;
r2 rectangle;
r3 rectangle;
inc_factor number := 5;
BEGIN
r1 := rectangle(3, 4);
r2 := rectangle(5, 7);
r3 := r1.enlarge(inc_factor);
r3.display;
IF (r1 > r2) THEN -- calling measure function r1.display;
ELSE
r2.display;
END IF;
END; /
Using Order method

CREATE OR REPLACE TYPE rectangle AS


OBJECT (length number,
width number,
member procedure display,
order member function measure(r rectangle)
return number );
/
CREATE OR REPLACE TYPE BODY rectangle AS MEMBER PROCEDURE display
IS
BEGIN
dbms_output.put_line('Length: '|| length);
dbms_output.put_line('Width: '|| width);
END display;
ORDER MEMBER FUNCTION measure(r rectangle) return number IS
BEGIN
IF(sqrt(self.length*self.length + self.width*self.width)> sqrt(r.length*r.length +
r.width*r.width)) then
return(1);
ELSE
return(-1);
END IF;
END measure;
END; /
DECLARE
r1 rectangle;
r2 rectangle;
BEGIN
r1 := rectangle(23, 44);
r2 := rectangle(15, 17);
r1.display;
r2.display;
IF (r1 > r2) THEN -- calling measure function r1.display;
ELSE
r2.display;
END IF;
END; /
ALTER TYPE

 Purpose
 Use the ALTER TYPE statement to add or drop member attributes or
methods. You can change the existing properties
(FINAL or INSTANTIABLE) of an object type, and you can modify the
scalar attributes of the type.
 You can also use this statement to recompile the specification or body of
the type or to change the specification of an object type by adding new
object member subprogram specifications.
 Prerequisites
 The object type must be in your own schema and you must
have CREATE TYPE or CREATE ANY TYPE system privilege, or you
must have ALTER ANYTYPE system privileges.
 Syntax
ELEMENT TYPE datatype
  This clause lets you increase the precision, size, or
length of a scalar datatype of a varray or nested table.
This clause is not valid for collections of object types.
 For a collection of NUMBER, you can increase the
precision or scale.
 For a collection of RAW, you can increase the
maximum size.
 For a collection of VARCHAR2 or NVARCHAR2,
you can increase the maximum length.
Increasing the Length of a Collection Type

 ALTER TYPE phone_list_typ MODIFY


ELEMENT TYPE VARCHAR(64) CASCADE;
ADD ATTRIBUTE
  The name of the new attribute must not conflict
with existing attributes or methods in the type
hierarchy. Oracle Database adds the new attribute
to the end of the locally defined attribute list.
 CREATE TABLE text ( doc_id NUMBER, description textdoc_tab)
NESTED TABLE description STORE AS text_store;
 ALTER TYPE textdoc_typ ADD ATTRIBUTE (author VARCHAR2)
CASCADE;
Partitioning on the tables

Partition improves overall performance but increases overhead on


database to search in each partition. Tables are easier to manage.

Backup and recovery operations may perform better.


Create table table_name(atrribute1,attribute2 ...)

partition by range(attribute)

(partition partitionname values less than(maxvalue),partition partition

values less than(maxvalue));


EXAMPLE

 Create table emp(emp_id varchar2(10),name varchar2(20))


partition by range(emp_id)
(partition part_id1 values less than(5),partition part_id2 values
less than(10));
XML command

 DTD(document type definition) any valid document


conforming to DTD should follow the specified structure:
1. name is given to the root tag and then to the elements and
their nested structure are specified.
2. a * means element can be repeated 0 or more times
3.A + element can be repeated one or more times
4.Any element appearing without any of the preceding
symbols must appear exactly once in the document.
Example
 <!DOCTYPE projects[
<!ELEMENT projects(project+)>
<ELEMENT project(pname,number,dept_no,workers)>
<ELEMENT pname(#PCDATA)>
<ELEMENT number(#PCDATA)>
<ELEMENT dept_no(#PCDATA)>
<ELEMENT workers(worker *)>
<ELEMENT eno(#PCDATA)>
<ELEMENT ename(#PCDATA)>
]>
 http://net-
informations.com/faq/asp/authentication.htm
How to Grant and Revoke privileges in Oracle

 Data Control Language (DCL) Statements


 Data Control Language Statements are used to grant privileges on
tables, views, sequences, synonyms, procedures to other users or
roles.
 The DCL statements are
 GRANT  :Use to grant privileges to other users or roles.
 REVOKE  :Use to take back privileges granted to other users and
roles.
 Privileges are of two types :
 System Privileges
 Object privileges
 System Privileges are normally granted by a DBA to
users.
 Examples of system privileges are CREATE SESSION,
CREATE TABLE, CREATE USER etc.
 Object privileges means privileges on objects such as
tables, views, synonyms, procedure.
 These are granted by owner of the object.
 Object Privileges are

 ALTER :Change the table definition with the ALTER TABLE statement.  

 DELETE :Remove rows from the table with the DELETE statement.

 Note: You must grant the SELECT privilege on the table along with the

DELETE privilege. 

 INDEX :Create an index on the table with the CREATE INDEX

statement.  

 INSERT: Add new rows to the table with the INSERT statement.  
Con…

 REFERENCES: Create a constraint that refers to the table. You

cannot grant this privilege to a role.  

 SELECT: Query the table with the SELECT statement.  

 UPDATE : Change data in the table with the UPDATE statement.

  

 Note: You must grant the SELECT privilege on the table along

with the UPDATE privilege. 


Grant
 Grant is use to grant privileges on tables, view, procedure to
other users or roles
 Examples
 Suppose you own emp table. Now you want to grant select, update,
insert privilege on this table to other user “SAMI”.
 grant select, update, insert on emp to sami;
 Suppose you want to grant all privileges on emp table to sami.
Then
 grant  all on emp to sami;

 Suppose you want to grant select privilege on emp to all other


users of the database. Then
 grant select on emp to public;
 Suppose you want to grant update and insert privilege
on only certain columns not on all the columns then
include the column names in grant statement.
 grant update (ename),insert (empno, ename)  on emp to
sami;
 To grant select statement on emp table to sami and to
make sami be able further pass on this privilege you
have to give WITH GRANT OPTION clause in
GRANT statement.
 grant select on emp to sami with grant option;
REVOKE
 Use to revoke privileges already granted to other users.
 For example to revoke select, update, insert privilege you have
granted to Sami then give the following statement.
 revoke select, update, insert on emp from sami;
 To revoke select statement on emp granted to public give the
following command.
 revoke select on emp from public;
 To revoke update privilege on ename column and insert privilege
on empno and ename columns give the following revoke statement.
 revoke update, insert on emp from sami;
ROLES
 A role is a group of Privileges. A role is very handy
in managing privileges, Particularly in such
situation when number of users should have the
same set of privileges.
 create role clerks
 Then grant privileges to this role.
 grant select,update on emp to clerks;
grant select,delete on dept to clerks;
 Now grant this clerks role to users like this
 grant clerks to sami, scott, ashi, tanya ;
 grant delete on emp to clerks;
 If you want to take back update privilege on emp
table from these users just take it back from clerks
role.
 revoke update on emp from clerks;
 To Drop a role
 Drop role clerks;

You might also like