CREATE TABLE table_primary_key_1 ( id NUMBER(1,0) , CONSTRAINT cp_table_primary_key_1_id PRIMARY KEY (id) -- PRIMARY KEY );Note: If the primary key is a composite key of multiple columns, you must specify those columns separated by commas (column1, column2, …).
SELECT constraint_name, constraint_type, table_name FROM user_constraints WHERE table_name = 'TABLE_PRIMARY_KEY_1';Query result:
| CONSTRAINT_NAME | CONSTRAINT_TYPE | TABLE_NAME | +---------------------------+-----------------+---------------------+ | CP_TABLE_PRIMARY_KEY_1_ID | P | TABLE_PRIMARY_KEY_1 |
SELECT index_name, index_type, uniqueness FROM user_indexes WHERE table_name = 'TABLE_PRIMARY_KEY_1';Query result:
| INDEX_NAME | INDEX_TYPE | UNIQUENESS | +---------------------------+------------+------------+ | CP_TABLE_PRIMARY_KEY_1_ID | NORMAL | UNIQUE |
DROP INDEX cp_table_primary_key_1_id;Query result:
Error starting at line 1 in command: DROP INDEX CP_TABLE_PRIMARY_KEY_1_ID Error report: SQL Error: ORA-02429: cannot drop index used for enforcement of unique/primary key. *Cause: user attempted to drop an index that is being used as the enforcement mechanism for unique or primary key. *Action: drop the constraint instead of the index.
ALTER TABLE table_primary_key_1 DROP CONSTRAINT cp_table_primary_key_1_id;Check the existence of the unique index in the data dictionary:
SELECT index_name FROM user_indexes WHERE table_name = 'TABLE_PRIMARY_KEY_1';Query result:
| INDEX_NAME | +------------+
CREATE TABLE table_primary_key_2 ( id NUMBER(1,0) , PRIMARY KEY (id) -- PRIMARY KEY );The difference from the previous syntax is that no explicit name is given to the primary key constraint.
SELECT constraint_name, constraint_type, table_name FROM user_constraints WHERE table_name = 'TABLE_PRIMARY_KEY_2';Query result:
| CONSTRAINT_NAME | CONSTRAINT_TYPE | TABLE_NAME | +-----------------+-----------------+---------------------+ | SYS_C007205 | P | TABLE_PRIMARY_KEY_2 |
SELECT index_name, index_type, uniqueness FROM user_indexes WHERE table_name = 'TABLE_PRIMARY_KEY_2';Query result:
| INDEX_NAME | INDEX_TYPE | UNIQUENESS | +---------------+------------+------------+ | SYS_C007205 | NORMAL | UNIQUE |
CREATE TABLE table_primary_key_3 ( id NUMBER(1,0) );Specify an explicit name for the constraint:
ALTER TABLE table_primary_key_3 ADD CONSTRAINT cp_table_primary_key_3_id PRIMARY KEY (id); -- PRIMARY KEYOr allow Oracle to generate a system name:
ALTER TABLE table_primary_key_3 ADD PRIMARY KEY (id); -- PRIMARY KEYNote: Before adding a primary key constraint to an existing table, ensure that the target column(s) contain no duplicate values and no NULL values, otherwise the operation will fail.
CREATE TABLE table_primary_key_4 ( id NUMBER(1,0) );
CREATE INDEX in_table_primary_key_4_id ON table_primary_key_4 (id);
ALTER TABLE table_primary_key_4 ADD CONSTRAINT cp_table_primary_key_4_id PRIMARY KEY (id) USING INDEX in_table_primary_key_4_id; -- PRIMARY KEYNote that the primary key can use a non-unique index. Oracle will automatically make it function as a unique index for constraint enforcement.
DROP INDEX in_table_primary_key_4_id;
Error starting at line 1 in command: DROP INDEX IN_TABLE_PRIMARY_KEY_4_ID Error report: SQL Error: ORA-02429: cannot drop index used for enforcement of unique/primary key. *Cause: user attempted to drop an index that is being used as the enforcement mechanism for unique or primary key. *Action: drop the constraint instead of the index.
ALTER TABLE table_primary_key_4 DROP PRIMARY KEY;Check the existence of the index in the data dictionary:
SELECT index_name FROM user_indexes WHERE table_name = 'TABLE_PRIMARY_KEY_4';Query result:
| INDEX_NAME | +---------------------------+ | IN_TABLE_PRIMARY_KEY_4_ID |This behavior allows you to maintain the index for query performance even after removing the primary key constraint.
ALTER TABLE table_primary_key_1 DISABLE CONSTRAINT cp_table_primary_key_1_id;Important: When a primary key constraint is disabled, Oracle does not enforce uniqueness or NOT NULL restrictions, but the underlying index may still exist depending on how the constraint was created. Disabling constraints should be done carefully as it can lead to data integrity issues.
ALTER TABLE table_primary_key_1 ENABLE CONSTRAINT cp_table_primary_key_1_id;Note: If the table contains data that violates the primary key constraint (duplicate values or NULL values in primary key columns), the enable operation will fail. You must clean up the data before enabling the constraint.
ALTER TABLE table_primary_key_1 DROP PRIMARY KEY;Or specify the constraint name explicitly:
ALTER TABLE table_primary_key_1 DROP CONSTRAINT cp_table_primary_key_1_id;Reminder: Dropping the primary key constraint also drops the associated index, except when the index was created separately before adding the constraint. This can significantly impact query performance if the index was being used by other queries.
CREATE TABLE table_primary_key_5 ( id1 NUMBER(1,0) , id2 NUMBER(1,0) , CONSTRAINT cp_table_primary_key_5_id PRIMARY KEY (id1, id2) -- PRIMARY KEY );
INSERT INTO table_primary_key_5 (id1, id2) VALUES (1, NULL);Query result:
Error starting at line 1 in command: INSERT INTO TABLE_PRIMARY_KEY_5 (ID1, ID2) VALUES (1, NULL) Error report: SQL Error: ORA-01400: cannot insert NULL into ("TABLE_PRIMARY_KEY_5"."ID2") *Cause: *Action:
INSERT INTO table_primary_key_5 (id1, id2) VALUES (NULL, 1);Query result:
Error starting at line 1 in command: INSERT INTO TABLE_PRIMARY_KEY_5 (ID1, ID2) VALUES (NULL, 1); Error report: SQL Error: ORA-01400: cannot insert NULL into ("TABLE_PRIMARY_KEY_5"."ID1") *Cause: *Action:
INSERT INTO table_primary_key_5 (id1, id2) VALUES (NULL, NULL);Query result:
Error starting at line 1 in command: INSERT INTO TABLE_PRIMARY_KEY_5 (ID1, ID2) VALUES (NULL, NULL); Error report: SQL Error: ORA-01400: cannot insert NULL into ("TABLE_PRIMARY_KEY_5"."ID1") *Cause: *Action:
CREATE TABLE table_pk_vs_unique_index ( id1 NUMBER(1,0) , id2 NUMBER(1,0) );
CREATE UNIQUE INDEX in_table_pk_vs_unique_index ON table_pk_vs_unique_index (id1, id2);
INSERT INTO table_pk_vs_unique_index (id1, id2) VALUES (1, NULL); -- OK INSERT INTO table_pk_vs_unique_index (id1, id2) VALUES (1, NULL); -- ERRORQuery result:
1 rows inserted. Error starting at line 2 in command: INSERT INTO TABLE_PK_VS_UNIQUE_INDEX (ID1, ID2) VALUES (1, NULL) Error report: SQL Error: ORA-00001: unique constraint (HR.IN_TABLE_PK_VS_UNIQUE_INDEX) violated *Cause: An UPDATE or INSERT statement attempted to insert a duplicate key. For Trusted Oracle configured in DBMS MAC mode, you may see this message if a duplicate entry exists at a different level. *Action: Either remove the unique restriction or do not insert the key.
INSERT INTO table_pk_vs_unique_index (id1, id2) VALUES (NULL, 1); -- OK INSERT INTO table_pk_vs_unique_index (id1, id2) VALUES (NULL, 1); -- ERRORQuery result:
1 rows inserted. Error starting at line 2 in command: INSERT INTO TABLE_PK_VS_UNIQUE_INDEX (ID1, ID2) VALUES (NULL, 1) Error report: SQL Error: ORA-00001: unique constraint (HR.IN_TABLE_PK_VS_UNIQUE_INDEX) violated *Cause: An UPDATE or INSERT statement attempted to insert a duplicate key. For Trusted Oracle configured in DBMS MAC mode, you may see this message if a duplicate entry exists at a different level. *Action: Either remove the unique restriction or do not insert the key.
INSERT INTO table_pk_vs_unique_index (id1, id2) VALUES (NULL, NULL); -- OK INSERT INTO table_pk_vs_unique_index (id1, id2) VALUES (NULL, NULL); -- OK!Query result:
1 rows inserted. 1 rows inserted.In this last example, both inserts were successful. It's important to remember that a standard (B*Tree) index does not index NULL values when all indexed columns are NULL. Since the value of both columns composing the index is
NULL
, these rows are not indexed and therefore do not violate the uniqueness constraint.