Skip to main content

create foreign key only if it doesn't exits

  • January 30, 2015
  • 1 reply
  • 8 views

anand_1
Hello ,

is there any way to achieve the following in vertica?

alter table add constraint fk_table IF NOT EXISTS foreign key (col) references ....;


1 reply

anand_1
  • Author
  • New Participant
  • February 4, 2015
I was able to acheive this by using the following approach 

\set fk_sql  'vsql -t -c "SELECT decode(count(*),0,'alter table temp2 ADD CONSTRAINT FK_TEMP2    FOREIGN KEY (ID)  REFERENCES TEMP1(ID)',null) as Cnt FROM v_catalog.constraint_columns where table_name='temp2' and constraint_name='temp_fk' ;"'

 --- execute the sql stored in variable

 :fk_sql;