2012-11-15 197 views
-1

我在這個問題上搜索了很多關於stackoverflow的問題。和我一樣的問題,但沒有解決我的問題。 我創建了具有用戶it..columns表都是這樣創建一對多關係

uid 
username 
name 
family 
.... 

財產以後,現在我需要建立一個黑名單,將存儲黑名單person.i中的用戶ID不知道如何創建這個表,因爲每個人可以有很多黑色名單上的人那麼黑名單表是這樣的

uid = 1 
blacklistid = 3 

uid = 1 
blacklist equal 4 

那麼在這種情況下,我沒有任何主鍵,我認爲這是我wrong.if中插入自動incerement模式一個主鍵我的財產以後將有一個非常大的整數,爲什麼我需要這個主鍵?爲什麼?我問了一個人,他告訴我也許你的d設計有問題

然後我需要知道如何設計這種情況?

回答

2

黑名單表中的主鍵是用戶標識和黑名單標識的組合。

User 
    id 
    name 
    ... 

Blacklist 
    user_id 
    target_id 

user_id和target_id的組合應該是唯一的。

不需要自動遞增的id字段。

+0

然後我有一個2主鍵的表,我說得對嗎? – HiDd3N

+0

是的,黑名單包含2個主鍵。這兩個都是用戶名的 – Michiel

+0

謝謝你,我認爲它的最好的解決方案在我的case.mark作爲接受 – HiDd3N

0

也許你想要一個多對多的關係:因爲一個人可以將其他幾個人列入黑名單;一個人可以被其他幾個人列入黑名單。

要實現多到許多你有兩列(對於blacklister和他們的黑名單下一列一列)一個單獨的表,例如:

1 2 // person 1 blacklists person 2 
1 3 // person 1 also blacklists person 3 
3 2 // person 2 is also blacklisted by person 3, as well as by person 1 

如果這只是一對-many(一個人可以將其他幾個人列入黑名單;每個人不能被超過一個列入黑名單)

+0

是啊,我以爲你是right.then我應該如何創建很多很多..我需要主鍵嗎? – HiDd3N

1

好的你正在尋找的是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 

所以,你需要加入回用戶表,以獲得用戶列入黑名單的詳細信息

希望這有助於

+0

謝謝,但在我的情況下,最好的解決方案是在一個表中有2個複合主鍵 – HiDd3N

+0

啊沒問題HiDd3N ... Michiel答案很短,甜蜜的和重點,但我只是想我會給你多一點使用主鍵...而不是複合:-) – Doiremik

+0

是使用外鍵或使用複合主鍵更好?你是什麼想想doiremik? – HiDd3N