我會去用含大家一個主表和幾個專業(1比0或1)只包含該類型的用戶表:
AllUsers
UserID int identity PK
Active char(1)?
DateRegistered date
FirstName {string}
..all other common columns here
BusinessUsers
UserID int PK and FK to AllUsers
Website {string}
..all business only columns
NormalUsers
UserID int PK and FK to AllUsers
BirthDate date
..all normal user only columns
..any other user types tables as needed
如果您需要加載一切爲了用戶做:
SELECT
* --please only return the columns you actually need!
FROM AllUsers u
LEFT OUTER JOIN BusinessUsers b ON u.UserID=b.UserID
LEFT OUTER JOIN NormalUsers n ON u.UserID=n.UserID
WHERE u.UserID=123
,如果你只需要加載數據爲特定類型的應用:
SELECT
* --please only return the columns you actually need!
FROM AllUsers u
LEFT OUTER JOIN NormalUsers n ON u.UserID=n.UserID
WHERE u.UserID=123
--or
SELECT
* --please only return the columns you actually need!
FROM NormalUsers n
WHERE n.UserID=123
這是正確的答案。此模式的名稱是「類表繼承」 –