Why single primary key is better than composite keys?

前端 未结 9 1091
栀梦
栀梦 2020-12-05 10:34

Why is the rejection of composite keys in favor of all tables using a single primary key named id? Cause generally all ORM follow this.

EDIT

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 11:09

    The only real limitation that I have run into using composite keys regards using an IN expression with a subquery. This is a problem, because a subquery in an IN expression must return a single column (at least in T-SQL).

    SELECT
        emp.Name,
        emp.UserDomain,
        emp.UserID
    FROM
        employee emp
    WHERE
        ???? IN (SELECT e.UserDomain, e.UserID FROM ... /* some complex 
                                                           non-correlated subquery 
                                                           or CTE */
                )
    

    There are always work-arounds, of course, but sometimes it could be an annoyance.

    This is hardly a reason to avoid a composite key in places where it makes sense to use one.

提交回复
热议问题