PostgreSQL常用语句

PostgreSQL常用语句

PostgreSQL常用语句

一、数据库
1.查询服务器版本

1.1查询详细信息

select version();

1.2查看版本信息

show server_version;

1.3查看数字版本信息包括小版本号

show server_version_num;

或

select current_setting('server_version_num');

注意:select current_setting(‘server_version_num’);返回类型为text,如果需要可以转换为interger

select current_setting('server_version_num')::integer;

2.创建数据库

create database testdb;

3.修改数据库

重命名数据库名称:

alter database testdb rename to new_name;

修改数据库并发连接数量:

alter database testdb connection limit 10;

修改数据库的表空间:

alter database testdb set tablespace new_tablespace;

4.删除数据库

drop database if exists testdb;

5.其他

查询用户

select usename from pg_user; 

二、表
1.新建表

create table student(
    id serial primary key,
    name varchar(64) not null,
    age integer not null,
    sex integer not null,
    createtime timestamp without time zone not null default now(),
    updatetime timestamp without time zone
);

id为主键,其中serial代表自增,默认从1开始增加,每次自增1。

复制表:

create table student_copy as select * from student;

2.删除表

delete table student;

3.查询表

查询student表是否存在:

select * from pg_class where relname = 'student' and relkind = 'r';
or
select * from pg_tables where tablename = 'student';

4.修改表

4.1对表本身操作

4.1.1重命名表名称

alter table student rename to new_student;

4.1.2添加表字段

alter table student add column height integer not null;

4.1.3删除表字段

alter table student drop column sex;

4.1.4重命名表字段

alter table student rename column name to new_name;

4.1.5更改字段属性

a)查询表所有字段属性

select c.relname, col_description(a.attrelid, a.attnum) as comment, format_type(a.atttypid, a.atttypmod) as type, a.attname as name, a.attnotnull as notnull from pg_class as c, pg_attribute as a where a.attrelid = c.oid and a.attnum > 0 and c.relname = 'student';

这里用到了pg_class和pg_attribute系统表,pg_class系统表记录了数据表、索引(仍然需要参阅pg_index)、序列、视图、复合类型和一些特殊关系类型的元数据;pg_attribute系统表存储所有表(包括系统表,如pg_class)的字段信息,数据库中的每个表的每个字段在pg_attribute表中都有一行记录。pg_class系统表中字段relname记录表名称,oid字段标识表的唯一id;pg_attribute系统表中attrelid也标识表的id。

b)查询表中指定字段的属性

select c.relname, col_description(a.attrelid, a.attnum) as comment, format_type(a.atttypid, a.atttypmod) as type, a.attname as name, a.attnotnull as notnull from pg_class as c, pg_attribute as a where a.attrelid = c.oid and a.attnum > 0 and c.relname = 'student' and a.attname = 'name';

pg_attribute系统表中atttypid与attname字段分别是表字段类型与名称。

c)更改字段由int4更改为int8:

alter table student alter column sex type bigint;

d)如果把字段name把属性Text转化为int,原来text里面存在空啥的,可以

alter table student alter column name type integer using (trim(name))::integer;

e)增加/删除字段约束

e1.增加/删除字段的非空约束

增加字段的非空约束:

alter table student alter column updatetime set not null;

这个约束会立即进行数据检查,所以表在加入约束之前必须确保已有数据符合约束条件,否则执行会报错;

这种情况下,可以先删除为null的数据行,再执行增加非空约束:

delete from student where updatetime  is null;
alter table student alter column updatetime set not null;

注意,只有增加非空约束才这样使用,而且非空约束没有名字。

删除非空约束:

alter table student alter column updatetime drop not null;

也只有非空约束才能这样删除。

e2.增加/删除 字段的检查约束:

在增加检查约束前,需要先删除已存在的不满足检查约束的数据:

delete from student where age <= 3;
--ck_student_cheack_age是检查约束名称
alter table student add constraint ck_student_check_age check(age > 3);

删除字段检查约束:

alter table student drop constraint ck_student_check_age ;

e3. 增加/删除 一个唯一约束:

在增加字段的唯一约束:

--唯一约束名称uk_student_nuique_name_age 为约名称,给name和age字段添加唯一约束
alter table student add constraint uk_student_unique_name_age unique(name,age);

删除字段的唯一约束:

alter table student drop constraint uk_student_unique_name_age;

4.2对表记录操作

4.2.1插入一条记录

