This topic describes simple primary key operations in Oracle mode of OceanBase Database, including adding, modifying, and dropping primary keys.
Add a primary key
The syntax for adding a primary key is as follows:
ALTER TABLE table_name ADD [CONSTRAINT constraint_name] PRIMARY KEY (column_name);
The parameters are described as follows:
table_name: the name of the table to which the primary key is to be added.constraint_name: the name of the constraint to be added.column_name: the name of a column in the table.
For more information, see ALTER TABLE.
Assume that the tbl1 table already exists in the database. Define the c1 column in the tbl1 table as the primary key.
obclient> ALTER TABLE tbl1 ADD PRIMARY KEY (c1);
Modify a primary key
The syntax for modifying a primary key is as follows:
ALTER TABLE table_name MODIFY PRIMARY KEY (column_name);
table_name specifies the name of the table to which the primary key belongs, and column_name specifies the primary key column. For more information, see ALTER TABLE.
Assume that the tbl1 table already exists in the database and the tbl1 table has a primary key. The following sample statement modifies the primary key in the tbl1 table and applies it to the c2 column:
obclient> ALTER TABLE tbl1 MODIFY PRIMARY KEY (c2);
Drop a primary key
The syntax for dropping a primary key is as follows:
ALTER TABLE table_name DROP PRIMARY KEY;
table_name specifies the name of the table to which the primary key belongs. For more information, see ALTER TABLE.
Assume that the tbl1 table exists in the database and the tbl1 table has a primary key. The following sample statement drops the primary key from the tbl1 table:
obclient> ALTER TABLE tbl1 DROP PRIMARY KEY;