CREATE TABLE foo ( x varchar(255) ); > INSERT INTO foo VALUES ( x='foo' ); Query OK, 1 row affected (0.01 sec)
I know that's not right, and as the answer above indicates that it should be
INSERT INTO tbl SET, but what's happening? How is MySQL interpreting that? In PostgreSQL, such a syntax is rejected.
test=# CREATE TABLE foo ( x varchar(255) ); INSERT INTO foo VALUES ( x='foo' );
PostgreSQL interprets it as a column reference and errors out, further providing a hint
ERROR: column "x" does not exist LINE 1: INSERT INTO foo VALUES ( x='foo' ); HINT: There is a column named "x" in table "foo", but it cannot be referenced from this part of the query.
MySQL Seems to check to make sure the field name exists at run time but for what end I have no idea.
> INSERT INTO foo VALUES ( DOESNOTEXIST=5 ); ERROR 1054 (42S22): Unknown column 'DOESNOTEXIST' in 'field list' > INSERT INTO foo VALUES ( x=5 ); Query OK, 1 row affected (0.01 sec)
If it does not exist you get an error. I'm not sure what happens if it does exist though.
NULL is inserted, but why? Why that would be any less of an error. Can someone explain?
I know MySQL has a User-Defined Variable syntax, however I don't think that has anything to do with it here.