在非过程语言中,什么指定了事情如何完成?

发布于 2024-09-12 13:59:08 字数 224 浏览 8 评论 0原文

如果比较 C 和 SQL,则争论如下:

与过程语言相比 例如C,它描述事物如何 应该这样做,SQL 是非过程化的 并描述了应该做什么。

那么,像 SQL 这样的语言的 how 部分是由语言本身指定的,不是吗?如果我想改变某些查询的工作方式该怎么办?假设我想更改 SELECT 的处理方式。这可能吗?

If you compare C vs SQL, this is the argument:

In contrast to procedural languages
such as C, which describe how things
should be done, SQL is nonprocedural
and describes what should be done.

So, the how part for languages like SQL is specified by the language itself, is it? What if I want to change the way some query works. Suppose I want to change the way a SELECT is handled. Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉梦枕江山 2024-09-19 13:59:08

所以,像这样的语言的 how 部分
SQL由语言指定
本身,是吗?

不严格由语言(即SQL)决定,但通常由数据库及其优化器决定。因此,即使从具有相同结构和相同索引的表中查询相同的数据,某些数据库也会以与其他数据库不同的方式构建结果集。

假设我想改变方式
SELECT 被处理。这可能吗?

在某种程度上,是的。您可以:

  1. 重写查询,以不同的方式获得相同的结果,或者
  2. 使用提示 - http://en.wikipedia.org/wiki/Hint_%28SQL%29

这些都没有直接指示数据库引擎使用哪种方法,但是它们都会影响结果集的返回方式 - 这可能因数据库而异。

此外,据我所知,某些数据库具有额外的接口,允许与数据库引擎进行更多低级交互,从而比普通 SQL 能够更好地控制查询的构建方式。 (但是,您的问题确实指定了 SQL。)

So, the how part for languages like
SQL is specified by the language
itself, is it?

Not strictly by the language (ie. SQL), but normally by the database and its optimiser. As such, even where the same data is being queried from tables with the same structures and the same indexes, some databases will build the resultset in a different way to others.

Suppose I want to change the way a
SELECT is handled. Is that possible?

To some degree, yes. You can either:

  1. Rewrite the query, to achieve the same result a different way, or
  2. Use hinting - http://en.wikipedia.org/wiki/Hint_%28SQL%29

Neither of these directly instruct the database engine which approach to use, but both of them will affect how the resultset is returned - this is likely to vary between databases.

Additionally, I understand that some databases have additional interfaces that allow more low-level interaction with the database engine, enabling greater control over how a query is built than is possible from plain SQL. (However, your question did specify SQL.)

温柔嚣张 2024-09-19 13:59:08

这实际上夸大了差异。没有明确的界限,一个人讲述事情是如何做的,另一个人只讲述事情做了什么。相反,一个人可能必须比另一个人更详细地指定做什么/如何完成事情。典型的 SQL 实现允许用户控制诸如使用(或忽略)哪些索引、执行哪种锁定等操作。

如果您要在 C 中完成相同的工作,您将(在某些时候)必须指定更多细节(除非您使用 ODBC 之类的东西)。尽管如此,您仍然在告诉应该做什么,而不是告诉应该如何完成的所有细节(例如,尽管缺乏汇编语言,C 仍然是尽可能低级的,但 C 仍然会自动执行一些类型转换,所以您不必告诉它如何执行诸如将整数与浮点数相加之类的操作 - 您只需告诉它将它们相加,它就会处理详细信息)。

底线:试图将一个称为程序性而另一个称为非程序性是误导性的。 SQL 并不总是需要那么多细节,但这是程度的差异,而不是真正的“如何”与“什么”。

This is actually exaggerating the difference. There is no clear-cut point at which one is telling how things are done and the other only telling what it done. Rather, one may have to specify what/how things are done at a greater level of detail than the other. A typical SQL implementation allows the user to control such things as what indexes are used (or ignored), what kind of locking to do, and so on.

If you were to do the same job in C, you would (at some point) have to specify a great deal more detail (unless you used something like ODBC). Nonetheless, you're still telling what should be done, not all the details of how it should be done (e.g., despite being about as low-level as possible short of assembly language, C will still do some type conversions automatically, so you don't have to tell it how to do something like adding an integer to a floating point number -- you just tell it to add them, and it handles the details).

Bottom line: trying to talk about one as procedural and the other as non-procedural is misleading. SQL doesn't always require as much detail, but it's a difference of degree, not really "how" versus "what".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文