Friday, 18 August 2017

sql - MySQL INSERT INTO syntax





I have the following structure



user_id int(11)
right int(11)
group_id int(11)

value tinyint(1)


And 3 queries



INSERT INTO  user_rights (`user_id`,`right`,`group_id`,`value`)
VALUES ( '42', '160', '1', '1' );

INSERT INTO user_rights ('user_id','right','group_id','value')
VALUES ( '42', '160', '1', '1' );


INSERT INTO user_rights (user_id,right,group_id,value)
VALUES ( '42', '160', '1', '1' );


Explain me WHYYYY only the first works????



I have all my life used the third one!


Answer



RIGHT is a mySQL reserved word. It will work only when wrapped in backticks.




When you're not using reserved words, it will work without the backticks, as well.



The second way will never work because quotes are used to quote strings, but never database, table or column identifiers.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...