zoukankan      html  css  js  c++  java
  • postgresql 基本语法

    postgresql数据库创建/修改/删除等写入类代码语法总结:

    1,创建库

    2,创建/删除表

      2.1 创建表 

    create table myTableName

      2.2 如果表不存在则创建表

    create table if not exists myTableName

      2.3 删除表

    drop table if exists myTableName;

      2.4 实例代码:

    drop table if exists myTableName;
    create table if not exists myTableName(
        id serial PRIMARY KEY,
        name char(11) NOT NULL,
        creationtime timestamp(6) with time zone
    );

     说明:serial,bigserial 是自增长字段数据类型,分别对应 int,long

    3,创建/修改字段

      3.1 添加字段

    ALTER TABLE myTableName ADD COLUMN wx_openid VARCHAR(28);
    ALTER TABLE myTableName ADD COLUMN wx_unionid VARCHAR(29);

    postgresql数据库查询/判断等读取类代码语法总结:

    查询所有的数据库:

    select * from pg_database;

    查询指定名字的数据库:

    select * from pg_database where datname='myDB'; 

    查询所有表信息:

    select * from pg_tables;

    查询指定数据表信息:

    select * from pg_tables where schemaname='public';
    select * from pg_tables where tablename='myTableName';

    查询指定表结构(包含字段名称,字段类型,是否可空等):

    /*查看表结构*/
    SELECT 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 c.relname ='myTableName' and a.attrelid = c.oid and a.attnum>0;

    查询当前时间:

      postgresql语句返回不包含毫秒级精度的当前日期时间:

    SELECT CURRENT_TIMESTAMP(0);  --postgresql语句返回不包含毫秒级精度的当前日期时间,例如 2016-12-29 16:54:41+08
    
    SELECT CURRENT_TIMESTAMP(1);  --postgresql语句返回包含1位毫秒级精度的当前日期时间,例如 2016-12-29 16:54:41.3+08
    
    SELECT CURRENT_TIMESTAMP(2);  --postgresql语句返回包含2位毫秒级精度的当前日期时间,例如 2016-12-29 16:54:41.34+08
    
    SELECT CURRENT_TIMESTAMP(3);  --postgresql语句返回包含3位毫秒级精度的当前日期时间,例如 2016-12-29 16:54:41.345+08
    
    SELECT CURRENT_TIMESTAMP(4);  --postgresql语句返回包含4位毫秒级精度的当前日期时间,例如 2016-12-29 16:54:41.3425+08
    
    SELECT CURRENT_TIMESTAMP(5);  --postgresql语句返回包含5位毫秒级精度的当前日期时间,例如 2016-12-29 16:54:41.34451+08
    
    SELECT CURRENT_TIMESTAMP(6);  --postgresql语句返回包含6位毫秒级精度的当前日期时间,例如 2016-12-29 16:54:41.345921+08
  • 相关阅读:
    600E
    题解报告:hdu 1124 Factorial(求N!尾数有多少个0。)
    求N!尾数有多少个0。
    poj 2185 Milking Grid(二维kmp)
    poj 2752 Seek the Name, Seek the Fame(kmp前缀表prefix_table的运用)
    hdu 1686 Oulipo(裸KMP)
    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-B-Perfect Numbers(完数)
    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-K-Matrix Multiplication(矩阵乘法)
    poj 3468 A Simple Problem with Integers(线段树区间lazy标记修改or树状数组)
    hdu 1698 Just a Hook(线段树区间lazy标记修改)
  • 原文地址:https://www.cnblogs.com/xiongzaiqiren/p/postgresql.html
Copyright © 2011-2022 走看看