Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protect column, disallow update, only allow insert if NULL in MySQL

Tags:

mysql

I want to protect existing dates in a date column from being overwritten. So disallow updates to the date column and only allow inserts if the existing field value is NULL (date column default is NULL). Are triggers the only way to accomplish this in MySQL? If so, would the trigger below work?

create trigger date_check
before insert, update on date
for each row
begin
if(date IS NOT NULL) then
 SIGNAL 'date already set'
end if ;
end ;

Background: I have a table with critical dates that was accidentally changed due to user error. I put some checks in the user interface to prevent this from happening again but want another layer of safety directly with the database if possible.

like image 203
reflexiv Avatar asked Sep 17 '13 03:09

reflexiv


People also ask

How do I restrict column values in MySQL?

MySQL CONSTRAINT is used to define rules to allow or restrict what values can be stored in columns. The purpose of inducing constraints is to enforce the integrity of a database. MySQL CONSTRAINTS are used to limit the type of data that can be inserted into a table.

Is NULL or empty MySQL?

The IS NULL constraint can be used whenever the column is empty and the symbol ( ' ') is used when there is empty value. mysql> SELECT * FROM ColumnValueNullDemo WHERE ColumnName IS NULL OR ColumnName = ' '; After executing the above query, the output obtained is.

Is NULL in SELECT MySQL?

Let's look at an example of how to use MySQL IS NULL in a SELECT statement: SELECT * FROM contacts WHERE last_name IS NULL; This MySQL IS NULL example will return all records from the contacts table where the last_name contains a NULL value.

Can we update NULL value in SQL?

Null Values can be replaced in SQL by using UPDATE, SET, and WHERE to search a column in a table for nulls and replace them.


2 Answers

Yes, in MySQL triggers are the only way to do this. MySQL does not support constraints.

Your trigger is not exactly right. First, you have update on date, but this should be update on <table name>. Second, you are checking the date value used for the update. Perhaps you mean:

create trigger date_check_update
before update on <the table name goes here>
for each row
begin
    if (old.date IS NOT NULL) then
        SIGNAL 'date already set'
    end if ;
end;

An insert trigger on this condition doesn't make sense.

like image 71
Gordon Linoff Avatar answered Nov 03 '22 05:11

Gordon Linoff


If anyone like me stumble upon this thread and is getting syntax error, it's because "When you try to raise errors via SIGNAL you need to specify the SQLSTATE which is the error code and for the user defined generic error codes its 45000 along with the message text MESSAGE_TEXT"

So the SIGNAL line should look like this.

signal SQLSTATE VALUE '45000' SET MESSAGE_TEXT = 'Your custom error message';

See this answer for more details.

https://stackoverflow.com/a/42827275/4164651

like image 41
Edmund Lee Avatar answered Nov 03 '22 05:11

Edmund Lee