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 ...
随机推荐
- BAT批处理(二)
在前一篇中已对BAT批处理基础作了一些总结,但是对于BAT批处理还有很多的知识点没有讲解到,比如DOS中的特殊符号:IF.FOR的使用:变量:更多的DOS命令等等.本文在前一篇的基础上继续对BAT批处 ...
- 20145319 《java程序设计》课程总结
20145319 <Java程序设计>课程总结 读书笔记链接总结 - 20145319 第一周学习总结 - 20145319 第二周学习总结 - 20145319 第三周学习总结 - 20 ...
- PHP--浏览器禁用cookie后,怎么使用session
sessionid是存储在cookie中的,解决方案如下: Session URL重写,保证在客户端禁用或不支持COOKIE时,仍然可以使用Session session机制.session机制是一种 ...
- 关于Bitcode的探索
Bitcode概述 Bitcode is an intermediate representation of a compiled program. Apps you upload t ...
- 1011 最大公约数GCD
1011 最大公约数GCD 基准时间限制:1 秒 空间限制:131072 KB 输入2个正整数A,B,求A与B的最大公约数. Input 2个数A,B,中间用空格隔开.(1<= A,B < ...
- 面向对象世界里转转七(Liskov替换原则)
前言:Liskov替换原则是关于继承机制的应用原则,是实现开放封闭原则的具体规范,违反了Liskov原则必然意味着违反了开放封闭原则.因此,有必要对面向对象的继承机制及其基本原则做以探索,来进一步了解 ...
- sqlserver总结-视图及存储过程
视图中不能声明变量,不能调用存储过程,如果写比较复杂的查询,需要应用存储过程 视图也可以和函数结合 存储过程通过select或其他语句返回结果集 除此之外,存储过程返回结果只有两种方式 1 retur ...
- jQuery基本选择器
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- SQL Server 视图修改后有错怎么办?
sp_refreshview 视图名:刷新指定视图 spsqlrefreshallviews:刷新全部视图
- imx6 android 进入文件系统闪屏
imx6进入文件系统的时候都会闪屏,应该是framebuffer未初始化,就已经打开了背光.目前解决办法,在kenel阶段关闭背光,显示android的开机动画之后(此时framebuffer已经初始 ...