tags:

views:

124

answers:

5

I have a table in MySQL that have a few columns that have default values specified, but when I try to insert a row, (not specifying values for those default columns), it throws an error saying I cannot insert NULL values.

I am no MySQL expert, but I would expect you to be able to insert a row without having to specify the values for the columns that have default values specified on them in the table.

Here is the table example;

CREATE TABLE `users` (
  `Id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `UniqueName` varchar(120) NOT NULL,
  `Password` varchar(1000) NOT NULL,
  `PublicFlag` tinyint(1) NOT NULL,
  `NoTimesLoggedIn` int(10) unsigned NOT NULL DEFAULT '0',
  `DateTimeLastLogin` timestamp NOT NULL DEFAULT '1971-01-01 00:00:00',
  `UserStatusTypeId` int(10) unsigned NOT NULL,
  `Private` tinyint(1) NOT NULL DEFAULT '0',
  `SiteName` varchar(255) NOT NULL,
  `CountryId` int(10) NOT NULL DEFAULT '0',
  `TimeZoneId` varchar(255) NOT NULL DEFAULT 'UTC',
  `CultureInfoId` int(10) unsigned NOT NULL DEFAULT '0',
  `DateCreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `UserCreated` int(10) unsigned NOT NULL,
  `LastUpdatedBy` int(10) unsigned NOT NULL,
  `DateLastUpdated` datetime DEFAULT NULL,
  PRIMARY KEY (`Id`),
  UNIQUE KEY `UniqueName_UNIQUE` (`UniqueName`),
  KEY `Index 3` (`SiteName`)
)

It complains about TimeZoneId, and when I populate TimeZoneId, it complains about CultureInforId.

I am using MySQL Version: 5.1.43-community

thanks in advance.

UPDATE: Sorry I removed the SiteName column, and a few others that will just clutter the query, but have now put the original back.

Here is the query I am trying to insert, grabbed from NHibernate Profiler:

Insert Sample: 

INSERT INTO Users
           (UniqueName,
            Password,
            PublicFlag,
            NoTimesLoggedIn,
            DateTimeLastLogin,
            SiteName,
            TimeZoneId,
            DateCreated,
            DateLastUpdated,
            Private,
            CountryId,
            CultureInfoId,
            UserCreated,
            LastUpdatedBy,
            UserStatusTypeId)
VALUES     ('[email protected]','u1uhbQviLp89P9b3EnuN/Prvo3A4KVSiUa0=',1,
0,'1/01/1971 12:00:00 AM','V9O1T80Q6D',NULL,'2/08/2010 2:13:44 AM',
'2/08/2010 2:13:44 AM',0, NULL, NULL, 4, 4,31)

UPDATE: Sorry, I missed pasting 3 columns.

+4  A: 

Use the DEFAULT keyword instead:

INSERT INTO users (TimeZoneId) VALUES (DEFAULT);
Bill Karwin
+1: I remember this coming up recently...
OMG Ponies
Thanks Bill, this works, but as for the answer, I am still investigating as to why it all of a sudden appeared today, and not before. SO it could be NHibernate, or MySQL not sure.
Ryk
If the behavior changed, then you must have changed something in your *environment*, your *code*, or your *data*. I can't guess which one. Or else the behavior did not change -- it worked this way all along and you never noticed. That happens to me sometimes.
Bill Karwin
+2  A: 

Do not insert NULL values. I'm assuming you were trying this syntax:

INSERT INTO users VALUES (null, 'Jones', 'yarg', 1, null, null, null);

Instead, use this syntax:

INSERT INTO users SET UniqueName='Jones', Password='yarg';

For more info, see the MySQL docs on INSERT.

Mark Eirich
This could be it, I am investigating now, since this was working before and just started complaining about NULL inserts.
Ryk
When you insert nulls on a multi-row insert, MySQL gives a warning instead of failing. Perhaps it was always doing a multi-row insert before, or something odd like that.
Mark Eirich
+1  A: 

You have "NOT NULL" set on fields that you are trying to INSERT NULL on.

eg. CountryId, CultureInfoId, TimeZoneId

execute the following:

ALTER TABLE `users` MODIFY `CountryId` int(10) DEFAULT '0' NULL;
ALTER TABLE `users` MODIFY `CultureInfoId` int(10) unsigned DEFAULT '0' NULL;
ALTER TABLE `users` MODIFY `TimeZoneId` varchar(255) DEFAULT 'UTC' NULL;

EDIT: Didn't realize he wanted the default value instead of NULL on "null" insert. Basically as already has been suggested use the DEFAULT keyword in place of NULL on the values.

OR leave the NULL fields and values out altogether and mysql will use the defined defaults eg.

INSERT INTO Users
       (UniqueName,
        Password,
        PublicFlag,
        NoTimesLoggedIn,
        DateTimeLastLogin,
        SiteName,
        DateCreated,
        DateLastUpdated,
        Private,
        UserCreated,
        LastUpdatedBy,
        UserStatusTypeId)
VALUES     ('[email protected]','u1uhbQviLp89P9b3EnuN/Prvo3A4KVSiUa0=',1,
0,'1/01/1971 12:00:00 AM','V9O1T80Q6D','2/08/2010 2:13:44 AM',
'2/08/2010 2:13:44 AM',0, 4, 4,31)
Josh Stuart
Yes, but the moment I allow null's it actually insert nulls and not the default values.
Ryk
Ah sorry should of read the q better :D. The DEFAULT keyword should be used then OR you leave them out of the INSERT process altogether.eg. INSERT INTO `users` (all_fields_except_nulls) VALUES (all_values_except_nulls)
Josh Stuart
Thats what the previous 2 answers said. Thanks a lot though, appreciate the help.
Ryk
Yeah soz. Should of read the q better.
Josh Stuart
A: 

As an alternative to using the DEFAULT keyword you can also just not specify values for the fields which you want to have default values. For instance if you just removed TimeZoneId, CountryId and CultureInfoId from your query entirely those columns will receive the default values automatically:

INSERT INTO Users
    (UniqueName,
    Password,
    PublicFlag,
    NoTimesLoggedIn,
    DateTimeLastLogin,
    SiteName,
    DateCreated,
    DateLastUpdated,
    Private,
    UserCreated,
    LastUpdatedBy,
    UserStatusTypeId)
VALUES
    ('[email protected]','u1uhbQviLp89P9b3EnuN/Prvo3A4KVSiUa0=',1,0,
    '1/01/1971 12:00:00 AM','V9O1T80Q6D','2/08/2010 2:13:44 AM',
    '2/08/2010 2:13:44 AM',0,4,4,31)

I'm not sure how that would work in the context of NHibernate however as that part of the question wasn't quite as well explained.

Rosco
Thanks Rosco. That will work yes, but it has already been suggested.
Ryk
So it was, didn't notice the other answer had been updated =)
Rosco
+1  A: 

TimeZoneIdvarchar(255) NOT NULL DEFAULT 'UTC', CultureInfoId` int(10) unsigned NOT NULL DEFAULT '0',

For this fields you have set constraints as "Not Null" and hence values inserted can't be null and hence either alter the table structure or just not specify values for the fields which you want to have default values.

Ankur Mukherjee