473,657 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2361

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

Similar topics

0
1695
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 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...
1
3019
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 which references A_TAB.AID. I insert valid data into both tables. However, the foreign key constraint is NOT being enforced. The database allows be to enter any integer into B_TAB.AID regardless of weather that value exists anywhere in the...
3
1532
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 query summarizes data and links to an Excel template report. Each week, I will need to do two things: 1) Re-import the data into Access and over-write the old tables, and 2) Update the date restriction in the where statement because the time...
2
2247
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 base="xs:string"><xs:pattern value="\d{5}" /></xs:restriction></xs:simpleType Datagrid does not enforce this rule, even though dataGrid1_Validating() is called. dataset.WriteXml() saves withouth any warning, but
0
1026
by: Frosty | last post by:
Following xsd ***************************** <?xml version="1.0" encoding="utf-8" ?><xs:schema id="EmployeesSchema" targetNamespace="http://tempuri.org/EmployeesSchema.xsd" elementFormDefault="qualified xmlns="http://tempuri.org/EmployeesSchema.xsd" xmlns:mstns="http://tempuri.org/EmployeesSchema.xsd xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:simpleType name="ZipCode"><xs:restriction base="xs:positiveInteger"><xs:pattern value="\d{5}"...
1
1721
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 rule, but I also need to enforce no duplicates among them. I haven't been able to figure out how to do this in the validation rule, so I have a function that gets as a parameter a number to identify which textbox is calling it that handles that...
5
7270
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> </xs:simpleType> </xs:schema>
7
1793
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 violations of these guidelines within the source code? I understand that there are a lot of code beautifiers out there, but i haven't seen one specially tailored for Python... Is there even a desire in Python community for a program like this (by Python...
2
2403
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/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:s0="http://***/directory/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"...
0
8312
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4159
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2732
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1622
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.