473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

not null column with null defaults

I know this has been asked before and yes, I have read the section in
the documentation about it.
But, my question is: If I have setup mysql with strict_trans_tables,
will MySQL allow "null" defaults in "not null" columns?
If the answer is yes, will that information be reflected when I ask for
metadata (column information, etc.)?
Currently, when I ask for metadata information, not-null columns will
return a default of "empty string".

As additional information, I currently use 5.0.21-community-nt

Jul 29 '06 #1
6 14376
ax******@gmail.com wrote:
But, my question is: If I have setup mysql with strict_trans_tables,
will MySQL allow "null" defaults in "not null" columns?
No: "NOT NULL DEFAULT NULL" is not a legal combination in strict mode.

Specifying no DEFAULT clause has the same effect as specifying DEFAULT
NULL, such that inserting to the table without specifying a value for
the column attempts to insert NULL to that column. A NOT NULL column
rejects it, and a NULLable column uses NULL as the implicit default.

The only difference is that "SHOW CREATE TABLE" reports "DEFAULT NULL"
if the column is NULLable, and no default clause for a column that is
declared as NOT NULL.

CREATE TABLE foo (i int NOT NULL);
SHOW CREATE TABLE foo;
-CREATE TABLE `foo` (
`i` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1

CREATE TABLE bar (i int);
SHOW CREATE TABLE bar;
-CREATE TABLE `bar` (
`i` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1

SELECT VERSION();
-5.0.21-community-nt-log

SELECT @@SQL_MODE;
-STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_ SUBSTITUTION

Regards,
Bill K.
Jul 30 '06 #2
Thanks for your reply.

I do have one more problem..

given the following table (I have strict_trans_tables):

create table some_table
(id int not null auto_increment,
some_text varchar(20) not null,
other_text varchar(20) default 'aaa',
another_text varchar(20),
primary key (id)
) type=innodb;

I execute "show columns from some_table" using both ODBC, connector/J
and mysql command line.
The command line returns "some_text" as having a NULL default.
ODBC and connector/J return "some_text" as having an empty string as
default.
While all of them report "another_text" as having a NULL default

Is this a bug? Is this by design?
Bill Karwin wrote:
ax******@gmail.com wrote:
But, my question is: If I have setup mysql with strict_trans_tables,
will MySQL allow "null" defaults in "not null" columns?

No: "NOT NULL DEFAULT NULL" is not a legal combination in strict mode.

Specifying no DEFAULT clause has the same effect as specifying DEFAULT
NULL, such that inserting to the table without specifying a value for
the column attempts to insert NULL to that column. A NOT NULL column
rejects it, and a NULLable column uses NULL as the implicit default.

The only difference is that "SHOW CREATE TABLE" reports "DEFAULT NULL"
if the column is NULLable, and no default clause for a column that is
declared as NOT NULL.

CREATE TABLE foo (i int NOT NULL);
SHOW CREATE TABLE foo;
-CREATE TABLE `foo` (
`i` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1

CREATE TABLE bar (i int);
SHOW CREATE TABLE bar;
-CREATE TABLE `bar` (
`i` int(11) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1

SELECT VERSION();
-5.0.21-community-nt-log

SELECT @@SQL_MODE;
-STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_ SUBSTITUTION

Regards,
Bill K.
Jul 31 '06 #3
ax******@gmail.com wrote:
create table some_table
(id int not null auto_increment,
some_text varchar(20) not null,
other_text varchar(20) default 'aaa',
another_text varchar(20),
primary key (id)
) type=innodb;

I execute "show columns from some_table" using both ODBC, connector/J
and mysql command line.
The command line returns "some_text" as having a NULL default.
I just tried your example table on my instance of MySQL 5.0.21, and it
shows this:

mysqlshow columns from some_table;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| some_text | varchar(20) | NO | | | |
| other_text | varchar(20) | YES | | aaa | |
| another_text | varchar(20) | YES | | NULL | |
+--------------+-------------+------+-----+---------+----------------+

Apologies if the formatting gets messed up by proportional fonts.

Anyway, notice that the "some_text" column shows NO for its nullability,
and an empty space for its default value.

So it is consistent with what you saw in ODBC and Java, and also agrees
with the docs on the issue. Perhaps you misread the output of show
columns in the mysql monitor?

Regards,
Bill K.
Jul 31 '06 #4
Here's my output... 5.0.21 running under XP.
Notice the column some_text.. Again, apologies for the lack of
formatting.

mysqlselect version() ;
+---------------------+
| version() |
+---------------------+
| 5.0.21-community-nt |
+---------------------+
1 row in set (0.00 sec)

mysql>
mysql>
mysqlshow columns from my_table ;
+---------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| some_text | varchar(20) | NO | | NULL | |
| other_text | varchar(20) | YES | | aaa | |
| another_table | varchar(20) | YES | | NULL | |
+---------------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysqlshow create table my_table ;
+----------+----------------------------------
----------------------------------------------
---------------------------------------------+
| Table | Create Table

|
+----------+----------------------------------
----------------------------------------------
---------------------------------------------+
| my_table | CREATE TABLE "my_table" (
"id" int(11) NOT NULL auto_increment,
"some_text" varchar(20) NOT NULL,
"other_text" varchar(20) default 'aaa',
"another_table" varchar(20) default NULL,
PRIMARY KEY ("id")
) |
+----------+----------------------------------
----------------------------------------------
---------------------------------------------+
Bill Karwin wrote:
ax******@gmail.com wrote:
create table some_table
(id int not null auto_increment,
some_text varchar(20) not null,
other_text varchar(20) default 'aaa',
another_text varchar(20),
primary key (id)
) type=innodb;

I execute "show columns from some_table" using both ODBC, connector/J
and mysql command line.
The command line returns "some_text" as having a NULL default.

I just tried your example table on my instance of MySQL 5.0.21, and it
shows this:

mysqlshow columns from some_table;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| some_text | varchar(20) | NO | | | |
| other_text | varchar(20) | YES | | aaa | |
| another_text | varchar(20) | YES | | NULL | |
+--------------+-------------+------+-----+---------+----------------+

Apologies if the formatting gets messed up by proportional fonts.

Anyway, notice that the "some_text" column shows NO for its nullability,
and an empty space for its default value.

So it is consistent with what you saw in ODBC and Java, and also agrees
with the docs on the issue. Perhaps you misread the output of show
columns in the mysql monitor?

Regards,
Bill K.
Jul 31 '06 #5
Here's my fundamental problem...

If I have a "not null" column with no default, MySQL treats it as if it
had "default NULL".
That's great, other DBMS do have "not null default null" as well.

My problem is whenever I query metadata or something like "show columns
from some_table"; MySQL reports these columns as having an "empty
string".

Now, from my point of view, an "empty string" is not the same thing as
a "null" value.

Could someone modify MySQL so that it returns a default of NULL
whenever "strict_trans_tables" is active?
Bill Karwin wrote:
ax******@gmail.com wrote:
create table some_table
(id int not null auto_increment,
some_text varchar(20) not null,
other_text varchar(20) default 'aaa',
another_text varchar(20),
primary key (id)
) type=innodb;

I execute "show columns from some_table" using both ODBC, connector/J
and mysql command line.
The command line returns "some_text" as having a NULL default.

I just tried your example table on my instance of MySQL 5.0.21, and it
shows this:

mysqlshow columns from some_table;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| some_text | varchar(20) | NO | | | |
| other_text | varchar(20) | YES | | aaa | |
| another_text | varchar(20) | YES | | NULL | |
+--------------+-------------+------+-----+---------+----------------+

Apologies if the formatting gets messed up by proportional fonts.

Anyway, notice that the "some_text" column shows NO for its nullability,
and an empty space for its default value.

So it is consistent with what you saw in ODBC and Java, and also agrees
with the docs on the issue. Perhaps you misread the output of show
columns in the mysql monitor?

Regards,
Bill K.
Aug 1 '06 #6
axelsino wrote:
My problem is whenever I query metadata or something like "show columns
from some_table"; MySQL reports these columns as having an "empty
string".
The output of "SHOW COLUMNS" or "DESCRIBE" is unfortunately ambiguous,
because it gives no visible distinction between no default and default ''.

Have a look at the output of "SHOW CREATE TABLE tablename" instead. It
makes the difference more clear.

You can also use the INFORMATION_SCHEMA if you use MySQL 5.0 or later:

create table test.foo (
c1 char(10) not null,
c2 char(10) not null default ''
);

select table_name, column_name, is_nullable, column_default
from information_schema.columns
where table_schema = 'test';

Returns:

table_name column_name is_nullable column_default
'foo' 'c1' 'NO' null
'foo' 'c2' 'NO' ''

Regards,
Bill K.
Aug 1 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jamie Burns | last post by:
Hello, I am trying to perform a LEFT JOIN on a table which may or may not have matching rows. If there are no matching rows, it returns NULL's for all the missing fields. Is there anyway of...
3
by: iStrain | last post by:
Hiya. I'm _sure_ this is an FAQ, but Googling hasn't produced the answer in a way I can make sense out of. I know I should get this, but so far no way... I'm creating tables and doing queries in...
8
by: btober | last post by:
I'm finding that column defaults are not being assigned to nulls when I do an insert by way of a an ON INSERT rule on a view. For example, the following script \set ON_ERROR_STOP ON \c...
19
by: Baldur Norddahl | last post by:
Hi, How come "X=null" is not the same as "X is null"? I got a few selects with queries like this: select * from foo where customer=#customer# or (#customer# is null and customer is null) ...
3
by: subaga | last post by:
Hi, I have to load data into a table with one of the columns defined as NOT NULL, but the file does not have data for it. i would like to load a constant value for this column and the constant...
5
by: Vicky | last post by:
What is the best way of converting Null value in datatable numeric column to 0. THanks
2
by: zeljko.prince | last post by:
This is a copy from http://forums.mysql.com/read.php?10,73797,73797#msg-73797. Perhaps someone on this group will know the answer. Given the following table: CREATE TABLE foo(field VARCHAR(20)...
26
by: ryampolsky | last post by:
I'm using strtok to break apart a colon-delimited string. It basically works, but it looks like strtok skips over empty sections. In other words, if the string has 2 colons in a row, it doesn't...
8
by: DaFrizzler | last post by:
Hi, I have received the following email from a colleague, and am quite frankly baffled by the idea. I am just wondering if anyone has any advice or suggestions about this???? === BEGIN MAIL...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.