Anda di halaman 1dari 4

CREATION OF TABLE: The CREATE TABLE statement defines a table.

The definition must include its name and the names and attributes of its columns. The definition can include other attributes of the table, such as its primary key or check constraints. Syntax: CREATE TABLE < table name> (col-name1 col-type( col-size) colconstraint , col-name2 col-type(col-size) col-constraint, ..... ,colname255 col-type(col-size) col-constraint). Example: CREATE TABLE EMPLOYEE (ID NAME DEPT JOB ('Sales', 'Mgr', 'Clerk')), SMALLINT NOT NULL, VARCHAR(9), SMALLINT , CHAR(5) CHECK (JOB IN

1986 OR SALARY > 40500) ) ALTER TABLE:

HIREDATE DATE, SALARY DECIMAL(7,), PRIMARY KEY (ID), CONSTRAINT YEARSAL CHECK (YEAR(HIREDATE) >

Adding a column to the EMPLOYEE table: Syntax: ALTER TABLE <table-name> ADD COLUMN <col-name> <data type> ALTER TABLE EMPLOYEE ADD COLUMN COMM DECIMAL(3,2) Syntax for alter the column size: ALTER TABLE <table-name> ALTER COLUMN <col-name> SET DATA TYPE <col-type>(<col-size>) ALTER TABLE MY_TABLE ALTER COLUMN MY_COLUMN SET DATA TYPE VARCHAR(20) Note: Only the size the column with data type varchar and char can be altered.

Syntax for adding constraints: ALTER TABLE <table-name> ADD CONSTRAINT <constraint-name> constraint-type(col-name)

To add a NOT NULL constraint: ALTER TABLE <table-name> ALTER COLUMN <col-name> ADD NOT NULL To drop a NOT NULL constraint: ALTER TABLE <table-name> ALTER COLUMN <col-name> DROP NOT NULL Note: Adding and dropping not null constraint is introduced in DB2 9 version. ALTER TABLE EMPLOYEE ADD CONSTRAINT primary_empno PRIMARY KEY(EMPNO) ALTER TABLE EMPLOYEE ADD CONSTRAINT unique_empno KEY(EMPNO) ALTER TABLE EMPLOYEE ADD CONSTRAINT check_dept BETWEEN 10 AND 100)) To drop Check constraint: ALTER TABLE EMPLOYEE DROP CONSTRAINT check_dept Note: When you have check constraint table that contains data, one of two things can happen. All the rows meet the check constraint. Some or all rows do not meet the check constraint. In the first case, when all the rows meet the check constraint, the constraint will created successfully. In the second case, when some rows meet the check constraint, the constraint will not be created. ( i.e., the Alter statement will fail) ALTER TABLE TO ADD IDENTITY COLUMN: ALTER TABLE <table-name> ALTER COLUMN <col-name> SET GENERATED AS IDENTIY (START WITH <initial-value> INCREMENT BY <incr-val>) PRIMARY

CHECK (DEPT

Example: ALTER TABLE VINAYAK.IDENTITY1 ALTER COLUMN ENO SET GENERATED AS IDENTITY ( START WITH 1 INCREMENT BY 1 NO CACHE ) ; Referential Integrity: Referential integrity can be defined as a method of ensuring data integrity between tables related by primary and foreign keys.

CREATE TABLE EMPLOYEE(ID SMALLINT NOT NULL, NAME VARCHAR(9), DEPTNO SMALLINT NOT NULL REFERENCES DEPT(DEPTNUMBER)) To ensure that this integrity remains intact, DB2 has a series of rules for inserting, deleting, and updating: When inserting a row with a foreign key, DB2 checks the values of the foreign key columns against the values of the primary key columns in the parent table. If no matching primary key columns are found, the insert is disallowed. A new primary key row can be inserted as long as the primary key is unique for the table. When updating foreign key values, DB2 performs the same checks as when it is inserting a row with a foreign key. If a primary key value is updated, DB2 does not allow there to be any existing foreign keys that refer back to the primary key that is changing. All foreign key rows first must be either deleted or be set to NULL before the value of the primary key can be changed. Deleting a row with a foreign key is always permitted. When deleting a row with a primary key, DB2 takes action as indicated in the DDL; it either restricts deletion, cascades deletes to foreign key rows, or sets all referenced foreign keys to null. Each referential constraint must define the action that will be taken on foreign key rows when a primary key is deleted. There are four options that can be specified: RESTRICT : disallows the deletion of the primary key row if any foreign keys relate to the row. CASCADE : allows the deletion of the primary key row and also deletes the foreign key rows that relate to it. SET NULL : allows the deletion of the primary key row and, instead of deleting all related foreign key rows, sets the foreign key columns to NULL. NO ACTION: the behavior of NO ACTION is similar to RESTRICT. The only difference between RESTRICT and NO ACTION is when the referential constraint is enforced. RESTRICT enforces the delete rule immediately; NO ACTION enforces the delete rule at the end of the statement.

Example: CREATE TABLE EMPLOYEE(ID SMALLINT NOT NULL, NAME VARCHAR(9), DEPTNO SMALLINT NOT NULL REFERENCES DEPT(DEPTNUMBER) ON DELETE RESTRICT) Placing the Table in Tablespace in AIX: CREATE TABLE EMPLOYEE SMALLINT,................. ) in userspace1 Placing the Table in Tablespace in WINDOWS: CREATE TABLE EMPLOYEE SMALLINT,................. ) in c:/ Creation of Index: The physical storage of rows in a base table is not ordered. When a row is inserted, it is placed in the most convenient storage location that can accommodate it. When searching for rows of a table that meet a particular selection condition and the table has no indexes, the entire table is scanned. An index optimizes data retrieval without performing a lengthy sequential search. The following SQL statement creates a non-unique index called LNAME from the LASTNAME column on the EMPLOYEE table, sorted in ascending order: CREATE INDEX LNAME ON EMPLOYEE (LASTNAME ASC) Note: You must use a DMS tablespaces to have table and indexes in separate tablespaces. (ID (ID

Anda mungkin juga menyukai