How do I add a column to a primary key?

Moderator: NorbertKrupa

Post Reply
User avatar
heather
Newbie
Newbie
Posts: 22
Joined: Fri Dec 28, 2012 3:01 pm

How do I add a column to a primary key?

Post by heather » Wed Jun 18, 2014 12:00 pm

Hi!

Can I add a column to a primary key, or will I have to drop the current primary key and add a new one?

NorbertKrupa
GURU
GURU
Posts: 527
Joined: Tue Oct 22, 2013 9:36 pm
Location: Chicago, IL
Contact:

Re: How do I add a column to a primary key?

Post by NorbertKrupa » Wed Jun 18, 2014 5:19 pm

You will have to drop and recreate it. Also remember that all columns participating in a primary key must be NOT NULL and will be converted if they are not:

Code: Select all

dbadmin=> CREATE TABLE public.test (
   a int PRIMARY KEY,
   b int NULL
);

dbadmin=> ALTER TABLE public.test ADD PRIMARY KEY (a, b);
WARNING 2623:  Column "b" definition changed to NOT NULL
ROLLBACK 4413:  Primary constraint for relation "test" already exists

dbadmin=> ALTER TABLE public.test DROP CONSTRAINT C_PRIMARY;

dbadmin=> ALTER TABLE public.test ADD PRIMARY KEY (a, b);
WARNING 2623:  Column "b" definition changed to NOT NULL
You can find the constraint name to drop in v_catalog.table_constraints (default is C_PRIMARY).
Checkout vertica.tips for more Vertica resources.

User avatar
heather
Newbie
Newbie
Posts: 22
Joined: Fri Dec 28, 2012 3:01 pm

Re: How do I add a column to a primary key?

Post by heather » Thu Jun 19, 2014 2:54 pm

Ok, that's what I figured. Thank you for writing back. Oh, and good to know about the not null constraint added :!:

Post Reply

Return to “New to Vertica Database Administration”