zoukankan      html  css  js  c++  java
  • PostgreSQL 表的基本操作

    个人学习笔记,谢绝转载!!! 原文:https://www.cnblogs.com/wshenjin/p/12974334.html


    建表

    CREATE TABLE:

    postgresql=# CREATE TABLE weather (
        "City"            varchar(80),
        "Temp_lo"         int,           -- 最低温度
        "Temp_hi"         int,           -- 最高温度
        "Prcp"            real,          -- 湿度
        "Date"            date
    );
    

    两个划线(“--”)引入注释,任何跟在它后面直到行尾的东西都会被忽略。 SQL 是对关键字和标识符大小写不敏感的语言,表名和字段名只有在标识符用双引号包围时才能保留它们的大小写。

    查看表结构:

    postgresql=# d weather               
                          Table "public.weather"
     Column  |         Type          | Collation | Nullable | Default 
    ---------+-----------------------+-----------+----------+---------
     City    | character varying(80) |           |          | 
     Temp_lo | integer               |           |          | 
     Temp_hi | integer               |           |          | 
     Prcp    | real                  |           |          | 
     Date    | date                  |           |          | 
    

    删除表:

    DROP TABLE [ IF EXISTS ] tabl1,table2,table3....

    修改表结构:

    ALTER TABLE [ IF EXISTS ]

    #重命名
    postgresql=# alter table weather rename to "Weather";
    
    #添加字段
    postgresql=# alter table "Weather" add column context varchar(100);
    postgresql=# alter table "Weather" add column "City_id" int;   
    
    #修改字段名称
    postgresql=# alter table "Weather" rename column "context" to "Context";
    
    #修改字段类型
    postgresql=# alter table "Weather" alter column "City_id" type smallint;
    
    #删除字段
    postgresql=# alter table "Weather" drop column "Context"; 
    
    #设置字段 NOT NULL 约束
    postgresql=# ALTER TABLE "Weather" ALTER "City" SET NOT NULL;
    
    #添加primary key
    postgresql=# ALTER TABLE "Weather" ADD CONSTRAINT primary_key PRIMARY KEY ("City_id");
    
    #删除primary key
    postgresql=# ALTER TABLE "Weather" DROP CONSTRAINT primary_key ;
    

    删除表数据:

    DELETE FROM table_name WHERE [condition];

    如果没有指定 WHERE 子句,表中的所有记录将被删除 一般我们需要在 WHERE 子句中指定条件来删除对应的记录,条件语句可以使用 AND 或 OR 运算符来指定一个或多个。

    清空表:

    TRUNCATE TABLE

  • 相关阅读:
    js面向对象编程-高级内容
    (转)js中的hasOwnProperty和isPrototypeOf方法
    Bootstrap_表单
    Bootstrap_表格
    Bootstrap_排版
    Bootstrap_网格系统
    Bootstrap_CSS概览
    redis的搜索组件 redis-search4j
    有哪些值得学习的spring boot开源项目?
    国内最火的10款Java开源项目,都是国人开发,CMS居多
  • 原文地址:https://www.cnblogs.com/wshenjin/p/12974334.html
Copyright © 2011-2022 走看看