如何使用 SQL 中的保留字作为表名? [英] How to use a reserved word in SQL as a table name?

查看:24
本文介绍了如何使用 SQL 中的保留字作为表名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此查询时:

INSERT INTO 订单 (order_quantity)值('50')

我收到一个错误:

<块引用>

您的 SQL 语法有错误;检查手册对应于您的 MariaDB 服务器版本,以便使用正确的语法在第 146 行的订单 (order_quantity) VALUES('50')"附近

我的查询有什么问题?

解决方案

不建议将保留字用作数据库、表、列、变量或其他对象名称.如果您希望将保留字用作 ANSI 标准语法中的对象名称,则必须将其括在双引号中,以允许关系引擎(无论是哪个)将该字用作对象而不是用作给定上下文中的关键字.

以下是针对不同 SQL 引擎的一些示例:

order 是一个 SQL 关键字,用于对结果进行排序(ORDER BY ...)

如果您使用的是

是的,这个查询有效:

使用[插入];从 [INTO] 中选择 [INTO].[VALUE(42)];

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?

解决方案

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)

Yes, this query works :

USE [INSERT];
SELECT [INTO].[VALUE(42)] FROM [INTO];

这篇关于如何使用 SQL 中的保留字作为表名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