Download presentation
Presentation is loading. Please wait.
Published byBartholomew Todd Modified over 9 years ago
2
Working with Data Objects Pertemuan 6 Matakuliah: T0413 Tahun: 2009
3
Bina Nusantara University 3 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)
4
Bina Nusantara University 4 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:. –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
5
Bina Nusantara University 5 CREATE TABLE artists (artno SMALLINT not null, name VARCHAR(50) with default 'abc', classification CHAR(1) not null, bio CLOB(100K) logged, picture BLOB(2M) not logged compact ) in mytbls1 2) Tables
6
Bina Nusantara University 6 Data Types DB2 Data Types
7
Bina Nusantara University 7 To store large character strings or files To store large binary strings or files Large Objects
8
Bina Nusantara University 8 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 FAILS User-Defined Types
9
Bina Nusantara University 9 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 NULL, NAME VARCHAR(9), DEPT SMALLINT not null with default 10, JOB CHAR(5), YEARS SMALLINT, SALARY DECIMAL(7,2), COMM DECIMAL(7,2) with default 15 ) Null Values
10
Bina Nusantara University 10 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
11
Bina Nusantara University 11 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) Declared Temporary Tables
12
Bina Nusantara University 12 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 ; DECLARE GLOBAL TEMPORARY TABLE tempprojects AS ( fullselect ) DEFINITION ONLY ON COMMIT PRESERVE ROWS NOT LOGGED WITH REPLACE IN TABLESPACE apptemps; Temporary Tables
13
Bina Nusantara University 13 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 Identity Columns
14
Bina Nusantara University 14 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 SEQUENCE objects
15
Bina Nusantara University 15 Row Compression (Not available in DB2 Express-C)
16
Bina Nusantara University 16 Table Partitioning (not available in DB2 Express-C) 64G 32K Partitions A-Z 64G A-C 64G D-M 64G N-Q 64G R-Z Backup Load Recover Backup Load Recover Backup Load Recover Backup Load Recover Backup Load Recover Backup Load Recover
17
Bina Nusantara University 17 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 NAME CLASSIFICATION ------ ----------------- -------------- 10 HUMANA 20 MY PLANT C 30 THE STOREE... 3) Views
18
Bina Nusantara University 18 Index Characteristics: ƒascending or descending ƒUnique or non-unique ƒcompound ƒcluster ƒbi-directional (default behavior)) Examples : create unique index artno_ix on artists (artno) 4) Indexes
19
Bina Nusantara University 19 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
20
Bina Nusantara University 20 Design Advisor
21
Bina Nusantara University 21 or unique constraint create table employee (empno............. primary key (empno) foreign key (workdept) references department on delete no action) in DMS01 Referential Integrity
22
Bina Nusantara University 22 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 –Foreign key fields set to null; other columns left unchanged. Referential Integrity Rules
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.