如何从两个属性组成的主键中仅引用一个属性作为外键

时间:2019-12-12 21:56:33

标签: sql foreign-keys

CREATE TABLE Performances (
    DT Datetime NOT NULL,
    Name varchar(20) NOT NULL,
    Start_Time time NULL,
    Min_price integer NOT NULL,
    Max_price integer NOT NULL,
    Location Varchar (20) NOT NULL,
    PRIMARY KEY (DT, Name),

create Table [TYPE OF SHOWS] (
    Name Varchar (20) NOT NULL ,
    Type_of_show Varchar (20),
    PRIMARY KEY (Name),
    CONSTRAINT  fk_PerformanceName  FOREIGN KEY (Name)
    REFERENCES Performances (Name), 
) 

我无法仅将外键设置为“名称”,该如何实施呢?

1 个答案:

答案 0 :(得分:1)

我怀疑您实际上是想要这种关系,Performances引用了Type of Shows,例如:

create Table [TYPE OF SHOWS] (
    Name Varchar (20) NOT NULL ,
    Type_of_show Varchar (20),
    PRIMARY KEY (Name)
);

CREATE TABLE Performances (
    DT Datetime NOT NULL,
    Name varchar(20) NOT NULL,
    Start_Time time NULL,
    Min_price integer NOT NULL,
    Max_price integer NOT NULL,
    Location Varchar (20) NOT NULL,
    PRIMARY KEY (DT, Name),
    CONSTRAINT  fk_PerformanceName FOREIGN KEY (Name) REFERENCES [TYPE OF SHOWS](Name)
);