首页 > 数据库 >postgreSQL常用命令

postgreSQL常用命令

时间:2022-12-16 17:22:38浏览次数:39  
标签:postgreSQL -- key1 tablename pg 常用命令 NULL alter

 

--建表案例
CREATE TABLE  gas_use_test  (
   id serial PRIMARY KEY NOT NULL,
   tenantId   integer NOT NULL,
   meterNo  varchar(255) unique NOT NULL  ,
   communicationDate  varchar(255) NOT NULL ,
   useGasAmount  decimal(10,4) DEFAULT NULL ,
   create_time  TIMESTAMP NOT null default now()
)

--批量更新
update test 
set info=tmp.info 
from (values (1,'new1'),(2,'new2'),(6,'new6')) as tmp (id,info) 
where test.id=tmp.id;  

--不存在插入、存在更新ON CONFLICT 只在 PostgreSQL 9.5 以上可用。
insert into tablename (key1,key2,key3) values ('xxx','xxx','xxx') 
on conflict(key1) 
do update set key2 = 'yyy',key3 = 'yyy';


--修改表中的字段名(将key1修改为key2)
alter table tablename rename key1 to key2;
--表中新增字段
alter table tablename add key1 character varying not null;
--修改表名
alter table "tablename" rename to "new_tablename";
--删除表中某个字段
alter table tablename drop column if exists key1;


--表名、主键
SELECT * FROM pg_tables where tablename='gas_alarm'
select * from pg_catalog.pg_constraint 
select * from pg_catalog.pg_database pd 
select * from pg_catalog.pg_type pt 
--查看表结构
SELECT a.attnum, a.attname AS field, t.typname AS type, a.attlen AS length, a.atttypmod AS lengthvar
    , a.attnotnull AS notnull, b.description AS comment
FROM pg_class c, pg_attribute a
    LEFT JOIN pg_description b
    ON a.attrelid = b.objoid
        AND a.attnum = b.objsubid, pg_type t
WHERE c.relname = 'ods_day_payrecord'
    AND a.attnum > 0
    AND a.attrelid = c.oid
    AND a.atttypid = t.oid
ORDER BY a.attnum;

 

标签:postgreSQL,--,key1,tablename,pg,常用命令,NULL,alter
From: https://www.cnblogs.com/wangbin2188/p/16987864.html

相关文章

  • Kubernetes(k8s) kubectl rollout resume常用命令
    kubectl在$HOME/.kube目录中查找一个名为config的配置文件。可以通过设置KUBECONFIG环境变量或设置--kubeconfig参数来指定其它kubeconfig文件。本文主要介绍K......
  • Linux 常用命令整理
    【常用命令】  查询当前时间:date  修改时间:tzselect #根据提示输入编号  显示当前绝对路径:pwd  重启系统:reboot  在指定文件中查找某内容:grep"某......
  • Linux常用命令
    几个常用命令ls(list)查看当前目录下的内容pwd(printworkdirecotry)查看当前所在目录cd[目录名](changedirectory)切换目录touch[文件名](touch)如果文件不存......
  • Linux常用命令与shell脚本学习
    Linux常用命令1、关机/重启/注销常用命令作用shutdown-hnow即刻关机shutdown-h1010分钟后关机shutdown-h11:0011:00关机shutdown-h+10预......
  • 从Mariadb迁移到postgresql
    前言本文主要实验所用的环境,后端是golang,ginweb框架.mysqldriver:github.com/go-sql-driver/mysqlv1.4.1迁移后PostgreSQLdriver:github.com/jackc/pgx/v5v......
  • postgresql数据库插入和读取图片
    postgresql插入和读取图片postgresql存储图片需要二进制类型bytea,创建一张测试表:postgres=#createtabletest_image(imgbytea);CREATETABLE使用jdbc插入1.jpgtry......
  • PostgreSQL 常用操作记录
    常用命令行命令1,连接数据库#需要输入密码psql-hhost-Udbuser-ddbname免密登录方法:方法一:设定环境变量PGPASSWORD​方法二:配置.pgpass​touch~/......
  • Anaconda 安装及常用命令
    Anaconda下载安装包linux,windows链接:https://pan.baidu.com/s/10VhNRzOr-W19Z_RemhnthA提取码:k6mo 常用命令:--激活condacondaactivate--退出condacon......
  • Docker 安装,常用命令
    安装Docker官方所有操作系统安装教程:InstallDockerEngineonCentOS|DockerDocumentation,其中CentOS安装docker引擎的代码:安装yum-utils,配置库的地址sudoyuminst......
  • linux常用命令2
    1、以下鉴定故障:有两种可能,其一密码输入错误;其二,未设置root密码。sudopasswdroot改密码就行。$su-su:鉴定故障2、......