Tải bản đầy đủ (.pptx) (53 trang)

Quản trị cơ sở dữ liệu Oracle 06 object relational

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (138.24 KB, 53 trang )

Object-Relational Database
Cơ sở dữ liệu hướng đối tượng


Tổng quan





object-relational database (ORD) là một DBMS dựa trên mô hình cơ sở dữ liệu
hướng đối tượng: object, class và inheritance (thừa kế).
Ưu điểm:



Ưu điểm chính là tái sử dụng(reuse) và chia sẻ (sharing).

Nhược điểm:




Tăng dung lượng lưu trữ
Chi phí thiết lập và bảo trì hệ thống cao


Oracle object type





Oracle object type là kiểu dữ liệu do người dùng định nghĩa (class) giúp mô hình
hóa các đối tượng trong thế giới thực như: khách hàng, hóa đơn…
Mô hình object type trong oracle tương tự như class trong C++ hoặc Java.


Oracle Object Type



Gồm 2 phần: attribute và method
Object Type person_type
Attribute

Method

name

get_areacode

phone

Tạo Object type:
CREATE TYPE person_type AS OBJECT (
name VARCHAR2(30),
phone VARCHAR2(20),
MEMBER FUNCTION get_areacode RETURN VARCHAR2 );

Xóa Object type:
DROP TYPE person_type;



Object Instance




Là một thể hiện (instance) của object type
Thường gọi là object

Object Type person_type
Attribute

Method

name

get_areacode

phone

Object instance
Name: Verna
Phone: 650-555-0125

Object instance
Name: John
Phone: 680-555-0135



Oracle Method




Là function hay procedure được khai báo trong object type nhằm thực thi hành vi
của object.
Method type:

– Member method


Là method truy xuất đến dữ liệu của object instance.



Được gọi (invoke) trên object type (không phải là instance)

– Static method

– Constructor method


Member Method



Member method được sử dụng để truy xuất đến giá trị của object instance.

CREATE OR REPLACE TYPE BODY person_type AS

MEMBER FUNCTION get_areacode RETURN VARCHAR2 IS
BEGIN
RETURN SUBSTR(phone, 1, 3);
END get_areacode;
END;
Object instance

SELECT c.contact.get_areacode()
FROM contacts c;

C.CONTACT.GET_AREACODE()

Name: Verna
Phone: 213-555-0125

Object instance

-------------------------------------------------------------213

Name: John

680

Phone: 680-555-0135


Constructor Method





System-Defined Constructor
User-Defined Constructor


Constructor Method
System-Defined Constructor





Mọi object type đều có một constructor method được tạo tiềm ẩn bởi hệ thống.
Trả về một thể hiện (instance) của object type và thiết lập giá trị cho attribute của object instance này.
Tên của constructor method giống như tên của object type.

p = person_type(‘Scott Tiger’, ‘321-123-1234’);

INSERT INTO contacts VALUES (
person_type(‘Scott Tiger’, ‘321-123-1234’), ’10 Feb 2004’));


Constructor Method
User-Defined Constructor



Ta có thể tự định nghĩa hàm constructor để khởi tạo giá trị ban đầu cho object.

Object Type person_type


CONSTRUCTOR FUNCTION person_type ( name VARCHAR2)
RETURN SELF AS RESULT IS

Attribute

Method

SELF.name := name;

name

person_type (name)

SELF. phone := '333-333-333';

phone

BEGIN

SELF. address:= 'New York';

address

RETURN;
END;

INSERT INTO contacts
VALUES (person_type(‘Scott Tiger’),
’10 Feb 2004’));



Ví dụ
Tạo object type có tên là shape gồm 2 thuộc tính name, area và một constructor có đối số truyền vào là name, còn area được gán giá trị mặc định
là 0.
Object Type shape
CREATE TYPE shape AS OBJECT (
name VARCHAR2(30),
area NUMBER,
CONSTRUCTOR FUNCTION shape( name VARCHAR2)
RETURN SELF AS RESULT
)

CREATE TYPE BODY shape AS
CONSTRUCTOR FUNCTION shape( name VARCHAR2)
RETURN SELF AS RESULT IS
BEGIN
SELF.name := name;
SELF.area := 0;
RETURN;
END;
END;

