PostgreSQL 中定义自己需要的数据类型

PostgreSQL解决某系数据库中的tinyint数据类型问题,创建自己需要的数据类型如下:

CREATE DOMAIN tinyint

  AS smallint

  CONSTRAINT tinyint_check CHECK (VALUE >= 0 AND VALUE <= 255);

ALTER DOMAIN tinyint

  OWNER TO postgres;

COMMENT ON DOMAIN tinyint

  IS 'tinyint type between 0 and 255';

postgres=# create table test_domain(id tinyint);

CREATE TABLE

postgres=# insert into test_domain values (1);

INSERT 0 1

postgres=# insert into test_domain values (0);

INSERT 0 1

postgres=# insert into test_domain values (255);

INSERT 0 1

postgres=# insert into test_domain values (256);

ERROR:  value for domain tinyint violates check constraint "tinyint_check"

STATEMENT:  insert into test_domain values (256);

ERROR:  value for domain tinyint violates check constraint "tinyint_check"

postgres=# insert into test_domain values (-1);

ERROR:  value for domain tinyint violates check constraint "tinyint_check"

STATEMENT:  insert into test_domain values (-1);

ERROR:  value for domain tinyint violates check constraint "tinyint_check"

postgres=# insert into test_domain values (100);

INSERT 0 1

postgres=# select * from test_domain ;

 id 

-----

   1

   0

 255

 100

(4 rows)

 

PostgreSQL中创建自己的枚举数据类型

[postgres@minion4 bin]$ ./psql test test

psql (9.3.9)

Type "help" for help.

test=# CREATE TYPE user_enum AS ENUM ('enum1', 'enum2', 'enum3');

CREATE TYPE

test=# \dT

        List of data types

 Schema |   Name    | Description

--------+-----------+-------------

 public | user_enum |

(1 row)

 

test=# select oid from pg_type where typname='user_enum';

  oid 

-------

 16902

(1 row)

test=# CREATE SCHEMA test;

CREATE SCHEMA

test=# CREATE TABLE test.test1 (

test(# column1 int NOT NULL,

test(# column2 int NOT NULL,

test(# column3 text,

test(# column4 timestamptz,

test(# column5 timestamp,

test(# column6 varchar(10),

test(# column7 char(10),

test(# column8 user_enum,

test(# CONSTRAINT t1_pkey PRIMARY KEY (column1)

test(# );

CREATE TABLE

test=# CREATE TABLE test.test2 (

test(# column1 int NOT NULL,

test(# column2 text,

test(# CONSTRAINT t2_pkey PRIMARY KEY (column1)

test(# );

CREATE TABLE

test=# INSERT INTO test.test1

test-# SELECT id,

test-#        id % 10,

test-#        to_char(id, 'FM00000'),

test-#        '2015-09-09'::timestamptz + ((id % 100) || ' days')::interval,

test-#        '2015-09-09'::timestamp + ((id % 100) || ' days')::interval,

test-#        id % 10,

test-#        id % 10,

test-#        'enum1'::user_enum

test-# FROM generate_series(1, 1000) id;

INSERT 0 1000

test=# INSERT INTO test.test2

test-# SELECT id,

test-#        'AAA' || to_char(id, 'FM000')

test-# FROM generate_series(1, 100) id;

INSERT 0 100

test=# analyze test.test1;

ANALYZE

test=# analyze test.test2;

ANALYZE

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

行动派大鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值