Hey everyone it's me again, I've just started on a class in my degree on Oracle/Databases, I've posted this a few days ago which was answered promptly and really appreciated it.
Learning Oracle, Setting boolean based on date
Following up on this, consider I have these 2 tables
CREATE TABLE CUSTOMER (
CustID NUMBER(5) NOT NULL,
CONSTRAINT CustID_PK PRIMARY KEY (CustID),
PlanTypeNo Number(1) NOT NULL,
CONSTRAINT PlanTypeNo_CustomerFK FOREIGN KEY (PlanTypeNo) REFERENCES PLANTYPE(PlanTypeNo),
CustName VARCHAR2(50) NOT NULL,
CustICNo VARCHAR2(9) NOT NULL,
CustTelNo VARCHAR2(8) NOT NULL,
CONSTRAINT UniqueCustomer UNIQUE (CustICNo,CustTelNo),
CONSTRAINT Exact_IC_Length CHECK (LENGTH(CustICNo) = 9),
CONSTRAINT Exact_Tel_Length CHECK (LENGTH(CustTelNo) = 8),
PaymentModePref VARCHAR2(15) NOT NULL,
CONSTRAINT PaymentModePref_Accepted CHECK (PaymentModePref IN ('Paypal','Cash','EFT')),
LastBillPaidDate DATE,
hasUnpaidBill NUMBER(1) NOT NULL,
CONSTRAINT UnpaidBill_Boolean CHECK (hasUnpaidBill IN ('0', '1')),
CompanyName VARCHAR2(50),
CompanyRegNo VARCHAR2(10),
CONSTRAINT If_NonResident CHECK ((((PlanTypeNo) = 1) OR (CompanyName IS NOT NULL AND CompanyRegNo IS NOT NULL)))
);
and
CREATE TABLE LOCATION (
LocationID NUMBER(6) NOT NULL,
CONSTRAINT LocationID_PK PRIMARY KEY (LocationID),
CustID Number(5) NOT NULL,
CONSTRAINT CustID_LocationFK FOREIGN KEY (CustID) REFERENCES CUSTOMER(CustID),
LocationName VARCHAR2(40) NOT NULL,
LocationAddress VARCHAR2(50) NOT NULL,
CONSTRAINT UniqueNameAddress_Location UNIQUE (LocationName, LocationAddress)
);
I'm relatively experienced with coding, but not database programs/scripting/querying. How would you go about creating a way (constraint maybe?) for the database to check if an owner that has an outstanding bill wants to add a new location?
I got as far as comparing the CustID Foreign Key to go back to the CUSTOMER table and check if they have any LastBillPaidDate that's > 60 days, either that or create a trigger that turns hasUnpaidBill to '1' if current date - LastBillPaidDate > 60, and then check if the value of hasUnpaidBill is 1 when attempting to INSERT a row
Hope it's not too confusing, I get the logic but have no idea how to implement it for databases :|
Thanks again!