Attribute

Method

name

shape (name)


area


Lưu trữ object trong table



Object có thể được lưu trữ trong 2 loại table:

– Object table: chỉ lưu trữ object


Trong object table, mỗi row biểu diễn một object.



Object được lưu trữ như là column của một relational table.

– Relational table: lưu trữ object cùng với dữ liệu khác của table.


Object table
Object Type person_type
Attribute
name

Object

Object


Name: Verna

Name: John

Phone: 650-555-0125

Phone: 680-555-0135

phone

Tạo object table:

Lưu trữ dữ liệu:

CREATE TABLE person_obj_table OF person_type;

INSERT INTO person_obj_table VALUES (
person_type('Verna', '650-555-0125') );

INSERT INTO person_obj_table VALUES (
person_type('John', '680-555-0135') );

Dữ liệu trong bảng person_obj_table
person_obj_table
person_type('Verna', '650-555-0125')
person_type('John', '680-555-0135')


Object table

Truy vấn dữ liệu:

SELECT * FROM person_obj_table

NAME

PHONE

Verna

650-555-0125

John

680-555-0135

Hiển thị số điện thoại của người tên John

SELECT PHONE
FROM person_obj_table
WHERE name = 'John'
PHONE
680-555-0135


Relational table
Object Type person_type
Attribute
name


Object

Object

Name: Verna

Name: John

Phone: 650-555-0125

Phone: 680-555-0135

phone
createdDate: '20-NOV-2013'

Tạo relational table:
CREATE TABLE contact_table(
contact person_type,
createdDate

DATE);

Lưu trữ dữ liệu vào table:

INSERT INTO contact_table VALUES (
person_type('Verna', '650-555-0125') , '20-NOV-2013');

INSERT INTO contact_table VALUES (
person_type('John', '680-555-0135') , '25-MAR-2013');


createdDate:25-MAR-2013


Relational table
Truy vấn dữ liệu:

contact_table

SELECT * FROM contact_table

contact

createdDate

person_type('Verna', '650-555-0125')

20-NOV-2013

person_type('John', '680-555-0135')

25-MAR-2013

Hiển thị số điện thoại của người tên John
SELECT c.contact.phone
FROM contact_table

c

WHERE c.contact.name = 'John'


CONTACT.PHONE
680-555-0135


Ví dụ


Tạo table shape_table lưu trữ thông tin về tên hình, diện tích và ngày tạo (sử dụng lại object type shape ở các slide trước).

Object Type shape

CREATE TABLE shape_table(
shapeInfo

shape,

Attribute

Method

createdDate

DATE);

name

shape (name)

area
INSERT INTO shape_table VALUES (

shape('circle', 20),

'20-NOV-2013');

INSERT INTO shape_table VALUES (
shape('rectangle'),

'26-NOV-2013');

shapeInfo

createdDate

Gọi đến constructor một đối số do người

shape('circle',

20)

dùng tự định nghĩa

shape('rectangle', 0)

20-NOV-2013
26-NOV-2013


Ví dụ



Thiết kế một object type rectangle có các thuộc tính là chiều dài, chiều rộng, diện tích và một constructor tự động tính diện tích hình chữ nhật.

CREATE TYPE rectangle_type AS OBJECT (
length NUMBER,
width NUMBER,
area NUMBER,
CONSTRUCTOR FUNCTION rectangle_type (length NUMBER, width NUMBER )
RETURN SELF as RESULT
);

CREATE TYPE BODY rectangle_type IS
CONSTRUCTOR FUNCTION rectangle_type (length NUMBER, width NUMBER )
RETURN
BEGIN
SELF.length := length;
SELF.width := width;
SELF.area := length*width;
RETURN ;
END;
END;

SELF AS RESULT IS


Ví dụ



Tạo một object table lưu trữ thông tin hình chữ nhật (sử dụng object type rectangle_type ở
slide trước).


CREATE TABLE rectangle_table OF rectangle_type ;

