当前位置:网站首页>PostgreSQL Basics--Common Commands
PostgreSQL Basics--Common Commands
2022-08-01 23:07:00 【Rosita.】
Table of Contents
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.
边栏推荐
猜你喜欢
文件查询匹配神器 【glob.js】 实用教程
小程序毕设作品之微信美食菜谱小程序毕业设计成品(6)开题答辩PPT
还在纠结报表工具的选型么?来看看这个
Background project Express-Mysql-Vue3-TS-Pinia page layout-sidebar menu
img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)
使用Jenkins做持续集成,这个知识点必须要掌握
Quarantine and downgrade
E - Integer Sequence Fair
从0到1:图文投票小程序设计与研发笔记
xctf攻防世界 Web高手进阶区 webshell
随机推荐
String - Trie
计算由两点定义的线的角度
Create virtual environments with virtualenv and Virtualenvwrapper virtual environment management tools
sys_kill系统调用
联邦学习在金融领域的发展和应用
xctf attack and defense world web master advanced area webshell
PAM Palindromic Automata
APP专项测试:流量测试
论文解读(GSAT)《Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism》
萍不回答
【数据分析03】
小程序毕设作品之微信体育馆预约小程序毕业设计成品(4)开题报告
Error creating bean with name ‘dataSource‘:Unsatisfied dependency expressed through field ‘basicPro
Use Jenkins for continuous integration, this knowledge point must be mastered
别看了,这就是你的题呀
Additional Features for Scripting
【SeaTunnel】从一个数据集成组件演化成企业级的服务
excel clear format
解决yolov5训练时出现:“AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train ”
Quarantine and downgrade