Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql UPDATE says column cannot be null. Why is it null?

Tags:

sql

mysql

I have a join table called carriers_rects that looks like this:

+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id         | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| carrier_id | int(11) unsigned | NO   |     | NULL    |                |
| rect_id    | int(11) unsigned | NO   |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+

I also have a rects table that looks like this:

+---------+-------------+------+-----+----------+----------------+
| Field   | Type        | Null | Key | Default  | Extra          |
+---------+-------------+------+-----+----------+----------------+
| id      | int(11)     | NO   | PRI | NULL     | auto_increment |
| name    | varchar(54) | NO   |     | new rect |                |
| width   | varchar(54) | NO   |     | NULL     |                |
| length  | varchar(54) | NO   |     | NULL     |                |
| rounded | tinyint(1)  | NO   |     | NULL     |                |
| xx      | varchar(54) | NO   |     | NULL     |                |
| yy      | varchar(54) | NO   |     | NULL     |                |
| height  | varchar(54) | NO   |     | NULL     |                |
+---------+-------------+------+-----+----------+----------------+

I am trying to add a case_id column to rects and just make it a one-to-many relationship and kill the carriers_rects table. We are moving our DB and we never used the many-to-many relationship.

So I added the case_id column to rects:

alter table rects add case_id int(11) not null;

Then I tried to update the case_id on the rects with all the case_id's that would match from the carriers_rects table.

update rects set case_id = (select carrier_id from carriers_rects where rect_id = id);

I am getting column case_id cannot be null.

I tested to see if there where any nulls and I can't seem to find any.

select * from (select * from carriers_rects where rect_id IN(select id from rects)) `b` where id is null;

I also tried it the other way around because honestly I am a little confused.

select id from rects where id IN(select rect_id from carriers_rects)

Clearly I am not a sql genius. But would love to be schooled here. Not sure why I am getting the error mentioned above.

like image 314
Johnston Avatar asked Dec 05 '16 14:12

Johnston


People also ask

How do you fix a column that Cannot be NULL?

Using PHPMYADMIN you can go to the table, Clik on Structure and the under ACTIONS edit the column that is giving you trouble. Then select NULL and save: Or if you prefer coding it directly on a query add the following: ALTER TABLE table_name CHANGE column_name column_name type DEFAULT NULL.

Why am I getting NULL in MySQL?

In MySQL, a NULL value means unknown. A NULL value is different from zero ( 0 ) or an empty string '' . A NULL value is not equal to anything, even itself. If you compare a NULL value with another NULL value or any other value, the result is NULL because the value of each NULL value is unknown.

How do you UPDATE NULL values in a column?

UPDATE [table] SET [column]=0 WHERE [column] IS NULL; Null Values can be replaced in SQL by using UPDATE, SET, and WHERE to search a column in a table for nulls and replace them. In the example above it replaces them with 0. Cleaning data is important for analytics because messy data can lead to incorrect analysis.

Is NULL and is not null in MySQL?

NOT NULL means that the column can not have a NULL value for any record; NULL means NULL is an allowable value (even when the column has a foreign key constraint).


1 Answers

What if you change to a update-join syntax rather than using subquery like

update rects r
join carriers_rects cr on cr.rect_id = r.id
set r.case_id = cr.carrier_id
where cr.carrier_id is not null;
like image 108
Rahul Avatar answered Sep 21 '22 13:09

Rahul