简体   繁体   English

两个表的唯一约束

[英]Unique constraint over two tables

I have two tables: 我有两个表:
Country : has ID, Name Country :具有ID,名称
State : has ID, Name, CountryID State :具有ID,名称,CountryID

I defined Country.Name as unique. 我将Country.Name定义为唯一。

I also want to define State.Name as unique per country - ie I can't just define it as unique because two countries may have the same state. 我还想将State.Name定义为每个国家/地区唯一-即我不能仅仅将其定义为唯一,因为两个国家可能具有相同的州。 But for one country, every state is unique. 但是对于一个国家来说,每个州都是独一无二的。

How can I do this? 我怎样才能做到这一点?

尝试为NameCountryID列定义UNIQUE ,如下所示:

CREATE UNIQUE INDEX uix ON State( Name, CountryID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM