OBJECTIVE
CREATE
a TABLE
for AccountsReceivables
with associated parameters (e.g invoice_id
, customer_id
, etc.). Ensure that CONSTRAINTS
are added to business_unit
and invoice_terms
to help standardize user input
APPROACH/SCHEMA
CUSTOMER table
CREATE TABLE customers
(id INT NOT NULL PRIMARY KEY,
global_risk_partner varchar(32) NOT NULL
);
ACCOUNTSRECEIVABLE table
CREATE TABLE AccountsReceivable
(upload_date TIMESTAMP NOT NULL,
invoice_id INT NOT NULL,
PRIMARY KEY(upload_date, invoice_id),
/*Pull in customer_id from another table */
customer_id INT NOT NULL,
FOREIGN KEY (customer_id) REFERENCES customers(id),
/*Add constraints to standardize which business unit is responsible for invoice */
business_unit varchar(16) NOT NULL,
CONSTRAINT chk_business_unit CHECK (business_unit in ('PLP', 'DSI', 'JIMCO', 'PATTON', 'THRO', 'FRANCE', 'SWEDEN', 'UK', 'POLAND', 'GERMANY', 'HOLLAND', 'PINNACLE', 'NBUSA TN', 'NBUSA IL')),
invoice_value float(2) NOT NULL,
invoice_date DATE NOT NULL,
/*Add constraints to standardize payment terms */
invoice_terms INT NOT NULL,
CONSTRAINT chk_invoice_terms CHECK (invoice_terms IN (30, 60, 90, 120)) /* dropdown list of invoice terms */
);
Inserting values into CUSTOMERS and ACCOUNTRECEIVABLE tables
INSERT INTO customers VALUES
(321, 'WALMART');
INSERT INTO AccountsReceivable VALUES
(CURRENT_DATE(), 123, 321, 'LP', 100.50, '2016-01-20', 30); /* This should throw an ERROR since 'LP' isn't a valid business_unit */
QUESTIONS
- When I run the schema, I am able to
INSERT
abusiness_unit
that is outside of theCONSTRAINT
(i.eINSERT...'LP'
whereLP
isn't in theCONSTRAINT chk_business_unit..
). How can I ensure that any user inputs are standardized so that the table ONLY includes valid parameters?
REFERENCES