how to overcome these type of issues where null value inserted into primary key not null column in table
As was stated in the comment
You're not inserting a NULL value, you're inserting ""
If you want to prevent an empty string ("") and NULLs from being used as ai_name
, you need a trigger. In your particular case, you need a BEFORE INSERT
trigger:
DELIMITER $$
CREATE TRIGGER check_for_blank_ai_name
BEFORE INSERT ON actionitems
FOR EACH ROW
BEGIN
DECLARE dummy INT;
IF ( IFNULL(NEW.ai_name,'') = '' ) THEN
SELECT 'Any Message' INTO dummy FROM mysql.user WHERE used = 'anything';
END IF;
END $$
DELIMITER ;
The line of code
SELECT 'Any Message' INTO dummy FROM mysql.user WHERE used = 'anything';
will break. That's by design. This will prevent the INSERT from occurring.
I have written triggers that break midstream before in my other posts:
Jun 09, 2013
: BEFORE INSERT trigger in MySQLDec 11, 2012
: Comparing dates in a BEFORE INSERT triggerDec 23, 2011
: check constraint does not work?Apr 25, 2011
: Trigger in MySQL to prevent insertion
Give it a Try !!!
NULL
value, you're inserting""
. – LowlyDBA Jan 25 '15 at 15:28