postgre 列举数据库语法
时间: 2024-10-12 18:16:50 浏览: 37
PostgreSQL是一种强大的开源对象关系型数据库系统,其SQL语法有一些特定之处。以下是一些基本的数据库操作和语法:
1. **创建数据库**:
```
CREATE DATABASE database_name;
```
2. **切换到数据库**:
```
\c database_name;
```
3. **创建表**:
```sql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);
```
例如创建用户表:
```sql
CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(50), email VARCHAR(100));
```
4. **插入数据**:
```
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
```
5. **查询数据**:
```sql
SELECT * FROM table_name;
SELECT column1, column2 FROM table_name WHERE condition;
```
6. **更新记录**:
```
UPDATE table_name SET column = new_value WHERE condition;
```
7. **删除记录**:
```
DELETE FROM table_name WHERE condition;
```
8. **创建索引**:
```sql
CREATE INDEX index_name ON table_name(column);
```
9. **视图**:
```sql
CREATE VIEW view_name AS SELECT ... FROM table_name;
```
10. **备份恢复**:
-U username -F c database_name > backup.sql
```
- 恢复:
```
psql -U username -d database_name < backup.sql
```
阅读全文