Constraint in a key Primary - SQL

Asked

Viewed 264 times

0

I’m having a problem intender what the intention of this code below when putting 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)
);

1 answer

0

Creation of the table primary key CustomerAddress:

CONSTRAINT PK_CustomerAddress PRIMARY KEY (IdCustomer, CdAddressType)

Creation of 2 foreign keys:

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

In the column IdCustomer table CustomerAddress only add values that exist in the column with the same table name Customer.

In the column CdAddressType table CustomerAddress only add values that exist in the column with the same table name AddressType.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.