To ensure that the data in a table conforms to your business requirements, you can define constraints at the column level.
Constraints limit the types of data that can be stored in columns. If you attempt to enter or update a value that does not satisfy the constraint on the corresponding column, an error is returned and the operation is rolled back. Likewise, if you attempt to add a constraint that conflicts with the existing data to a column in an existing table, an error is returned and the operation is rolled back.
Types of constraints
Oracle tenants of OceanBase Database support the NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK constraints.
NOT NULL
A NOT NULL constraint requires that the constrained column do not contain a NULL value. For a column with the NOT NULL constraint, you must specify the column value in an INSERT statement unless you have defined a default value for the column.
The following sample code creates a table named tbl1 and specifies the NOT NULL constraint for the col1 column:
obclient> CREATE TABLE tbl1(col1 INT NOT NULL,col2 INT);
UNIQUE
A UNIQUE constraint requires that the constrained column do not contain duplicate values. However, multiple NULL values are allowed.
The following sample code creates a table named tbl2 and specifies the UNIQUE constraint for the col1 column:
obclient> CREATE TABLE tbl2(col1 INT UNIQUE,col2 INT);
PRIMARY KEY
A PRIMARY KEY constraint is the combination of the NOT NULL constraint and the UNIQUE constraint.
The following sample code creates a table named tbl3 and specifies the PRIMARY KEY constraint for the col1 column.
obclient> CREATE TABLE tbl3(col1 INT PRIMARY KEY,col2 INT);
In the example, the col1 column must contain unique values and must not contain NULL values.
FOREIGN KEY
Values in the column constrained by a FOREIGN KEY constraint come from a primary key column of another table, which is referred to as a parent table.
When you create a FOREIGN KEY constraint, you can use the following keywords to specify the cascaded operation on the constrained column of the child table when a DELETE operation is performed on the referenced column of the parent table:
CASCADE: If the referenced column of the parent table is dropped, the corresponding constrained column of the child table is automatically dropped.SET NULL: If the referenced column of the parent table is dropped, values of the corresponding constrained column of the child table are set toNULL.If you specify the
SET NULLkeyword, the constrained column cannot be a generated column.
If you do not specify the ON DELETE clause when you create a FOREIGN KEY constraint, the operation to drop the referenced column of the parent table violates the FOREIGN KEY constraint and will be rejected.
The following sample code creates a table named tbl4 and specifies to associate the col2 column with the primary key column col1 of a table named tbl3.
obclient> CREATE TABLE tbl4(col1 INT,col2 INT,FOREIGN KEY(col2) REFERENCES tbl3(col1) ON DELETE SET NULL);
In this example, if the col1 column of the parent table tbl3 is dropped, the system sets the values of the FOREIGN KEY column of the child table to NULL.
CHECK
A CHECK constraint requires that the values of a column must meet specified conditions.
View constraints
You can check the constraints that you created by querying the ALL_CONS_COLUMNS, DBA_CONS_COLUMNS, and USER_CONS_COLUMNS views.
Here is an example:
obclient> SELECT * FROM USER_CONS_COLUMNS WHERE table_name='TBL3';
+-------+----------------------------+------------+-------------+----------+
| OWNER | CONSTRAINT_NAME | TABLE_NAME | COLUMN_NAME | POSITION |
+-------+----------------------------+------------+-------------+----------+
| SYS | TBL4_OBPK_1649992639477914 | TBL3 | COL1 | 1 |
+-------+----------------------------+------------+-------------+----------+
1 row in set
Manage constraints
Manage PRIMARY KEY constraints
After you create a table, you can add a PRIMARY KEY constraint to the table or modify a PRIMARY KEY constraint for the table.
Add a
PRIMARY KEYconstraint to the table.The SQL syntax is as follows:
obclient> ALTER TABLE table_name ADD PRIMARY KEY(column_name);We recommend that you add a
PRIMARY KEYconstraint to a table when you create the table.Modify a
PRIMARY KEYconstraint for the table.The SQL syntax is as follows:
obclient> ALTER TABLE table_name MODIFY PRIMARY KEY(column_name);Drop a
PRIMARY KEYconstraint from the table.The SQL syntax is as follows:
obclient> ALTER TABLE table_name DROP PRIMARY KEY;
Here is an example:
Create a table named
tbl4.obclient> CREATE TABLE tbl4(col1 INT,col2 INT);Add a
PRIMARY KEYconstraint to the table.obclient> ALTER TABLE tbl4 ADD PRIMARY KEY(col1);Change the column constrained by the
PRIMARY KEYconstraint tocol2for the table.obclient> ALTER TABLE tbl4 MODIFY PRIMARY KEY(col2);Drop a
PRIMARY KEYconstraint from the table.obclient> ALTER TABLE tbl4 DROP PRIMARY KEY;
Manage FOREIGN KEY constraints
After you create a table, you can manage FOREIGN KEY constraints for the table as follows:
Enable or disable a
FOREIGN KEYconstraint.By default, a constraint is enabled after it is created. If you have added a
FOREIGN KEYconstraint to a table when you created the table, you can enable or disable the constraint.The SQL syntax is as follows:
obclient> ALTER TABLE table_name ENABLE | DISABLE CONSTRAINT constraint_name;Add a
FOREIGN KEYconstraint to the table.The syntax is as follows:
obclient> ALTER TABLE table_name1 ADD CONSTRAINT fk_name FOREIGN KEY (column_name1) REFERENCES table_name2(column_name2);where
table_name1specifies the name of the table to which you want to add the constraint, andtable_name2specifies the name of the associated table.fk_namespecifies the name of theFOREIGN KEYconstraint to be added.column_name1specifies the name of the column to which the constraint applies, andcolumn_name2specifies the name of the primary key column in the associated table.
Drop a
FOREIGN KEYconstraint from the table.The syntax is as follows:
obclient> ALTER TABLE test DROP CONSTRAINT fk_name;
Here is an example:
Create a table named
tbl5and a table namedtbl6.obclient> CREATE TABLE tbl5(col1 INT PRIMARY KEY,col2 INT);obclient> CREATE TABLE tbl6(col3 INT,col4 INT);Add a
FOREIGN KEYconstraint to thetbl6table.obclient> ALTER TABLE tbl6 ADD CONSTRAINT fk FOREIGN KEY(col4) REFERENCES tbl5(col1);Drop the
FOREIGN KEYconstraint from thetbl6table.obclient> ALTER TABLE tbl6 DROP CONSTRAINT fk;
Manage CHECK constraints
After you create a table, you can manage CHECK constraints for the table as follows:
Enable or disable a
CHECKconstraint.If you have added a
CHECKconstraint to a table when you created the table, you can enable or disable the constraint.The SQL syntax is as follows:
obclient> ALTER TABLE table_name ENABLE | DISABLE CONSTRAINT constraint_name;Add a
CHECKconstraint to the table.The SQL syntax is as follows:
ALTER TABLE table_name ADD CONSTRAINT constraint_name CHECK(expression);Here is an example:
obclient> ALTER TABLE ware ADD CONSTRAINT cst CHECK(w_city='hz');
Manage NOT NULL constraints
After you create a table, you can add a NOT NULL constraint to the table or drop a NOT NULL constraint from the table.
Add a
NOT NULLconstraint to the table.The SQL syntax is as follows:
obclient> ALTER TABLE table_name MODIFY c1 NOT NULL;Drop a
NOT NULLconstraint from the table.The SQL syntax is as follows:
obclient> ALTER TABLE table_name DROP CONSTRAINT constraint_name;You can also use the following statement to drop a
NOT NULLconstraint:obclient> ALTER TABLE table_name MODIFY c1 NULL;
For more information about the syntax for managing constraints, see ALTER TABLE.
Default setting for a date or time column
We recommend that you specify a default value for a column with the NOT NULL constraint. For a date or time column, you can set the current date or time of the database as the default value.
Example: Call the current_timestamp function to set the default value for the datetime column of a table.
Create a table named
t1, and specify to use the current time of the database as the default values of thegmt_createandgmt_modifiedcolumns.obclient> CREATE TABLE t1( id INT NOT NULL PRIMARY KEY, gmt_create TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, gmt_modified TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP );Insert data into the table.
obclient> INSERT INTO t1(id) VALUES(1),(2),(3);View the data in the table.
obclient> SELECT * FROM t1;The query result is as follows:
+----+------------------------------+------------------------------+ | ID | GMT_CREATE | GMT_MODIFIED | +----+------------------------------+------------------------------+ | 1 | 10-NOV-23 10.12.36.417000 AM | 10-NOV-23 10.12.36.417000 AM | | 2 | 10-NOV-23 10.12.36.417000 AM | 10-NOV-23 10.12.36.417000 AM | | 3 | 10-NOV-23 10.12.36.417000 AM | 10-NOV-23 10.12.36.417000 AM | +----+------------------------------+------------------------------+ 3 rows in set