ORDBMS Flashcards
Set type
setof(varchar(20));
arrays
varchar(20) array[10]
Large object type
clob - character large object
blob - binary large object
creating types
create type [name] as (
[attributes]
)
the type ‘name’ ca now be the attribute for another type or table
Create table over type objects
Create table [table name] of [obj name];
define composite attribute in line
create type Book as ( ... row publisher ( name varchar(20), year int ), .... );
add methods to table/type
create type Book as ( ... method read(pages integer) )
create method body separately
insert complex values into relation
insert into books
values
(‘Compilers’ array[‘smith’, ‘jones’], Publisher(‘McGraw Hill’, ‘New york’), set(‘a’, ‘b’))
inheritance of types
create type [name] under [super class type]
(
[only need to define new attributes here]
)
multiple inheritance
create type TA under
under
Student with (attribute as a) Teacher with (attribute as b)
there is a collision of a similarly named attribute so we have to rename them to avoid the collision
Reference declaration
an attribute of our table can be a reference (or essentially a pointer) to an object in another table
- this prevents having to create duplicate objects
- it also prevents the user from create long cumbersome insert statements if the object is large and has say 100s of attributes
Reference declaration syntax
create type Department ( name varchar(20), head ref(Person) scope people )
head = name of attribute
ref(Person) = head is a reference to a Person type
scope people = the Person to which head will reference is from the people table
insert into table with reference attribute
insert into departments values (‘CS’, null)
update departments
set head = (select ref(p) from people as p where name = ‘John)
where name = ‘CS’;
self-referential attribute
to specify how objects are to be referenced
ex)
create table people of Person ref is oid system generated
oid = object identifier
User created object identifiers
create type Person as (
…
ref using varchar(20)
)
create table people of Person
ref is oid user generated
insert into departments values
(‘CS’, ‘023956203’)
Derived object identifier
we can also have object identifiers use an existing PK as the identifier.
ex) create type Person(name varchar(20) primary key,address varchar(20))ref from(name)create table people of Person ref is oid derived
nsert into departments values(CS’,
John’)
path expressions
select head –> name, head –> address
head - a reference to a person.
name, address - The attribute on the other side of the arrow is the attribute from the referenced person that we want to retrieve.
Why use path expressions
We can get attributes from a reference object without needing to use a join.
easier to express queries
Querying structured types
use dot operator to get attribute of a composite type
select title, publisher.name
From …
Querying data with collection attributes - Sets - in the where clause
select title
from books
where ‘database’ in( unnest([set name]))
the word ‘unnest’ puts the set in a form dbms can search through
Querying data from arrays
select author-array[1], author-array[2]
from books
where title = ‘blah’
simply access elements by use of indices
Nesting
can retrieve an unnested relation as a nested relation (one with multi valued attributes)
select title, set(author)
from flat-books
use the word ‘set’
SQL 1999 functions
create function [function name]({arguments}) returns [return type] begin [declarations go here] [rest of code goes here] end
SQL procedures
create procedure [name] ({[in/out] argument})
begin
[code here]
end