468,514 Members | 1,528 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,514 developers. It's quick & easy.

How does MYSQL handle NOT NULL columns when added to tables with records?

61
Hi guys,

I am trying to work out how MYSQL deals with NULL values.

When a new column is inserted on a table already populated with records (40,000 in my case), and I specify it as NOT NULL, I can insert values without any issues. I was expecting some kind of DB errors to throw up.

This is when adding new records from both PhpMyAdmin & the PHP applications.

Does MYSQL have any utility to handle these cases? Like when a column is specified as Varchar it puts in '' or when its of type INT it puts in 0.

Thanks.
May 24 '12 #1
1 3055
Atli
5,058 Expert 4TB
Hey.

The way MySQL handles this depends on the mode it is running in. You may want to read this page for details on how this all works:
11.1.4. Data Type Default Values

For data entry into a NOT NULL column that has no explicit DEFAULT clause, if an INSERT or REPLACE statement includes no value for the column, or an UPDATE statement sets the column to NULL, MySQL handles the column according to the SQL mode in effect at the time:
  • If strict SQL mode is enabled, an error occurs for transactional tables and the statement is rolled back. For nontransactional tables, an error occurs, but if this happens for the second or subsequent row of a multiple-row statement, the preceding rows will have been inserted.
  • If strict mode is not enabled, MySQL sets the column to the implicit default value for the column data type.
Implicit defaults are defined as follows:
  • For numeric types, the default is 0, with the exception that for integer or floating-point types declared with the AUTO_INCREMENT attribute, the default is the next value in the sequence.
  • For date and time types other than TIMESTAMP, the default is the appropriate “zero” value for the type. For the first TIMESTAMP column in a table, the default value is the current date and time. See Section 11.3, “Date and Time Types”.
  • For string types other than ENUM, the default value is the empty string. For ENUM, the default is the first enumeration value.
May 25 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Marcus | last post: by
10 posts views Thread by Python_it | last post: by
6 posts views Thread by Dean Slindee | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.