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
OceanBase Database supports the following types of constraints:
NOT NULLconstraint: requires that the constrained column must not containNULLvalues.For a column with the
NOT NULLconstraint, you must specify the values in theINSERTstatement unless you have defined a non-null default value for the column.For example, create a table named
tbl1and specify theNOT NULLconstraint for thecol1column.obclient> CREATE TABLE tbl1(col1 INT NOT NULL,col2 INT); Query OK, 0 rows affected obclient> DESC tbl1; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | col1 | int(11) | NO | | NULL | | | col2 | int(11) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in setUNIQUEconstraint: requires that the constrained column must not contain duplicate values.NULLvalues are not prohibited by this constraint type.For example, create a table named
tbl6and specify theUNIQUEconstraint for thecol2column.obclient> CREATE TABLE tbl6(col1 INT UNIQUE,col2 INT); Query OK, 0 rows affectedPRIMARY KEYconstraint: It is the combination of theNOT NULLconstraint and theUNIQUEconstraint.For example, create a table named
tbl2and specify thePRIMARY KEYconstraint for thecol1column.obclient> CREATE TABLE tbl2(col1 INT PRIMARY KEY,col2 INT); Query OK, 0 rows affected obclient> DESC tbl2; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | col1 | int(11) | NO | PRI | NULL | | | col2 | int(11) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in setIn the example, the
tbl2table contains a primary key columncol1. This column must contain unique values and must not containNULLvalues.FOREIGN KEYconstraint: requires that the constrained column must contain only values from the primary key column of another table.For example, create a
FOREIGN KEYconstraint.obclient> CREATE TABLE tbl4(col1 INT,col2 INT,FOREIGN KEY(col2) REFERENCES tbl2(col1)); Query OK, 0 rows affectedIn the example, the
col2column in thetbl4table is associated with the primary key columncol1in thetbl2table.If you do not specify the foreign key name when you create a
FOREIGN KEYconstraint, the system automatically assigns a constraint name in thetable name_OBFK_creation timestampformat, such ast1_OBFK_1627747200000000.By default, OceanBase Database checks foreign keys. To disable or enable foreign key check, modify the
foreign_key_checkstenant variable.For more information about the
foreign_key_checksvariable, see foreign_key_checks.CHECKconstraint: enforces that the values of a column in the table must conform to specified conditions.You can define one or more
CHECKconstraints for a single column so that only specified values are allowed for the column. You can also define table-levelCHECKconstraints to apply the constraints to multiple columns. When you modify a table name, theCHECKconstraint name remains unchanged. When you drop a table, theCHECKconstraints applied to the table are also dropped.For example, create a table named
tbl8and set a constraint where the value of thecol1column must be greater than0.obclient> CREATE TABLE tbl8(col1 INT CHECK(col1>0),col2 INT); Query OK, 0 rows affectedIf you do not specify the constraint name when you create a
CHECKconstraint, the system automatically assigns a constraint name in thetable name_OBCHECK_creation timestampformat, such ast1_OBCHECK_1629350823880271.
For more information about the syntax for creating constraints, see CREATE TABLE.
View constraints
You can check the constraints that you created by querying the information_schema.TABLE_CONSTRAINTS view. Here is an example:
obclient> SELECT * FROM information_schema.TABLE_CONSTRAINTS WHERE TABLE_NAME='tbl6';
+--------------------+-------------------+-----------------+--------------+------------+-----------------+
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | TABLE_SCHEMA | TABLE_NAME | CONSTRAINT_TYPE |
+--------------------+-------------------+-----------------+--------------+------------+-----------------+
| def | test | col1 | test | tbl6 | UNIQUE |
+--------------------+-------------------+-----------------+--------------+------------+-----------------+
1 row in set
Manage constraints
You can manage constraints in OceanBase Database.
Manage PRIMARY KEY constraints
You can add a PRIMARY KEY constraint to a table or drop a PRIMARY KEY constraint from a table.
The SQL syntax for adding a PRIMARY KEY constraint is as follows:
ALTER TABLE table_name ADD PRIMARY KEY (column_name);
The SQL syntax for dropping a PRIMARY KEY constraint is as follows:
ALTER TABLE table_name DROP PRIMARY KEY;
Here is an example:
Create a table named
tbl2.obclient> CREATE TABLE tbl2(col1 INT,col2 INT);Add a
PRIMARY KEYconstraint to thetbl2table.obclient> ALTER TABLE tbl2 ADD PRIMARY KEY (col1);Drop the
PRIMARY KEYconstraint from thetbl2table.obclient> ALTER TABLE tbl2 DROP PRIMARY KEY;
Manage FOREIGN KEY constraints
You can add a FOREIGN KEY constraint to a table or drop a FOREIGN KEY constraint from a table.
Here is an example:
Create a table named
tbl5and add aFOREIGN KEYconstraint to thecol2column in thetbl5table. The associated primary table istbl2.obclient> CREATE TABLE tbl5(col1 INT,col2 INT); Query OK, 0 rows affected obclient> ALTER TABLE tbl5 ADD CONSTRAINT tbl5_fk1 FOREIGN KEY(col2) REFERENCES tbl2(col1); Query OK, 0 rows affectedDrop the
FOREIGN KEYconstraint on thecol1column from thetbl5table.obclient> SELECT TABLE_SCHEMA database name,TABLE_NAME table name,COLUMN_NAME column name,CONSTRAINT_NAME constraint name FROM information_schema.KEY_COLUMN_USAGE; +--------------+--------+--------+----------------------------+ | Database name | Table name | Column name | Constraint name | +--------------+--------+--------+----------------------------+ | test | tbl2 | col1 | PRIMARY | | test | tbl3 | col1 | PRIMARY | | test | tbl3 | col2 | PRIMARY | | test | tbl4 | col2 | tbl4_OBFK_1645173651329873 | | test | tbl5 | col1 | tbl5_OBFK_1645174931376110 | | test | tbl5 | col2 | tbl5_fk1 | +--------------+--------+--------+----------------------------+ 6 rows in set obclient> ALTER TABLE tbl5 DROP FOREIGN KEY tbl5_OBFK_1645174931376110; Query OK, 0 rows affected
Manage UNIQUE constraints
You can add UNIQUE constraints to a table.
For example, create a table named tbl7 and add a UNIQUE constraint to the col2 column in the tbl7 table.
obclient> CREATE TABLE tbl7(col1 INT,col2 INT);
Query OK, 0 rows affected
obclient> ALTER TABLE tbl7 ADD UNIQUE(col2);
Query OK, 0 rows affected
Manage NOT NULL constraints
You can change the attribute of a column in a table to NOT NULL.
For example, create a table named tbl9 and change the attribute of a column in the table to NOT NULL.
obclient> CREATE TABLE tbl9(c1 int);
Query OK, 0 rows affected
obclient> ALTER TABLE tbl9 MODIFY c1 int NOT NULL;
Query OK, 0 rows affected
Notice
In OceanBase Database, NOT NULL is a column attribute. Therefore, you cannot check the NOT NULL attribute of columns in a table by querying the information_schema.TABLE_CONSTRAINTS view.
Manage CHECK constraints
You can add a CHECK constraint to a table and modify or drop a CHECK constraint from a table.
Here is an example:
Add a
CHECKconstraint to thecol2column in thetbl8table.obclient> ALTER TABLE tbl8 ADD CONSTRAINT tbl8_c1 CHECK(col2>0);Modify the
CHECKconstraint for the table.After you create a
CHECKconstraint, it is enabled by default. If you want to invalidate the constraint, you can disable it.Disable a
CHECKconstraint.obclient> ALTER TABLE tbl8 ALTER CHECK tbl8_c1 NOT ENFORCED;Enable a
CHECKconstraint.obclient> ALTER TABLE tbl8 ALTER CHECK tbl8_c1 ENFORCED;
Drop the
CHECKconstraint.obclient> ALTER TABLE tbl8 DROP CHECK tbl8_c1;
For more information about the syntax for modifying 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.
- For example, call the
current_timestampfunction to set the default value for the datetime column of a table.
obclient> CREATE TABLE t1(
id bigint NOT NULL PRIMARY KEY
, gmt_create datetime NOT NULL default current_timestamp
, gmt_modified datetime NOT NULL default current_timestamp
);
Query OK, 0 rows affected
obclient> INSERT INTO t1(id) VALUES(1),(2),(3);
Query OK, 3 rows affected
obclient> SELECT * FROM t1;
+----+---------------------+---------------------+
| id | gmt_create | gmt_modified |
+----+---------------------+---------------------+
| 1 | 2020-02-27 17:09:23 | 2020-02-27 17:09:23 |
| 2 | 2020-02-27 17:09:23 | 2020-02-27 17:09:23 |
| 3 | 2020-02-27 17:09:23 | 2020-02-27 17:09:23 |
+----+---------------------+---------------------+
3 rows in set
References
For more information about FOREIGN KEY constraints, see FOREIGN KEY constraints.