Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
soyeric128 authored Jul 17, 2023
1 parent 9183edc commit 665a325
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
5 changes: 3 additions & 2 deletions docs/doc/13-sql-reference/41-access-control-privileges.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,14 @@ Databend offers a range of privileges that allow you to exercise fine-grained co

## Database Privileges

Please note that you can use the [USE DATABASE](../14-sql-commands/00-ddl/10-database/ddl-use-database.md) command to specify a database once you have any of the following privileges to the database or any privilege to a table in the database.

| Privilege | Description |
|:----------|:--------------------------------------------------------------------------------|
| Alter | Renames a database. |
| CREATE | Creates a database. |
| DROP | Drops or undrops a database. Restores the recent version of a dropped database. |
| SELECT | SHOW CREATE a database. USE a database. |

| SELECT | SHOW CREATE a database. |

## Session Policy Privileges

Expand Down
51 changes: 51 additions & 0 deletions docs/doc/14-sql-commands/00-ddl/10-database/ddl-use-database.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: USE DATABASE
---

Selects a database for the current session. This statement allows you to specify and switch to a different database. Once you set the current database using this command, it remains the same until the end of the session unless you choose to change it.

## Syntax

```sql
USE <database_name>
```

## Examples

```sql
-- Create two databases
CREATE DATABASE database1;
CREATE DATABASE database2;

-- Select and use "database1" as the current database
USE database1;

-- Create a new table "table1" in "database1"
CREATE TABLE table1 (
id INT,
name VARCHAR(50)
);

-- Insert data into "table1"
INSERT INTO table1 (id, name) VALUES (1, 'John');
INSERT INTO table1 (id, name) VALUES (2, 'Alice');

-- Query all data from "table1"
SELECT * FROM table1;

-- Switch to "database2" as the current database
USE database2;

-- Create a new table "table2" in "database2"
CREATE TABLE table2 (
id INT,
city VARCHAR(50)
);

-- Insert data into "table2"
INSERT INTO table2 (id, city) VALUES (1, 'New York');
INSERT INTO table2 (id, city) VALUES (2, 'London');

-- Query all data from "table2"
SELECT * FROM table2;
```

0 comments on commit 665a325

Please sign in to comment.