這是一個有趣的問題。如果是我,我會使用兩個表格:一個用於所有常見屬性,另一個用於用戶的所有外圍屬性。事情是這樣的:
CREATE TABLE _UserProperties(
ID int ,
Property varchar(1000) ,
Value varchar(1000) ,
UserID int
) ;
insert into _UserProperties(id, property, value, userid)
values (1,'employee id', '123556',1),
(2,'CustomerID', '345677',2),
(2,'SalesGroup', 'ABFD34',2),
(2,'SalesToDate', '2344',2),
(3,'Project', 'cat juggling',3);
CREATE TABLE _Users(
ID int ,
UserName varchar(50) ,
UserAddress varchar(1000) ,
UserType varchar(50)
) ;
insert into _Users (ID, UserName, UserAddress, UserType)
values (1,'Fred Jones','123 Mample','Customer'),
(2,'Sam Smith','234 Oak','Employee'),
(3,'Sally Black','345 Pine','Consultant');
然後你可以用一個簡單的查詢一切結合起來,如:
select * from _users a left join _userProperties b
on a.id = b.userid
或使用數據透視表製作中的列行。
這裏有一個SQL Fiddle View
這是明智的,如果它可以更容易執行的大多數需要在這些表進行操作。這些選項是稱爲[具體表繼承](http://martinfowler.com/eaaCatalog/concreteTableInheritance.html)和[類表繼承](http://martinfowler.com/eaaCatalog/classTableInheritance.html)的模式。根據您需要支持的功能,您可以決定使用哪一個。編輯:還有第三個相關的模式,稱爲[單表繼承](http://martinfowler.com/eaaCatalog/singleTableInheritance.html) – GolfWolf
@ w0lf我編輯了我的問題,你可以請看看它嗎? –