I am trying to insert into my table something with partial data and leaving the rest default but somehow it keeps saying wrong syntax
My query is like this:
INSERT INTO day
(1030, 1100, date, tech)
VALUES ('356-635-3633', '356-635-3633', '2019-04-07', 'Thy')
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '1030, 1100, date, tech) VALUES('356-635-3633', '356-635-3633', '2019-04-07', 'Th' at line 1
A query like this works though:
INSERT INTO day (date, tech) VALUES ('2019-04-07', 'Thy')
The datatype for all those columns are varchar(30)
You would need to quotes these all-digits identifiers, using backticks;
INSERT INTO day
(`1030`, `1100`, date, tech)
VALUES ('356-635-3633', '356-635-3633', '2019-04-07', 'Thy')
From the documentation:
Identifiers may begin with a digit but unless quoted may not consist solely of digits.
Demo on DB Fiddle (you can uncomment the original column list to generate the error).
NB: day and date correspond to names of MySQL functions. Using them like you do do not generate error, but it would still be a good idea to surround them with backticks a well, just to avoid any ambiguity, hence:
INSERT INTO `day`
(`1030`, `1100`, `date`, tech)
VALUES ('356-635-3633', '356-635-3633', '2019-04-07', 'Thy')
Related
I am currently trying to insert brackets into the column name of my table. However, that results in an error when I run my script.
The format of my table in my script previously reads:
cursor.execute("CREATE TABLE IF NOT EXISTS table (date date, voltage decimal (2,2))")
I then made changes to this part of the script to add brackets to my table column name. It now reads:
cursor.execute("CREATE TABLE IF NOT EXISTS table (date date, voltage(V) decimal (2,2))")
After adding the brackets i.e. (V), the script fails to run.
The error I get is:
SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(V) decimal (2,2))' at line 1
How can I add brackets to the column name without obtaining an error?
If you want to use special characters in the name of a database, table, or column, put the name in backticks.
CREATE TABLE IF NOT EXISTS table (
date date,
`voltage(V)` decimal (2,2)
)
You'll also need to use the backticks in all queries that refer to the column, so it will probably annoy all your other programmers.
See When to use single quotes, double quotes, and backticks in MySQL for more information about quoting in MySQL.
Executing this query
INSERT INTO classes( '_fkUserID', 'date', 'time' )
VALUES (
'1', '2017-07-04', '8:15'
)
gives me the following error
#1064 - 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 ''_fkUserID', 'date', 'time') VALUES ('1', '2017-07-04', '8:15')' at line 1
I'm assuming the error has something to do with parsing the underscore, but I can't manage to find a way around it.
Change
classes('_fkUserID', 'date', 'time')
To
classes(`_fkUserID`, `date`, `time`)
Single quotes makes the fields as strings
You don't necessarily need to add the backticks on the column names unless they are Reserved Words to the MySql. In your sql statement, date and time are reserved words, so you must use the backticks on it, which means that it would also work as:
classes(_fkUserID, `date`, `time`)
Backticks are Identifier Quote Characters which means that its purpose is to make MySql understand that it should identify whatever is within it as an identifier, in your case a column name.
It is actually:
INSERT INTO classes(_fkUserID,date,time)
VALUES (
'1', '2017-07-04', '8:15'
)
Try removing the single quotes and use back tick symbol instead (
I need to do batch MYSQL insert/updates. I got batch insert statement to work, but when the insert comes as multiple one liners it does not.. Similarly I have not been able to generate a batch update. Please see examples below.
Batch insert statement works
$sql = "INSERT INTO `test` (`somefield`) VALUES ('test', 'test');";
db::statement($sql);
Multiple separate insert statements NOT working
$sql = "INSERT INTO `test` (`somefield`) VALUES ('test'); INSERT INTO `test` (`somefield`) VALUES ('test');";
db::statement($sql);
SQLSTATE[42000]: Syntax error or access violation: 1064 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 'INSERT INTO test (somefield) VALUES ('test')' at line 1 (SQL: INSERT INTO test (somefield) VALUES ('test'); INSERT INTO test (somefield) VALUES ('test');)
Batch update statement not working
$sql = "INSERT INTO 'flights' (`id`, `airline`) VALUES ('142832', 'BA') ON DUPLICATE KEY UPDATE `airline`=VALUES(`airline`);"
db::statement($sql);
1064 - 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 ''flights' (id, airline) VALUES ('142832', 'BA') ON DUPLICATE KEY UP' at line 1
Reviewed multiple Stackoverflow posts - but I am getting something wrong
Multiple insert statements - Multiple SQL Update Statements in single query
Batch update statement - Multiple Updates in MySQL
Would appreciate help on this - thanks!
This is weird. at a quick glance it appears that the batch statement is wrong, which is supposedly working.
A semantically correct batch statement would have the brackets separate each row of data, ie. like this:
INSERT INTO test
VALUES
('test1')
, ('test2')
, ('test3');
The separate insert statements look fine, however, your database driver might not support multiple statements in its statement method (most don't, AFAIK). The work around would be to start a transaction from your client, loop through the array of statements and execute. Then when all the statements execute, commit if there were no errors, or roll back the transaction. The first option is faster though.
The update statement doesn't work because the tablename flights is quoted using single-quotes. If you want to quote schema / table / column identifiers, use back-ticks, and reserve single-quotes for string values & dates, as you have done elsewhere in the same query. It is only necessary to escape a database element name if it is a reserved word, but naming database elements things like 'into', 'user', etc. is bad practice and should be avoided.
INSERT INTO flights (`id`, `airline`)
VALUES
('142832', 'BA')
ON DUPLICATE KEY UPDATE
airline=VALUES(`airline`)
Try to unquote flights, or quote it with back ticks in your last query (the batch update statement).
I'm facing a problem with select inside insert statements.
I've take a look at the questions similar to this but still the query is not working.
first, I'm working with MySQL version 5.6.24 with engine InnoDB, and I'm trying to insert this row:
INSERT INTO form (SELECT course_name FROM course WHERE course_id ='1' ), '123456','5','3','6','1','3','6','1','2','5','6','1','4','1','2','3','good','not bad','bad')
I want the first column to be retrieved (which is only one value), but not the other.
I've tried many syntax formats, with VALUES, with semicolon, with more parentheses, etc... but non work. Here is the error:
#1064 - 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 ' '123456','5','3','6','1','3','6','1','2','5','6','1','4','1','2','3','good','no' at line 1
Thanks.
Use an INSERT-SELECT:
INSERT INTO form
SELECT course_name, '123456','5','3','6','1','3','6','1','2','5','6','1','4','1','2','3','good','not bad','bad'
FROM course
WHERE course_id = '1'
I have a table named "calendar" that was created with this SQL statement in a PHP program.
$sql="CREATE TABLE calendar (
mdate DATE,
PRIMARY KEY(mdate),
special CHAR(20),
director CHAR(20),
dealer CHAR(20),
meeting CHAR(20))";
When I tried to insert data into this table I received an error 1064 from MySQL. In order to determine where the error was, I copied the INSERT statement from my PHP program and used constants instead of variables. This was the INSERT statement:
INSERT INTO TABLE calendar (mdate, special, director, dealer, meeting) VALUES('2013-05-01','Special Game','Director','Dealer','Meeting');
Here is the error message I received:
#1064 - 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 'TABLE calendar (mdate, special, director, dealer, meeting) VALUES('2013-05-01','' at line 1
I assume that the error involves the "mdate" column. I tried several formats for the date with no change in the error message. I reviewed the MySQL manual 9.1.3. Date and Time Literals and it appears that YYYY-MM-DD is a valid format for DATE columns. I also checked 9.3. Reserved Words to make sure that none of my column names were reserved words.
I had originally planned to use LOAD DATA INLINE to update my table but it turns out that statement needs FILE authority and the host of my web site was unwilling to grant that authority.
This seems too simple to be a problem. I hope that someone can see something that I am not seeing.
You must not use the keyword TABLE in the INSERT sentence. It should be like this:
INSERT INTO calendar (mdate, special, director, dealer, meeting)
VALUES('2013-05-01','Special Game','Director','Dealer','Meeting');
calendar is already table you dont need to say TABLE
INSERT INTO calendar (mdate, special, director, dealer, meeting)
VALUES('2013-05-01','Special Game','Director','Dealer','Meeting');