473,395 Members | 2,443 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,395 software developers and data experts.

Enforcing NOT NULL restriction on UPDATE

I am experiencing odd behavior, and I'm hoping someone can explain why
it is behaving this way, and how to get around it...

When I update a row in a table with a field's data set to NULL, but
the table has a NOT NULL restriction on the field, the insert still
completes successfully, but transforms the NULL value to an empty
string.

The preferred behavior would be for mysql to emit and error and not
accept the attempt to set a NULL value for a NOT NULL field, and not
transform the data.

Sample sql to reproduce is below:

create table domain
(
-- define columns
id int(10) unsigned not null auto_increment,
name varchar(128) not null,

-- set primary key
primary key (id),

-- build indexes
unique key domain_name (name),

) type=innodb;
mysql> insert into domain (name) values (NULL);
ERROR 1048: Column 'name' cannot be null
-- This is desired behavior;

mysql> insert into domain (name) values ('google.com');
Query OK, 1 row affected (0.01 sec)

mysql> select * from domain;
+----+------------+
| id | name |
+----+------------+
| 57 | google.com |
+----+------------+
1 row in set (0.00 sec)

mysql> update domain set name = NULL where id = 57;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
-- update is accepted, why??
-- shouldn't it error??

mysql> select * from domain;
+----+------+
| id | name |
+----+------+
| 57 | |
+----+------+
1 row in set (0.01 sec)

mysql> select * from domain where name = '';
+----+------+
| id | name |
+----+------+
| 57 | |
+----+------+
1 row in set (0.00 sec)

mysql> select * from domain where name = NULL;
Empty set (0.00 sec)

For some reason, 'show create table domain' shows a different sql
statement that what I actually used to create the table, notably the
appended "default ''" on the definition of the name field:

CREATE TABLE `domain` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(128) NOT NULL default '',
PRIMARY KEY (`id`),
UNIQUE KEY `domain_name` (`name`)
) TYPE=InnoDB

I'm not sure why mysql is allowing attempted inserts of a NULL value
on a NOT NULL field to succeed, or why it is altering the data being
sent in. Is there a way to suppress this behavior and have it be more
strict in what data it accepts for NOT NULL fields?

Thanks for any input.
Jul 19 '05 #1
0 1684

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

Similar topics

0
by: Steven Hilton | last post by:
I am experiencing odd behavior, and I'm hoping someone can explain why it is behaving this way, and how to get around it... When I update a row in a table with a field's data set to NULL, but...
1
by: Saqib Ali | last post by:
I have created 2 tables in my MySQL database. A_TAB and B_TAB. They have auto-incrementing integer primary keys respectively named A_ID & B_ID. When I created B_TAB, I declared a field named A_ID...
3
by: spw | last post by:
Hello, I am beginner/intermediate user of Access and primariy use the 'Tables' and Queries' objects. I have a query that is pointing to a set of tables that will be updated every week. This...
2
by: Frosty | last post by:
Howto make datagrid enforce rules of xml schema? Created xml schema in the designer. Constraints created there using the following <xs:simpleType name="zipcode"><xs:restriction...
0
by: Frosty | last post by:
Following xsd ***************************** <?xml version="1.0" encoding="utf-8" ?><xs:schema id="EmployeesSchema" targetNamespace="http://tempuri.org/EmployeesSchema.xsd"...
5
by: bclark76 | last post by:
I am getting a strange error, maybe someone knows why it is occurring.. I get the following error when I try to validate Untitled8.xml in Altova XMLSPY: Validation error in another file:...
1
by: Thelma Lubkin | last post by:
I have a form w/ 11 identical text boxes in each of which user can enter a number between 1 and 11. Each defaults to 0, which I use to flag empty. Each has the 1 to 11 restriction as its validation...
5
by: hello | last post by:
How can I define the schema so that myage element has to be double or null? <xs:simpleType name="myage"> <xs:restriction base="xs:double"> <xs:enumeration value="null"/> </xs:restriction>...
7
by: montyphyton | last post by:
Some recent posts about Python programming style got me thinking. Since we have the PEP 8 which gives some guidelines about the style to be used, do we have any program that can check for...
2
by: blixit | last post by:
I have a WSDL: <?xml version="1.0" encoding="utf-8"?> <!-- edited with XMLSpy v2008 sp1 (http://www.altova.com) by me --> <definitions xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.