Skip to content
Advertisement

How to reference only one attribute as a foreign key from a primary key composited of two attributes

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), 
) 

I’m unable to set the foreign key only to Name, how can I implemnt this?

Advertisement

Answer

I suspect that you actually want the relation the other way around, with Performances referencing Type of Shows, like:

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)
);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement