1

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

1
  • 4
    First step: add a primary key
    – marc_s
    Jul 4, 2011 at 11:05

3 Answers 3

6

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.

4
  • Thanks for replying .......... Is there not any button or way other then query which allow us to Delete entire data's of table. or clearing the content of table??? Jul 4, 2011 at 11:15
  • As mentioned in one of the other answers, DELETE * FROM dbo.Patient will clear the table for you. Then, you can add a primary key and start filling the table with data again. Jul 4, 2011 at 11:22
  • You mean DELETE FROM dbo.Patient without the *? Or alternatively TRUNCATE TABLE dbo.Patient.
    – MatBailie
    Jul 4, 2011 at 12:35
  • Yeah, TRUNCATE TABLE dbo.Patient would work too, and it doesn't matter if the * is included or not - the query will either way. You're right, though, the * isn't required, so it's perfectly acceptable to leave it out. Jul 4, 2011 at 12:51
2

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.

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.