constraint on a primary key - SQL

0

I am having a problem understanding what the intent of this code below is when placing a constraint to create a primary key for a foreign key.

CREATE TABLE Customer(
    IdCustomer integer PRIMARY KEY NOT NULL IDENTITY(1,1),
    NmCustomer varchar(255) NOT NULL,
    CpfCnpj numeric NOT NULL
);

CREATE TABLE AddressType(
    CdAddressType char(1) PRIMARY KEY NOT NULL,
    AddressType varchar(50) NOT NULL
);

CREATE TABLE CustomerAddress(
    IdCustomer integer,
    CdAddressType char(1),
    Street varchar(255) NOT NULL,
    Lot integer NOT NULL,
    Reference varchar(255) NULL,
    ZipCode varchar(50) NOT NULL
    CONSTRAINT FK_IdCustomer FOREIGN KEY (IdCustomer) REFERENCES Customer(IdCustomer),
    CONSTRAINT FK_CdAddressType FOREIGN KEY (CdAddressType) REFERENCES AddressType(CdAddressType),
    CONSTRAINT PK_CustomerAddress PRIMARY KEY (IdCustomer, CdAddressType)
);
    
asked by anonymous 31.01.2018 / 18:38

1 answer

0

Creating the primary key of the CustomerAddress table:

CONSTRAINT PK_CustomerAddress PRIMARY KEY (IdCustomer, CdAddressType)

Creating 2 foreign keys:

CONSTRAINT FK_IdCustomer FOREIGN KEY (IdCustomer) REFERENCES Customer(IdCustomer),
CONSTRAINT FK_CdAddressType FOREIGN KEY (CdAddressType) REFERENCES AddressType(CdAddressType),

In the IdCustomer column of the CustomerAddress table it is only possible to add values that exist in the column with the same name as the Customer table.

In the CdAddressType column of the CustomerAddress table it is only possible to add values that exist in the column with the same name as the AddressType table.

    
31.01.2018 / 19:02