Disallow MySQL to accept Empty string

Codes here !

Moderators: macek, egami, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 87
Joined: Wed May 16, 2012 4:17 pm

Disallow MySQL to accept Empty string

Post by jplus2 » Tue May 22, 2012 9:37 pm

I found out that even if you declare a column in a table in mysql as NOT NULL
it would still allow you to insert an empty string. ''.

how can I prevent this from happening in mysql?
I can always make the check in php, but, I want it to be in both ends.

Thank you in advance guys

User avatar
php-forum Fan User
php-forum Fan User
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: Disallow MySQL to accept Empty string

Post by Nullsig » Wed May 23, 2012 7:26 am

Technically you could do it with a trigger.

You would need to execute it in MySQL console or in PHPMyAdmin

Code: Select all

CREATE TRIGGER <name of the trigger> BEFORE INSERT ON <name of table>
  IF NEW.<fieldname> = ''
    SET NEW='Error: Cannot insert record empty value for <fieldname>';

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest