Lab (1)
Lab (1)
• You need only create a database once, regardless of how many data files it
has or how many instances access it.
Creation Prerequisites
To create a new database, you must have the following:
• sufficient disk storage space for the planned database on the computer that executes
Oracle
Object Types
• Listener port: Allows Oracle client connections to the database via the Oracle's
SQL*Net protocol. You can configure it during installation. Port 1521 is the default client
connections port, however, you can configure another TCP port via the Oracle
configuration and administration tools.
• To create table in oracle database, you use the create table statement.
……
Table_constraint
);
Oracle create table example
• The following example shows how to create a new table named persons in
csStudent schema
PRIMARY KEY(person_id)
);
create user csStudent2 IDENTIFIED by "oracle";
grant all privileges to csStudent identified by "oracle";
Oracle
Insert statement:
insert into CSSTUDENT.persons values(1, 'abebe', 'kebede’);
Select statement
select * from csstudent.persons;
Update statement
update csstudent.persons
set first_name = 'Belay'
where last_name = 'kebede’;
Drop table statement
drop table csStudent.persons;
Oracle alter table example
• The alter table statement is used to add, delete, or modify columns in existing table.
• the alter table statement is also used to add and drop various constraints on an existing table.
• Syntax:
ALTER TABLE table_name
ADD column_name datatype;
• Example:
Alter table csStudent.persons
Add email varchar(255);
Oracle alter table example
• Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;
• Syntax:
ALTER TABLE table_name
MODIFY COLUMN column_name data_type;
Introduction to Oracle Objects
• Oracle object types are user-defined types that make it possible to model real-world
entities, such as customers and purchase orders, as objects in the database.
• New object types can be created from any built-in database types and any previously
created object types, object references, and collection types.
• Object types can work with complex data, such as images, audio, and video.
• Oracle Database stores metadata for user-defined types in a schema that is available to
SQL, PL/SQL, Java, and other languages.
• Like classes, the reusability of objects makes it possible to develop database applications
faster and more efficient.
• Objects offer other advantages over a purely relational approach, such as:
• Object types
• Object instances
• Object methods
• Object identifiers
• Object views
• Type inheritance
Object Types
• An object type is a kind of data type.
• You can use it in the same ways that you use standard data types such as NUMBER or
VARCHAR2.
Oracle Objects
• Using objects allow you to implement real world objects with specific structure of
data and methods for operating it.
• Attributes are properties of an object and are used for storing an object's state; and
methods are used for modelling its behaviour.
• Objects are created using the CREATE [OR REPLACE] TYPE statement.
Oracle create object example
(house_no varchar2(10),
street varchar2(30),
city varchar2(20),
state varchar2(10),
pincode varchar2(10)
);
Oracle Methods
• Object methods, also known as subprograms, are functions or procedures that you can
declare in an object type definition to implement behaviour that you want objects of that
type to perform.
• Static Methods
• Constructor Methods
• You define a member method in the object type for each operation that you want an
object of that type to be able to perform.
(code number(5),
name varchar2(30),
contact_no varchar2(12),
addr address,
);
Instantiating an Object
• You can access the attributes and methods of the object using the instance name and the
access operator (.) .
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;
Member Methods
• Member methods are used for manipulating the attributes of the object.
• You provide the declaration of a member method while declaring the object type.
• The object body defines the code for the member methods. The object body is created
using the CREATE TYPE BODY statement.
Map method
The Map method is a function implemented in such a way that its value depends upon the
value of the attributes.
Order method
The Order method implements some internal logic for comparing two objects.
https://docs.oracle.com/en/database/oracle/oracle-database/19/adobj/object-
methods.html#GUID-62ACE97A-5DD9-402A-B8B0-999AC488CAA2
Using Map method
length number,
width number,
);
The syntax to create a function in Oracle is:
CREATE [OR REPLACE] FUNCTION function_name
[ (parameter [,parameter]) ]
RETURN return_datatype
IS | AS
[declaration_section]
BEGIN
executable_section
[EXCEPTION
exception_section]
END [function_name];
Drop Function
Once you have created your function in Oracle, you might find that you need to remove
it from the database.
DROP FUNCTION function_name;
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
Creating the type BEGIN
BEGIN
return (sqrt(length*length + width*width));
END measure;
END;
DECLARE
r1 rectangle;
r2 rectangle;
r3 rectangle;
inc_factor number := 5;
Now using the
BEGIN
rectangle object r1 := rectangle(3, 4);
END;
Using Order method
body 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
Now using the
r1 := rectangle(23, 44);
rectangle object r2 := rectangle(15, 17);
and its member r1.display;
functions r2.display;
IF (r1 > r2) THEN -- calling measure function
r1.display;
ELSE
r2.display;
END IF;
END;
Here are some other differences between map and order methods
Automatic invocation
• Both map and order methods are called automatically when two objects of the same type need to
be compared.
Number of methods
• You can only have one map or order method in an object type definition.
Object type
• Only a type that is not derived from another type can declare an order method.
Return values
• Map methods return values that are Oracle built-in data types, or ANSI SQL types.
• Order methods return a negative number, zero, or a positive number.
You can implement either a map or order method in the CREATE TYPE and CREATE TYPE BODY
statements.
Inheritance for PL/SQL Objects
• To implement inheritance, the base objects should be declared as NOT FINAL. The
default is FINAL.
• Let us create another object named TableTop, this is inherited from the Rectangle
object.
(length number,
width number,
END enlarge;
MEMBER PROCEDURE display IS
BEGIN
material varchar2(20),
)
Creating the type body for the child object tabletop
BEGIN
END;
DECLARE
t1 tabletop;
Using the
t2 tabletop;
tabletop object
and its member BEGIN
t1.display;
t2.display;
END;
Abstract Objects in PL/SQL
• The NOT INSTANTIABLE clause allows you to declare an abstract object. You cannot
use an abstract object as it is;
• you will have to create a subtype or child type of such objects to use its
functionalities.
create table csstudent.graphics_table (
bfile_id number,
bfile_desc varchar2(30),
bfile_loc bfile,
bfile_type varchar2(4)
);