zoukankan      html  css  js  c++  java
  • postgresql逻辑结构--表(二)

    一、创建表

    语法:

    create table table_name(

    col01_name data_type,

    col02_name data_type,

    col03_name data_type,
    );

    1.1 创建表的时候可以指定主键:

    postgres=# create table test01(
    postgres(# id int primary key,
    postgres(# note varchar(20));
    CREATE TABLE
    

      

    1.2 如果使用复合主键,则需要使用约束字句的语法:

    constraint constrait_name primary key (coll_name, col2_name,...)

    postgres=# create table test02(
    postgres(# id1 int,
    postgres(# id2 int,
    postgres(# note varchar(20),
    postgres(# constraint pk_test02 primary key (id1,id2));
    CREATE TABLE
    postgres=# d test02
               Table "public.test02"
     Column |         Type          | Modifiers
    --------+-----------------------+-----------
     id1    | integer               | not null
     id2    | integer               | not null
     note   | character varying(20) |
    Indexes:
        "pk_test02" PRIMARY KEY, btree (id1, id2)
    
    postgres=#
    

      

    1.3 创建表指定唯一键,约束主键的一种。constraint constraint_name unique(col1_name,col2_anme,...)

    postgres=# create table test03(
    postgres(# id1 int
    postgres(# ^C
    postgres=# create table test03(
    postgres(# id1 int,
    postgres(# id2 int,
    postgres(# id3 int,
    postgres(# note varchar(20),
    postgres(# constraint pk_test03 primary key(id1,id2),
    postgres(# constraint uk_test03 unique(id3));
    CREATE TABLE
    

      

    1.4  check 也是一种约束形式,用于定义某些字段的值必须满足某种要求,

    constraint constraint_name check(expression)

    例如:建立一张child表,要求字段(age)不能大于18岁:

    postgres=# create table child(
    postgres(# name varchar(20),
    postgres(# age int,
    postgres(# note text,
    postgres(# constraint ck_child_age check(age < 18));
    CREATE TABLE
    

      

    1.5  使用其他表作为模板创建新表:

    postgres=# create table baby (like child);
    CREATE TABLE
    postgres=# d baby
                Table "public.baby"
     Column |         Type          | Modifiers
    --------+-----------------------+-----------
     name   | character varying(20) |
     age    | integer               |
     note   | text                  |
    
    postgres=# d child
                Table "public.child"
     Column |         Type          | Modifiers
    --------+-----------------------+-----------
     name   | character varying(20) |
     age    | integer               |
     note   | text                  |
    Check constraints:
        "ck_child_age" CHECK (age < 18)
    
    postgres=#
    

      

    1.5  用模板创建的表没有把源表上的约束复制过来,如果想完全复制多来源表的其它信息,需要‘including’:

    including defults

    including constraints

    including indexes

    including storage

    including comments

    including all

    postgres=# create table baby (like child including all);
    CREATE TABLE
    

     

    二、临时表

    postgresql支持两类临时表,会话结束时,临时表就会消失。

    会话临时表:数据可以一直保存在整个会话的生命周期中。

    事务临时表:数据只存在于这个失误的生命周期中。

    如果在两个不同的session中创建同名的临时表,实际上创建的两个不同的表。

    postgres=# create temporary table tmp_t1(
    postgres(# id int primary key,
    postgres(# note text);
    CREATE TABLE
    postgres=# d    \ 可以查到
    List of relations Schema | Name | Type | Owner -----------+-------------+-------+---------- pg_temp_2 | tmp_t1 | table | postgres

      

    当重新打开一个终端session时,用d是查不到这个临时表的。d把schema名称加上:

    postgres=# d pg_temp_2.tmp_t1;
       Table "pg_temp_2.tmp_t1"
     Column |  Type   | Modifiers
    --------+---------+-----------
     id     | integer | not null
     note   | text    |
    Indexes:
        "tmp_t1_pkey" PRIMARY KEY, btree (id)
    

      # 可以查到,但是无法访问数据,也不可以插入修改数据。

    创建事物级的临时表,加上  on commit delete rows

    postgres=# create temporary table tmp_t2(id int primary key,note text) on commit delete rows;
    CREATE TABLE
    postgres=# begin;
    BEGIN
    postgres=# insert into tmp_t2 values(1,'aaa');
    INSERT 0 1
    postgres=# insert into tmp_t2 values(2,'bbb');
    INSERT 0 1
    postgres=# select * from tmp_t2;
     id | note
    ----+------
      1 | aaa
      2 | bbb
    (2 rows)
    postgres=# end;
    COMMIT
    postgres=# select * from tmp_t2;
     id | note
    ----+------
    (0 rows)
    

      #  事务结束后,表就消失了。

    temporary  可以缩写成  temp

  • 相关阅读:
    ansible
    celery 计划任务使用
    11 session 使用
    10 模版继承和页面之间的调用
    9 模版语言 jinja2
    8 公共函数
    7 文件上传
    6 获取请求头和URL信息
    5 获取Form表单取值
    4 文件操作 支持图片 视频 mp3 文本等
  • 原文地址:https://www.cnblogs.com/sunshine-long/p/9051558.html
Copyright © 2011-2022 走看看