471,092 Members | 1,029 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

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

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.

May 24 '12 #1
1 3111
5,058 Expert 4TB

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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.