Foreign Key naming scheme

前端 未结 10 1801
悲&欢浪女
悲&欢浪女 2020-12-12 10:55

I\'m just getting started working with foreign keys for the first time and I\'m wondering if there\'s a standard naming scheme to use for them?

Given these tables:

10条回答
  •  爱一瞬间的悲伤
    2020-12-12 11:37

    I use two underscore characters as delimiter i.e.

    fk__ForeignKeyTable__PrimaryKeyTable 
    

    This is because table names will occasionally contain underscore characters themselves. This follows the naming convention for constraints generally because data elements' names will frequently contain underscore characters e.g.

    CREATE TABLE NaturalPersons (
       ...
       person_death_date DATETIME, 
       person_death_reason VARCHAR(30) 
          CONSTRAINT person_death_reason__not_zero_length
             CHECK (DATALENGTH(person_death_reason) > 0), 
       CONSTRAINT person_death_date__person_death_reason__interaction
          CHECK ((person_death_date IS NULL AND person_death_reason IS NULL)
                  OR (person_death_date IS NOT NULL AND person_death_reason IS NOT NULL))
            ...
    

提交回复
热议问题