Composite key

Hello,
i have to addcomposite to an existing table .
The table already has primary key defined .

Any suggestions?

Thanks
RIASAsked:
Who is Participating?
 
Éric MoreauConnect With a Mentor Senior .Net ConsultantCommented:
can you do it using the designer in SSMS? if you can, just delete the PK from the UI.

otherwise, check your spelling.
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
So whats the question here, do you need the syntax ? check this link https://docs.microsoft.com/en-us/sql/t-sql/statements/create-index-transact-sql?view=sql-server-2017
0
 
Éric MoreauSenior .Net ConsultantCommented:
you want to replace your existing primary key with a new one?

drop your existing key:
ALTER TABLE myTable
DROP CONSTRAINT pk_myConstraint  

Open in new window


create your new key:
ALTER TABLE myTable
    ADD CONSTRAINT pk_myConstraint PRIMARY KEY (Column1,Column2)

Open in new window

0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
RIASAuthor Commented:
Eric,
Get an error

XYZ is not a constraint.
Msg 3727, Level 16, State 0, Line 4
Could not drop constraint. See previous errors.
0
 
RIASAuthor Commented:
Tried it but no luck
0
 
Éric MoreauSenior .Net ConsultantCommented:
why no luck? might not have enough privileges?
0
 
RIASAuthor Commented:
Sorry Eric, Yes it worked !
0
 
RIASAuthor Commented:
NNow this:
Msg 8111, Level 16, State 1, Line 3
Cannot define PRIMARY KEY constraint on nullable column in table 'TABLE1'.
Msg 1750, Level 16, State 0, Line 3
Could not create constraint or index. See previous errors.
0
 
Éric MoreauSenior .Net ConsultantCommented:
As the message says: one of your column is nullable. A primary key cannot be created on nullable columns.
0
 
RIASAuthor Commented:
Thanks Eric!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.