当前位置:网站首页>PostgreSQL Basics--Common Commands

PostgreSQL Basics--Common Commands

2022-08-01 23:07:00 Rosita.

Table of Contents

Common commands:

PgSql basic syntax:

Extended:


Common commands:

1. View all tables in the database: \d

2. View the definition of a table: \d table name

3. List all databases:     \l

4. Switch to the specified database: \c database name

5. Exit:           \q

PgSql basic syntax:

1. Create a table: create table table name (field 1 type primary key ,...);

2. Drop table: drop table name;

3. Insert statement: insert into table name values(value 1,...);

4. Update statement: update table name set field = "";

5. Delete statement: delete from table name;

6. Query all data statement: select * from table name

7. Sort (with select): Ascending: order by field

Descending: order by field desc

8. Group query (used with select and aggregation functions): group by

#Group by age: select age,count(*) from student group by age;

9. Multi-table joint query

Pay attention to giving each table an alias, where is the relationship between multiple tables

select a.student_name,b.class_name from student a, class b where a.id = b.id;

10.insert into ...select statement (insert data from one table into another table)

#student table structure

create table student(id serial primary key, student_name varchar(32), age int,class_id int);

#class table structure

create table class(id int, class_name varchar(32) primary key);

insert into student values(1,'Zhang San',19,1);

#Auto-increment data insertion (id is not 0):

insert into student select max(id+1),'Li Si',18,2 from student;

#Insert class data

insert into class select class_id,'third grade' from student where student_name = 'Zhangsan';

11 Cleanup table

truncate table table name;

12. Modify the field name:

alter table table name rename column field name to new field name

13 Modify table name:

alter table table name rename to new table name;

14 Delete fields:

alter table table name alter field name drop not null;

Extended:

SQL commands are generally divided into DQL, DML, DDL

DQL: Data query statement

DML: data manipulation language, mainly insert, update, delete data

DDL: Data Definition Language, mainly used to create, delete, modify tables, indexes, etc.

原网站

版权声明
本文为[Rosita.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/213/202208012301324270.html