简体   繁体   English

SQL是一种什么样的语言?

[英]What kind of language is SQL?

SQL 是一种上下文无关语言还是某种其他类型的语言?

According to https://stackoverflow.com/a/31265136 SQL is not a regular language.根据https://stackoverflow.com/a/31265136 SQL 不是常规语言。 The short explanation is that each select query looks like简短的解释是每个选择查询看起来像

 SELECT x FROM y WHERE z

and y can be another select query itself, so it cannot be simulated with finite-state machine.并且y可以是另一个选择查询本身,因此不能用有限状态机模拟。 As mentioned before, there are some CFGs for SQL standarts in Backus–Naur Form , thereby SQL is nonregular context free language.如前所述,在Backus–Naur Form 中有一些 SQL 标准的 CFG,因此 SQL 是非正则上下文无关语言。

@aquinas wrote: @aquinas 写道:

Do you mean is SQL also regular?你的意思是 SQL 也是常规的吗? CFG's encompass regular languages. CFG 包含常规语言。 So, they aren't mutually exclusive.因此,它们并不相互排斥。 To answer your question though, SQL is not a regular language.不过,要回答您的问题,SQL 不是常规语言。

@MSX wrote: @MSX 写道:

Just to clarify, a language is context-free when it is generated by a context-free grammar.澄清一下,当语言由上下文无关文法生成时,它就是上下文无关的。 There're SQL context-free grammar definitions online.网上有 SQL 上下文无关语法定义。 Just google around and you'll find some.只要谷歌一下,你就会找到一些。 Here's one, for example.例如,这里有一个。

Any CFG for SQL will do most of the work, but will always be slightly too permissive.任何用于 SQL 的 CFG 都可以完成大部分工作,但总是稍微过于宽松。

A good example of an SQL CFG comes from antlr: https://github.com/antlr/grammars-v4/blob/master/sql/plsql/PlSqlParser.g4#L1982 SQL CFG 的一个很好的例子来自 antlr: https : //github.com/antlr/grammars-v4/blob/master/sql/plsql/PlSqlParser.g4#L1982

But at that line (1982), you see that in a values_clause , column values are recursively added regardless of how many columns might be specified, or how many values are in another row, which is invalid sql:但是在该行(1982 年)中,您会看到在values_clause ,无论可能指定多少列,或者另一行中有多少值,都会递归添加列值,这是无效的 sql:

insert into xyz values
  (1, 'hello'),
  (2, 'bye'),
  (3, 'hi', 'uhm...'); -- invalid!

Try running it here: https://www.db-fiddle.com/f/6gwgcg6qBLKpFbCEPtB6dy/0尝试在此处运行: https : //www.db-fiddle.com/f/6gwgcg6qBLKpFbCEPtB6dy/0

This syntax can never be fully encapsulated by a CFG, as it is equivalent to the { (a^n)(b^n)(c^n) : n ≥ 1 } language, which is famously not allowed in CFGs ( but allowed in CSGs )这种语法永远不能被 CFG 完全封装,因为它等价于{ (a^n)(b^n)(c^n) : n ≥ 1 }语言,这在 CFG 中是众所周知的( 但允许在 CSG 中

You could argue this is a runtime error instead of a parsing error.您可能会争辩说这是运行时错误而不是解析错误。 But you can use the same argument for every language that is interpreted, so it's a bit of a grey area.但是你可以对每一种被解释的语言使用相同的参数,所以它有点灰色区域。

PL/SQL上下文中的相似可以是什么代码就是SQL或者PL/SQL因为如果我们把函数关键字定义为具体的,那么如果我们想控制可以使用哪些函数,这个上下文必须是已知的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM