当前位置:网站首页>PostgreSQL 基础--常用命令
PostgreSQL 基础--常用命令
2022-08-01 23:01:00 【Rosita.】
目录
常用命令:
1.查看数据库所有表 : \d
2.查看某表的定义情况 : \d 表名
3.列出所有数据库: \l
4.切到指定数据库: \c 数据库名
5.退出: \q
PgSql基础语法:
1.创建表:crearte table 表名(字段1 类型 primary key ,...);
2.删除表:drop table name;
3.插入语句:insert into 表名 values(值1,...);
4.更新语句:update 表名 set 字段 = "";
5.删除语句:delete from 表名;
6.查询所有数据语句:select * from 表名
7.排序(配合select用):升序:order by 字段
降序:order by 字段 desc
8.分组查询(配合select和聚合函数用):group by
#按照学生年龄分组:select age,count(*) from student group by age;
9.多表联查
注意给每个表起别名,where多表的关联关系
select a.student_name,b.class_name from student a, class b where a.id = b.id;
10.insert into ...select语句(把数据从一张表插入另一张表)
#student表结构
create table student(id serial primary key, student_name varchar(32), age int,class_id int);
#class表结构
create table class(id int, class_name varchar(32) primary key);
insert into student values(1,'张三',19,1);
#自增数据插入(id不为0):
insert into student select max(id+1),'李四',18,2 from student;
#插入class数据
insert into class select class_id,'三年级' from student where student_name = '张三';
11清理表
truncate table 表名;
12.修改字段名:
alter table 表名 rename column 字段名 to 新字段名
13 修改表名:
alter table 表名 rename to 新表名;
14删除字段:
alter table 表名 alter 字段名 drop not null;
拓展:
SQL命令一般分DQL,DML,DDL
DQL:数据查询语句
DML:数据操纵语言,主要是插入,更新,删除数据
DDL:数据定义语言,主要用于创建,删除,修改表,索引等
边栏推荐
猜你喜欢
When solving yolov5 training: "AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train"
y84. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (15)
npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)
下载安装 vscode(含汉化、插件的推荐和安装)
小程序中的多表联合查询
使用Jenkins做持续集成,这个知识点必须要掌握
域名重定向工具 —— SwitchHosts 实用教程
深度学习Course2第二周Optimization Algorithms习题整理
小程序毕设作品之微信体育馆预约小程序毕业设计成品(3)后台功能
The must-have "fishing artifact" for programmers is here!
随机推荐
Flutter基础学习(一)Dart语言入门
SAP Spartacus Accessibility E2E 端到端测试
Still struggling with reporting tool selection?To take a look at this
The must-have "fishing artifact" for programmers is here!
得物客服热线的演进之路
excel vertical to horizontal
y84.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十五)
美赞臣EDI 940仓库装运订单详解
用virtualenv和Virtualenvwrapper虚拟环境管理工具创建虚拟环境
y84. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (15)
部门项目源码分享
小程序毕设作品之微信美食菜谱小程序毕业设计成品(5)任务书
杭电多校3 1012. Two Permutations dp*
46.全排列
Jmeter是什么
JS prototype hasOwnProperty in Add method Prototype end point Inherit Override parent class method
图论——强连通分量缩点+拓扑排序
Wechat Gymnasium Appointment Mini Program Graduation Design Finished Work (4) Opening Report
使用分类权重解决数据不平衡的问题
别看了,这就是你的题呀