Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hey guys !
i have a problem with my tables!
i forgot to select my primary key when i was making it !
i saved the table and after several minutes, i remember that i didnt choose primary key !
well what can i do? i dont want make table again ! its a big table! please help !

tnx...
Posted

Try this:
SQL
ALTER TABLE Production.TransactionHistoryArchive 
ADD CONSTRAINT PK_TransactionHistoryArchive_TransactionID PRIMARY KEY CLUSTERED (TransactionID);

From: http://msdn.microsoft.com/en-us/library/ms189039.aspx[^]
Note: Before using the ALTER TABLE command to add a primary key, you'll need to make sure that the field is defined as 'NOT NULL' -- in other words, NULL cannot be an accepted value for that field.

When a PRIMARY KEY constraint is added to an existing column or columns in the table, Microsoft SQL Server checks the existing data in the columns to ensure that the existing data follows the rules for primary keys:
-No null values
-No duplicate values

If a PRIMARY KEY constraint is added to a column that has duplicate or null values, SQL Server returns an error and does not add the constraint.

In addition have a look on:
ALTER TABLE (Transact-SQL)[^]
To know how to change the length, precision, or scale of a column by specifying a new size for the column data type in the ALTER COLUMN clause
 
Share this answer
 
Comments
BillW33 18-Jul-12 11:18am    
Very nice answer, +5 :)
Prasad_Kulkarni 18-Jul-12 23:45pm    
Thank you
You can alter your table using alter command as follows.
SQL
ALTER TABLE table_name
ADD PRIMARY KEY (column_name)


Hope this will solve your problem.
 
Share this answer
 
I googled for 'SQL Set Primary Key'[^] (you don't say what flavour of SQL)

You might want to google for 'SQL Backup Command'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900