Pregunta

I restored a mysql database from a mysql server running on linux to one running on windows. Everything seemed fine until I tried to run an ALTER TABLE on one of the tables. The error I get if I try to ALTER anything is: Error Code: 1067. Invalid default value for 'creation_date'

Now the strange thing is that dump file successfully restored and created the table. If I use Workbench to get the create statement for the table I see:

`creation_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',

For the column which is causing an issue.

How can the CREATE TABLE statement succeed in such a way that I cannot perform alters on it now?

¿Fue útil?

Solución

Some background on sql_mode and 'NO_ZERO_DATE': http://dev.mysql.com/doc/refman/5.1/en/sql-mode.html

You mentioned that you restored the tables from another server. If you used mysqldump then the answer to your question is that mysql turned off 'NO_ZERO_DATE' mode while loading the dumped SQL. Mysqldump (tested in 5.5) puts this line at the top of the dumped SQL:

/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;

It also turns off foreign key checks and other useful things while loading the dump.

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;

This explains why you were able to restore the tables, but when trying to alter you must play by stricter rules. If you really want to override this you can try the following just before your alter table:

mysql> SET SESSION sql_mode='';

Then alter table.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top