0

我的数据库中有 3 个表“ countries, cities and address_book

表:国家

country_code   (primary key)
country_Name

表:城市

country_code   (primary key)
city_code (primary key)
city_Name

表:地址簿

country_code   (primary key)
city_code (primary key)
address

我需要设置cities如下表

country_id       city_id   ciTy_name
IN               1         IN_city_name_1  
IN               2         IN_city_name_2
AE               1         AE_city_name_1  
AE               2         AE_city_name_2
  1. 但是我city_i表中 d 的主键cities不允许我用重复键更新表

  2. 当我city_id在表中更新时cities。我需要city_id根据address_book主键更新country_id

4

1 回答 1

1

当您的字段引用另一个表的键时,您应该使用FOREIGN KEY。这适用country_codecities

country_code您还应该从中删除address_book,因为 tablecities已经包含它。

我会怎么做:

Table : countries
  country_code   (primary key)
  country_Name

Table : cities
  city_code (primary key)
  country_code (FOREIGN KEY)
  city_Name

Table : address_book
  id (primary key)
  city_code (FOREIGN KEY)
  address
于 2013-05-20T13:20:00.770 回答