我的“创建表”语句中的错误逻辑在哪里?

时间:2019-02-15 14:14:54

标签: sql oracle primary-key create-table composite-primary-key

我正在尝试运行这些语句,但显示错误消息no matching unique or primary key for this column-list。您能帮我解决这个问题吗?

尝试创建表SITE时出现问题:

CREATE TABLE OLMP_COUNTRY (
NOC CHAR(3),
TEAM VARCHAR2(100),
CITY VARCHAR2(100),
CONSTRAINT country_pk PRIMARY KEY(NOC)
);

CREATE TABLE ATHLETE (
ATHELTE_ID CHAR(8),
NAME VARCHAR2(100),
AGE CHAR(3),
SEX CHAR(1),
HEIGHT CHAR(3), 
WEIGHT DECIMAL(3,1), 
NOC CHAR(3), 
CONSTRAINT athlete_pk PRIMARY KEY(ATHLETE_ID), 
CONSTRAINT country_fk FOREIGN KEY(NOC) REFERENCES OLMP_COUNTRY(NOC) 
); 

CREATE TABLE SITE (
NOC CHAR(3),
CITY VARCHAR2(100),
SEASON VARCHAR2(20),
YEAR CHAR(4),
CONSTRAINT site_pk PRIMARY KEY(NOC),
CONSTRAINT country_fk FOREIGN KEY(CITY) REFERENCES OLMP_COUNTRY(CITY)
);

CREATE TABLE RESULTS (
RESULT_ID CHAR(8),
MEDAL CHAR(6),
ATHLETE_ID CHAR(8),
SPORT_EVENT VARCHAR2(100), 
YEAR CHAR(4), 
GAMES VARCHAR2(50), 
CONSTRAINT results_pk PRIMARY KEY(RESULTS_ID) 
);

CREATE TABLE EVENT (
SPORT_EVENT VARCHAR2(100), 
SPORT VARCHAR2(50),
GAMES VARCHAR2(50) 
CONSTRAINT event_pk PRIMARY KEY(SPORT_EVENT)
);

1 个答案:

答案 0 :(得分:2)

外键应引用它所引用表的主键。

所以我想你想要

CREATE TABLE SITE (
    NOC CHAR(3),
    CITY VARCHAR2(100),
    SEASON VARCHAR2(20),
    YEAR CHAR(4),
    CONSTRAINT site_pk PRIMARY KEY(NOC),
    CONSTRAINT site_country_fk FOREIGN KEY(NOC) REFERENCES OLMP_COUNTRY(NOC)
);

我不知道您为什么在两个表中都重复CITY,但是外键约束应该是主键。您可以使用JOIN查找城市。不应重复。