Title Goes Here - Binus Repository

Download Report

Transcript Title Goes Here - Binus Repository

Matakuliah
Tahun
: T0413
: 2009
Working with Data Objects
Pertemuan 6
Database Objects
Schema: All DB2 objects have a two part name and the schema is
the first half of that name.
Table: A collection of data logically arranged in columns and rows
View: A representation of the data in tables
Index: An ordered set of keys each pointing to a row in a table
Database Application Objects: (not all will be covered in this
section)
Sequences
Triggers (covered in another presentation)
User Defined Functions (UDFs) (covered in another presentation)
Stored Procedures (covered in another presentation)
Bina Nusantara University
3
1) Schema
• Schemas are name spaces for a collection of database
objects
• Schemas are primarily used to:
– Provide an indication of object ownership or relationship to
an application
– Logically group related objects together
• All database objects belong to schemas and are qualified by a
two-part name:
<schema_name>.<object_name>
– A fully qualified object name must be unique
• When you connect to a database and create or reference an
object without specifying the schema, DB2 uses the user ID
you connected to the database with for the schema name
Bina Nusantara University
4
2) Tables
CREATE TABLE artists
(artno
SMALLINT
name
VARCHAR(50)
classification CHAR(1)
bio
CLOB(100K)
picture
BLOB(2M)
)
in mytbls1
Bina Nusantara University
not null,
with default 'abc',
not null,
logged,
not logged compact
5
• DB2 Data
Types
Data Types
Data Types
Numeric
Integer
SMALLINT
INTEGER
BIGINT
DECIMAL
DECIMAL
Floating
Point
REAL
DOUBLE
String
Character
String
Single Byte
Double Byte
Binary
String
CHAR
VARCHAR
LONG VARCHAR
CLOB
GRAPHIC
VARGRAPHIC
LONG VARGRAPHIC
DBCLOB
BLOB
Datetime
DATE
TIME
TIMESTAMP
Bina Nusantara University
XML
6
Large Objects
To store large character strings or files
To store large binary strings or files
Action
News
DB2
By The Book
B inary
L arge
OB ject
C haracter
L arge
OB ject
D ouble
B yte
C haracter
L arge
OB ject
DB2
Bina Nusantara University
7
User-Defined Types
CREATE DISTINCT TYPE POUND AS INTEGER WITH COMPARISONS
CREATE DISTINCT TYPE KILOGRAM AS INTEGER
WITH COMPARISONS
CREATE TABLE person
(f_name
varchar(30),
weight_p POUND NOT NULL,
weight_k KILOGRAM NOT NULL )
SELECT F_NAME FROM PERSON
WHERE weight_p > POUND(30)
SELECT F_NAME FROM PERSON
WHERE weight_p > weight_k
Bina Nusantara University
8
FAILS
Null Values
A null value represents an unknown state
ƒ The CREATE TABLE statement can contain the phrase NOT
NULL following the definition of each column.
ƒ This will ensure that the column contains a known data value.
Can specify a default value if NOT NULL is entered
CREATE TABLE Staff (
ID
SMALLINT NOT
DEPT
SMALLINT not
JOB
CHAR(5) ,
YEARS
SMALLINT ,
SALARY
DECIMAL(7,2)
COMM
DECIMAL(7,2)
)
Bina Nusantara University
NULL, NAME VARCHAR(9) ,
null with default 10,
,
with default 15
9
System Catalog Tables
•
•
•
•
Each database has its own system catalog tables/views
These store meta data about the database objects
You can query these tables just like any other tables
Reside in three schemas:
– SYSIBM - base tables, optimized for DB2
– SYSCAT - views based on SYSIBM tables, optimized for ease of use
– SYSSTAT - database statistics
examples:
» SYSCAT.TABLES
» SYSCAT.INDEXES
» SYSCAT.COLUMNS
» SYSCAT.FUNCTIONS
» SYSCAT.PROCEDURES
Bina Nusantara University
10
Declared Temporary Tables
Created and used by an application and dropped (automatically) when the
application terminates
Can only be accessed by the application that created the table
No entry exists in any catalog table
Logging
ƒ NOT LOGGED clause optional
Automatic cleanup
Performace
ƒ avoid catalog contention
ƒ no locking of rows
ƒ no logging (but logging is optional)
ƒ no authority checking
Index support
ƒ any standard index can be created on a temporary table
Statistics support (RUNSTATS supported against the table)
Bina Nusantara University
11
Temporary Tables
Declared temporary tables reside in a user temporary tablespace
ƒ Must be defined prior to creating any declared temporary tables
CREATE USER TEMPORARY TABLESPACE apptemps
MANAGED BY SYSTEM USING ('apptemps');
DECLARE GLOBAL TEMPORARY TABLE temployess
LIKE employee NOT LOGGED;
DECLARE GLOBAL TEMPORARY TABLE tempdept
( deptid CHAR(6), deptname CHAR(20) )
ON COMMIT DELETE ROWS NOT LOGGED ;
Bina Nusantara University
DECLARE GLOBAL TEMPORARY TABLE tempprojects
AS ( fullselect ) DEFINITION ONLY
ON COMMIT PRESERVE ROWS NOT LOGGED
WITH REPLACE IN TABLESPACE apptemps;
12
Identity Columns
A numeric column in a table which automatically generates a unique
numeric value for each row that is inserted
One Identity column per table maximum
Values can be generated by DB2 always or by default
ƒ Generated always
–values are always generated by DB2
–applications are not allowed to provide an explicit value.
ƒ Generated by default
–values can be explicitly provided by an application or if no
value is given, then DB2 generates one
–DB2 cannot guarantee uniqueness
–intended for data propagation, unload/reload of a table
Bina Nusantara University
13
SEQUENCE objects
Unlike identity columns, sequences are independent of tables
example:
CREATE SEQUENCE myseq
START WITH 1
INCREMENT BY 1
NO CYCLE
INSERT INTO t1 VALUES (nextval for myseq, ...)
SELECT prevval for myseq FROM sysibm.sysdummy1
Bina Nusantara University
14
Row Compression (Not available in DB2 Express-C)
Bina Nusantara University
15
Table Partitioning (not available in DB2 Express-C)
32K Partitions
64G
64G
A-Z
A-C
Backup
Load
Recover
Backup
Load
Recover
64G
64G
64G
D-M
N-Q
R-Z
Backup
Load
Recover
Backup
Load
Recover
Backup
Load
Recover
Backup
Load
Recover
Bina Nusantara University
16
3) Views
Data for view not stored separately
Nested view supported
View information kept in: SYSCAT.VIEWS, SYSCAT.VIEWDEP,
SYSCAT.TABLES
CONNECT TO MYDB1
CREATE VIEW MYVIEW1
AS SELECT ARTNO, NAME, CLASSIFICATION
FROM ARTISTS
SELECT * FROM MYVIEW1
ARTNO
-----10
20
30
NAME
----------------HUMAN
MY PLANT
THE STORE
CLASSIFICATION
-------------A
C
E
...
Bina Nusantara University
17
4) Indexes
Index Characteristics:
ƒ
ƒ
ƒ
ƒ
ƒ
ascending or descending
Unique or non-unique
compound
cluster
bi-directional (default behavior))
Examples:
create unique index artno_ix on artists (artno)
Bina Nusantara University
18
Launching the Design Advisor
• Advises you on the design of your database to optimize it for
a given SQL workload
Control
Center > (expand) All Databases Folder > (right-click) Database > Design Advisor
Bina Nusantara University
19
Design Advisor
Bina Nusantara University
20
Referential Integrity
or unique constraint
Bina Nusantara University
create table employee (empno
.............
primary key (empno)
foreign key (workdept)
references department on delete no action)
in DMS01
21
Referential Integrity Rules
Insert Rules
ƒ Rule is implicit when a foreign key is specified.
ƒ backout insert if not found
Delete Rules
ƒ Restrict
–Parent row not deleted if dependent rows are found.
ƒ Cascade
–Deleting row in parent table automatically deletes any related rows in dependent
tables.
ƒ No Action (default)
–Enforces presence of parent row for every child after all other referential constraints
applied
ƒ Set Null
Bina Nusantara University
–Foreign key fields set to null; other columns left unchanged.
22