SQL中的语句和查询之间的差异

时间:2023-01-12 13:26:24

I still live in this ambiguity: conceptually what's the difference between a statement and a query in SQL? Can anybody give a definition for each of them? It would be useful, for example when choosing variables names inside programs in a way that will be clear for everybody. Thanks!

我仍然生活在这种模糊性中:从概念上讲,SQL中的语句和查询有什么区别?每个人都能给出定义吗?它将是有用的,例如,当在程序中选择变量名时,以一种对每个人都很清楚的方式。谢谢!

ADDITIONALLY: How can I call a chunk of SQL code made by more than one statement where statements are separated by a semicolon (;)? Who already replied can edit his answer. Many thanks!

另外:如何调用由多个语句组成的SQL代码块,其中语句由分号(;)分隔?已经回复的人可以修改他的答案。很多谢谢!

6 个解决方案

#1


24  

A statement is any text that the database engine recognizes as a valid command. As of SQL-92:

语句是数据库引擎识别为有效命令的任何文本。正如sql - 92:

An SQL-statement is a string of characters that conforms to the format and syntax rules specified in this international standard.

sql语句是一串符合本国际标准规定的格式和语法规则的字符。

A query is a statement that returns a recordset (possibly empty).

查询是返回记录集(可能为空)的语句。

How can I call a chunk of SQL code made by more than one statement where statements are separated by a semicolon (;)? Who already replied can edit his answer. Many thanks!

如何调用由多个语句组成的SQL代码块,其中语句由分号(;)分隔?已经回复的人可以修改他的答案。很多谢谢!

A series of SQL statements sent to the server at once is called a batch.

同时发送到服务器的一系列SQL语句称为批处理。

Not all SQL engines required the statements in a batch to be semicolon delimited. SQL Server, for instance, generally does not and breaks the statements based on context. CTE statements starting with WITH are a notable exception.

并非所有SQL引擎都要求批处理中的语句具有分号分隔。例如,SQL Server一般不会根据上下文来破坏语句。以CTE开头的语句是一个明显的例外。

#2


8  

A statement is any SQL command such as SELECT, INSERT, UPDATE, DELETE.

语句是任何SQL命令,如SELECT、INSERT、UPDATE、DELETE。

A query is a synonym for a SELECT statement.

查询是SELECT语句的同义词。

#3


7  

From Wikipedia - SQL Language Elements

来自Wikipedia的SQL语言元素

The SQL language is sub-divided into several language elements, including:

SQL语言被细分为几种语言元素,包括:

  • Clauses, which are constituent components of statements and queries. (In some cases, these are optional.)[9]
  • 子句,它们是语句和查询的组成部分。(在某些情况下,这些是可选的)[9]
  • Expressions, which can produce either scalar values or tables consisting of columns and rows of data.
  • 表达式,可以生成标量值或由列和数据行组成的表。
  • Predicates, which specify conditions that can be evaluated to SQL three-valued logic (3VL) or Boolean (true/false/unknown) truth values and which are used to limit the effects of statements and queries, or to change program flow.
  • 谓词,它指定可以计算为SQL三值逻辑(3VL)或布尔(true/false/unknown)真值的条件,并用于限制语句和查询的影响,或更改程序流。
  • Queries, which retrieve data based on specific criteria.
  • 查询,根据特定的标准检索数据。
  • Statements, which may have a persistent effect on schemas and data, or which may control transactions, program flow, connections, sessions, or diagnostics.
    • SQL statements also include the semicolon (";") statement terminator. Though not required on every platform, it is defined as a standard part of the SQL grammar.
    • SQL语句还包括分号(“;”)语句终止符。虽然不是每个平台都需要它,但它被定义为SQL语法的标准部分。
  • 语句,它可能对模式和数据具有持久的影响,也可能控制事务、程序流、连接、会话或诊断。SQL语句还包括分号(“;”)语句终止符。虽然不是每个平台都需要它,但它被定义为SQL语法的标准部分。
  • Insignificant whitespace is generally ignored in SQL statements and queries, making it easier to format SQL code for readability.
  • 在SQL语句和查询中,不重要的空白通常被忽略,这使得为可读性编写SQL代码变得更加容易。

#4


4  

A statement is the general term for a piece of complete, correct SQL that you can send to a DBMS. A query is a statement that will return data, thus a query is a special kind of statement.

语句是可以发送给DBMS的完整、正确的SQL的通用术语。查询是返回数据的语句,因此查询是一种特殊的语句。

A SELECT ... would be a query, a DELETE... just a statement.

选择……会是一个查询,一个删除…只是一个声明。

#5


1  

They're used interchangeably by most, but some often use the word "query" to mean, specifically, SELECT statements, because when you query something or someone, you want information. And SELECT queries return result sets, so that'd fit the description nicely. This also is evident in the fact that SELECT statements are formally called DQL (Data Query Language) statements.

大多数情况下它们是可以互换使用的,但是有些人经常使用“查询”这个词来表示,特别是“选择语句”,因为当你查询某个东西或某人时,你想要的是信息。SELECT查询返回结果集,这样就能很好地符合描述。这一点也很明显,SELECT语句被正式地称为DQL(数据查询语言)语句。

