6

When using this query :

INSERT INTO order (order_quantity)
           VALUES ('50')

I'm getting an error :

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 'order (order_quantity) VALUES('50')' at line 146

What's wrong with my query?

Cid
  • 14,968
  • 4
  • 30
  • 45
Danial Afridi
  • 97
  • 1
  • 4
  • 1
    Please edit your question with a better title and tags, this does not appear to be anything to do with php but rather sql - and which sql is that exactly, please tag it with that. Please add additional clarification of the exact single question in the body of the question. As it stands now, the first sentence appears to be the answer, clarify why that is not so. – Mark Schultheiss Feb 01 '19 at 12:56
  • 3
    Possible duplicate of [You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use nea](https://stackoverflow.com/questions/34321222/you-have-an-error-in-your-sql-syntax-check-the-manual-that-corresponds-to-your) – PrakashG Feb 01 '19 at 13:00
  • 1
    I edited the title, please update if this is not the actual question. – Mark Schultheiss Feb 01 '19 at 13:02
  • I don't think it's a good idea to edit the title for the OP. Especially when it's not even clear, what the actual question is. – Olafant Feb 01 '19 at 13:12
  • 2
    @Olafant Perhaps, but as OP is a new user it is better in my opinion to illustrate to the OP how to improve the question rather than simply closing it as unclear what you are asking when the actual question can be inferred from the original post. – Mark Schultheiss Feb 01 '19 at 13:19
  • @Olafant the title is far more clearer than the previous *"Facing some issue"* one. – Cid Feb 01 '19 at 13:20
  • I edited the question to be more (I hope) StackOverflow compliant. – Cid Feb 01 '19 at 13:26
  • @MarkSchultheiss if i could upvote again your comment on guidance to an OP, i would. – YvesLeBorg Feb 01 '19 at 13:30
  • @YvesLeBorg Strictly speaking PrakashG is correct and closing as a duplicate is a valid response if this were not a new contributors question. I did however also not favor that question due to the title of it, and the answers were IMHO less clear that the one I up-voted here. I tend to work with "Be kind always, you never know what struggles any person is dealing with and we all have those" - but honestly at times I may be more direct in my response at times, usually after repeated attempts to communicate have failed. – Mark Schultheiss Feb 01 '19 at 14:12

1 Answers1

18

Reserved words are not recommended for use as database, table, column, variable or other object names. If you desire to use a reserved word is used as an object name in ANSI standard syntax, it must be enclosed in double-quotes to allow the Relational Engine (whichever that one is) that the word is being used as an object and not as a keyword in the given context.

Here are some examples specific to different SQL engines:

order is a SQL Keyword, used to sort results (ORDER BY ...)

Wrap backticks around it if you are using MySQL or Maria DB

INSERT INTO `order` (order_quantity) VALUES ('50');

Wrap brackets around it if you are using MS SQL Server

INSERT INTO [order] (order_quantity) VALUES ('50');

Wrap double quotes around it if you are using pgSQL

INSERT INTO "order" (order_quantity) VALUES ('50');

In example, nothing (but common sense) prevents you from creating a database named INSERT with a table INTO having a column VALUE(42)

enter image description here

Yes, this query works :

USE [INSERT];
SELECT [INTO].[VALUE(42)] FROM [INTO];
Cid
  • 14,968
  • 4
  • 30
  • 45