views:

30

answers:

1

I have table with following details

Table name EMPLOYEE and columns

EMPID (PK smallint not null)
EMPNAME (varchar 256 not null)
ORG (FK smallint not null)
FUNCTION (FK smallint not null)
EFF_DATE (datetime null)
AUDIT_ID (varchar null)

Now I have to add an extra coulmn to this table ADD_UID and make it also primary key

I am using this query but failing.

ALTER TABLE CVADMIN.EMPLOYEE
 ADD ADD_UID  varchar(32) NULL,
 CONSTRAINT PK_EMPLOYEE PRIMARY KEY [NON]CLUSTERED (ADD_UID)
go

Table 'EMPLOYEE' already has a primary key defined on it.

EDIT

The idea here is the new column should be unique so that if it fails I can throw _KEY_VIOLATION so that some code manipulation is done

+4  A: 

To add a unique constraint (which is additional to the primary key) do this:

ALTER TABLE EMPLOYEE ADD CONSTRAINT uc_UID UNIQUE (ADD_UID)
nickd
Thanks this works !!! I have a question here.If I want to design the DB for future such that if I add another unique constraint over a new column .How do I distinguish which is seeking unique from JDBC perspective
GustlyWind
I'm not sure I understand your question, but in the example above uc_UID is the name of the constraint and given that it will appear in any exception thrown, you could use that to derive the field that is incorrect.
nickd