Unable to insert row into MySQL text table

For some reason, my queries freeze when I write to a text column. Here is an example:

Describe messages;

Field         Type          Null  Key  Default  Extra
id            int(11)       NO    PRI  NULL     auto_increment
title         varchar(255)  YES        NULL 
body          text          YES        NULL 
to            text          YES        NULL 
content_type  varchar(255)  YES        NULL 
is_sms        tinyint(1)    YES        NULL 
user_id       int(11)       YES        NULL 
created_at    datetime      YES        NULL 
updated_at    datetime      YES        NULL

Then I will try to insert:

INSERT INTO messages (id,title,body,to) VALUES ('1','Test Message','This is a test message. This is a test message. This is a test message. This is a test message.', 'an email' );

For some reason, this causes a common MySQL syntax error. The query works fine if I remove the "to" column and its corresponding value from the query.

Any ideas?

+3
source share
5 answers

'to' is a reserved keyword in MySQL. You will need to rename the column.

http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html

However, reserved words are allowed as identifiers if you quote them.

http://dev.mysql.com/doc/refman/5.1/en/identifiers.html

+9

INSERT INTO messages (`id`,`title`,`body`,`to`) 
   VALUES ('1','Test Message','This is a test message. 
   This is a test message. This is a test message. This is a test message.', 
   'an email' );
+4
INSERT
INTO     messages (id,title,body,`to`)
VALUES   ('1','Test Message','This is a test message. This is a test message. This is a test message. This is a test message.', 'an email' );
+3

, "to" backtics:

INSERT INTO messages (id,title,body,`to`) VALUES ('1','Test Message','This is a test message. This is a test message. This is a test message. This is a test message.', 'an email' );

- .

+1

MySQL; : 'to', 'not', 'join'...

INSERT INTO (id, title, body, test)  VALUES ('1', 'Test Message', ' .   . . . ',  'e-mail');

0

Source: https://habr.com/ru/post/1706471/


All Articles