If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > NOT NULL field not supplied but still no error

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-29-11, 03:33
newbielgn newbielgn is offline
Registered User
 
Join Date: Jul 2011
Posts: 14
NOT NULL field not supplied but still no error

Hi,

I have set field 'name' as NOT NULL , but still INSERT INTO command given below did not display any error where is have missed 'name' filed.

Could you please explain how to make sure that error gets generated if 'name' filed. is not filled.

Thanks,
newbielgn
mysql> describe student;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| name | varchar(20) | NO | | | |
| sex | enum('F','M') | NO | | | |
| student_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
+------------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)


mysql> INSERT INTO student (sex,student_id) VALUES('M',NULL);
Query OK, 1 row affected, 1 warning (0.00 sec)
Reply With Quote
  #2 (permalink)  
Old 07-29-11, 03:44
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
From the MySQL manual http://dev.mysql.com/doc/refman/5.1/en/insert.html

Quote:
Originally Posted by MySQL Manual
Warnings indicates the number of attempts to insert column values that were problematic in some way. Warnings can occur under any of the following conditions:

* Inserting NULL into a column that has been declared NOT NULL.
You need to issue a SHOW WARNINGS (http://dev.mysql.com/doc/refman/5.1/...-warnings.html) to see the actual error message.

If you change your SQL_MODE to strict (??) than this produce an error and not a warning if I'm not mistaken...
Reply With Quote
  #3 (permalink)  
Old 07-29-11, 04:45
newbielgn newbielgn is offline
Registered User
 
Join Date: Jul 2011
Posts: 14
Thank you very much.

After setting sql_mode , error was displayed when 'name' filed was not input.

mysql> set session sql_mode=STRICT_ALL_TABLES;
mysql> insert into student (sex) values ('M');
ERROR 1364 (HY000): Field 'name' doesn't have a default value
mysql> insert into student (name,sex) values ("newname",'M');
Query OK, 1 row affected (0.00 sec)
Reply With Quote
  #4 (permalink)  
Old 07-29-11, 04:54
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Indeed sql_mode settings need to be changed:

mysql> create table students (name varchar(20) not null, sex enum('F','M') not null, student_id int primary key);
Query OK, 0 rows affected (0.02 sec)

mysql> set sql_mode = '';
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO students (sex,student_id) VALUES('M',1);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+-------------------------------------------+
| Level | Code | Message |
+---------+------+-------------------------------------------+
| Warning | 1364 | Field 'name' doesn't have a default value |
+---------+------+-------------------------------------------+
1 row in set (0.00 sec)

mysql> set sql_mode = 'STRICT_ALL_TABLES';
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO students (sex,student_id) VALUES('M',2);
ERROR 1364 (HY000): Field 'name' doesn't have a default value
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #5 (permalink)  
Old 07-29-11, 05:20
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Just to complete the picture: in case the INSERT succeeds (with the warning) an empty string is stored in that column (not a NULL value).
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On