Custom PrimaryKey Generation with autoincrement

后端 未结 6 777
小蘑菇
小蘑菇 2021-01-22 21:08

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

6条回答
  •  清歌不尽
    2021-01-22 21:29

    What you are trying to implement is referred to as subtypes: special cases of the same kind of object. There are a few common ways to implement it:

    1. make one table containing all the columns required for both types. Use NULLable columns for those properties that do not apply to all types, and mandatory columns for all other properties. There is only one table, so only on pk.
    2. create one table with the generated pk for all common properties (the base type). Add separate tables for each subtype. these tables have a pk that uses a foreign key to point to the pk of the base type (and thus, have a 1:1 relationship with th base type). So both types draw from the same set of key values

    (there are more ways, but these are most useful, and most popular)

    I would advise vehementlyu against all kinds of "smart" keys. Of course, you can have a " type" column in your table (the one for the base type) to quickly determine what you are dealing with. But a key scheme like you are suggesting is just complicating things with no real value.

提交回复
热议问题