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 2 years ago.
I have a table as shown and I am trying to insert the data to it but I am constantly getting error . Can anyone suggest what should be the query ??
Here's my table structure:
My insert query:
INSERT INTO country (id, country_name, iso_code, country_pic_url, rank, created_time, updated_time, deleted_time) VALUES(1, 'Afghanistan', 'AF', null, 0, '2013-09-04 13:30:00', '2013-09-04 13:30:00', '2013-09-04 13:30:00')
Don't use null instead Put NULL in your syntax or either leave it blank.
Check if the rank you are trying to put is a string, so instead of just using 0, put '0' in your query.
Related
This question already has answers here:
Get the new record primary key ID from MySQL insert query?
(13 answers)
Closed 3 years ago.
INSERT INTO db.a (a,b,c,d,e,f,g,h,i)
VALUES (2,2,"a",'b','c','d',1,'e',0)
The first insert will insert a new row with a incremental_id. I need to take that incremental_id and make an entry into db.b using that incremental_id, for example, if it was 6005 I would make the following insert.
INSERT INTO db.b
(a_id, s_id, use_i)
VALUES (6005,7,0)
How can I automatically grab the id of the first insert so my second insert can be built dynamically after the first query?
You can use function LAST_INSERT_ID() which will return exactly what you need.
This question already has answers here:
Insert into a MySQL table or update if exists
(12 answers)
Closed 6 years ago.
I tried using from mySQL the If Exists statement but i got an error from mysql saying there's a #1064 syntax error, but i really couldn't find it. There are my codes:
If EXISTS (select * from points where username= 'john')
update points set points = "4" where username='john'
ELSE
insert into points (username, points) values ('john', 5);
You have a syntax error in your statement. You are missing "THEN" keyword after EXISTS and "END IF" at the end, and also missing a semicolon on UPDATE statement. If you still want to go with this statement, it should be like this:
IF EXISTS (select * from points where username= 'john') THEN
UPDATE points set points = "4" where username = 'john';
ELSE
INSERT into points (username, points) values ('john', 5);
END IF;
Please take a note that this statement can be only used in routine such as Stored Procedure or Stored Function and not in normal SQL.
On the other hand, what #TimBiegeleisen said in his answer is a more efficient way to go.
One way to achieve your logic would be to use ON DUPLICATE KEY UPDATE while doing your INSERT:
INSERT INTO points (username, points)
VALUES ('john', 5)
ON DUPLICATE KEY UPDATE points=4
This query will insert ('john', 5') into your table, but if the primary key username john already exists, then it will points to 4.
This question already has answers here:
How can I write SQL for a table that shares the same name as a protected keyword in MySql? [duplicate]
(3 answers)
Closed 9 years ago.
Not sure what's wrong, just running this in PHP MyAdmin right now. Does anything pop out to you? Thanks.
INSERT INTO order (CustomerID, BillAddr, ShipAddr, Date, Total)
VALUES ('test', 'test', 'test', '2012-07-02', 22)
Error:
MySQL said: #1064
Structure of order table:
FIELD TYPE
OrderID int(11) auto increment
CustomerID varchar(50)
BillAddr varchar(200)
ShipAddr varchar(200)
Date date
Total double
(I leave the OrderID out of the INSERT as it is an auto increment)
EDIT
Same error with this syntax:
SQL query:
INSERT INTO 'order'( CustomerID, BillAddr, ShipAddr, 'Date', Total )
VALUES (
'test', 'test', 'test', '2012-07-02', 22
)
MySQL said: Documentation
#1064 -
The word Date is a keyword. Try it like this:
INSERT INTO `order` (CustomerID, BillAddr, ShipAddr, `Date`, Total)
VALUES ('test', 'test', 'test', '2012-07-02', 22)
ETA: And order is also a keyword :)
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 8 years ago.
I have a MySQL database with the word "group" in one of the column names.
I can't change this database and column's name; it's not mine.
Table users, columns: id, name, password, group, and other.
I need to insert a record into this table. I tried INSERT INTO users (name, group) VALUES ('John', '9'), but it's not working because of "group".
Can you help me, how to insert a record into this table, please?
Try:
INSERT INTO users (`name`, `group`) VALUES ('John', '9')
use backticks(`) around column names when you use reserved keywords in query:
INSERT INTO users (`name`,`group`) VALUES ('John', '9')
Read here: Reserved Words
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 8 years ago.
I have a simple mysql database but get an error. It could be the field status, which is an notnull enum('actief', 'wachtend', 'verborgen'), but should be correct this way.
I have checked the comma's 50 times but maybe i'm staring at it too long now. I've tried googling it but still cant find the problem.
All the fields are notnull (i've left out the ones that can be null)
INSERT INTO `restaurants` (id_naam, korte_naam, lange_naam, straat, huisnummer, postcode, plaats, provincie, land, type_aanbieding, type_keuken, lat, long, status) VALUES ('test1', 'test1', 'Test restaurant 1', 'straatnaam', '1', '1234AB', 'plaatsnaam', 'Drenthe', 'landnaam', '2emenu', 'frans', '52', '5', 'verborgen');
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 'long,
status) VALUES ('test1', 'test1',
'Test restaurant 1', 'straatnaam',
'1', ' at line 1
It must be the long field which is causing the error. It's a reserved word in MySQL, so you need to wrap it in backticks:
INSERT INTO `restaurants` (... `long`, status) VALUES ...
Nothing to do with enums though, as you can see.