'How primary key in one table connect to other table with the same primary key?

How primary key in one table connect to another table with the same primary key?

I am trying to make it like this, which those two primary key in the table of CustomerCreditCard is connect to the table of Customer and table of Credit card]

https://i.stack.imgur.com/lIBUE.png

--3
CREATE TABLE Customer
(
    CustomerID INT IDENTITY PRIMARY KEY,
    FirstName  VARCHAR(50) NOT NULL,
    LastName   VARCHAR(50) NOT NULL,
);

--5
CREATE TABLE CreditCard
(
    CreditCardNumber    VARCHAR(16) PRIMARY KEY,
    CreditCardOwnerName VARCHAR(50) NOT NULL,
);

--6
CREATE TABLE CustomerCreditCard
(
    CreditCardNumber VARCHAR(16) NOT NULL,
    CustomerID       INT IDENTITY NOT NULL,

    PRIMARY KEY(CreditCardNumber, CustomerID)
);


Solution 1:[1]

--6
CREATE TABLE CustomerCreditCard
(
CreditCardNumber            VARCHAR(16)     NOT NULL,
CustomerID                  INT NOT NULL,

CONSTRAINT pk_CustomerCreditCard
PRIMARY KEY(CreditCardNumber,CustomerID ),

CONSTRAINT fk_CustomerCreditCard_CreditCardNumber
FOREIGN KEY(CreditCardNumber)
REFERENCES CreditCard(CreditCardNumber)
ON DELETE CASCADE ON UPDATE CASCADE,

CONSTRAINT fk_CustomerCreditCard_CustomerID
FOREIGN KEY(CustomerID)
REFERENCES Customer(CustomerID)
ON DELETE CASCADE ON UPDATE CASCADE

);

To solve the problem of this question, add the foreign key to the table that has two primary keys, then reference to other tables that you want to connect with the same primary key.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1