Negative Primary Keys

我只是一个虾纸丫 提交于 2019-11-28 10:49:10

Like others have said, the database is fine with this.

But it would be a problems for a .NET application that uses DataSet+DataAdapter as they use negative keys as temporaries for new records.

Other data-access layers may use similar tricks.

This is perfectly fine from SQL Server's perspective. The real question is going to be your application.

You'll want to review legacy code and look for where developers have sorted on primary key as a lazy/sloppy way of sorting by date (because identity pk's usually are strongly or perfectly correlated to time).

Only problem is you won't be able to add a third data source this way!

Not a problem.

It is slightly un-orthodox, but apart from that, fine.

The default offered by SQL Server is just that - a default, and can be changed to suit needs. Looks like you got a good compromise.

If negative numbers turn out to break something, use even numbers for one and odd numbers for the other.

It is not an issue. Just make sure your Identity column is of a type that allows negative numbers.

Another option is to prefix the legacy keys with a string like "OLD_". The ony problem is your key field will be non-numeric.

If you have to have numeric keys, you could introduce a "legacy" indicator column, and the primary key would be a combination of the numeric ID and the legacy indicator (hopefully, that combination should be unique).

I think having 2 sources is not the right reason for this approach, while technically allowed. It's not scalable (+1 to Larry Lustig's answer for that).

I would just create a view or stored procedure that combines both data, by converting the IDs as needed, and would have application to use it instead of direct table reads and unions. This would be scalable by modifying the view/SP later to add one more source.

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