MySQL column set to NOT NULL but still allowing NULL values

doitlikejustin

I have every column set to NOT NULL but for some reason I am still able to add a NULL value in each column. Here is my table info (create syntax):

CREATE TABLE `addresses` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `address` varchar(100) NOT NULL,
    `city` varchar(100) NOT NULL,
    `state` varchar(4) NOT NULL,
    `zip` varchar(30) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4545 DEFAULT CHARSET=utf8;

Here is a sample INSERT that works:

INSERT INTO `addresses` (`street`, `city`, `state`, `zip`) VALUES ('', '', '', '');

Any ideas as to why this is happening?

Sudhir Bastakoti

You are inserting empty strings, and empty string are not NULL, to check for NULL error do:

INSERT INTO `addresses` (`street`, `city`, `state`, `zip`) VALUES (NULL, NULL, NULL, NULL);

and you will see error. The NOT NULL checks only for values that are not NULL.

To prevent empty string either you have to use triggers, or do the checks on server side programming language to convert empty strings to NULL before performing INSERT query. An example trigger for INSERT may be like: (this is just an example)

CREATE TRIGGER avoid_empty
    BEFORE INSERT ON addresses
        FOR EACH ROW
        BEGIN
        IF street = '' THEN SET street = NULL END IF;
END; 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Migration: How to make it an index while still allowing for nil values?

From Dev

mysql how to update a column of every row with a given set of values

From Dev

How can I set foldlevelstart in vim to just fold nothing initially, still allowing zr to work immediately?

From Dev

How do I set an element's width while still allowing text to stretch it?

From Dev

Set alias for column values

From Dev

MySQL column with multiple values

From Dev

MySQL - Get values by Column

From Dev

Multiple values in column MYSQL

From Dev

in mysql how to set a column for every row in a table from a give set of values

From Dev

in mysql how to set a column for every row in a table from a give set of values

From Dev

How do i set the HTML tag's “background” property with JS, still allowing other properties from a CSS file to be unchanged?

From Dev

Conditionally Select and Set Column Values

From Dev

Using MySQL "NOT IN" but allowing for substrings

From Dev

allowing users to edit in the mysql

From Dev

allowing users to edit in the mysql

From Dev

Using MySQL "NOT IN" but allowing for substrings

From Dev

Create a new column by comparing set of column values

From Dev

MySQL query to get value of coumn 1 having set of values in column 2

From Dev

Disable editing on combobox but still allowing search:possible?

From Dev

TableView AllowsMultipleSelection - Still allowing multiple selections

From Dev

ACL group permission still not allowing write permissions

From Dev

Separating SQL Tables while still allowing connections

From Dev

How to shift column values in MySQL?

From Dev

adding two column values in mysql

From Dev

MySQL Finding column values in rows

From Dev

Concatenate mysql column values in php

From Dev

fetch MySQL column values separately

From Dev

Compare column values in mysql tables

From Dev

Concat values into new column in mySQL

From Dev

MySQL Finding column values in rows

Related Related

  1. 1

    Migration: How to make it an index while still allowing for nil values?

  2. 2

    mysql how to update a column of every row with a given set of values

  3. 3

    How can I set foldlevelstart in vim to just fold nothing initially, still allowing zr to work immediately?

  4. 4

    How do I set an element's width while still allowing text to stretch it?

  5. 5

    Set alias for column values

  6. 6

    MySQL column with multiple values

  7. 7

    MySQL - Get values by Column

  8. 8

    Multiple values in column MYSQL

  9. 9

    in mysql how to set a column for every row in a table from a give set of values

  10. 10

    in mysql how to set a column for every row in a table from a give set of values

  11. 11

    How do i set the HTML tag's “background” property with JS, still allowing other properties from a CSS file to be unchanged?

  12. 12

    Conditionally Select and Set Column Values

  13. 13

    Using MySQL "NOT IN" but allowing for substrings

  14. 14

    allowing users to edit in the mysql

  15. 15

    allowing users to edit in the mysql

  16. 16

    Using MySQL "NOT IN" but allowing for substrings

  17. 17

    Create a new column by comparing set of column values

  18. 18

    MySQL query to get value of coumn 1 having set of values in column 2

  19. 19

    Disable editing on combobox but still allowing search:possible?

  20. 20

    TableView AllowsMultipleSelection - Still allowing multiple selections

  21. 21

    ACL group permission still not allowing write permissions

  22. 22

    Separating SQL Tables while still allowing connections

  23. 23

    How to shift column values in MySQL?

  24. 24

    adding two column values in mysql

  25. 25

    MySQL Finding column values in rows

  26. 26

    Concatenate mysql column values in php

  27. 27

    fetch MySQL column values separately

  28. 28

    Compare column values in mysql tables

  29. 29

    Concat values into new column in mySQL

  30. 30

    MySQL Finding column values in rows

HotTag

Archive