db2 tables, datatypes, indexes, views

system tables (SYSCATSPACE) may have one of four (4) schema names:
  SYSIBM - read-only system tables (e.g. SYSIBM.SYSTABLESPACES, SYSIBM.SYSTABLES, etc.)
  SYSCAT - read-only views on the system tables (e.g. SYSCAT.TABLESPACES, SYSCAT.TABLES, etc.)
  SYSFUN - UDF (User Defined Functions)
  SYSSTAT - hold statistics (e.g. SYSSTAT.TABLES,  SYSSTAT.COLUMNS, SYSSTAT.INDEXES, etc.)

db2 list tables for system
db2 list tables for schema db2inst1
db2 list tables

Compare:
db2 'select * from SYSIBM.SYSTABLES fetch first 1 rows only'
db2 'select * from SYSCAT.TABLES fetch first 1 rows only'
db2 "select TABNAME from SYSCAT.TABLES where TABNAME like 'A%'"

Database Object SYSIBM system table SYSCAT system table view SYSSTAT updateable view
Tablespace SYSTABLESPACES TABLESPACES
Table SYSTABLES TABLES TABLES
Schema SYSSCHEMATA SCHEMATA
View SYSVIEWS VIEWS
Column SYSCOLUMNS COLUMNS COLUMNS
Index SYSINDEXES INDEXES INDEXES
Data Type SYSDATATYPES DATATYPES
Check Constraint SYSCHECKS CHECKS
Referential Integrity SYSRELS REFERENCES
Function SYSFUNCTIONS FUNCTIONS FUNCTIONS
Bufferpool SYSBUFFERPOOLS BUFFERPOOLS

User tables - simply tables created by users (permanent tables)
Temporary tables:
   - derived temp.tables - created dynamically by DB2 as needed in temp. tablespaces. Automatically dropped at the end of transaction.
   - declared temporary tables (version 7) - created by user with the DECLARE command with the schema called SESSION.
      db2 declare global temporarytable ... ( ????)

db2 'select typename from syscat.datatypes'

TYPENAME
------------------
BIGINT          (8 bytes)
BLOB          (Binary Large Object - up to 2 GB - video, sound & pictures)
BOOLEAN
CHARACTER   (char(1) is default, char(n), where n = 1..254)
CLOB          (Character Large Object - 32K .. 2 GB) - also DBCLOB to store 2-byte texts (Chinese, etc.)
DATALINK
DATE             (4 bytes)
DECIMAL        (decimal(5,0) by default)
DOUBLE         (similar to real,but 8 bytes)
INTEGER         (4 bytes)
LONG VARCHAR   (old type, when varchar was shorter)
REAL              (4 bytes)
REFERENCE
SMALLINT       (2 bytes)
TIME              (3 bytes)
TIMESTAMP    (10 bytes)
VARCHAR       (1..32672 bytes)

  17 record(s) selected.
There are also some other types (and user-defined types)

Difference between date, time, and timestamp types:

DATE (4 bytes)
TIME (3 bytes)
TIMESTAMP (10 bytes)

select CURRENT DATE from (values 1) a
select CURRENT TIMESTAMP from (values 1) a
select CURRENT TIME from (values 1) a

select CURRENT SERVER from (values 1) a
select user from (values 1) a
db2 "values(user)"

create table inst.test (d date, t time, ts timestamp) in inst_4K
insert into inst.test values (date(current timestamp), time(current timestamp), timestamp(current timestamp))
select * from inst.test
        D       T       TS
1       2002-04-29      09:33:53        2002-04-29 09:33:53.207283
--------------------------------------------

datetime arithmetic:

labeled:
  select sales_date, sales_date + 1 YEAR + 2 DAYS from sales

date: yyyymmdd - decimal(8,0)
  select sales date, sales_date + 00010001. from sales
  select sales_date, char(sales_date - '04/01/1994') from sales

time:  hhmmss - decimal (6,0)
  select ... time('12:23:10') - 011005.
               time('12:23:10') - '01:01:01'

timestamp:  yyyyMMddHHmmSS.nnnnnn   decimal(20,6)
  timestamp(20010301171230') - 00000000011005.000022
  char( timestamp(20010301171230') - '2001-03-01-16.02.24.999978' )

useful functions:
date(), day(), days(),hour(), minute(), month(), second(), time(), timestamp(), timestapm_iso(),year()

Identity columns:

create table test_identity (
  myid int generated always as identity,
  lname varchar(25),
  fname varchar(20)
);
insert into test_identity (lname, fname) values ('l1','f1');
insert into test_identity  (lname, fname) values ('l2','f2');
insert into test_identity  (lname, fname) values ('l3','f3');
insert into test_identity  (lname, fname) values ('l4','f4');

select * from test_identity

Note: when defining identity column, you can also provide attributes, for example:

    * start with NN
    * increment by NN
    * minvalue NN
    * no minvalue
    * maxvalue NN
    * no maxvalue
    * cycle
    * no cycle
    * cache NN
    * order
    * no order

You can also provide a identity "generation-expression"

not null , with default:
create table inventory (
  productID int not null with default 12345,
  quantity integer,
   price decimal(4,2),
   dept smallint with default)

Table Constraints:
  Primary key, foreign key
db2 alter table salary add constraint chk_salbonus CHECK (salary + bonus < 200000)
db2 create table salary ( employeeID smallint not null, ...... ,  constraint uempID UNIQUE(employeeID))

Insert rule
delete rules  (NO ACTION, RESTRICT, CASCADE, SET NULL)
update rules (NO ACTION, RESTRICT)

create table tab1 ( id, ...)
create table tab2 (id, ...)
create table tab3 (
   a ..,
   b ..,
   c ..,
   constraint ca foreign key(a) references tab1(id),
   constraint cb foreign key(b) references tab2(id))

Indexes:
db2 create index  i1 on tab1 ( col1 desc)
db2 create index  i2 on tab1 (col1, col2)
db2 create unique index i1 on tab1(col1)
db2 create index i1 on tab1(col1) cluster
db2 create index i1 on tab1(col1) allow reverse scans
db2 alter index ...
db2 drop index ...

Views:
db2 "create view as select col1 from tab1 where col1 like 'A%' "

Bufferpools - cache for database pages (see in SYSCAT.BUFFERPOOLS table)
Log Files - logging can be turned off to speed up operations

你可能感兴趣的:(cache,db2,UP)