INSERT INTO error in mysql (desc included) - mysql

I got tables like below and I am trying to INSERT data.
mysql> desc subscribers
-> ;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | smallint(6) | NO | PRI | NULL | auto_increment |
| email | varchar(32) | NO | | NULL | |
| comment | text | NO | | NULL | |
+---------+-------------+------+-----+---------+----------------+
Data insert
INSERT INTO `subscribers` VALUES (1, ‘someone#gmail.com’, ‘thanks’);
But It gives me an error.
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '#gmail.com’, ‘ thanks’)' at line 1
Does anybody see an error??

You are surrounding your strings to be inserted with some sort of apostrophe instead of a single quote. Use this instead (and copy it verbatim from this answer):
INSERT INTO `subscribers`
VALUES (1, 'someone#gmail.com', 'thanks');

Your problem is the curly single quote. But, to prevent future errors, you should always include the column list in the insert:
INSERT INTO `subscribers`(id, email, comment)
VALUES (1, 'someone#gmail.com', 'thanks');
In fact, a natural way to create the table is using an auto-incremented id. If you did then, then the INSERT would look like:
INSERT INTO `subscribers`(email, comment)
VALUES ('someone#gmail.com', 'thanks');
The id would be assigned automatically.

Related

Is string to integer conversion automatic in SQL?

I have a SQL statement to insert a record into a table. The table is defined as;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(255) | NO | | NULL | |
| description | text | NO | | NULL | |
| filename | varchar(255) | NO | | NULL | |
| type | varchar(255) | NO | | NULL | |
| size | int(11) | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
The SQL statement is;
INSERT INTO photos
(title, description, filename, type, size)
VALUES
('Title D', 'Description D', 'Filename_D', 'BMP', '2000');
I was always taught that if you did not match column type to the type of data you were trying to insert or update, SQL would throw an error.
To my surprise the SQL statement worked in MySQL and MS Access even though the "size" column is defined as integer and the size data in the SQL statement is enclosed in quotes making it a string ('2000').
Is this a MySQL and MS Access feature or is this how SQL works across all SQL compliant databases?
String to number conversion is automatic in most databases. So, you can express your query this way:
INSERT INTO photos (title, description, filename, type, size)
VALUES ('Title D', 'Description D', 'Filename_D', 'BMP', '2000');
This is a bad idea, because implicit conversion can generate conversion errors -- and you have no clue where they come from. I strongly advocate using the right types or explicit conversion:
INSERT INTO photos (title, description, filename, type, size)
VALUES ('Title D', 'Description D', 'Filename_D', 'BMP', 2000);
Thank you Gordon Linoff for the answer.
Eric Brandt, I will open a fresh question on this. I'm also interested in seeing the response.

How to insert data into a table using SQL?

Insert into the App.settings table the following values:
(99, DEFAULT, "horizontal", "2015-09-15 04:01:04")
I have a DATABASE called App with a settings table. I am trying to insert into the table but I can not seem to get it right.
My statement:
INSERT INTO App.settings
VALUES(99, DEFAULT, "horizontal", "2015-09-15 04:01:04");
Am I doing it right? It says my answer is wrong.
mysql> DESC App.settings;
+-----------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------+-------+
| user_id | int(7) | NO | | NULL | |
| email_frequency | tinyint(2) unsigned | YES | | NULL | |
| layout | varchar(70) | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+-----------------+---------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
When you use insert, always list the columns in the table. Second, the default string delimiter is the single quote in SQL rather than the double quote.
So I would expect to see:
INSERT INTO App.settings (col1, col3, col4) -- your real column names here
VALUES (99, 'horizontal', '2015-09-15 04:01:04');
Note that col2 was removed from the INSERT and the VALUES because you seem to want a DEFAULT value. Not all databases support that syntax.
you are sure that all the fields of the tuple are there and if so, they are in the correct order if you are not entering all the fields of the tuple you should use the following form: INSERT INTO App.settings(value, config, position, date ) VALUES(99, "DEFAULT", "horizontal", "2015-09-15 04:01:04");
In the same order
this is only an example i dont know the fields names you must be change for the you are using
Like #GordonLinoff said, you should include the columns in your query. That way, you can also skip entering the DEFAULT value for email_frequency.
As for the mistake, it's most likely the double quotes that you're currently using instead of single quotes.
Try the following:
INSERT INTO App.settings (user_id, layout, updated_at)
VALUES (99, 'horizontal', '2015-09-15 04:01:04');

MySQL show columns query to string

I am looking for a way to convert the result of a query to a string.
The query can be
DESC entries;
or
SHOW COLUMNS FROM entires;
For example, both yield the same result if I execute them in the CLI which is:
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(45) | YES | | NULL | |
| content | text | YES | | NULL | |
| time | int(11) | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
However, I need this table as one string. It is NOT an option for me to get his information out of the information_schema.columns table because this information is not present there. It is further necessary to achieve this on the same query. Using PHP or another language is also not an option.
I have tried various things but none of them have been successful so far.
These queries all resulted in an error:
SELECT group_concat(Field) FROM (SHOW COLUMNS FROM entries);
SELECT group_concat(SHOW COLUMNS FROM entries);
SELECT group_concat(SHOW COLUMNS FROM entries LIMIT 1);
SELECT Field from (SHOW COLUMNS FROM entries);
SELECT 1 from (SHOW COLUMNS FROM entries);
SELECT group_concat(SHOW COLUMNS FROM entries);
SELECT group_concat(SHOW COLUMNS) FROM entries;
I have tried the same with desc entries; but with the same result.
I always get an error like this:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'show columns
from entries)' at line 1
Neither Google nor the manual could tell me how to achieve this, maybe I was looking for the wrong phrases. Any help is highly appreciated.

