Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

updating column value based on another column value

I'm working on Library Database and it contains a table called book_transaction. There are 2 columns called issued_date and due_date. due_date should be 7 days from issued_date. Can I specify this condition using default key word while creating the table?

If it is not possible please leave an alternative for the same.

Thank you,

like image 320
Narasimha Maiya Avatar asked Feb 14 '26 13:02

Narasimha Maiya


2 Answers

Oracle default constraints cannot refer to other columns. You can get the same functionality using a trigger (see this answer):

CREATE TRIGGER book_transaction_trigger
  BEFORE INSERT ON book_transaction
  FOR EACH ROW
BEGIN
  IF :new.due_date IS NULL THEN
    :new.due_date := :new.issued_date + 7;
  END IF;
END book_transaction_trigger;

You can add days by adding a number to a date.

like image 79
Andomar Avatar answered Feb 17 '26 06:02

Andomar


You can create a trigger for the table..

CREATE TRIGGER test_trigger BEFORE INSERT ON `book_transaction` 
FOR EACH ROW SET NEW.issued_date = IFNULL(NEW.entryDate,NOW()),
NEW.due_date = TIMESTAMPADD(DAY,7,NEW.issued_date)
like image 45
Arsalan Avatar answered Feb 17 '26 08:02

Arsalan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!