Skip to content
Closed
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: 2 additions & 0 deletions docs/_data/menu-sql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@
url: sql-ref-syntax-qry-select-limit.html
- text: Set operations
url: sql-ref-syntax-qry-select-setops.html
- text: USE database
url: sql-ref-syntax-qry-select-usedb.html
- text: Common Table Expression(CTE)
url: sql-ref-syntax-qry-select-cte.html
- text: Subqueries
Expand Down
60 changes: 60 additions & 0 deletions docs/sql-ref-syntax-qry-select-usedb.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
layout: global
title: USE Database
displayTitle: USE Database
license: |
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
---

### Description
`USE` statement is used to set the current database. After the current database is set,
the unqualified database artifacts such as tables, functions and views that are
referenced by SQLs are resolved from the current database.
The default database name is 'default'.

### Syntax
{% highlight sql %}
USE database_name
{% endhighlight %}

### Parameter

<dl>
<dt><code><em>database_name</em></code></dt>
<dd>
Name of the database will be used. If the database does not exist, an exception will be thrown.
</dd>
</dl>

### Example
{% highlight sql %}
-- Use the 'userdb' which exists.
USE userdb;
+---------+--+
| Result |
+---------+--+
+---------+--+

-- Use the 'userdb1' which doesn't exist
USE userdb1;
Error: org.apache.spark.sql.catalyst.analysis.NoSuchDatabaseException: Database 'userdb1' not found;(state=,code=0)
{% endhighlight %}
Copy link
Contributor

@amanomer amanomer Aug 24, 2019

Choose a reason for hiding this comment

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

Add related statements like ALTER DATABASE, CREATE DATABASE, DROP DATABASE and SHOW DATABASE.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


### Related statements.
- [CREATE DATABASE](sql-ref-syntax-ddl-create-database.html)
- [DROP DATABASE](sql-ref-syntax-ddl-drop-database.html)
- [CREATE TABLE ](sql-ref-syntax-ddl-create-table.html)