SQL Server how to maintain GUIDs between tables in the same DB
I want to create a DB where each PK table will be a GUID and that will be unique in the DB,
Example: my database name is "LOCATION". And I have 3 tables like "CITY", "STATE" and "COUNTRY".
I want all three tables to have the same PK field as GUID and this value will be unique across the DB.
How to do this in SQL Server, any idea? I have never used SQL Server before, so it will be helpful to explain briefly.
source to share
What do you mean? Just create a table, add an Id field to each table, set the data type of the Id field to "uniqueidentifier" and you're good to go. Then add a main constraint on those columns and make sure that when you insert a new record, you assign a new pointer to that column (for example, using the newid () function).
source to share
I can't think of any good reason to have a unique number shared by three tables, why not just give each table a unique index with a foreign key reference? Indexed fields are requested faster than random numbers.
I would create a "Location" table with the foreign keys CityId, StateId and CountryId to link them logically.
edit: If you are adding a unique ID to the City, State and Country tables, then why not just use them as fields in the same table? I would have thought that the reason for decomposing them into 3 tables was to reduce duplication in the database.
source to share