Can A MySQL Field Have Requirements That Interface With Other Fields?
Example: I have 2 date/time fields in a table,
delivery_date should always have a greater value (later in the calendar) than
Is such a requirement enforceable in the schema for the table? That is, if I try to add a new row with a
delivery_date before a
quote_date, it will reject the change.
Yes, you can use a CHECK constraint for this, for example:
ALTER TABLE mytable ADD CHECK (delivery_date > quote_date);
quote_date are defined as the MySQL
DATE type or another temporal type (i.e. not strings).
CHECK constraints require MySQL 8.0.16 or later. If you must implement this on an older version of MySQL, you would have to write triggers before INSERT and before UPDATE, and use SIGNAL to abort the change if it doesn't pass your intended constraint.
- → I can't do a foreign key, constraint error
- → How to implement DbDongle::convertTimestamps as workaround of invalid timestamps with MySql strict
- → MySQL error "Foreign key constraint is incorrectly formed"
- → Eloquent Multitable query
- → "Laravel 5.1" add user and project with userId
- → Database backup with custom code in laravel 5 and get the data upto 10 rows from per table in database
- → Laravel 5.1 QueryException when trying to delete a project
- → Using Array in '->where()' for Laravel Query Building
- → Chaining "Count of Columns" of a Method to Single Query Builder
- → Laravel Eloquent Joining Strange query
- → convert time using mysql laravel 5
- → How to update a column after an expiration date in MySQL?
- → Foreign key constraint fails on existing key