create primary key on existing table without primary key in SQL Server

WHZW

I am trying to assign an existing column to be the primary key(and another in different table as foreign key) in a table without primary key. I tried this: Altering a column: null to not null, and it didn't work. The column contains number, text and null values.

Could someone create a step-by-step guide and share with everyone here?

This is the error message:

Msg 8152, Level 16, State 13, Line 2
String or binary data would be truncated.

It would great if you can help interpret the error message.

Thanks!

Greg the Incredulous

To add a primary key to an existing table you can do this:

ALTER TABLE yourTableName ADD yourColumnName INT NOT NULL PRIMARY KEY IDENTITY(1, 1)

which will add a new column and automatically populate it with unique integer values starting from one. To add a foreign key to another table use similar syntax to above but don't make the column NOT NULL until you've worked out how to link it to your existing primary key - which is a whole different question.

ALTER TABLE yourOtherTable ADD yourFKColumnName INT WITH CONSTRAINT [FK_SensibleForeignKeyName] FOREIGN KEY ([yourFKColumnName]) REFERENCES yourTableName([yourColumnName])

I haven't tested it but that should be pretty close to what you need.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

C# Entity Framework Requires Primary Key to be Defined Error with Existing SQL Server Table with Identity Primary Key

SQL Server add auto increment primary key to existing table

Getting error when adding primary key to existing table Sql server

Import CSV without primary key to existing table

SQL Delete duplicate rows in the table without primary key on SQL Server

Table without a primary key

If I create a table without a primary key then add a primary key, is the primary key a clustered index?

Is it possible to create a table in the Room without a primary key?

Could not create constraint or index or foreign key on primary key of existing table

Add primary key to an existing table

C# Entity Framework Core requires primary key to be defined error with existing SQL Server table with identity primary key

Altering an existing column to 'Primary Key' and 'NOT NULL' without dropping the table

Create a table with a varchar as primary key

SQL Server create primary key constraint duplicate key error

SQL - Concatenation without primary key

Creating and populating a primary key column in an existing table using SQL

How can I import Excel data into an existing table in SQL Server and keep primary key incrementing accordingly

Need my EF core to see already existing primary key in my SQL server table and work with it

Add auto increment with labeled primary key to existing table in SQL Server and H2 / Liquibase

SQL Server - Remove duplicate rows and maintain existing primary key

SQL Server: Primary Key Conflict

Duplicate primary key in SQL Server

create a foreign key on a primary key of another table

How to add a primary key to a mysql table without a primary key

How to create table without primary key in Entity Framework Core?

It's possible, create a table without primary key in TypeOrm?

Insert auto increment primary key to existing table

Postgres: change primary key in existing table

Add PRIMARY KEY constraint to existing table