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

PostgreSQL 中定义自己需要的数据类型的更多相关文章

  1. PostgreSQL中的Object Identifier(oid)数据类型

    PostgreSQL在内部使用对象标识符(OID)作为各种系统表的主键.OID不会添加到用户创建的表中,除非在创建表时指定了WITH OIDS,或者启用了default_with_oids配置变量.类 ...

  2. PostgreSQL中标准的SQL boolean数据类型

  3. golang自己定义数据类型查询与插入postgresql中point数据

    golang自己定义数据类型查询与插入postgresql中point数据 详细代码例如以下: package main import ( "bytes" "databa ...

  4. Postgresql中的数据类型大全

    一.数值类型: 下面是PostgreSQL所支持的数值类型的列表和简单说明: 名字 存储空间 描述 范围 smallint 2 字节 小范围整数 -32768 到 +32767 integer 4 字 ...

  5. Postgresql中的large object

    1.初识postgresql large object 一位同事在对使用pg_dump备份出来的文件(使用plain格式)进行恢复时,觉得速度非常慢,让我分析一下是什么原因. 我拿到他的.bak文件, ...

  6. 用ArcMap在PostgreSQL中创建要素类需要执行”create enterprise geodatabase”吗

    问:用Acmap在PostgreSQL中创建要素类需要执行"create enterprise geodatabase"吗? 关于这个问题,是在为新员工做postgresql培训后 ...

  7. JavaScript jQuery 中定义数组与操作及jquery数组操作

    首先给大家介绍javascript jquery中定义数组与操作的相关知识,具体内容如下所示: 1.认识数组 数组就是某类数据的集合,数据类型可以是整型.字符串.甚至是对象Javascript不支持多 ...

  8. PostgreSQL 中日期类型转换与变量使用及相关问题

    PostgreSQL中日期类型与字符串类型的转换方法 示例如下: postgres=# select current_date; date ------------ 2015-08-31 (1 row ...

  9. 在C++中定义常量的两种方法的比较

    常量是定以后,在程序运行中不能被改变的标识符.C++中定义常量可以用#define .const 这两种方法.例如:#define PRICE 10 //定义单价常量10const int PRICE ...

随机推荐

  1. endsWith和startsWith同样效果其他形式的写法(2016.1.12)

    //判断以什么开始startWith str = "abcdef"; //用其他的形式写的startsWith if(str.indexOf("abc")==0 ...

  2. mysql查询昨天本周上周上月

    昨天 $yestoday = date("Y-m-d 00:00:00",strtotime('-1day'));$today = date("Y-m-d 00:00:0 ...

  3. WeakHashMap 理解笔记

    An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. M ...

  4. [LeetCode]题解(python):051-N-Queens

    题目来源 https://leetcode.com/problems/n-queens/ The n-queens puzzle is the problem of placing n queens ...

  5. Android笔记:gson处理多层嵌套的复杂形式的json

    当一个Class的字段属性中包含另一个class时gson能正常处理吗? 最初看到网上有说使用static的说法 经验证是不需要的 直接当普通类来用就可以了. 直接使用gson.fromJson方法即 ...

  6. Advanced REST client的使用说明

    1.  为什么要使用REST Client 在实际企业开发过程中经常会有这样的需求: 1.我当前开发的这个系统是需要调用其他系统的接口,也就是我们需要频繁的测试接口,尝试不同的入参参数去查看返回结果, ...

  7. 复合事件ready,hover,toggle

    1.ready 2.hover 3.toggle(fn1,fn2, …)(被废弃) 2.hover(fn(){……},fn(){……}) 特别强调一点,hover的是mouseenter和mousel ...

  8. 微信公众平台开发(98) UnionID

    关键字 微信公众平台 微信开放平台 UnionID作者:方倍工作室原文:http://www.cnblogs.com/txw1958/p/weixin98-get-user-UnionID.html ...

  9. 对JSP和Servlet性能优化,提升执行效率

    你的J2EE应用是不是运行的很慢?它们能不能承受住不断上升的访问量?本文讲述了开发高性能.高弹性的JSP页面和Servlet的性能优化技术.其意思是建立尽可能快的并能适应数量增长的用户及其请求.在本文 ...

  10. struts配置测试中遇到报错信息,记录下

    tomcat7 jdk7myeclipse2014 部署完成后,访问页面报错struts.xml文件内容: <?xml version="1.0" encoding=&quo ...