#6


0  

Queries is used for retrieve data based on specific criteria, but statement may have a persistent effect on schemas and data, or which may control transactions, program flow, connections, sessions, or diagnostics. See also Wikipedia.

查询用于基于特定的标准检索数据,但是语句可能对模式和数据有持久的影响,或者可以控制事务、程序流、连接、会话或诊断。参见*。

#1


24  

A statement is any text that the database engine recognizes as a valid command. As of SQL-92:

语句是数据库引擎识别为有效命令的任何文本。正如sql - 92:

An SQL-statement is a string of characters that conforms to the format and syntax rules specified in this international standard.

sql语句是一串符合本国际标准规定的格式和语法规则的字符。

A query is a statement that returns a recordset (possibly empty).

查询是返回记录集(可能为空)的语句。

How can I call a chunk of SQL code made by more than one statement where statements are separated by a semicolon (;)? Who already replied can edit his answer. Many thanks!

如何调用由多个语句组成的SQL代码块,其中语句由分号(;)分隔?已经回复的人可以修改他的答案。很多谢谢!

A series of SQL statements sent to the server at once is called a batch.

同时发送到服务器的一系列SQL语句称为批处理。

Not all SQL engines required the statements in a batch to be semicolon delimited. SQL Server, for instance, generally does not and breaks the statements based on context. CTE statements starting with WITH are a notable exception.

并非所有SQL引擎都要求批处理中的语句具有分号分隔。例如,SQL Server一般不会根据上下文来破坏语句。以CTE开头的语句是一个明显的例外。

#2


8  

A statement is any SQL command such as SELECT, INSERT, UPDATE, DELETE.

语句是任何SQL命令,如SELECT、INSERT、UPDATE、DELETE。

A query is a synonym for a SELECT statement.

查询是SELECT语句的同义词。

#3


7  

From Wikipedia - SQL Language Elements

来自Wikipedia的SQL语言元素

The SQL language is sub-divided into several language elements, including:

SQL语言被细分为几种语言元素,包括:

  • Clauses, which are constituent components of statements and queries. (In some cases, these are optional.)[9]
  • 子句,它们是语句和查询的组成部分。(在某些情况下,这些是可选的)[9]
  • Expressions, which can produce either scalar values or tables consisting of columns and rows of data.
  • 表达式,可以生成标量值或由列和数据行组成的表。
  • Predicates, which specify conditions that can be evaluated to SQL three-valued logic (3VL) or Boolean (true/false/unknown) truth values and which are used to limit the effects of statements and queries, or to change program flow.
  • 谓词,它指定可以计算为SQL三值逻辑(3VL)或布尔(true/false/unknown)真值的条件,并用于限制语句和查询的影响,或更改程序流。
  • Queries, which retrieve data based on specific criteria.
  • 查询,根据特定的标准检索数据。
  • Statements, which may have a persistent effect on schemas and data, or which may control transactions, program flow, connections, sessions, or diagnostics.
    • SQL statements also include the semicolon (";") statement terminator. Though not required on every platform, it is defined as a standard part of the SQL grammar.
    • SQL语句还包括分号(“;”)语句终止符。虽然不是每个平台都需要它,但它被定义为SQL语法的标准部分。
  • 语句,它可能对模式和数据具有持久的影响,也可能控制事务、程序流、连接、会话或诊断。SQL语句还包括分号(“;”)语句终止符。虽然不是每个平台都需要它,但它被定义为SQL语法的标准部分。
  • Insignificant whitespace is generally ignored in SQL statements and queries, making it easier to format SQL code for readability.
  • 在SQL语句和查询中,不重要的空白通常被忽略,这使得为可读性编写SQL代码变得更加容易。

#4


4  

A statement is the general term for a piece of complete, correct SQL that you can send to a DBMS. A query is a statement that will return data, thus a query is a special kind of statement.

语句是可以发送给DBMS的完整、正确的SQL的通用术语。查询是返回数据的语句,因此查询是一种特殊的语句。

A SELECT ... would be a query, a DELETE... just a statement.

选择……会是一个查询,一个删除…只是一个声明。

#5


1  

They're used interchangeably by most, but some often use the word "query" to mean, specifically, SELECT statements, because when you query something or someone, you want information. And SELECT queries return result sets, so that'd fit the description nicely. This also is evident in the fact that SELECT statements are formally called DQL (Data Query Language) statements.

大多数情况下它们是可以互换使用的,但是有些人经常使用“查询”这个词来表示,特别是“选择语句”,因为当你查询某个东西或某人时,你想要的是信息。SELECT查询返回结果集,这样就能很好地符合描述。这一点也很明显,SELECT语句被正式地称为DQL(数据查询语言)语句。

#6


0  

Queries is used for retrieve data based on specific criteria, but statement may have a persistent effect on schemas and data, or which may control transactions, program flow, connections, sessions, or diagnostics. See also Wikipedia.

查询用于基于特定的标准检索数据,但是语句可能对模式和数据有持久的影响,或者可以控制事务、程序流、连接、会话或诊断。参见*。