2015-02-23 22 views
-3

我想爲某公司插入用戶名,但用戶名不應在一個公司重複。sql - 將不等數據安裝到其他屬性相同的表中

id | company_name|     id|username|company | 
---|-------------|     --|--------|--------| 
1 | company1 |     1 | admin |company1| !! 
2 | company2 | 1 ------------ M 2 | admin |company1| !! 
3 | company3 |     3 | admin |company2|  
+3

1.在用戶名列上創建一個唯一索引2.檢查以查看用戶名是否存在*在*之前*您嘗試插入一個包含它的記錄 – 2015-02-23 20:57:56

+0

我已投票了@JohnConde評論,還有另一個問題,爲什麼那麼你需要在這種情況下的第二張桌子? – Alex 2015-02-23 21:03:19

回答

1

您想在第二個表中使用唯一索引:table2(username, company)

如果您嘗試在公司內插入重複的用戶名,則會出現錯誤。

相關問題