473,385 Members | 1,548 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.

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 2344

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"...
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/"...
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
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.