This topic describes how to add, modify, and drop a primary key in OceanBase Database in Oracle mode.
Add a primary key
The syntax is as follows:
ALTER TABLE table_name ADD [CONSTRAINT constraint_name] PRIMARY KEY (column_name);
The following table describes the parameters of the syntax.
table_name: the name of the table to which you want to add a primary key.constraint_name: the name of the constraint to be added.column_name: the name of the column to which you want to add a primary key.
For more information, see ALTER TABLE.
Assume that a table named tbl1 exists in the database. The following example adds a primary key to the c1 column in the tbl1 table:
obclient> ALTER TABLE tbl1 ADD PRIMARY KEY (c1);
Modify a primary key
The syntax is as follows:
ALTER TABLE table_name MODIFY PRIMARY KEY (column_name);
In the syntax, table_name specifies the name of the table to which you want to add a primary key, and column_name specifies the name of the column to which you want to add a primary key. For more information, see ALTER TABLE.
Assume that a table named tbl1 exists in the database and that the table has a primary key. The following example modifies the primary key of the tbl1 table and applies it to the c2 column:
obclient> ALTER TABLE tbl1 MODIFY PRIMARY KEY (c2);
Drop a primary key
The syntax is as follows:
ALTER TABLE table_name DROP PRIMARY KEY;
In the syntax, table_name specifies the name of the table to which you want to add a primary key. For more information, see ALTER TABLE.
Assume that a table named tbl1 exists in the database and that the table has a primary key. The following example drops the primary key of the tbl1 table:
obclient> ALTER TABLE tbl1 DROP PRIMARY KEY;
