Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion _data/sidebar_doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ entries:
- title: <code>SET DATABASE</code>
url: /set-database.html

- title: <code>SET TIMESZONE</code>
- title: <code>SET TIME ZONE</code>
url: /set-time-zone.html

- title: <code>SET TRANSACTION</code>
Expand Down
25 changes: 25 additions & 0 deletions _includes/sql/diagrams/set_database.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<svg width="452" height="80">

<polygon points="9 17 1 13 1 21"></polygon>
<polygon points="17 17 9 13 9 21"></polygon>
<rect x="31" y="3" width="44" height="32" rx="10"></rect>
<rect x="29" y="1" width="44" height="32" class="terminal" rx="10"></rect>
<text class="terminal" x="39" y="21">SET</text>
<rect x="95" y="3" width="90" height="32" rx="10"></rect>
<rect x="93" y="1" width="90" height="32" class="terminal" rx="10"></rect>
<text class="terminal" x="103" y="21">DATABASE</text>
<rect x="225" y="3" width="38" height="32" rx="10"></rect>
<rect x="223" y="1" width="38" height="32" class="terminal" rx="10"></rect>
<text class="terminal" x="233" y="21">TO</text>
<rect x="225" y="47" width="30" height="32" rx="10"></rect>
<rect x="223" y="45" width="30" height="32" class="terminal" rx="10"></rect>
<text class="terminal" x="233" y="65">=</text>

<rect x="303" y="3" width="122" height="32"></rect>
<rect x="301" y="1" width="122" height="32" class="nonterminal"></rect>
<text class="nonterminal" x="311" y="21">database_name</text>

<path class="line" d="m17 17 h2 m0 0 h10 m44 0 h10 m0 0 h10 m90 0 h10 m20 0 h10 m38 0 h10 m-78 0 h20 m58 0 h20 m-98 0 q10 0 10 10 m78 0 q0 -10 10 -10 m-88 10 v24 m78 0 v-24 m-78 24 q0 10 10 10 m58 0 q10 0 10 -10 m-68 10 h10 m30 0 h10 m0 0 h8 m20 -44 h10 m122 0 h10 m3 0 h-3"></path>
<polygon points="443 17 451 13 451 21"></polygon>
<polygon points="443 17 435 13 435 21"></polygon>
</svg>
1 change: 1 addition & 0 deletions generate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ func main() {
{name: "savepoint_stmt", inline: []string{"savepoint_name"}},
{name: "select_stmt", inline: []string{"select_no_parens", "simple_select", "opt_sort_clause", "select_limit"}},
{name: "set_stmt", inline: []string{"set_rest", "set_rest_more", "generic_set"}, exclude: regexp.MustCompile("CHARACTERISTICS"), replace: map[string]string{"'TRANSACTION' transaction_mode_list | ": ""}},
{name: "set_database", stmt: "set_stmt", inline: []string{"set_rest", "set_rest_more", "generic_set"}, exclude: regexp.MustCompile("CHARACTERISTICS"), replace: map[string]string{"var_name": "'DATABASE'", "'DEFAULT'": "var_list", "var_list": "database_name", "'LOCAL'": "", "'SESSION'": "", "'TRANSACTION' transaction_mode_list | ": ""," | 'TIME' 'ZONE' zone_value ": ""},unlink: []string{"database_name"}},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think with a match you can get rid of the optional empty branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjibson, we figured it out using more replacements, but I forgot to update the link. You can see it here: http://cockroach-docs-review.s3-website-us-east-1.amazonaws.com/dfafce3078cf944c7eb72fe42f9125f24e8691c4/set-database.html

I believe I tried with match and couldn't get it to work. If you have a sec to help out, I'm happy to try again, if you like.

{name: "set_transaction", stmt: "set_stmt", inline: []string{"set_rest", "transaction_mode_list", "transaction_iso_level", "transaction_user_priority"}, replace: map[string]string{" | set_rest_more": ""}, match: regexp.MustCompile("'TRANSACTION'")},
{name: "show_columns", stmt: "show_stmt", match: regexp.MustCompile("'SHOW' 'COLUMNS'"), replace: map[string]string{"var_name": "table_name"}, unlink: []string{"table_name"}},
{name: "show_constraints", stmt: "show_stmt", match: regexp.MustCompile("'SHOW' 'CONSTRAINTS'"), replace: map[string]string{"var_name": "table_name"}, unlink: []string{"table_name"}},
Expand Down
82 changes: 77 additions & 5 deletions set-database.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,90 @@ toc: false

The `SET DATABASE` [statement](sql-statements.html) sets the default database for the current session. When connected to the default database, you don't need to reference it explicitly in statements.

{{site.data.alerts.callout_danger}}In rare cases, CockroachDB may reset the default database, so it's most reliable to set the database in the client's connection string. For examples in different languages, see <a href="build-a-test-app.html">Build a Test App</a>.{{site.data.alerts.end}}

<div id="toc"></div>

## Synopsis

{% include sql/diagrams/set.html %}
{% include sql/diagrams/set_database.html %}

## Required Privileges

No [privileges](privileges.html) are required to set the default database.

## Parameters
## Examples

### Set the default database via `SET DATABASE`

~~~ shell
$ cockroach sql
# Welcome to the cockroach SQL interface.
# All statements must be terminated by a semicolon.
# To exit: CTRL + D.
~~~
~~~ sql
> SET DATABASE = db1;
> CREATE TABLE t3 (a INT PRIMARY KEY, b STRING(20));
> SHOW TABLES;
~~~
~~~ shell
+-------+
| Table |
+-------+
| t1 |
| t2 |
| t3 |
+-------+
(3 rows)
~~~

### Set the default database via the client connection

~~~ shell
$ cockroach sql --database=db1
# Welcome to the cockroach SQL interface.
# All statements must be terminated by a semicolon.
# To exit: CTRL + D.
~~~
~~~ sql
> CREATE TABLE t3 (a INT PRIMARY KEY, b STRING(20));
> SHOW TABLES;
~~~
~~~ shell
+-------+
| Table |
+-------+
| t1 |
| t2 |
| t3 |
+-------+
(3 rows)
~~~

### Use a non-default database

~~~ shell
$ cockroach sql --database=db1
# Welcome to the cockroach SQL interface.
# All statements must be terminated by a semicolon.
# To exit: CTRL + D.
~~~
~~~ sql
> CREATE TABLE db2.t1 (a SERIAL PRIMARY KEY, b DECIMAL);
> SHOW TABLES FROM db2;
~~~
~~~ shell
+-------+
| Table |
+-------+
| t1 |
+-------+
(1 row)
~~~

## See Also

| Parameter | Description |
|-----------|-------------|
| | |
- [`SET TIME ZONE`](set-time-zone.html)
- [`SET TRANSACTION`](set-transaction.html)