insert into student (name, age, sex, createtime, updatetime) values('Tom', '18', 1, '2018-11-29 17:00:02', '2018-11-29 17:00:02') ;

可以把student1表符合条件的记录拷贝到表student2:

insert into student1 select * from student2 where age > 18;

4.2.2删除记录

删除符合条件的记录:

delete from student where id = 1;
 
delete from student where age > 18;
 
delete from student where createtime <= '2018-01-01 00:00:00';

清空表记录:

delete from student;
or
truncate table student;

区别:truncate table 表名 (注:不带where语句) 速度快,而且效率高。因为delete语句每次删除一行,并在事务日志中为所删除的每行记录一项。truncate table通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放。

4.2.3查询记录

--查询全部记录
select * from student;
 
--查询符合条件的记录
select * from student where age > 18;
 
--指定查询条件和要查询的字段
select name, age, sex from student where age > 18;

另外可以使用下面的sql查询数据库的连接信息:

select * from pg_stat_activity;

包含:客户端user、ip、执行语句,状态、时间 。

4.2.4修改记录

--更新符合条件记录的更新时间,保留到系统时间的秒位
update student set updatetime = date_trunc('second', now())  where age = 18;
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/101782.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • visual studio2010注释快捷键_html中注释的快捷键

    visual studio2010注释快捷键_html中注释的快捷键VS注释快捷键操作:  注释:先CTRL+K,然后CTRL+C  取消注释:先CTRL+K,然后CTRL+U

    2022年8月15日
  • 一文带你了解常见的数据指标都有哪些【数据分析】[通俗易懂]

    一文带你了解常见的数据指标都有哪些【数据分析】[通俗易懂]为了进一步提升自己分析业务的能力,首先得先了解好在日常业务分析当中往往会用到那些数据指标。接下来我给大家整理了常见的一些数据指标,建议收藏后慢慢阅读。目录用户获取渠道到达量渠道转化率渠道ROI日应用下载量日新增用户数用户获取成本一次会话用户数用户活跃活跃用户PV和UV用户会话次数用户访问时长功能使用率用户留存留存率用户流失率退出率和跳出率市场营销用户生命周期用户生命周期价值用户忠诚指数用户流失指数用户价值指数结束语用户获取渠道到达量这个俗称曝光量,如果在CSDN有发过博客的朋友应该知道“展现量”这

    2022年5月10日
  • Latex 公式换行问题,(换行,等号对齐)

    Latex 公式换行问题,(换行,等号对齐)1.换行后等式对齐\begin{equation}\begin{aligned}R(S_2)&=p_2\cdotS_2=\sum_{i\in\mathcal{I}^+(p_2)}B_i+\betaB_{l’}\\&\leq\sum_{i\in\mathcal{I}^+(p_2)}B_i+B_{l’}\leq\sum_{i\in\mat

    2022年4月30日
  • baq在聊天中啥意思_baq的含义_baq是什么的缩写_baq的词语_baq代表的意思,baq.com/net/cn域名的含义 – 星云网…

    baq在聊天中啥意思_baq的含义_baq是什么的缩写_baq的词语_baq代表的意思,baq.com/net/cn域名的含义 – 星云网…[DB]CONNECT:[UseTime:0.001102s]mysql:host=127.0.0.1;port=3306;dbname=ci;charset=utf8mb4[SQL]SHOWCOLUMNSFROM`cmf_hook`[RunTime:0.003591s][SQL]SELECT`hook`FROM`cmf_hook`WHERE`typ…

    2022年6月15日
  • python的学生信息管理系统_学员信息管理系统设计

    python的学生信息管理系统_学员信息管理系统设计一.系统需求使用面向对象编程思想完成学员管理系统的开发,具体如下:系统要求:学员数据存储在文件中系统功能:添加学员、删除学员、修改学员信息、查询学员信息、显示所有学员信息、保存学员信息及退出系统等功能。程序文件如下:程序入口文件:main.py学员文件:student.py管理系统⽂文件:managerSystem.pymain.py#1.导入managerSystem模块frommanagerSystemimport*#2.启动学员管理系统if__name__

    2022年9月20日
  • app唤起小程序_微信小程序支付轮训

    app唤起小程序_微信小程序支付轮训在同一开放平台账号下的移动应用及小程序无需关联即可完成跳转,非同一开放平台账号下的小程序需与移动应用(APP)成功关联后才支持跳转。可在“管理中心-移动应用-应用详情-关联小程序信息”,为通过审核的

    2022年8月2日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号