Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add primary key to a table with many records

Tags:

I have a table in SQL Server 2005 containing 10000054 records; these records are inserted through a bulk insert operation. The table does not contain a primary key and I want to have one. If I try to modify the table's structure, adding a new column, PK, set as int with isidentity, the management console gives me a warning:

"Changes to tables with large amounts of data could take a considerable amount of time. While changes are being saved, table data will not be accessible."

then outputs error:

" Unable to modify table. Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. "

I want a PK into my table.

How can I add it?

like image 826
lmsasu Avatar asked Feb 08 '10 10:02

lmsasu


People also ask

Can a primary key have multiple records?

If a table has a primary key defined on any field(s), then you cannot have two records having the same value of that field(s). Note − You would use these concepts while creating database tables.

Can we add primary key existing table with data?

Because a table can have only one primary key, you cannot add a primary key to a table that already has a primary key defined. To change the primary key of a table, delete the existing key using a DROP clause in an ALTER TABLE statement and add the new primary key.

Do many to many tables need a primary key?

Many-to-many relationshipsThe primary key of the junction table consists of the foreign keys from both table A and table B. For example, the "Authors" table and the "Titles" table have a many-to-many relationship that is defined by a one-to-many relationship from each of these tables to the "TitleAuthors" table.

How do you add a primary key in multiple columns?

We can set PRIMARY KEY constraint on multiple columns of an existing table by using ADD keyword along with ALTER TABLE statement.

How to add a primary key to an existing table?

You can use the ALTER statement to create a primary key. However, the primary key can only be created on columns that are defined as NOT NULL. You cannot create a primary key on a column that allows NULLs. If you need to do, you have to drop and recreate the table. We have added a primary key constraint to an already existing table.

How many primary keys can a table have in SQL?

A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields). The following SQL creates a PRIMARY KEY on the "ID" column when the "Persons" table is created:

How to create a primary key in access?

Create Primary Key in Access 1 In Object Explorer, right-click the table to which you want to add a unique constraint, and click Design. 2 In Table Designer, click the row selector for the database column you want to define as the primary key. ... 3 Right-click the row selector for the column and select Set Primary Key. See More....

How to create a table with course id as a primary key?

Let's create a Table with One Column as a SQL Primary Key. Step 1) Run the Query by clicking on 'Execute.' Result: Course_Id is now a Primary Key. Example: Let's see if it allows entering Multiple Records with Same Course ID. Step 2) Verify all Data Inserted successfully by running the Select query.


1 Answers

If in Management Studio you set the primary key in Design view (without saving), when you next right click you have an option "Generate Change Script" - this option is also available on the "Table Designer" menu at the top.

That provides the raw SQL (safely wrapped in a transaction) which you can copy to clipboard, take that over to run as a New Query (button top left, or File > New > Query with Current Connection), paste it in, select the right DB and execute the query.

like image 93
Chris Avatar answered Sep 28 '22 03:09

Chris