我有一个包含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
我非常感谢任何帮助
答案 0 :(得分:3)
添加唯一约束。
ALTER TABLE mytable ADD CONSTRAINT UNIQUE(BranchRef, Code);