PostgreSQL 基础--常用命令

目录

常用命令:

PgSql基础语法:

拓展:


常用命令:

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:数据定义语言,主要用于创建,删除,修改表,索引等

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值