Skip to content
Advertisement

check date in create table sql

edit: version 8.0.20

I’m learning sql just now, I try to fix it but I’m not able to do it. I have 3 table

This is an easier scheme but it’s enough. The return_date must be less than delivery_date + 30 days I tried to use a check on return_date but no work, so I created a view to select the delivered products only

and I tried again with check on return_date.

Can someone help me to understand how fix it?

thanks in advance 🙂

Advertisement

Answer

Two common options for solving this are:

  1. Write a trigger to be sure the condition is true.
  2. Write a user-defined function and use that in a check constraint.

An alternative method might suffice in your case. Change the definition of return_date so it is days_to_return. Then you can use a check constraint:

Of course, to get the actual date, you will need to join the two tables to fetch the delivery date.

EDIT:

The insert trigger would look something like this:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement