SSMS permits duplicate records in a table, but not subsequent updates

匆匆过客 提交于 2019-12-04 06:31:11

All management studio ever does is provides a UI to create some SQL for you and runs it against the DB.

In your case, every time you added a row, it produced an INSERT statement. This is perfectly valid.

When you then tried to use the UI to DELETE or UPDATE a single record out of all these duplicate records it was unable to produce the SQL to do that. The reason being, as there was no key on the table, there is no way of producing a WHERE clause that would represent the record you were trying to UPDATE or DELETE.

It's "error" messages sound perfectly clear and valid to me.

As for your comments:

To my surprise, it also allowed me to enter "value1" on the second row and enter down - this should be impossible since now there are two identical rows. However, since I was just messing around, this didn't bother me.

Obviously this is strange and breaks relational theory, but I didn't really care since this is just a table I created to mess around with.

There is nothing wrong with having a database table that allows duplicates, it is a perfectly valid thing to do if it's what you need. As for not "caring" or being "bothered" that you had allowed duplicates. That is where the mistake lies. It is then that you should've realised you forgot to add a primary key.

A quirk yes, but a flaw, no. It's perfectly legitimate to have a table with no unique key, but it's not possible to delete a row from it with the table editor in SSMS (or Enterprise Manager before it) if there are identical rows.

It isn't SSMS that let you create the duplicate rows, it is you that allowed it when you created your table with no primary key. You can always create an auto-incrementing identity column that would solve the problem.

I wouldn't use the table editor for this sort of stuff, I would script INSERT statements.

I'm not sure how you would expect the tool to know which row to delete if you did not have a unique key. Would you want it to delete just one of the duplicates or both? if just one, what is the key it should use.

The management studio is a tool. It's job is not to enforce perfect table design or basic database 101, which would include having some unique key the majority of the time. What if you did have some crazy business model that required duplicate rows? Wouldn't the complaint then be that the tool prevented duplicate roles?

Bottom line is that 1. You shouldn't be editing or deleting data with the tool anyways. You should be scripting out the statements for most things.
2. You should have a unique key

Can't blame any tool for not having those things in place.

The record editor in Sql Server Management Studio is a small (and relatively unimportant) part of the SQL Server product offering. I think you can safely accept the quirks of the editor without being concerned about the relative quality of the server itself.

Behind the scenes, the Management Studio is executing SQL statements to perform your actions, just as if you typed that SQL yourself. So if you break a rule, you pay the fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!