Sunteți pe pagina 1din 23

Creating database objects

Presented By Satrio Agung Wicaksono

Learning Outcome
List object hierarchy Create the following objects:
Schema, Table, View, Alias, Index

Review the use of Temporary Tables Explore the use and implementation of Check Constraints, Referential Integrity and Triggers Explore the need for and the use of Large Objects

Object Hierarchy

Creating objects
Create Schema Create Table Create View Create Alias Create Index Create Trigger

Schema
A schema is a collection of named objects A schema contain : tables, views, nicknames, triggers, functions, packages, and other objects Sintax :
CREATE SCHEMA PAYROLL AUTHORIZATION DB2ADMIN; COMMENT ON Schema PAYROLL IS 'schema for payroll application';

Set current schema


connect to musicdb user Keith; select * from employee; Will select from KEITH.EMPLOYEE set current schema = 'PAYROLL; select * from employee; Will select from PAYROLL.EMPLOYEE

Table
Databases store data in tables Table consists of data logically arranged in columns and rows To create a table :
must be connected to a database, either implicitly or explicitly. must have SYSADM or DBADM authority, or, must have CREATETAB privilege and one of either IMPLICIT_SCHEMA or CREATEIN privilege on the schema. must have USE privilege over all table spaces referenced.

DB2 supports page sizes of 4, 8, 16, and 32 KB


Regular tablespaces use a 4-byte Row ID, which allows 16 million pages with up to 255 rows per page Large table spaces use a 6-byte Row ID, which allows up to 512 million pages to be addressed

Types of tables
Regular tables Multidimensional clustering (MDC) tables Range-clustered tables (RCT) Temporary tables Materialized query tables

Create Table statement

Declared Temporary Table


A System Administrator creates the user temporary table space

The application uses SQL statements to declare and access the table

Large objects: The Need

VIEW
A view is an efficient way of representing data without the need to maintain it. A view is not an actual table and requires no permanent storage A view provides a different way of looking at the data in one or more tables

the relationship between tables and views

Depending on how a view is defined


Deletable views Insertable views Updatable views Read-only views

Deletable views
Each FROM clause of the outer fullselect identifies only one table (with no OUTER clause), deletable view (with no OUTER clause), deletable nested table expression, or deletable common table expression.v The database manager needs to be able to derive the rows to be deleted in the table using the view definition. Certain operations make this impossible
A grouping of multiple rows into one using a GROUP BY clause or column functions result in a loss of the original row and make the view non deletable. Similarly when the rows are derived from a VALUES there is no table to delete from. Again the view is not deletable.

The outer fullselect doesn't use the GROUP BY or HAVING clauses. The outer fullselect doesn't include column functions in its select list. The outer fullselect doesn't use set operations (UNION, EXCEPT, or INTERSECT) with the exception of UNION ALL The tables in the operands of a UNION ALL must not be the same table, and each operand must be deletable. The select list of the outer fullselect does not include DISTINCT

Insertable views
A view is insertable if an INSTEAD OF trigger for the insert operation has been defined for the view, or at least one column of the view is updatable (independent of an INSTEAD OF trigger for update), and the full select of the view does not include UNION ALL. A given row can be inserted into a view (including a UNION ALL) if, and only if, it fulfills the check constraints of exactly one of the underlying tables. To insert into a view that includes non-updatable columns, those columns must be omitted from the column list

Updatable views
The view is deletable. The column resolves to a column of a table (not using a dereference operation) and the READ ONLY option is not specified. All the corresponding columns of the operands of a UNION ALL have exactly matching data types (including length or precision and scale) and matching default values if the fullselect of the view includes a UNION ALL.

Read-only views
A view is read-only if it is not deletable, updatable, or insertable. A view can be read-only if it is a view that does not comply with at least one of the rules for deletable views

Alias
An alias is an indirect method of referencing a table or a view An alias can be created on another alias An alias can be used in a view or trigger definition and in any SQL statements An alias can be defined for a table, view, or alias that does not exist Sintak :
CREATE ALIAS ADMIN.MUSICIANS FOR ADMIN.ARTISTS;

Index
Indexes might allow more efficient access to rows in a table. Unique indexes ensure uniqueness of the index key. An index key is a column or an ordered collection of columns on which an index is defined

Overview of triggers: The need

Business rules that might be supported by triggers include


Validation of input Automatic generation of a value for an inserted row Reading from other tables for crossreferencing purposes Writing to other tables for audit-trail purposes Maintaining business rules Providing support for user alerts

S-ar putea să vă placă și