简体   繁体   English

如何在SQL中将表1中的复合主键引用为表2作为外键?

[英]How to reference the composited PRIMARY KEY in table1 to table 2 as foreign key in SQL?

I having a problem with creating foreign key that pointing to a composite primary key in other table. 我在创建指向其他表中的复合主键的外键时遇到问题。

CREATE TABLE CityCountry(
    city_code VARCHAR(5) NOT NULL,
    city VARCHAR(100),
    country_code VARCHAR(5) NOT NULL,
    country VARCHAR(100),
    citycountry_id VARCHAR(105) PRIMARY KEY (city_code, country_code) 
)

CREATE TABLE FlightRoute(
    flightroute_id INT IDENTITY(1, 1) PRIMARY KEY,
    flight_departureCountryCity VARCHAR(105),
    flight_id INT FOREIGN KEY 
        REFERENCES Flight(flight_id)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION,
    flight_departureDateTime DATETIME,
    flight_arrivalDateTime DATETIME,
    aircraft_code VARCHAR(15) FOREIGN KEY 
        REFERENCES Aircraft(aircraft_code)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION,
    flight_order SMALLINT
)

I tried the solution in stackoverflow but didnt work out 我在stackoverflow中尝试了解决方案,但没有解决

alter table flightroute add constraint FK_FlightRoute_CityCountry foreign key FlightRoute(flight_departureCountryCity) references CityCountry (city_code, country_code) 

In order to create a foreign key referenced columns shoud be PK or unique index . 为了创建外键,引用的列应该是PKunique index Since you already have a PK you can make your column a unique index : 由于您已经拥有PK ,因此可以使您的列成为unique index

create unique index UI_CityCountry
   on CityCountry(citycountry_id); 

Now create FK : 现在创建FK

alter table flightroute
add constraint FK_FlightRoute_CityCountry
foreign key (flight_departureCountryCity)
references CityCountry (citycountry_id) 

Try defining your tables like this: 尝试像这样定义表:

CREATE TABLE CityCountry(
    citycountry_id VARCHAR(105) NOT NULL PRIMARY KEY,
    city_code VARCHAR(5) NOT NULL,
    city VARCHAR(100),
    country_code VARCHAR(5) NOT NULL,
    country VARCHAR(100)
);

CREATE TABLE FlightRoute(
    flightroute_id INT IDENTITY(1, 1) PRIMARY KEY,
    flight_departureCountryCity VARCHAR(105) FOREIGN KEY
        REFERENCES CityCountry(citycountry_id),
    flight_id INT FOREIGN KEY 
        REFERENCES Flight(flight_id)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION,
    flight_departureDateTime DATETIME,
    flight_arrivalDateTime DATETIME,
    aircraft_code VARCHAR(15) FOREIGN KEY 
        REFERENCES Aircraft(aircraft_code)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION,
    flight_order SMALLINT
);

That is, declare the primary key correctly in the first table and thenuse it in the second. 也就是说,在第一个表中正确声明主键,然后在第二个表中使用它。

Personally, I prefer primary keys that are incremented numeric values, but I'm leaving your original types. 就个人而言,我更喜欢主键是递增的数值,但是我要保留原始类型。

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

相关问题 如何从由两个属性组成的主键中仅引用一个属性作为外键 - How to reference only one attribute as a foreign key from a primary key composited of two attributes sql中的同一表中的外键和主键 - Foreign key and primary key in same table in sql SQL具有复合主键的表的外键 - SQL Foreign key to a table with a composite primary key SQL、主键、外键和表 - SQL, primary key, foreign key and table 如何将一个表中的多个外键引用到 ms sql 中的单个主键 - How can I reference multiple foreign key in one table to a single primary key in ms sql SQL:如何将外键连接到某些表中的主键 - SQL: How to connect a foreign key to primary key in the some table 外键SQL:如何将一个表中的两个属性引用到在另一个表中包含3列的复合主键? - Foreign Key SQL: How can I reference two attributes in one table to a composite primary key that contains 3 columns in the other table? 使用表1主表(表2中的外键)显示Access数据库的表记录 - Display the table records of an Access database using table1 primary which is the foreign key in table 2 SQL创建表相同的主键和外键 - sql create table same primary & foreign key 将外键引用到同一个表中的主键 - reference a foreign key to a primary key within the same table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM