diff --git a/_data/sidebar_doc.yml b/_data/sidebar_doc.yml index a0707d13b73..430fbc13fd3 100644 --- a/_data/sidebar_doc.yml +++ b/_data/sidebar_doc.yml @@ -250,6 +250,9 @@ entries: - title: Keywords & Identifiers url: /keywords-and-identifiers.html + - title: Constants + url: /sql-constants.html + - title: Indexes url: /indexes.html @@ -304,6 +307,15 @@ entries: - title: BYTES url: /bytes.html + - title: Expressions + thirdlevelitems: + + - title: Value Expressions + url: /sql-expressions.html + + - title: Table Expressions + url: /table-expressions.html + - title: Privileges url: /privileges.html diff --git a/sql-constants.md b/sql-constants.md index d5c3489925b..4abca30a2ef 100644 --- a/sql-constants.md +++ b/sql-constants.md @@ -121,22 +121,22 @@ values equivalent to the `'aaa'` string literal. Numeric literals can have the following forms: ~~~ - [+-]9999 - [+-]9999.[9999][e[+-]999] - [+-][9999].9999[e[+-]999] - [+-]9999e[+-]999 - [+-]0xAAAA +[+-]9999 +[+-]9999.[9999][e[+-]999] +[+-][9999].9999[e[+-]999] +[+-]9999e[+-]999 +[+-]0xAAAA ~~~ Some examples: ~~~ - +4269 - 3.1415 - -.001 - 6.626e-34 - 50e6 - 0xcafe111 ++4269 +3.1415 +-.001 +6.626e-34 +50e6 +0xcafe111 ~~~ The actual data type of a numeric constant depends both on the context @@ -193,8 +193,8 @@ This feature is inspired from MySQL. A constant of any data type can be formed using either of the following formats: ~~~ - type 'string' - 'string':::type +type 'string' +'string':::type ~~~ The value of the string part is used as input for the conversion function to the @@ -203,11 +203,11 @@ specified data type, and the result is used as a constant with that data type. Examples: ~~~ - DATE '2013-12-23' - BOOL 'FALSE' - '42.69':::INT - 'TRUE':::BOOL - '3 days':::INTERVAL +DATE '2013-12-23' +BOOL 'FALSE' +'42.69':::INT +'TRUE':::BOOL +'3 days':::INTERVAL ~~~ Additionally, for compatibility with PostgreSQL, the notation @@ -232,5 +232,5 @@ type during expression evaluation is determined based on context. ## See Also -- [Expressions](sql-expressions.html) +- [Value Expressions](sql-expressions.html) - [Data Types](data-types.html) diff --git a/sql-expressions.md b/sql-expressions.md index 3e5169d9b2a..57f9cd40488 100644 --- a/sql-expressions.md +++ b/sql-expressions.md @@ -106,11 +106,11 @@ All comparisons accept any combination of argument types and result in type `BOO Syntax: ~~~ - IN - IN ( ... subquery ... ) + IN + IN ( ... subquery ... ) - NOT IN - NOT IN ( ... subquery ... ) + NOT IN + NOT IN ( ... subquery ... ) ~~~ Returns `TRUE` if and only if the value of the left operand is part of @@ -134,10 +134,10 @@ to match the tuple element type. The result has type `BOOL`. Syntax: ~~~ - LIKE - ILIKE - NOT LIKE - NOT ILIKE + LIKE + ILIKE + NOT LIKE + NOT ILIKE ~~~ Evaluates both expressions as strings, then tests whether the string on the left @@ -170,10 +170,10 @@ The operands must be either both `STRING` or both `BYTES`. The result has type ` Syntax: ~~~ - ~ - ~* - !~ - !~* + ~ + ~* + !~ + !~* ~~~ Evaluates both expressions as strings, then tests whether the string on the left @@ -207,8 +207,8 @@ The operands must be either both `STRING` or both `BYTES`. The result has type ` Syntax: ~~~ - SIMILAR TO - NOT SIMILAR TO + SIMILAR TO + NOT SIMILAR TO ~~~ Evaluates both expressions as strings, then tests whether the string on the left @@ -266,7 +266,7 @@ operators in expressions: General syntax: ~~~ - ( ) + ( ) ~~~ A built-in function name followed by an opening parenthesis, followed @@ -299,7 +299,7 @@ In addition, the following SQL special forms are also supported: | `CURRENT_DATE` | `current_date()` | | `CURRENT_TIMESTAMP` | `current_timestamp()` | -### Typing rule +#### Typing rule In general, a function call requires the arguments to be of the types accepted by the function, and returns a value of the type determined @@ -319,7 +319,7 @@ values, `a[3]` will retrieve the 3rd value. The first value has index If the index is smaller or equal to 0, or larger than the size of the array, then the result of the subscripted expression is `NULL`. -### Typing rule +#### Typing rule The subscripted expression must have an array type; the index expression must have type `INT`. The result has the element type of the @@ -341,7 +341,7 @@ especially when an operand would be invalid otherwise. For example, Syntax: ~~~ - IF ( , , ) +IF ( , , ) ~~~ Evaluates ``, then evaluates `` if the condition is true, @@ -358,11 +358,11 @@ expression that was evaluated. Syntax: ~~~ - CASE - WHEN THEN - [ WHEN THEN ] ... - [ ELSE ] - END +CASE + WHEN THEN + [ WHEN THEN ] ... + [ ELSE ] +END ~~~ Evaluates ``, then picks the `WHEN` branch where `` is @@ -381,7 +381,7 @@ The result has the same type as the `THEN`/`ELSE` expressions. Syntax: ~~~ - NULLIF ( , ) +NULLIF ( , ) ~~~ Equivalent to: `IF ( = , NULL, )` @@ -395,8 +395,8 @@ Both operands must have the same type, which is also the type of the result. Syntax: ~~~ - IFNULL ( , ) - COALESCE ( [, [, ] ...] ) +IFNULL ( , ) +COALESCE ( [, [, ] ...] ) ~~~ `COALESCE` evaluates the first expression first. If its value is not @@ -415,8 +415,8 @@ The operands must have the same type, which is also the type of the result. Syntax: ~~~ - AND - OR + AND + OR ~~~ These operators compute the boolean AND or OR function of their @@ -430,7 +430,7 @@ operands, using short-circuit evaluation: returns `FALSE` directly and does not evaluate its right operand, whereas `OR` evaluates and returns the value of the right operand. -### Typing rule +#### Typing rule The operands must have type `BOOL`. The result has type `BOOL`. @@ -440,8 +440,8 @@ An aggregate expression has the same syntax as a function call, with a special case for `COUNT`: ~~~ - ( ) - COUNT ( * ) + ( ) +COUNT ( * ) ~~~ The difference between aggregate expressions and function calls is @@ -454,7 +454,7 @@ An aggregate expression computes a combined value, depending on which aggregate function is used, across all the rows currently selected. -### Typing rule +#### Typing rule [The operand and return types are determined like for regular function calls](#function-calls-and-sql-special-forms). @@ -463,14 +463,14 @@ selected. A window function call has the syntax of a function call followed by an `OVER` clause: ~~~ - ( ) OVER - ( * ) OVER + ( ) OVER + ( * ) OVER ~~~ It represents the application of a window or aggregate function over a subset ("window") of the rows selected by a query. -### Typing rule +#### Typing rule [The operand and return types are determined like for regular function calls](#function-calls-and-sql-special-forms). @@ -479,8 +479,8 @@ subset ("window") of the rows selected by a query. Syntax: ~~~ - :: - CAST ( AS ) + :: +CAST ( AS ) ~~~ Evaluates the expression and converts the resulting value to the @@ -493,7 +493,7 @@ coercion. See the section on [type annotations](#explicitly-typed-expressions) below for more details. -### Typing rule +#### Typing rule The operand can have any type. The result has the type specified in the `CAST` expression. @@ -507,7 +507,7 @@ operand. [See our blog post for more details](https://www.cockroachlabs.com/blog Syntax: ~~~ - COLLATE + COLLATE ~~~ Evaluates the expression and converts its result to a collated string @@ -515,7 +515,7 @@ with the specified collation. For example: `'a' COLLATE de` -### Typing rule +#### Typing rule The operand must have type `STRING`. The result has type `COLLATEDSTRING`. @@ -524,15 +524,15 @@ The operand must have type `STRING`. The result has type `COLLATEDSTRING`. Syntax: ~~~ - EXISTS ( ... subquery ... ) - NOT EXISTS ( ... subquery ... ) +EXISTS ( ... subquery ... ) +NOT EXISTS ( ... subquery ... ) ~~~ Evaluates the subquery and then returns `TRUE` or `FALSE` depending on whether the subquery returned any row (for `EXISTS`) or didn't return any row (for `NOT EXISTS`). -### Typing rule +#### Typing rule The operand can have any table type. The result has type `BOOL`. @@ -541,7 +541,7 @@ The operand can have any table type. The result has type `BOOL`. Syntax: ~~~ - ( ... subquery ... ) +( ... subquery ... ) ~~~ Evaluates the subquery, asserts that it returns a single row and single column, @@ -556,7 +556,7 @@ For example: returns `TRUE` if there are more rows in table `users` than in table `admins`. -### Typing rule +#### Typing rule The operand must have a table type with only one column. The result has the type of that single column. @@ -566,7 +566,7 @@ The result has the type of that single column. Syntax: ~~~ - ARRAY[ , , ... ] +ARRAY[ , , ... ] ~~~ Evaluates to an array containing the specified values. @@ -595,7 +595,7 @@ specified explicitly using a type annotation. For example: > SELECT ARRAY[]:::int[]; ~~~ -### Typing rule +#### Typing rule The operands must all have the same type. The result has the array type with the operand type as element type. @@ -605,8 +605,8 @@ The result has the array type with the operand type as element type. Syntax: ~~~ - (, , ...) - ROW (, , ...) +(, , ...) +ROW (, , ...) ~~~ Evaluates to a tuple containing the values of the provided expressions. @@ -627,7 +627,7 @@ For example: The data type of the resulting tuple is inferred from the values. Each position in a tuple can have a distinct data type. -### Typing rule +#### Typing rule The operands can have any type. The result has a tuple type whose item types are the types of the operands. @@ -637,8 +637,8 @@ The result has a tuple type whose item types are the types of the operands. Syntax: ~~~ - ::: - ANNOTATE_TYPE(, ) +::: +ANNOTATE_TYPE(, ) ~~~ Evaluates to the given expression, requiring the expression to have @@ -663,7 +663,13 @@ message (that `now()` does not have type `DATE`). Check our blog for [more information about context-dependent typing](https://www.cockroachlabs.com/blog/revisiting-sql-typing-in-cockroachdb/). -### Typing rule +#### Typing rule The operand must be implicitly coercible to the given type. The result has the given type. + +## See Also + +- [Constants](sql-constants.html) +- [Table Expressions](table-expressions.html) +- [Data Types](data-types.html) diff --git a/sql-statements.md b/sql-statements.md index 30c02c7a83e..f20ca29585b 100644 --- a/sql-statements.md +++ b/sql-statements.md @@ -6,7 +6,9 @@ toc: false CockroachDB supports the following SQL statements. Click a statement for more details. -## Query and Update Statements +
+ +## Data Manipulation Statements Statement | Usage ----------|------------ diff --git a/table-expressions.md b/table-expressions.md index 6f418a925a3..d80d8b4e2e1 100644 --- a/table-expressions.md +++ b/table-expressions.md @@ -14,8 +14,8 @@ Table expressions define a data source in the `FROM` clause of Table expressions are used prominently in the `SELECT` clause: ~~~sql - SELECT ... FROM ,
, ... - INSERT INTO ... SELECT ... FROM
,
, ... +> SELECT ... FROM
,
, ... +> INSERT INTO ... SELECT ... FROM
,
, ... ~~~ CockroachDB recognizes the following table expressions: @@ -40,8 +40,8 @@ the `JOIN` syntax below. Syntax: ~~~ - identifier - identifier.identifier +identifier +identifier.identifier ~~~ A single SQL identifier in a table expression context designates @@ -54,8 +54,8 @@ is searched in the database with that name. For example: ~~~sql - SELECT * FROM users -- uses table `users` in the current database; - SELECT * FROM mydb.users -- uses table `users` in database `mydb`; +> SELECT * FROM users -- uses table `users` in the current database; +> SELECT * FROM mydb.users -- uses table `users` in database `mydb`; ~~~ ## Table Generator Functions @@ -63,7 +63,7 @@ For example: Syntax: ~~~ - name ( arguments... ) +name ( arguments... ) ~~~ The name of a table generator function, followed by an opening @@ -97,7 +97,7 @@ For example: Syntax: ~~~ - ( ... subquery ... ) +( ... subquery ... ) ~~~ The subquery can be expressed either as a `SELECT` or `VALUES` clause. @@ -106,8 +106,8 @@ The parentheses around the subquery are mandatory. For example: ~~~sql - SELECT * FROM (VALUES(1), (2), (3)); - SELECT c+2 FROM (SELECT COUNT(*) AS c FROM users); +> SELECT * FROM (VALUES(1), (2), (3)); +> SELECT c+2 FROM (SELECT COUNT(*) AS c FROM users); ~~~ ## Aliased Table Expressions @@ -115,8 +115,8 @@ For example: Syntax: ~~~ -
AS -
AS (, , ...) +
AS +
AS (, , ...) ~~~ In the first form, the table expression is equivalent to its left operand @@ -127,30 +127,30 @@ In the second form, the columns are also renamed. For example: ~~~sql - SELECT c.x FROM (SELECT COUNT(*) AS x FROM users) AS c; - SELECT c.x FROM (SELECT COUNT(*) FROM users) AS c(x); +> SELECT c.x FROM (SELECT COUNT(*) AS x FROM users) AS c; +> SELECT c.x FROM (SELECT COUNT(*) FROM users) AS c(x); ~~~ ## Join Expressions Syntax: -~~~ - -- Inner joins: -
[ INNER ] JOIN
ON -
[ INNER ] JOIN
USING(, , ...) -
NATURAL [ INNER ] JOIN
-
CROSS JOIN
+~~~ shell +# Inner joins: +
[ INNER ] JOIN
ON +
[ INNER ] JOIN
USING(, , ...) +
NATURAL [ INNER ] JOIN
+
CROSS JOIN
- -- Left outer joins: -
LEFT [ OUTER ] JOIN
ON -
LEFT [ OUTER ] JOIN
USING(, , ...) -
NATURAL LEFT [ OUTER ] JOIN
+# Left outer joins: +
LEFT [ OUTER ] JOIN
ON +
LEFT [ OUTER ] JOIN
USING(, , ...) +
NATURAL LEFT [ OUTER ] JOIN
- -- Right outer joins: -
RIGHT [ OUTER ] JOIN
ON -
RIGHT [ OUTER ] JOIN
USING(, , ...) -
NATURAL RIGHT [ OUTER ] JOIN
+# Right outer joins: +
RIGHT [ OUTER ] JOIN
ON +
RIGHT [ OUTER ] JOIN
USING(, , ...) +
NATURAL RIGHT [ OUTER ] JOIN
~~~ These expressions designate the @@ -164,7 +164,7 @@ Currently works only with small data sets; find more info in our [blog post](htt Syntax: ~~~ -
WITH ORDINALITY +
WITH ORDINALITY ~~~ Designates a data source equivalent to the table expression operand with @@ -203,3 +203,9 @@ For example: surrounding query. Use it sparingly if performance is a concern, and always check the output of EXPLAIN in case of doubt. {{site.data.alerts.end}} + +## See Also + +- [Constants](sql-constants.html) +- [Value Expressions](sql-expressions.html) +- [Data Types](data-types.html)