Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 implement primary key by sql code

Author  Topic 

mavericky
Posting Yak Master

117 Posts

Posted - 2011-11-08 : 18:22:51
Hi,
I have a table with the fields:
StartTime
Type
Zone
KWh
and data-

StartTime Zone Type KWh
2011-09-01 00:15:00.000 MVEC A 5305.0957
2011-09-01 00:30:00.000 MVEC A 5067.29390

I have 100 rows with different Zone values.

I want to make Zone and StartTime as PrimaryKeys by sql code. Can it be done, even though i already inserted all data?

Thanks,
Mavericky

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-11-08 : 18:32:29
Sure.

ALTER TABLE <table name> ADD CONSTRAINT <constraint name> PRIMARY KEY (<column names>)

--
Gail Shaw
SQL Server MVP
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-09 : 01:05:51
one thing to note is because you already have data, there should not any duplicate combination of zone,startime present else pk will fail. so check for and remove duplicates before applying the pk

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -