nSets are an instance of collection types. Other instances include
ØArrays (are supported in SQL:1999)
êE.g. author-arrayvarchar(20) array[10]
êCan access elements of array in usual fashion:
–E.g. author-array[1]
ØMultisets (not supported in SQL:1999)
êI.e., unordered collections, where an element may occur multiple times
ØNested relations are sets of tuples
êSQL:1999 supports arrays of tuples
Large Object Types
nLarge object types
Øclob: Character large objects
book-reviewclob(10KB)
Øblob: binary large objects
imageblob(10MB)
movie blob (2GB)
nJDBC/ODBC provide special methods to access large objects in small pieces
ØSimilar to accessing operating system files
ØApplication retrieves a locator for the large object and then manipulates the large object from the host language
Structured and Collection Types
nStructured types can be declared and used in SQL
create type Publisher as (namevarchar(20), branch varchar(20)) create type Book as (title varchar(20), author-array varchar(20) array [10], pub-date date, publisher Publisher, keyword-set setof(varchar(20)))
ØNote: setof declaration of keyword-set is not supported by SQL:1999
ØUsing an array to store authors lets us record the order of the authors
nStructured types can be used to create tables
create table books ofBook
ØSimilar to the nested relation books, but with array of authors instead of set
nStructured types allow composite attributes of E-R diagrams to be represented directly.
nUnnamed row types can also be used in SQL:1999 to define composite attributes
ØE.g. we can omit the declaration of type Publisher and instead use the following in declaring the type Book
nUsing inheritance to define the student and teacher types create typeStudent under Person (degree varchar(20), department varchar(20)) create type Teacher under Person (salary integer, department varchar(20))
nSubtypes can redefine methods by using overriding method in place of method in the method declaration
Multiple Inheritance
nSQL:1999 does not support multiple inheritance
nIf our type system supports multiple inheritance, we can define a type for teaching assistant as follows: create type Teaching Assistant under Student, Teacher
nTo avoid a conflict between the two occurrences of department we can rename them
create type Teaching Assistant under Student with (department as student-dept), Teacher with (department as teacher-dept)
Table Inheritance
nTable inheritance allows an object to have multiple types by allowing an entity to exist in more than one table at once.
nE.g. people table: create table people of Person
nWe can then define the students and teachers tables as subtables of people
create table students of Student under people create table teachers of Teacher under people
nEach tuple in a subtable (e.g. students and teachers) is implicitly present in its supertables (e.g. people)
nMultiple inheritance is possible with tables, just as it is possible with types. create table teaching-assistants of Teaching Assistant under students, teachers
ØMultiple inheritance not supported in SQL:1999
Table Inheritance: Roles
nTable inheritance is useful for modeling roles
npermits a value to have multiple types, without having a most-specifictype (unlike type inheritance).
Øe.g., an object can be in the students and teachers subtables simultaneously, without having to be in a subtable student-teachers that is under both students and teachers
Øobject can gain/lose roles: corresponds to inserting/deleting object from a subtable
nNOTE: SQL:1999 requires values to have a most specific type
Øso above discussion is not applicable to SQL:1999
Table Inheritance: Consistency Requirements
nConsistency requirements on subtables and supertables.
ØEach tuple of the supertable (e.g. people) can correspond to at most one tuple in each of the subtables (e.g. students and teachers)
ØAdditional constraint in SQL:1999:
All tuples corresponding to each other (that is, with the same values for inherited attributes) must be derived from one tuple (inserted into one table).
êThat is, each entity must have a most specific type
êWe cannot have a tuple in people corresponding to a tuple each in students and teachers
Table Inheritance: Storage Alternatives
nStorage alternatives
HStore only local attributes and the primary key of the supertable in subtable
êInherited attributes derived by means of a join with the supertable
HEach table stores all inherited and locally defined attributes
êSupertables implicitly contain (inherited attributes of) all tuples in their subtables
êAccess to all attributes of a tuple is faster: no join required
êIf entities must have most specific type, tuple is stored only in one table, where it was created
HOtherwise, there could be redundancy
Reference Types
nObject-oriented languages provide the ability to create and refer to objects.
nIn SQL:1999
ØReferences are to tuples, and
ØReferences must be scoped,
êI.e., can only point to tuples in one specified table
nWe will study how to define references first, and later see how to use references
Reference Declaration in SQL:1999
nE.g. define a type Department with a field name and a field head which is a reference to the type Person, with table people as scope
create type Department( name varchar(20), head ref(Person) scope people)
nWe can then create a table departments as follows
create table departments of Department
nWe can omit the declaration scope people from the type declaration and instead make an addition to the create table statement: create table departments of Department (head withoptions scopepeople)
Initializing Reference Typed Values
nIn Oracle, to create a tuple with a reference value, we can first create the tuple with a null reference and then set the reference separately by using the function ref(p) applied to a tuple variable
nE.g. to create a department with name CS and head being the person named John, we use
insert into departments
values (`CS’, null)
update departments
sethead = (select ref(p)
from people as p
where name=`John’)
where name = `CS’
nSQL:1999 does not support the ref() function, and instead requires a special attribute to be declared to store the object identifier
nThe self-referential attribute is declared by adding a ref is clause to the create table statement:
create table people of Person ref is oid system generated
ØHere, oid is an attribute name, not a keyword.
nTo get the reference to a tuple, the subquery shown earlier would use
select p.oid
instead of select ref(p)
User Generated Identifiers
nSQL:1999 allows object identifiers to be user-generated
ØThe type of the object-identifier must be specified as part of the type definition of the referenced table, and
ØThe table definition must specify that the reference is user generated
ØE.g.
create type Person (name varchar(20) address varchar(20)) ref using varchar(20) create table people of Person ref is oid user generated
nWhen creating a tuple, we must provide a unique value for the identifier (assumed to be the first attribute):
insert into people values (‘01284567’, ‘John’, `23 Coyote Run’)
nWe can then use the identifier value when inserting a tuple intodepartments
ØAvoids need for a separate query to retrieve the identifier:
E.g. insert into departments values(`CS’, `02184567’)
nIt is even possible to use an existing primary key value as the identifier, by including the ref from clause, and declaring the reference to be derived
create type Person (name varchar(20) primary key, address varchar(20)) ref from(name) create table people of Person ref is oid derived
nWhen inserting a tuple for departments, we can then use
insert into departments values(`CS’,`John’)
Path Expressions
nFind the names and addresses of the heads of all departments:
select head –>name, head –>address from departments
nAn expression such as “head–>name” is called a path expression
nPath expressions help avoid explicit joins
ØIf department head were not a reference, a join of departments with people would be required to get at the address
ØMakes expressing the query much easier for the user
Querying with Structured Types
nFind the title and the name of the publisher of each book.
select title, publisher.name from books
Note the use of the dot notation to access fields of the composite attribute (structured type) publisher
Collection-Value Attributes
nCollection-valued attributes can be treated much like relations, using the keyword unnest
ØThe books relation has array-valued attribute author-array and set-valued attribute keyword-set
nTo find all books that have the word “database” as one of their keywords, select title from books where ‘database’ in (unnest(keyword-set))
ØNote: Above syntax is valid in SQL:1999, but the only collection type supported by SQL:1999 is the array type
nTo get a relation containing pairs of the form “title, author-name” for each book and each author of the book
select B.title, A from books as B, unnest (B.author-array) as A
nWe can access individual elements of an array by using indices
ØE.g. If we know that a particular book has three authors, we could write:
select author-array[1], author-array[2], author-array[3] from books where title = `Database System Concepts’
Unnesting
nThe transformation of a nested relation into a form with fewer (or no) relation-valued attributes us called unnesting.
nE.g.
select title, A as author, publisher.name as pub_name, publisher.branch as pub_branch, K as keyword
from books as B, unnest(B.author-array) as A, unnest (B.keyword-list) as K
Nesting
nNesting is the opposite of unnesting, creating a collection-valued attribute
nNOTE: SQL:1999 does not support nesting
nNesting can be done in a manner similar to aggregation, but using the function set() in place of an aggregation operation, to create a set
nTo nest the flat-books relation on the attribute keyword:
select title, author, Publisher(pub_name, pub_branch) as publisher, set(keyword) as keyword-list from flat-books groupby title, author, publisher
nTo nest on both authors and keywords:
select title, set(author) as author-list, Publisher(pub_name, pub_branch) as publisher, set(keyword) as keyword-list from flat-books groupby title, publisher
nAnother approach to creating nested relations is to use subqueries in the select clause.
select title, ( select author from flat-books as M where M.title=O.title) as author-set, Publisher(pub-name, pub-branch) as publisher, (select keyword from flat-books as N where N.title = O.title) as keyword-set from flat-books as O
nCan use orderby clause in nested query to get an ordered collection
ØCan thus create arrays, unlike earlier approach
Functions and Procedures
nSQL:1999 supports functions and procedures
ØFunctions/procedures can be written in SQL itself, or in an external programming language
ØFunctions are particularly useful with specialized data types such as images and geometric objects
êE.g. functions to check if polygons overlap, or to compare images for similarity
ØSome databases support table-valued functions, which can return a relation as a result
nSQL:1999 also supports a rich set of imperative constructs, including
ØLoops, if-then-else, assignment
nMany databases have proprietary procedural extensions to SQL that differ from SQL:1999
SQL Functions
nDefine a function that, given a book title, returns the count of the number of authors (on the 4NF schema with relations books4 and authors).
create function author-count(name varchar(20)) returns integer begin declare a-count integer; select count(author) into a-count from authors where authors.title=name return a=count; end
nFind the titles of all books that have more than one author.
select name from books4 where author-count(title)> 1
SQL Methods
nMethods can be viewed as functions associated with structured types
ØThey have an implicit first parameter called self which is set to the structured-type value on which the method is invoked
ØThe method code can refer to attributes of the structured-type value using the self variable
êE.g. self.a
nThe author-count function could instead be written as procedure:
create procedure author-count-proc (in title varchar(20), out a-count integer) begin select count(author) into a-count from authors where authors.title = title end
nProcedures can be invoked either from an SQL procedure or from embedded SQL, using the call statement.
ØE.g. from an SQL procedure
declare a-count integer; call author-count-proc(`Database systems Concepts’, a-count);
nSQL:1999 allows more than one function/procedure of the same name (called name overloading), as long as the number of arguments differ, or at least the types of the arguments differ
External Language Functions/Procedures
nSQL:1999 permits the use of functions and procedures written in other languages such as C or C++
nDeclaring external language procedures and functions
create procedure author-count-proc(intitle varchar(20), out count integer) language C external name’ /usr/avi/bin/author-count-proc’
create function author-count(title varchar(20)) returns integer language C external name ‘/usr/avi/bin/author-count’
nBenefits of external language functions/procedures:
Ømore efficient for many operations, and more expressive power
nDrawbacks
ØCode to implement function may need to be loaded into database system and executed in the database system’s address space
êrisk of accidental corruption of database structures
êsecurity risk, allowing users access to unauthorized data
ØThere are alternatives, which give good security at the cost of potentially worse performance
ØDirect execution in the database system’s space is used when efficiency is more important than security
Security with External Language Routines
nTo deal with security problems
ØUse sandbox techniques
ê that is use a safe language like Java, which cannot be used to access/damage other parts of the database code
ØOr, run external language functions/procedures in a separate process, with no access to the database process’ memory
êParameters and results communicated via inter-process communication
nBoth have performance overheads
nMany database systems support both above approaches as well as direct executing in database system address space
Procedural Constructs
nSQL:1999 supports a rich variety of procedural constructs
nCompound statement
Øis of the form begin … end,
Ømay contain multiple SQL statements between begin and end.
ØLocal variables can be declared within a compound statements
nWhileand repeat statements
declare n integer default 0;
while n < 10 do
set n = n+1
end while
repeat
set n = n – 1
until n = 0
end repeat
nFor loop
ØPermits iteration over all results of a query
ØE.g. find total of all balances at the Perryridge branch declare n integer default 0; for r as select balance from account where branch-name = ‘Perryridge’ do set n = n + r.balance end for
nConditional statements (if-then-else) E.g. To find sum of balances for each of three categories of accounts (with balance <1000, >=1000 and <5000, >= 5000)
ifr.balance < 1000 then set l = l + r.balance elseif r.balance < 5000 then set m = m + r.balance else set h = h + r.balance end if
nSQL:1999 also supports a case statement similar to C case statement
nSignaling of exception conditions, and declaring handlers for exceptions
declare out_of_stock condition declare exit handler for out_of_stock begin … .. signal out-of-stock end
ØThe handler here is exit -- causes enclosing begin..end to be exited
ØOther actions possible on exception
Comparison of O-O and O-R Databases
nSummary of strengths of various database systems:
nRelational systems
Øsimple data types, powerful query languages, high protection.
nPersistent-programming-language-based OODBs
Øcomplex data types, integration with programming language, high performance.
nObject-relational systems
Øcomplex data types, powerful query languages, high protection.
nNote: Many real systems blur these boundaries
ØE.g. persistent programming language built as a wrapper on a relational database offers first two benefits, but may have poor performance.
Finding all employees of a manager
nProcedure to find all employees who work directly or indirectly for mgr
nRelation manager(empname, mgrname)specifies who directly works for whom
nResult is stored in empl(name)
create procedure findEmp(inmgr char(10)) begin create temporary table newemp(name char(10)); create temporary table temp(name char(10)); insert into newemp -- store all direct employees of mgr in newemp select empname from manager where mgrname = mgr
repeat insert into empl -- add all new employees found to empl select name from newemp;
insert into temp -- find all employees of people already found (select manager.empname from newemp, manager where newemp.empname = manager.mgrname; ) except ( -- but remove those who were found earlier select empname from empl );
delete from newemp; -- replace contents of newemp by contents of temp insert into newemp select * from temp; delete from temp;
until not exists(select* from newemp) -- stop when no new employees are found end repeat; end
No comments:
Post a Comment