Creating Tables with the CREATE TABLE SQL StatementIn Chapter 4, we saw that a table is a logical database object that acts as the main repository in a database. We also saw that there are two ways a table can be created: by using the Create Table Wizard provided with the Control Center and by using the CREATE TABLE SQL statement. Up until now, we've seen how to use a relatively simple form of the CREATE TABLE statement to construct very basic tables. Yet, the CREATE TABLE is probably the most complex SQL statement available (in fact, over 60 pages of the DB2 UDB SQL Reference manual are devoted to this statement alone). And because this statement is so complex, its syntax can be quite intimidating. Fortunately, you do not have to know all the nuances of the CREATE TABLE statement to pass the DB2 UDB V8.1 Family Fundamentals certification exam (Exam 700). Still, you do need to know the basics, and the remainder of this chapter is devoted to the CREATE TABLE statement and to the syntax you must be familiar with. With that said, let's begin by taking a look at the simplest form of the CREATE TABLE SQL statement. In its simplest form, the syntax for the CREATE TABLE SQL statement is: CREATE TABLE [ TableName ] ( [ Element ] ,...) <IN [ TablespaceName ]> <INDEX IN [ TablespaceName ]> <LONG IN [ TablespaceName ]> where:
The basic syntax used to define a column is: [ ColumnName ] [ DataType ] <NOT NULL> <WITH DEFAULT <[ DefaultValue ] CURRENT DATE CURRENT TIME CURRENT TIMESTAMP>> < UniqueConstraint > < CheckConstraint > < ReferentialConstraint > where:
Table 6-4. Data Type Definitions That Can Be Used with the CREATE TABLE Statement
The syntax used to create a unique or primary key constraint as part of a column definition is: <CONSTRAINT [ ConstraintName ]> [UNIQUE PRIMARY KEY] where:
The syntax used to create a check constraint as part of a column definition is: <CONSTRAINT [ ConstraintName ]> CHECK ( [ CheckCondition ] ) <ENFORCED NOT ENFORCED> <ENABLE QUERY OPTIMIZATION DISABLE QUERY OPTIMIZATION> where:
And finally, the syntax used to create a referential constraint as part of a column definition is: <CONSTRAINT [ ConstraintName ]> REFERENCES [ PKTableName ] < ( [ PKColumnName ] ,...) > <ON UPDATE [NO ACTION RESTRICT]> <ON DELETE [CASCADE SET NULL NO ACTION RESTRICT]> <ENFORCED NOT ENFORCED> <ENABLE QUERY OPTIMIZATION DISABLE QUERY OPTIMIZATION> where:
Thus, if you wanted to create a table that had three columns in it, two of which use an integer data type and another that uses a fixed-length character string data type, you could do so by executing a CREATE TABLE SQL statement that looks something like this: CREATE TABLE EMPLOYEES (EMPID INTEGER, NAME CHAR(50) DEPT INTEGER) If you wanted to create the same table such that the EMPID column had both the NOT NULL constraint and a unique constraint associated with it, you could do so by executing a CREATE TABLE statement that looks something like this: CREATE TABLE EMPLOYEES (EMPID INTEGER NOT NULL PRIMARY KEY, NAME CHAR(50) DEPT INTEGER) And if you wanted to create the same table such that the DEPT column participates in a referential constraint with the DEPARTMENT table, you could do so by executing a CREATE TABLE statement that looks something like this: CREATE TABLE EMPLOYEES (EMPID INTEGER, NAME CHAR(50) DEPT INTEGER REFERENCES DEPARTMENT (DEPTID)) As you can see, a unique constraint, a check constraint, and/or a referential constraint that involves a single column can be defined as part of that particular column's definition. But what if you needed to define a constraint that encompasses multiple columns in the table? You do this by defining a constraint as another element, rather than as an extension to a single column's definition. The basic syntax used to define a unique constraint as an individual element is: <CONSTRAINT [ ConstraintName ]> [UNIQUE PRIMARY KEY] ( [ ColumnName ] ,...) where:
The syntax used to create a check constraint as an individual element is the same as the syntax used to create a check constraint as part of a column definition: <CONSTRAINT [ ConstraintName ]> CHECK ( [ CheckCondition ] ) <ENFORCED NOT ENFORCED> <ENABLE QUERY OPTIMIZATION DISABLE QUERY OPTIMIZATION> where:
And finally, the syntax used to create a referential constraint as an individual element is: <CONSTRAINT [ ConstraintName ]> FOREIGN KEY ( [ ColumnName ] ,...) REFERENCES [ PKTableName ] < ( [ PKColumnName ] ,...) > <ON UPDATE [NO ACTION RESTRICT]> <ON DELETE [CASCADE SET NULL NO ACTION RESTRICT]> <ENFORCED NOT ENFORCED> <ENABLE QUERY OPTIMIZATION DISABLE QUERY OPTIMIZATION> where:
Thus, a table that was created by executing a CREATE TABLE statement that looks something like this: CREATE TABLE EMPLOYEES (EMPID INTEGER NOT NULL PRIMARY KEY, NAME CHAR(50) DEPT INTEGER REFERENCES DEPARTMENT (DEPTID)) could also be created by executing a CREATE TABLE statement that looks something like this: CREATE TABLE EMPLOYEES (EMPID INTEGER NOT NULL, NAME CHAR(50) DEPT INTEGER, PRIMARY KEY (EMPID), FOREIGN KEY (DEPT) REFERENCES DEPARTMENT (DEPTID)) Creating Tables That Are Similar to Existing TablesAt times, it may be desirable to create a new table that has the same definition as an existing table. To perform such an operation, you could execute a CREATE TABLE statement that looks identical to the CREATE TABLE statement used to define the original table. Or better still, you could use a special form of the CREATE TABLE statement. The syntax for this form of the CREATE TABLE is: CREATE TABLE [ TableName ] LIKE [ SourceTable ] <[INCLUDING EXCLUDING] COLUMN DEFAULTS> <[INCLUDING EXCLUDING] IDENTITY COLUMN ATTRIBUTES> where:
When this form of the CREATE TABLE is executed, the table that is ultimately created will have the same number of columns as the source table specified, and these columns will have the same names , data types, and nullability characteristics as those of the source table. In addition, unless the EXCLUDING COLUMN DEFAULTS option is specified, any default constraints defined for columns in the source table will be copied to the new table as well. However, no other attributes of the source table will be duplicated . Thus, the table that is created will not contain unique constraints, referential constraints, triggers, or indexes that have been defined for the source table used. A Word about Declared Temporary TablesBefore we look at some more complex examples of the CREATE TABLE statement, another type of table that is commonly used should be mentioned. This type of table is known as a declared temporary table. Unlike base tables, whose descriptions and constraints are stored in the system catalog tables of the database to which they belong, declared temporary tables are not persistent and can only be used by the application that creates them ”and only for the life of the application. When the application that creates a declared temporary table terminates, the rows of the table are deleted, and the description of the table is dropped. Whereas base tables are created with the CREATE TABLE SQL statement, declared temporary tables are created with the DECLARE GLOBAL TEMPORARY TABLE statement. |