Add column in a PK

DBA Geral (instalação, configuração do banco, scripts do Sql*Plus, shell scripts, batch scripts, etc...)
Post Reply
lobo
Rank: Programador Júnior
Rank: Programador Júnior
Posts: 19
Joined: Tue, 09 May 2006 2:04 pm
Location: Suzano
Valeu!!!!

Hello Friends of Oracle

I would like to know how to add a column in a PK.
If someone can help me thank you.

ATT,

Dan
gilbertoca
Rank: DBA Sênior
Rank: DBA Sênior
Posts: 372
Joined: Tue, 24 Jan 2006 3:33 pm
Location: Palmas - TO
Contact:

If you have created your table without restriction, that is, without the primary key, do the following:

Select all

 
CREATE TABLE CHAVE( 
  CHAVE_PRIMARIA NUMBER NOT NULL, 
  DESCRICAO VARCHAR2(60) 
); 
 
ALTER TABLE CHAVE ADD CONSTRAINT CHAVE_PK PRIMARY KEY (CHAVE_PRIMARIA); 

or if the table already has the primary key but you want to add a column in the primary key, You need to delete the restriction first and then rebuild it:

Select all

 
CREATE TABLE CHAVE( 
  CHAVE_PRIMARIA NUMBER NOT NULL, 
  CHAVE_SECUNDARIA VARCHAR2(5) NOT NULL, 
  DESCRICAO VARCHAR2(60), 
  CONSTRAINT CHAVE_PK PRIMARY KEY (CHAVE_PRIMARIA ) )   
); 
 
ALTER TABLE CHAVE DROP CONSTRAINT CHAVE_PK; 
ALTER TABLE CHAVE ADD CONSTRAINT CHAVE_PK PRIMARY KEY (CHAVE_PRIMARIA, CHAVE_SECUNDARIA); 
CAUTION! I did not run this script! You better test the syntax first.
until
Gilberto
Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests