SQL SQL中语句和查询的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4735856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between a statement and a query in SQL
提问by bluish
I still live in this ambiguity: conceptually what's the difference between a statementand a queryin 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 代码,其中语句用分号 ( ;
)分隔?已经回复的人可以编辑他的答案。非常感谢!
采纳答案by Quassnoi
A statementis 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 queryis 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
开头的语句WITH
是一个明显的例外。
回答by Tony Andrews
A statementis any SQL command such as SELECT, INSERT, UPDATE, DELETE.
一个说法是任何SQL命令,如SELECT,INSERT,UPDATE,DELETE。
A queryis a synonym for a SELECT statement.
一个查询是一个SELECT语句的代名词。
回答by MicSim
From Wikipedia - SQL Language Elements
The SQL language is sub-divided into several language elements, including:
- Clauses, which are constituent components of statements and queries. (In some cases, these are optional.)[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.
- 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.
- Insignificant whitespaceis generally ignored in SQL statements and queries, making it easier to format SQL code for readability.
SQL 语言细分为几个语言元素,包括:
- 子句,它们是语句和查询的组成部分。(在某些情况下,这些是可选的。)[9]
- 表达式,可以生成标量值或由数据的列和行组成的表。
- Predicates,指定可以评估为 SQL 三值逻辑 (3VL) 或布尔 (true/false/unknown) 真值的条件,用于限制语句和查询的效果,或更改程序流程。
- 查询,根据特定条件检索数据。
- Statements,它可能对模式和数据有持久的影响,或者可能控制事务、程序流、连接、会话或诊断。
- SQL 语句还包括分号 (";") 语句终止符。虽然不是每个平台都需要,但它被定义为 SQL 语法的标准部分。
- SQL 语句和查询中通常会忽略不重要的空格,从而更容易格式化 SQL 代码以提高可读性。
回答by sleske
A statementis the general term for a piece of complete, correct SQL that you can send to a DBMS. A queryis a statement that will return data, thus a query is a special kind of statement.
一个说法是一块完整的,正确的SQL,您可以发送到一个数据库管理系统的总称。一个查询是将返回数据的语句,因此查询是一种特殊的语句。
A SELECT ...
would be a query, a DELETE...
just a statement.
ASELECT ...
将是一个查询,DELETE...
只是一个语句。
回答by BoltClock
They're used interchangeably by most, but some often use the word "query" to mean, specifically, SELECT
statements, because when you querysomething 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
查询返回结果集,这样会很好地适应了说明。这一点也很明显,因为SELECT
语句正式称为 DQL(数据查询语言)语句。