0

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

  1. When I run the schema, I am able to INSERT a business_unit that is outside of the CONSTRAINT (i.e INSERT...'LP' where LP isn't in the CONSTRAINT chk_business_unit..). How can I ensure that any user inputs are standardized so that the table ONLY includes valid parameters?

REFERENCES

Mike Sherrill 'Cat Recall'
  • 91,602
  • 17
  • 122
  • 185
jonplaca
  • 797
  • 4
  • 16
  • 34
  • 2
    My SQL does not support CHECK contraints. Perhaps annoyingly, they will parse, but they are ignored - http://stackoverflow.com/questions/2115497/check-constraint-in-mysql-is-not-working The lists of values you have here seem like pretty good candidates for a separate table, and a foreign key constraint anyway. – GarethD Nov 28 '16 at 17:30
  • Thank you - typically I search before I post, but in this instance, it looks like I rushed to ask the question before researching. – jonplaca Nov 28 '16 at 18:41

0 Answers0