Foreign Key naming scheme

前端 未结 10 1789
悲&欢浪女
悲&欢浪女 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:32

    I usually just leave my PK named id, and then concatenate my table name and key column name when naming FKs in other tables. I never bother with camel-casing, because some databases discard case-sensitivity and simply return all upper or lower case names anyway. In any case, here's what my version of your tables would look like:

    task (id, userid, title);
    note (id, taskid, userid, note);
    user (id, name);
    

    Note that I also name my tables in the singular, because a row represents one of the objects I'm persisting. Many of these conventions are personal preference. I'd suggest that it's more important to choose a convention and always use it, than it is to adopt someone else's convention.

提交回复
热议问题