好的你正在尋找的是1到很多表,但扭曲的是,你有一個參考回到原來的表,以獲得黑名單上的用戶的詳細信息。所以你的用戶表看起來像這樣,AppUserID是唯一標識用戶的PK。
CREATE TABLE [dbo].[AppUser](
[AppUserID] [bigint] IDENTITY(1,1) NOT NULL, -- Pk for the user
[UserName] [nvarchar](50) NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[LastName] [nvarchar](50) NOT NULL,
[EmailAddress] [nvarchar](255) NULL,
CONSTRAINT [PK_APP_USER] PRIMARY KEY CLUSTERED ([AppUserID] ASC)
)
GO
你的黑名單表將有0,1,N ..特定AppUserId列入黑名單的用戶。您需要AppUserBlacklistID唯一引用當前用戶的特定黑名單用戶。以防需要刪除或更新它們。所以,你會使用AppUserBlackListId
CREATE TABLE [dbo].[AppUserBlackList](
[AppUserBlackListID] [bigint] IDENTITY(1,1) NOT NULL,
[AppUserID] [bigint] NOT NULL, -- Foreign Key to the AppUser table to identify the users black listed 'Users'
[BlackListedAppUserID] [bigint] NOT NULL, -- Foreign Key to the AppUser table to identify the user that is black listed
[Reason] [nvarchar](255) NOT NULL,
CONSTRAINT [PK_APP_ROLE] PRIMARY KEY CLUSTERED (AppUserBlackListID ASC)
) ON [PRIMARY]
現在創建一些外鍵約束上
-- Foreign key to the users table. This is used to list all the black listed users for a particular user
ALTER TABLE [dbo].[AppUserBlackList] WITH CHECK ADD CONSTRAINT [FK_AppUserBlackList.AppUserID_AppUser] FOREIGN KEY([AppUserID])
REFERENCES [dbo].[AppUser] ([AppUserID])
-- This is a Foreign Key to the App user for the user that is black listed. It should also be unique in that one user can only blacklist another
-- User one time.
ALTER TABLE [dbo].[AppUserBlackList] WITH CHECK
ADD CONSTRAINT [FK_AppUserBlackList.BlackListedAppUserID_AppUser] FOREIGN KEY([BlackListedAppUserID])
REFERENCES [dbo].[AppUser] ([AppUserID])
現在是有點緊你可以把唯一約束的設計表明用戶不能列入黑名單的人不止一次,他們不能黑名單本身。
要獲取特定用戶的所有黑名單用戶..你加入了2個表
Select AppUserBlackListID, AppUserID,BlackListedUserName
from
AppUser auCurrentUser
Inner join AppUserBlackList auBl
on auCurrentUser.AppUserId = auBl.AppuserID
Inner join AppUser auBlackListedUserDetails
on auBL.BlackListedAppUserID =auBlackListedUserDetails.AppUserID
Where au.AppUserId = 10
所以,你需要加入回用戶表,以獲得用戶列入黑名單的詳細信息
希望這有助於
然後我有一個2主鍵的表,我說得對嗎? – HiDd3N
是的,黑名單包含2個主鍵。這兩個都是用戶名的 – Michiel
謝謝你,我認爲它的最好的解決方案在我的case.mark作爲接受 – HiDd3N