![]() |
SQL-J Language Reference
|
Reference Manual |
CONSTRAINT clauseA CONSTRAINT clause is an optional part of a CREATE TABLE statement or ALTER TABLE statement. A constraint is a rule to which data must conform. Constraint names are optional. A CONSTRAINT can be one of the following:
NOTE: If the foreign key consists of multiple columns, and any column is NULL, the whole key is considered NULL. The insert is permitted no matter what is on the non-null columns. Column constraints and table constraints have the same function; the difference is in where you specify them. Table constraints allow you to specify more than one column in a PRIMARY KEY, UNIQUE, CHECK, or FOREIGN KEY constraint definition. Column-level constraints (except for check constraints) refer to only one column. Columns that store Java objects cannot be part of a PRIMARY KEY, UNIQUE, or FOREIGN KEY constraints unless they are orderable. SyntaxColumn-Level Constraint
{ Table-Level Constraint
[CONSTRAINT ConstraintName] ReferencesSpecificationREFERENCES TableName [ ( SimpleColumnName [ , SimpleColumnName ]* ) ] searchConditionA searchCondition is any Boolean expression that meets the requirements specified in Requirements for Search Condition. If a ConstraintName is not specified, Cloudscape generates a unique constraint name (for either column or table constraints). Constraint names are not generated for NOT NULL constraints. NOTE: Cloudscape recommends that you name all constraints. Primary Key and Unique ConstraintsA primary key defines the set of columns that uniquely identifies rows in a table. When you create a primary key, none of the columns can have NULL constraints; that is, they must not permit NULL values. When you add columns without explicit NOT NULL or NULL constraints to a primary key, they gain implicit NOT NULL constraints. A unique constraint enforces the uniqueness of data. Unique constraints allow NULLs. A table can have at most one PRIMARY KEY constraint, but can have multiple UNIQUE constraints. Primary key and unique constraints are permitted on Java data types only if they are orderable (see Orderable Java Data Types). Foreign Key ConstraintsForeign keys provide a way to enforce the referential integrity of a database. A foreign key is a column or group of columns within a table that references a key in some other table (or sometimes, though rarely, the same table). The foreign key must always include the columns of which the types exactly match those in the referenced primary key or unique constraint. For a table-level foreign key constraint in which you specify the columns in the table that make up the constraint, you cannot use the same column more than once. If there is a column list in the ReferencesSpecification (a list of columns in the referenced table), it must correspond either to a unique constraint or to a primary key constraint in the referenced table. The ReferencesSpecification can omit the column list for the referenced table if that table has a declared primary key. If there is no column list in the ReferencesSpecification and the referenced table has no primary key, a statement exception is thrown. (This means that if the referenced table has only unique keys, you must include a column list in the ReferencesSpecification.) Foreign key constraints are permitted on Java data types only if they are orderable (see Orderable Java Data Types). A foreign key constraint is satisfied if there is a matching value in the referenced unique or primary key column, or if the value is NULL. If the foreign key consists of multiple columns, the foreign key value is considered NULL if any of its columns contains a NULL. NOTE: It is possible for a foreign key consisting of multiple columns to allow one of the columns to contain a value for which there is no matching value in the referenced columns, per the SQL-92 standard. To avoid this situation, create NOT NULL constraints on all of the foreign key's columns. Foreign Key Constraints and DMLWhen you insert into or update a table with an enabled foreign key constraint, Cloudscape checks that the row does not violate the foreign key constraint by looking up the corresponding referenced key in the referenced table. If the constraint is not satisfied, Cloudscape rejects the insert or update with a statement exception. When you update or delete a row in a table with a referenced key (a primary or unique constraint referenced by a foreign key), Cloudscape checks every foreign key constraint that references the key to make sure that the removal or modification of the row does not cause a constraint violation. If removal or modification of the row would cause a constraint violation, the update or delete is not permitted and Cloudscape throws a statement exception. You can temporarily disable foreign key and check constraints. See SET CONSTRAINTS statement. Cloudscape performs constraint checks at the time the statement is executed, not when the transaction commits. Backing IndexesUNIQUE, PRIMARY KEY, and FOREIGN KEY constraints generate indexes that enforce or "back" the constraint (and are sometimes called backing indexes). UNIQUE and PRIMARY KEY constraints generate unique indexes. FOREIGN KEY constraints generate non-unique indexes. Therefore, if a column or set of columns has a UNIQUE, PRIMARY KEY, or FOREIGN KEY constraint on it, you do not need to create an index on those columns for performance. Cloudscape has already created it for you. See Indexes and Constraints. You can specify storage properties for the indexes backing the PRIMARY KEY, UNIQUE, and FOREIGN KEY constraints with a PROPERTIES clause. A PROPERTIES clause allows you to specify page size for the backing index on a constraint and to override the values for those properties set on a database-wide basis. For backing indexes, you can specify only page size. For more information, see PROPERTIES clause. These indexes are available to the optimizer for query optimization (see CREATE INDEX statement) and have system-generated names. You cannot drop backing indexes with a DROP INDEX statement; you must drop the constraint or the table. Check ConstraintsA check constraint can be used to specify a wide range of rules for the contents of a table. A search condition (which is a boolean expression) is specified for a check constraint. This search condition must be satisfied for all rows in the table. The search condition is applied to each row that is modified on an INSERT or UPDATE at the time of the row modification. The entire statement is aborted if any check constraint is violated. NOTE: According to the SQL-92 standard, if the boolean expression returns an UNKNOWN, it satisfies the search condition. This means that if you have a check constraint such as "c = 3", and a row is inserted in which the value in column c is null, c=3 evaluates to UNKNOWN for that column. This means that the insert is allowed. If this situation is not acceptable to you, use the IS expression (see "IS" in Table 1-10). Requirements for Search ConditionThe search condition can reference any column in the current row of the table to which the constraint is being applied. The search condition must always return the same value if applied to the same values. Thus, it cannot contain any of the following:
About Java Methods and Fields in CHECK ConstraintsUse Java methods and fields with care. They should return the same results given the same circumstances; they should not depend on anything except the arguments. For example, methods could potentially return random results. Such methods are not appropriate. It is also possible to use methods that execute SQL. Some SQL is appropriate, but some is not, because it might depend on something other than the arguments to the method (for example, data in another table). Cloudscape does not check the appropriateness of Java methods and fields in check constraints. It is up to the database designer to ensure that the use of the method or field is appropriate. Here is an extended example showing you how seemingly harmless SQL could get you into trouble. Subqueries are not allowed in check constraints. Suppose that is the functionality that you need. You want to check that t1.c1 is in t2.c1, and you cannot do it with a foreign key because there is no primary key or unique constraint on t2.c1. You could add a method call that contains SQL to the check constraint. If subqueries were allowed, here's how you would like to define the table:
-- CREATE TABLE t1(c1 INT CHECK (c1 IN SELECT c1 FROM t2), This statement is not allowed. Instead, you could do the following:
-- CREATE TABLE t1(c1 INT CHECK(booleanMethod(c1)), where booleanMethod essentially performs the subquery. The problem with this approach is that the contents of t2 can change independently of the contents of t1. Consider the following scenario:
-- succeeds since 1 is in t2
-- nothing to stop the delete
-- fails since check constraint will fail Disabling ConstraintsYou can temporarily disable foreign key and check constraints. For example, you might want to disable such constraints before loading a lot of data. See SET CONSTRAINTS statement. CREATE TABLE Examples
CREATE TABLE Cities
CREATE TABLE HotelAvailability (hotel_id INT ,
-- Use a column-level constraint for an arithmetic check;
-- use a check constraint to allow only appropriate
-- create a table whose city_id column references the
-- create a table with a table-level primary key constraint
-- add a unique constraint to a column Dependency SystemINSERT and UPDATE statements depend on all constraints on the target table. DELETEs depend on unique, primary key, and foreign key constraints. These statements are invalidated if a constraint is added to or dropped from the target table. CREATE Statements |
|
![]() Cloudscape Version 3.6 For information about Cloudscape technical support, go to: www.cloudscape.com/support/.Copyright © 1998, 1999, 2000 Informix Software, Inc. All rights reserved. |