How to set a Primary key to an existing table on Oracle?

Asked

Viewed 2,076 times

2

I usually do it directly from the IDE (Sql Developer), but I need to run the script this time.

Assuming the existing table TB_CR_INDICADOR_PERIODO and the column COD_CR_INDICADOR_PERIODO as the single identification column, how could I set this Primary key?

1 answer

4


The correct command to add to Primary key would be:

ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n);

Applying to your case would be:

ALTER TABLE TB_CR_INDICADOR_PERIODO ADD CONSTRAINT pk_tb_cr_indicador_periodo PRIMARY KEY (COD_CR_INDICADOR_PERIODO);

Error may occur if the table already has some information.

Source:
http://www.techonthenet.com/oracle/primary_keys.php

  • 1

    select COD_CR_INDICADOR_PERIODO from TB_CR_INDICADOR_PERIODO group by Cod_cr_indicador_periodohaving Count(*) > 1 will show if there are any duplicates in this key.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.