I'm in the middle of developing a new database that should support replication, and I'm stuck in deciding what to choose as the primary key.
In our current database, we use two int columns for the primary key, the first column is private, and the other is used to describe which server the row is inserted on. Now I want to avoid using two columns for the primary key and just use only one column. So far, I have two ways to do this:
Use a GUID for my primary key
This ensures that on any number of servers there will always be a unique key. What I don't like about this is that the GUID is 16 bytes in size, and when used for a foreign key in many tables, it will be empty. It is also harder to use it when writing queries, and it will be slower to query.
Use int or bigint and manually specify the seed and increment value for each table on each server. For example, if there are two servers, table X on the first server will start at number 1, and on the second server it will start at number 2, each will increase by 2. Thus, there will be (1,3,5 ...) on the first and (2,4,6, ...) on the second server. The good thing with this design is that it is easier to use when writing queries, it is fast, and it uses less space for foreign keys. The bad news is that we never know how many servers will work, so it’s harder to say what the increment value will be. It is also more difficult to manage schema changes on the server.
What is the best practice for managing multiple servers and what is the best way, if any, to do in this case, if the situation?
sql-server identity primary-key
Mladen macanović
source share