跳到主要内容

SQL 语法概述

本章提供了有关如何使用 SQL 语句、SPL 语句和语法段的信息。

如何输入 SQL 语句

SQL 语言至少有一个空白字符或其它定界符将关键字和标识与其它语法标记分开。 除了在带引号字符串中以外,SQL 是不区分大小写的。

输入 SQL 语句的主要帮助包括:

  • 语法图和语法表的组合
  • 出现在用法规则中的语法示例
  • 对相关信息的引用

使用语法图和语法表

本节将为您详细讲解如何理解语法图和语法表。其中,语法图包括输入规范(如标识、表达式、文件名、主机变量或其它项)时,在语法图后面将会附一个表,该表描述如何正确的输入该项。每个语法表包含以下四列:

  • Element 列出了语法图中的每个变量项。
  • Description 简述了该项并标识缺省值(如果该项有缺省值的话)。
  • Restrictions 总结了该项的限制。
  • Syntax 指向给出该项的详细语法的 SQL 段。

如何输入 SQL 注释

本节将为您详细讲解如何为SQL语句添加注释。

  • 双连字符( -- )符合 SQL 的 ANSI/ISO 标准。
  • 花括号( { } )是 ANSI/ISO 标准 的 GBase 8s 扩展。
  • C 语言样式的斜杠和星号( /* . . . */ )符合 SQL-99 标准。

SQL 注释符号示例

注释与语句显示在同一行的场景

SELECT * FROM customer; -- Selects all columns and rows
SELECT * FROM customer; {Selects all columns and rows}
SELECT * FROM customer; /*Selects all columns and rows*/

该注释与语句显示在不同行的场景

SELECT * FROM customer;
-- Selects all columns and rows
SELECT * FROM customer;
{Selects all columns and rows}
SELECT * FROM customer;
/*Selects all columns and rows*/

多行注释的场景

SELECT * FROM customer;
-- Selects all columns and rows
-- from the customer table
SELECT * FROM customer;
{Selects all columns and rows
from the customer table}
SELECT * FROM customer;
/*Selects all columns and rows
from the customer table*/

混杂样式注释的场景

SELECT *           -- Selects all columns and rows
FROM customer; -- from the customer table
SELECT * {Selects all columns and rows}
FROM customer; {from the customer table}
SELECT * /*Selects all columns and rows*/
FROM customer; /*from the customer table*/
注意

1.如果您使用自定义样式的注释时,那么结束注释符号必须与开始注释符号的样式相同。

2.如果数据库环境支持非 ASCII 字符(包括多字节字符),则可以在 SQL 注释中输入非 ASCII 字符。