一、创建表

语法:

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

postgresql逻辑结构--表(二)的更多相关文章

  1. postgresql逻辑结构--表空间(四)

    一.创建表空间 1. 语法:create tablespace tablespace_name [owner user_name] location 'directory' postgres=# cr ...

  2. PostgreSQL的存储系统二:REDOLOG文件存储结构二

    REDOLOG文件里的用户数据和数据文件里的用户数据存储结构相同 几个月前同事给台湾一家公司培训<pg9 ad admin>时,有个学员提及WAL里记录的内容为Query时的SQL语句(比 ...

  3. mybatis使用注解往postgresql数据库表insert数据[主键自增]的写法

    建表SQL: DROP TABLE IF EXISTS person; CREATE TABLE person( person_id serial PRIMARY KEY NOT NULL, pers ...

  4. Postgresql两表联结更新

    Postgresql两表联合更新近日使用Postgresql感到有点不好用,一个联合更新非要这样写语法才对:update d_routetripset name=b.name ,    descrip ...

  5. postgresql逻辑结构(一)

    一.数据库逻辑结构介绍 数据库:应用连接到一个数据库时,一般不能访问其它数据库,除非使用dblink等其他手段. 表.索引:postgresql中标的术语为relation,其它数据库中成为table ...

  6. [转]PostgreSQL 逻辑结构 和 权限体系 介绍

    摘要: 本文旨在帮助用户理解PostgreSQL的逻辑结构和权限体系,帮助用户快速的理解和管理数据库的权限. 逻辑结构 最上层是实例,实例中允许创建多个数据库,每个数据库中可以创建多个schema,每 ...

  7. 跟我一起读postgresql源码(二)——Parser(查询分析模块)

    上篇博客简要的介绍了下psql命令行客户端的前台代码.这一次,我们来看看后台的代码吧. 十分不好意思的是,上篇博客我们只说明了前台登陆的代码,没有介绍前台登陆过程中,后台是如何工作的.即:后台接到前台 ...

  8. ORACLE体系结构一 (逻辑结构)-表空间、段、区和数据块

    一.Oracle的逻辑结构 Oracle的逻辑结构是一种层次结构.主要由:表空间.段.区和数据块等概念组成.逻辑结构是面向用户的,用户使用Oracle开发应用程序使用的就是逻辑结构.数据库存储层次结构 ...

  9. ORACLE体系结构逻辑结构-表空间、段、区和数据块

    转自: https://www.cnblogs.com/sunziying/p/8994792.html 一.Oracle的逻辑结构 Oracle的逻辑结构是一种层次结构.主要由:表空间.段.区和数据 ...

随机推荐

  1. android-基础编程-democoderjoy-架构篇

    设计这个demo很简单,针对每个控件放到一个listitem中去,主activity继承之listActivity,这样再override其单击效果进入到每个控件. 主界面流程 1.继承 MainAc ...

  2. (day20)javaEE三大组件之一Servlet (简介(二)servletconfig,servletContext,session,cookie,request,response,out)

    javaEE是服务器编程,javaEE提供了服务器的接口让具体的服务器去创建实现的对象 JavaEE是sun公司为了解决企业级开发定义的一套技术,只提供了规范,具体的实现是由服务器完成的 servle ...

  3. _编程语言_C语言_C++_时间

    C++ 标准库没有提供日期类型,使用C预压的日期和事件操作. 使用时引入头文件  <ctime> . 四个和事件相关的函数: clock_t // 系统时间和日期表示为某种整数time_t ...

  4. ubuntu下mysql远程连接

    第一步: vim /etc/mysql/my.cnf找到bind-address = 127.0.0.1 注释掉这行,如:#bind-address = 127.0.0.1               ...

  5. HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化

    HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...

  6. yum改成网易的源

    用网易的源会快很多,步骤如下:http://mirrors.163.com/.help/centos.html 1.首先备份/etc/yum.repos.d/CentOS-Base.repo mv / ...

  7. FastReport套打 和连续打印

    FastReport套打,纸张是连续的带锯齿的已经印刷好的,类似于通信公司发票这里设计的是客户销售记录.客户有两个要求:1.因为打印纸张是印刷的,明细记录只有8行,所以,如果明细记录如果不到8行,就将 ...

  8. [转载]DevOps发展的四个重要阶段

    DevOps是敏捷开发的延续,它将敏捷的精神延伸至IT运营(IT Operation)阶段.敏捷开发的主要目的是响应变化,快速交付价值.以2001年的敏捷宣言发布这个里程碑为起点,开始几年内企业主要在 ...

  9. ipad协议

    getLoginQRCode (获取登录二维码)CheckLoginQRCode(检测扫码状态)ManualAuth(扫码登录)ManualAuth(62数据登录)ManualAuth(账号密码登录) ...

  10. sql server 查看列备注、类型、字段大小

    select 列名 = a.name ,类型 = c.name ,长度 = columnproperty(a.id,a.name,'precision') ,备注 = g.value from sys ...