This topic describes simple primary key operations in the MySQL 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 PRIMARY KEY (column_name);
Here, table_name specifies the name of the table to which the primary key is to be added, and column_name specifies the primary key column. For more information, see ALTER TABLE.
Assume that the tbl1 table already exists in the database. The following sample statement adds a primary key index to the tbl1 table and applies the index to the c1 column:
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 DROP PRIMARY KEY, ADD PRIMARY KEY (column_name);
Here, 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 DROP PRIMARY KEY, ADD PRIMARY KEY(c2);
Drop a primary key
The syntax for dropping a primary key is as follows:
ALTER TABLE table_name DROP PRIMARY KEY;
Here, 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;