By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,982 Members | 1,939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,982 IT Pros & Developers. It's quick & easy.

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

P: 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
Share this Question
Share on Google+
1 Reply


Atli
Expert 5K+
P: 5,058
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.