每个分区的sql server唯一键

时间:2016-03-17 08:19:07

标签: sql sql-server sql-server-2008-r2 primary-key unique-key

我有一个包含3列的表:

ID int [主键]

BranchRef int [外键]

Code int

我需要[Code]在每个[BranchRef]

中都是唯一的

像:

mysql -u root -p --host=127.0.0.1  
mysql -u root -p --protocol=TCP

mysql  Ver 14.14 Distrib 5.6.28, for debian-linux-gnu (x86_64) using  EditLine wrapper

Connection id:      55
Current database:   
Current user:       root@localhost
SSL:            Not in use
Current pager:      stdout
Using outfile:      ''
Using delimiter:    ;
Server version:     5.6.28-0ubuntu0.15.10.1 (Ubuntu)
Protocol version:   10
Connection:     localhost via TCP/IP
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
TCP port:       3306
Uptime:         9 min 38 sec

我非常感谢任何帮助

1 个答案:

答案 0 :(得分:3)

添加唯一约束。

ALTER TABLE mytable ADD CONSTRAINT UNIQUE(BranchRef, Code);