PostgreSQL Type的创建与Type在函数中的使用
postgres=# create type complex as(
postgres(# r double precision,
postgres(# i double precision
postgres(# );
CREATE TYPE
postgres=# create type inventory_item as(
postgres(# name text,
postgres(# supplier_id integer,
postgres(# price numeric);
CREATE TYPE
postgres=# create table on_hand(
postgres(# item inventory_item,
postgres(# count integer
postgres(# );
CREATE TABLE
postgres=# insert into on_hand values (ROW('fuzzy dice',42,1.99),1000);
INSERT 0 1
postgres=# create function price_extension(inventory_item,integer) returns numeric
postgres-# as 'sel
postgres-# as 'select $1.price * $2' language sql;
CREATE FUNCTION
postgres=# select price_extension(item,10) from on_hand;
price_extension
-----------------
19.90
(1 row)
postgres=# insert into on_hand values (('Apple',22,4.4),2000);
INSERT 0 1
postgres=# select * from on_hand ;
item | count
------------------------+-------
("fuzzy dice",42,1.99) | 1000
(Apple,22,4.4) | 2000
(2 rows)
postgres=# insert into on_hand values (row('Apple',22,4.4),2000);
INSERT 0 1
postgres=# select * from on_hand ;
item | count
------------------------+-------
("fuzzy dice",42,1.99) | 1000
(Apple,22,4.4) | 2000
(Apple,22,4.4) | 2000
(3 rows)
postgres=# insert into on_hand values (ROW('Apple',22,4.4),2000);
INSERT 0 1
postgres=# select * from on_hand ;
item | count
------------------------+-------
("fuzzy dice",42,1.99) | 1000
(Apple,22,4.4) | 2000
(Apple,22,4.4) | 2000
(Apple,22,4.4) | 2000
(4 rows)
postgres=# insert into on_hand values (ROW('Orange',22,55),3000);
INSERT 0 1
postgres=# insert into on_hand values (ROW('Orange1',22,66),3000);
INSERT 0 1
postgres=# \d on_hand
Table "public.on_hand"
Column | Type | Modifiers
--------+----------------+-----------
item | inventory_item |
count | integer |
postgres=# select item from on_hand ;
item
------------------------
("fuzzy dice",42,1.99)
(Apple,22,4.4)
(Apple,22,4.4)
(Apple,22,4.4)
(Orange,22,55)
(Orange1,22,66)
(6 rows)
postgres=# select (item).name from on_hand where (item).price >10;
name
---------
Orange
Orange1
(2 rows)
postgres=# select (item).name from on_hand ;
name
------------
fuzzy dice
Apple
Apple
Apple
Orange
Orange1
(6 rows)
postgres=# select (on_hand.item).name from on_hand where (on_hand.item).price > 10;
name
---------
Orange
Orange1
(2 rows)
select just one field from the result of a function that returns a composite value,you'd need to
write something like:
select (my_func(...)).field from table_name;
postgres=# create table complex_col (col complex);
CREATE TABLE
postgres=# insert into complex_col values ((1.1,2.2));
INSERT 0 1
postgres=# insert into complex_col (col.r,col.i) values (8.8,9.9);
INSERT 0 1
postgres=# select * from complex_col ;
col
-----------
(1.1,2.2)
(8.8,9.9)
(2 rows)
PostgreSQL Type的创建与Type在函数中的使用的更多相关文章
- 在PostgreSQL自定义一个“优雅”的type
是的,又是我,不要脸的又来混经验了.我们知道PostgreSQL是一个高度可扩展的数据库,这次我聊聊如何在PostgreSQL里创建一个优雅的type,如何理解优雅?大概就是不仅仅是type本身,其它 ...
- [Python]利用type()动态创建类
Python作为动态语言,可以动态地创建函数和类定义.比如说定义一个Hello类,就写一个hello.py模块: #! /usr/bin/env python #coding=utf-8 class ...
- Python 对象(type/object/class) 作用域 一等函数 (慕课--Python高级,IO并发 第二章)
在python中一共有两种作用域:全局作用域和函数作用域全局作用域:在全局都有效,全局作用域在程序执行时创建,在程序执行结束时销毁:所有函数以外的区域都是全局作用域:在全局作用域中定义的变量,都属于全 ...
- 转载 SharePoint【Site Definition 系列】– 创建Content Type
转载原地址: http://www.cnblogs.com/wsdj-ITtech/archive/2012/09/01/2470274.html Sharepoint本身就是一个丰富的大容器,里面 ...
- Python 之 type方法创建类
type()方法作为元类,用来创建类: type(类名, 父类的元组(针对继承的情况,可以为空),包含属性的字典(名称和值)) 以下代码可以用type()方法来创建: class Myclass(ob ...
- 使用python type动态创建类
使用python type动态创建类 X = type('X', (object,), dict(a=1)) # 产生一个新的类型 X 和下列方法class X(object): a = 1效 ...
- type动态创建类
在一些特定场合,需要动态创建类,比如创建表单,就会用到type动态创建类,举个例子: class Person(object): def __init__(self,name,age): self.n ...
- Django - Form嵌套的Meta类 + 为什么type()能创建类
Form里面嵌套了一个Meta类 class PostForm(forms.ModelForm): class Meta: model = Post # field to be exposed fie ...
- Host does not support domain type kvm for virtualization type 'hvm' arch 'x86_64'
kvm创建虚拟机报错: qemu-img create -f qcow2 /tmp/centos.qcow2 10G virt-install --virt-type kvm --name cento ...
随机推荐
- MyCAT安装指南
MyCAT安装指南 MyCAT 1.2版本 快速上手-安装指南(安装单机) Mycat的server和mysql位于同一台服务器,centos6.2.4环境 Mycat:10.191.116.175 ...
- 拼写纠正 Artificial Intelligence: A Modern Approach
Artificial Intelligence: A Modern Approach http://mindhacks.cn/2008/09/21/the-magical-bayesian-metho ...
- CC2540的使用入门
目录 1. 介绍 2. 开发环境 3. SDCC 1. 介绍 CC2540是一款2.4GHz Bluetooth® low energy SOC,基于8051 MCU 首先,你需要硬件设备 笔者的开发 ...
- 备份mysql
#!/bin/bash # 要备份的数据库名,多个数据库用空格分开USER=rootPASSWORD=rootdatabases=("shopnc") # 备份文件要保存的目录ba ...
- VS2013编译Qt5.6.0静态库(乌合之众)
获取qt5.6.0源码包 直接去www.qt.io下载就好了,这里就不详细说了. 这里是我已经编译好的** 链接:http://pan.baidu.com/s/1pLb6wVT 密码: ak7y ** ...
- 基于vs2005以上版本Qt程序发布的注意事项(讲了manifest的问题)
最近发现了一个非常恼人的程序deployment的问题,估计大家有可能也会遇到,特此memo. 问题的出现我觉得主要还是微软搞的花头太多, 一个不知所谓的manifest文件让本来简单的程序发布变得困 ...
- Node的Buffer
var buf3 = new Buffer([1,2,3,4,-10,256],'utf8');//默认为utf8 console.log(buf3[0]);//正常的范围是0~255 console ...
- 使用C#发送正文带图片邮件
最近有个地方用到正文带图片的邮件发送功能,由于发送邮件调用的是web service,要求正文必须是string,而接收方要能看到图片,还不能单纯的添加一个图片地址链接,查阅了很多资料,基本上都是从头 ...
- 在express项目中有效组织和使用mongoose
平凡之路 1.创建express项目 express mongooseExpress 2.最简express var express = require("express"); v ...
- C#程序读取数据库中包含null的列的值
private void btn2_Click(object sender, RoutedEventArgs e) { using (SqlConnection ...