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
PostgreSQL 中定义自己需要的数据类型的更多相关文章
- PostgreSQL中的Object Identifier(oid)数据类型
PostgreSQL在内部使用对象标识符(OID)作为各种系统表的主键.OID不会添加到用户创建的表中,除非在创建表时指定了WITH OIDS,或者启用了default_with_oids配置变量.类 ...
- PostgreSQL中标准的SQL boolean数据类型
- golang自己定义数据类型查询与插入postgresql中point数据
golang自己定义数据类型查询与插入postgresql中point数据 详细代码例如以下: package main import ( "bytes" "databa ...
- Postgresql中的数据类型大全
一.数值类型: 下面是PostgreSQL所支持的数值类型的列表和简单说明: 名字 存储空间 描述 范围 smallint 2 字节 小范围整数 -32768 到 +32767 integer 4 字 ...
- Postgresql中的large object
1.初识postgresql large object 一位同事在对使用pg_dump备份出来的文件(使用plain格式)进行恢复时,觉得速度非常慢,让我分析一下是什么原因. 我拿到他的.bak文件, ...
- 用ArcMap在PostgreSQL中创建要素类需要执行”create enterprise geodatabase”吗
问:用Acmap在PostgreSQL中创建要素类需要执行"create enterprise geodatabase"吗? 关于这个问题,是在为新员工做postgresql培训后 ...
- JavaScript jQuery 中定义数组与操作及jquery数组操作
首先给大家介绍javascript jquery中定义数组与操作的相关知识,具体内容如下所示: 1.认识数组 数组就是某类数据的集合,数据类型可以是整型.字符串.甚至是对象Javascript不支持多 ...
- PostgreSQL 中日期类型转换与变量使用及相关问题
PostgreSQL中日期类型与字符串类型的转换方法 示例如下: postgres=# select current_date; date ------------ 2015-08-31 (1 row ...
- 在C++中定义常量的两种方法的比较
常量是定以后,在程序运行中不能被改变的标识符.C++中定义常量可以用#define .const 这两种方法.例如:#define PRICE 10 //定义单价常量10const int PRICE ...
随机推荐
- jwplayer直播
<div class='container'> <div class='row'> <div class='col-sm-10 col-md-10 col-sm-offs ...
- data abstractions 数据抽象
Computer Science An Overview _J. Glenn Brookshear _11th Edition In this chapter we investigate how d ...
- Delphi dbf文件如何定位记录(即设置Table的RecNum属性来移动到该记录号)
Delphi的table的RecNum属性,可以用来定位记录,如:self.Table1.RecNum:=23;即可以让数据库记录移动到23号记录上,但这种作用仅限于Paradox数据库,而不是dBA ...
- jquery mobile 方法收集.
1.在列表项和按钮上禁用文本截断 如果你的列表项或者按钮上是一个很长的文本,它将会被jQuery Mobile自动截断,要禁用这个截断设置,需要在CSS选择器上添加属性"white- ...
- 點擊按鈕后彈出新頁面導致原頁面CSS失效
比方说在页面里面有个LinkButton,要点击以后要打开新窗口,而且新窗口的URL是根据用户选择结果动态产生的.LinkButton的代码这样写: protected void Service ...
- Shell 字符串常见操作
参考文章:http://blog.csdn.net/chen_jp/article/details/8922582 一 字符替换 origin=原字符串 str=替换后的字符串 替换命令: str= ...
- 20145211 《Java程序设计》第九周学习总结——垂死病中惊坐起
教材学习内容总结 JDBC简介 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操作,开发人员无须接触底层数据库驱动程序的差异性 JDBC标准分为两个部分:J ...
- magento 切换数据库,使用不同数据库
1. 在app/etc/local.xml 中,添加新的数据库选项 <?xml version="1.0"?> <config> <global> ...
- 【上手centos】一、前情以及sublime_text_3安装
笔记本自大一入手,只重装过一次系统,从不曾拆机清灰过.读研之后,日常工作与学习都在实验室进行,笔记本一直在宿舍的桌子上落灰,只偶尔打开来看个电影.上周末,心血来潮,把笔记本抱到实验室拆了清灰,以前一直 ...
- PySe-004-Se-WebDriver 启动浏览器之二 - Chrome
上篇文章简略讲述了 WebDriver 启动 firefox 浏览器的示例脚本源码,具体请参阅: PySe-003-Se-WebDriver 启动浏览器之一 - Firefox 此文主要讲述在 Mac ...