I need to define and generate primary key for 2 or more tables.
Tables hold same type of data but FOR Some BUSINESS RULES we have to make them separate say like
TableA = LOCAL_CUSTOMERS
TableB = INTERNATIONAL_CUSTOMERS
Both hold same columns like, CUSTOMER_ID C_NAME, CITY ….etc
While designing the Primary Key I want these keys to be a combination of
PREFIX and an auto generated number, as an auto increamented int PK will do.
for example, CUSTOMER_ID (PK) for the table “LOCAL_CUSTOMERS”
LOC1, LOC2, LOC3,…… LOC5000
and CUSTOMER_ID (PK) for the table “INTERNATIONAL_CUSTOMERS”
INT1, INT2, INT3,…… INT5000
WHERE LOC and INT are prefix for LOCAL , INTERNATIONAL.
we can set auto increment in “identity specification” by using int but how can i do this ?
Is it “Computed Column Specification” I need to set for the column ?
Thanks
You can definitely do this with a computed column.
Either, if you stick with your two tables, you could just simply add a single computed, persisted column like this:
If you decide to have a single table with a discrimator column, e.g. “IsDomestic” of type BIT, you could do a single computed column that will use “LOC” or “INT” as prefix, based on the value of the “IsDomestic” column:
Either way – your “ID” field of type INT IDENTITY will be automatically increased for each row, and the computed column will create a more human-readable “CustomerID” automagically, without any further effort on your part.