Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a reserved word in SQL as a table name?

Tags:

sql

mariadb

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?

like image 832
Danial Afridi Avatar asked Feb 01 '19 12:02

Danial Afridi


1 Answers

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];
like image 200
Cid Avatar answered Nov 15 '22 00:11

Cid