zoukankan      html  css  js  c++  java
  • psql-06表:约束

    默认值

    • 可以理解为建表时没定义的默认值为null,表示未知,//注意和js中null不一样;

    • 建表时设置

    create table child(id int, age int default 18);
    
    • 更新时使用
    update child set age = default where id = 1;
    
    • 默认值可以是个表达式,它会在插入时计算
    create table test (id int, created_time timestamp default now());
    

    约束

    检查约束

    • check给某些字段值指定一个范围:
    create table child (name text, age int constraint ck_age check (age < 18 and age > 7) ); 
    create table NotChild (name text, age int constraint ck_age check (age >18 or age < 7) ); 
    
    //也可以都放到最后
    create table books (book_no int, name text, price numeric, discounted_price numeric,
        check(price > 0), check(discounted_price > 0), check(price > discounted_price) );
    
    create table books (book_no int, name text, price numeric, discounted_price numeric,
        check(price > 0 and discounted_price > 0 and price > discounted_price) );
    

    非空约束

    • 确保一个字段值不为空
    create table books (book_no int not null, name text);
    
    //相当于
    create table books (book_no int, name text, check(book_no is not null));
    

    主键

    • 如果表的主键只是一个字段组成,则可以通过直接在后面加primary key来指定主键;
    • 如果由多个字段组成,成为组合主键;
    create table test (id1 int, id2 int, note varchar(20), constraint key_test primary key(id1, id2) );
    
    

    唯一约束

    • 也可以指定唯一键;
      • 主键一定是唯一键,反之不是;
      • 主键不能为空,唯一键可以;
    create table test (id1 int, id2 int, id3 int, note varchar(20), constraint key_test primary key(id1, id2) , constraint uk_test unique(id3) );
    

    外键约束

    • 用于约束本表中的一个字段或多个字段的数值必须出现在另一个表的资格或多个字段中
    //注意:这里要确保class_no unique
    
    create table class(
      class_no int primary key,
      class_name varchar(40)
    );
    
    create table student(
      student_no int primary key,
      student_name varchar(40),
      age int,
      class_no int references class(class_no)
    );
    
    //建立外键之后,student中就不能插入class中不存在的class_no值
    

    用其他表作为模版创建

    create table baby (like child);
    
    //注意这样创建并没有把源表的约束复制过来,需要添加
    including defaults
    including constraints
    including indexs
    including storage
    including comments
    including all
    
    create table baby2 (like child including all);
    

    临时表

    • 创建的临时表会在一个特殊的模式中: pg_temp_XX;
    • 无论是那种临时表,会话结束后就会消失;
    • 会话级的临时表, 数据只保存在当前会话周期中;
    • 在另一个session中无法访问
    create temporary table tmp (id int primary key, note text);   //temporary可以缩写为temp
    
    • 事务级临时表,数据只保存在当前事务中;
    create temporary table tmp (id int primary key, note text) on commit delete rows;
    
    begin; //事务开始
    
    insert into values(1, 'a');  //注意在事务外是无法插入数据的
    insert into values(2, 'b');
    select * from tmp;
    end; //事务结束,tmp表还存在。但数据已经消失;
    
    

    修改表

    • 对已经建立的表使用alter table来修改表结构

    增加字段

    alter table class add column class_teacher varchar(40);
    
    //设置默认值会对已经存在的行会自动填充;
    

    删除字段

    • 和这个字段相关的约束也会被删除;
    alter table class drop column class_no;
    
    • 如果存在外键依赖不能直接删除,必须要指明删除任何依赖在该字段的东西;
    alter table class drop column class_no cascade;
    

    增加约束

    • 给表增加约束前要确定该表符合约束条件,否则会失败;
    alter table student alter column student_name set not null;  //非空约束
    
    alter table student add check(age < 16);  //检查约束
    
    alter table class add constraint unique_class_teacher unique(class_teacher);  //唯一约束
    

    删除约束

    alter table student drop constraint student_age_check;
    
    //非空约束是没有名称的
    
    alter table student alter column student_name drop not null;
    

    修改默认值

    • 不会影响已保存的行数据
    alter table student alter column age set default 15;
    

    删除默认值

    • 不会影响已保存的行数据
    alter table student alter column age drop default;
    

    修改字段数据类型

    • 只有每一行的该项数据都能隐式地替换成新的类型,转行才能成功;
    alter table student alter column student_name type text;
    
    • 注意PostgesSQL会试图把字段的约束/默认值做相应转换,但不一定会成功;所有最好成功改变类型后再手动添加一遍;

    重命名字段

    alter table book rename column book_no to book_id;
    

    重命名表

    alter table class rename to classes;
    

    增加约束时的判断方法

    • 对表增加约束,如果约束已经存在则跳过;
     CREATE OR REPLACE FUNCTION create_constraint_if_not_exists (t_name text, c_name text, constraint_sql text)
          RETURNS void
        AS
        $$
          begin
            if not exists (select constraint_name
                  from information_schema.constraint_column_usage
                  where table_name = t_name  and constraint_name = c_name) then
              execute 'ALTER TABLE ' || t_name || ' ADD CONSTRAINT ' || c_name || ' ' || constraint_sql;
            end if;
        end;
        $$
        LANGUAGE plpgsql VOLATILE;
    
    //excute
    
    SELECT create_constraint_if_not_exists ('user_sessions', 'session_pkey', 'PRIMARY KEY ("sid") NOT DEFERRABLE INITIALLY IMMEDIATE');
    
  • 相关阅读:
    Linux进入单用户模式(passwd root修改密码)
    stark组件的分页,模糊查询,批量删除
    stark组件的增删改(新)
    stark组件的增删改
    stark组件之展示数据(查)
    stark组件配置,二层URL
    单例模式及设计url分发
    Django之modelform
    rbac组件权限按钮,菜单,可拔插
    rbac权限+中间件
  • 原文地址:https://www.cnblogs.com/jinkspeng/p/5024352.html
Copyright © 2011-2022 走看看