INSERT INTO rectangle_table VALUES (rectangle_type (20, 30, 700));
INSERT INTO rectangle_table VALUES (rectangle_type (25, 10));
INSERT INTO rectangle_table VALUES (rectangle_type (4, 2));

rectangle_table
rectangle_type (20, 30, 700)
rectangle_type (25, 10, 250)
rectangle_type (4, 2, 8)


Ví dụ
Hiển thị thông tin các hình chữ nhật:

SELECT * FROM rectangle_table

LENGTH

WIDTH

AREA

20

30

600


25

10

250

4

2

8

Hiển thị thông tin chiều dài, chiều rộng và diện tích của những hình chữ nhật có diện tích lớn hơn 30

SELECT r.length, r.width, r.area
FROM rectangle_table r
WHERE r.area>30

LENGTH

WIDTH

AREA

20

30

600


25

10

250


Ví dụ


Thiết kế một object type rectangle có các thuộc tính là chiều dài, chiều rộng, một member function tính diện tích hình chữ nhật, một member
function tính chu vi hình chữ nhật.

CREATE TYPE rectangle_type AS OBJECT (
length NUMBER,
width NUMBER,
MEMBER FUNCTION getArea RETURN

NUMBER,

MEMBER FUNCTION getPerimeter RETURN NUMBER
);

CREATE TYPE BODY rectangle_type IS
MEMBER FUNCTION getArea RETURN NUMBER IS
BEGIN
RETURN length * width;
END;
----------MEMBER FUNCTION getPerimeter RETURN NUMBER IS
BEGIN

RETURN (length + width) * 2;
END;
END;


Ví dụ



Tạo một object table lưu trữ thông tin hình chữ nhật (sử dụng object type ở slide trước).

CREATE TABLE rectangle_table OF rectangle_type ;

INSERT INTO rectangle_table VALUES (rectangle_type (20, 30));
INSERT INTO rectangle_table VALUES (rectangle_type (25, 10));
INSERT INTO rectangle_table VALUES (rectangle_type (4, 2));

rectangle_table
rectangle_type (20, 30)
rectangle_type (25, 10)
rectangle_type (4, 2)


Ví dụ
Hiển thị thông tin các hình chữ nhật:

SELECT * FROM rectangle_table

LENGTH


WIDTH

20

30

25

10

4

2

Hiển thị thông tin chiều dài, chiều rộng,diện tích , chu vi của hình chữ nhật

SELECT r.length, r.width, r.getArea(), r.getPerimeter()
FROM rectangle_table r

LENGTH

WIDTH

R.GETAREA()

R.GETPERIMETER()

20

30


600

100

25

10

250

70

4

2

8

12


Ví dụ



Tạo table lưu trữ thông tin hình chữ nhật (sử dụng rectangle_type ở slide trước) và thông tin
về người dùng đã nhập dữ liệu này (sử dụng person_type ở slide trước).

CREATE TABLE rectangle_table(

rectangle

rectangle_type ,

createdUser

person_type

);

INSERT INTO rectangle_table VALUES (rectangle_type (20, 30),
person_type('Verna', '650-555-0125') );
INSERT INTO rectangle_table VALUES (rectangle_type (25, 10),
person_type('John', '680-555-0135') );
INSERT INTO rectangle_table VALUES (rectangle_type (4, 2),
person_type('Verna', '650-555-0125') );


Ví dụ
Hiển thị thông tin các hình chữ nhật:

SELECT * FROM rectangle_table

RECTANGLE

CREATEDUSER

RECTANGLE_TYPE(20,30)

PERSON_TYPE('Verna','650-555-0125')


RECTANGLE_TYPE(25,10)

PERSON_TYPE('John','680-555-0135')

RECTANGLE_TYPE(4,2)

PERSON_TYPE('Verna','650-555-0125')

Hiển thị thông tin chiều dài, chiều rộng,diện tích và tên người dùng.

SELECT r.rectangle.length , r.rectangle.width, r.rectangle.getArea(), r.createdUser.name
FROM rectangle_table r

RECTANGLE.LENGTH

RECTANGLE.WIDTH

R.RECTANGLE.GETAREA()

CREATEDUSER.NAME

20

30

600

Verna


25

10

250

John

4

2

8

Verna


×