Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1107611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:57:45+00:00 2026-05-17T01:57:45+00:00

In my MySQL table, every column by itself can be NULL, but there must

  • 0

In my MySQL table, every column by itself can be NULL, but there must be at least one column with a non-NULL value. At the moment, I am wrapping an insert statement in a stored procedure which prevents insertion of all-NULL rows, but that of course does not keep anyone from using native INSERT statements, circumventing my wrapper procedure.

Is there a ‘native’ way to define the table with that constraint ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:57:45+00:00Added an answer on May 17, 2026 at 1:57 am

    Since MySQL doesn’t enforce check constraints, you may want to emulate one with a trigger. I suggest checking out this MySQL Forge article:

    • Triggers : Emulating Check Constraints

    The idea is this to move your check logic to a trigger. If the check fails, call a stored procedure that fails by raising a unique key violation. This allows us to return a descriptive error message back to the client.

    Your trigger will probably look something like this:

    DELIMITER $$
    CREATE TRIGGER check_not_all_null BEFORE INSERT ON your_table FOR EACH ROW
    BEGIN
       IF COALESCE(field_1, field_2, field_3) IS NOT NULL THEN
          CALL fail('All fields cannot be null');
       END IF;
    END $$
    DELIMITER ;
    

    We need to make the fail sproc raise a unique key violation in order to have the INSERT aborted when the check fails. The above mentioned article suggests creating a memory table defined as follows:

    CREATE TABLE `Error` (                                                               
       `ErrorGID` int(10) unsigned NOT NULL auto_increment,                               
       `Message`  varchar(128) default NULL,                                
       `Created`  timestamp NOT NULL default CURRENT_TIMESTAMP
                  ON UPDATE CURRENT_TIMESTAMP,           
        PRIMARY KEY (`ErrorGID`),                                                   
        UNIQUE KEY `MessageIndex` (`Message`)
    ) ENGINE=MEMORY DEFAULT CHARSET=latin1 ROW_FORMAT=FIXED 
    

    Then the fail sproc could be implemented as follows:

    DELIMITER $$
    CREATE PROCEDURE `fail`(_message VARCHAR(128))
    BEGIN
      INSERT INTO error (message) VALUES (_message);
      INSERT INTO error (message) VALUES (_message);
    END$$
    DELIMITER ;
    

    The double INSERT will ensure that the unique key violation is raised. If the same message already exists in the table, the violation will get raised on the first INSERT, but it doesn’t matter as long as it fails.

    We can try the fail sproc from the command line:

    mysql> CALL fail('All fields cannot be null');
    ERROR 1062 (23000): Duplicate entry 'All fields cannot be null' for key 2
    

    The good news is that we get back a readable error message. However we don’t get back the correct error code, and we don’t really have a “duplicate entry”. This is obviously one limitation of this method, especially when updating or inserting records in a procedure which uses error handling, in particular handling the 1062 Duplicate Entry error specifically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.