我有一個不尋常的情況在MS SQL Server數據庫中建模:表的主鍵是由5個外鍵(固定大小)組成的多段「自然」鍵。如何用可尋址的成員定義用戶定義的數據類型?
我希望能夠定義一個用戶定義的數據類型來實現基於CHAR(8)基元的數據結構,使元素可作爲單獨的字段尋址。
例如(在惡劣的僞代碼):
UDT seggy
(
seg1 char(2),
seg2 char(1),
seg3 char(1),
seg4 char(2),
seg5 char(2)
)
create table yotable
(
pkfield seggy NOT NULL,
etc varchar(14), --whatever etc.
)
with pkfield as the primary key,
and also with seg1 as a foreign key to tableseg1,
and also with seg2 as a foreign key to tableseg2,
and so on
,然後可以做這樣的事情:
insert into yotable (pkfield, etc) values ('abcdefgh','whatever')
select * from yotable where seg2 = 'c'
insert into yotable (seg1,seg2,seg3,seg4,seg5,etc)
values ('ab','c','d','ef','gh', 'whatever')
到目前爲止,所有我發現這是CodeProject article不走足夠遠或提供更多信息的鏈接,以及這個基本的MSDN link。
顯然我的google-fu今晚很弱,任何鏈接/提示非常感謝!
備用標題:如何模擬SQL SERVER中的'overlay'字段?
MS SQL SERVER 2005或更高版本假設/首選。
感謝(a)唯一的答案和(b)替代方案。我不認爲UDT會起作用,但不能通過谷歌搜索來證明消極;-)。計算列也是PK,但根據http://www.devx.com/tips/Tip/15397這是可行的。 – 2008-12-17 00:45:48