Suposse I am using a SQL platform and want to implement a relationship of cardinality one-to-zero-or-one (1-0..1). This situation would pertain to a business context in which let us say that:
- A
product
can have zero or oneprice
.
So:
- What are the differences between the two scenarios (DDL structures) shown bellow?
- When should one be preferred over the other?
- Is any of them considered a bad design?
Scenario 1:
CREATE TABLE product
(
product_id INT NOT NULL,
name CHAR(30) NULL,
product_price_id INT NULL,
CONSTRAINT PK_product PRIMARY KEY (product_id),
CONSTRAINT UQ_product_price UNIQUE (product_price_id)
);
CREATE TABLE product_price
(
product_price_id INT NOT NULL,
price DECIMAL NOT NULL,
tax_rate DECIMAL NOT NULL,
currency_id INT NOT NULL,
CONSTRAINT PK_product_price PRIMARY KEY (product_price_id),
CONSTRAINT FK_product_price_to_product FOREIGN KEY (product_price_id)
REFERENCES product (product_id)
);
Scenario 2:
CREATE TABLE product
(
product_id INT NOT NULL,
name CHAR(30) NULL,
CONSTRAINT PK_product PRIMARY KEY (product_id)
);
CREATE TABLE product_price
(
product_id INT NOT NULL, -- Assume that this column might be set as PRIMARY KEY or UNIQUE.
price DECIMAL NOT NULL,
tax_rate DECIMAL NOT NULL,
currency_id INT NOT NULL,
CONSTRAINT PK_product_price PRIMARY KEY (product_id),
CONSTRAINT FK_product_price_to_product FOREIGN KEY (product_id)
REFERENCES product (product_id)
);