Aryan SuryaWansi
Aryan SuryaWansi

Reputation: 2741

SQL Server Does Not Delete Records

I am newbie to MSSQL Server and don't have any knowledge about it.

i have below question.

I have added nine records with same value as show per below image in SQL Server 2005.

i Have not given any primary key to Table.

Now when i selecting one record or multiple record and hit the delete key it does not delete the records from table instead it gives me error.

enter image description here

Upvotes: 1

Views: 3918

Answers (3)

CristiC
CristiC

Reputation: 22698

In MSSQL you need to have a primary key for the table. This will uniquely identify each row of that particular table.

For example in Oracle you don't need this as there you can use ROWID (meaning every row from every table has a unique ID in the database). Once you know this ID you Oracle knows for sure from which table it is.

So now you can add a primary key to the table and you can make it be auto-increment - ensuring uniqueness.

Upvotes: 2

Marek Musielak
Marek Musielak

Reputation: 27132

That's because you don't have any primary key and server doesn't know which row to remove. Clear the table ( DELETE * FROM dbo.Patient ) and create new Id column as a primary key.

Upvotes: 2

Edoardo Pirovano
Edoardo Pirovano

Reputation: 8334

You need to add a primary key to uniquely identify each record, otherwise the SQL server has no way of distinguishing the records, and therefore no way of knowing which one to delete, causing an error.

Upvotes: 6

Related Questions