MYSQL - Insert statement with syntax Error [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 7 years ago.
I have following table setup.
+-------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| date | date | YES | | NULL | |
| limit | int(11) | YES | | NULL | |
| contract_id | int(11) | YES | | NULL | |
+-------------+---------+------+-----+---------+----------------+
And this insert query
INSERT INTO userlimit (date, limit, contract_id) VALUES (now(), 10, 1);
Always when I want to execute it I receive following error
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'limit, contract_id) VALUES (now(), 10, 1)' at line
1
My syntax looks perfectly fine to me. Why do I get this Error?
You need to quote field names with backticks
INSERT INTO userlimit (`date`, `limit`, `contract_id`) VALUES (now(), 10, 1)

Mind writing my sql statement for me?

This kind of sad but I've been at it a while and I just can't seem to figure this statement out, google searches turn up similar questions but I haven't successfully applied the solutions.
I have a table of music, and every time I insert a song into it(each row is a song) I want to insert the song into a table of clean music if it is flagged as clean. I'm using mysql.
use music;
CREATE TRIGGER cache_clean_music BEFORE INSERT ON music
FOR EACH ROW
if new.clean then
insert into clean_music values (new.artist, new.album, new.song, new.filename, new.clean);
end if;
The Error I get is
ERROR 1064 (42000) at line 3: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4
and here is a description of the music table, the clean_music table is exactly the same
+----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+----------------+
| artist | varchar(100) | YES | | NULL | |
| album | varchar(100) | YES | | NULL | |
| song | varchar(100) | YES | | NULL | |
| filename | varchar(100) | YES | | NULL | |
| clean | tinyint(1) | YES | | NULL | |
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
+----------+---------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
If the two tables are identical (or almost), you probably do not need triggers (and all their mess) at all.
You may use a VIEW instead of a table (with duplicate data) for cache_clean_music:
CREATE VIEW cache_clean_music AS
SELECT artist
, album
, song
, filename ---- and possibly other fields you need
, id
FROM music
WHERE clean ;
Adding an index on music.clean would be a good idea in this case.
Does it help if you wrap a BEGIN...END around things?
CREATE TRIGGER ...
FOR EACH ROW BEGIN
IF ...
....
END IF;
END
The error - ERROR 1064 (42000) at line 3: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4 - means that the values in some of your input params isn't correct, perhaps there is some mismatching single quote. Can you display your query or the value in the NEW. variables?
Also, once you have fixed that error, your query will also return another error that "the column count doesn't match value count". And that will be because your table has 6 columns but your INSERT has only 5. Mention the columns in your INSERT query and it should be fine, like:
insert into clean_music (artist, album, song, filename, clean) values (new.artist, new.album, new.song, new.filename, new.clean);
Your clue is this
" check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4"
Do you know what version of mysql server you are running?
Did you check the manual to make sure that the command you have written is allowed in that version?