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在函数中的使用的更多相关文章

  1. 在PostgreSQL自定义一个“优雅”的type

    是的,又是我,不要脸的又来混经验了.我们知道PostgreSQL是一个高度可扩展的数据库,这次我聊聊如何在PostgreSQL里创建一个优雅的type,如何理解优雅?大概就是不仅仅是type本身,其它 ...

  2. [Python]利用type()动态创建类

    Python作为动态语言,可以动态地创建函数和类定义.比如说定义一个Hello类,就写一个hello.py模块: #! /usr/bin/env python #coding=utf-8 class ...

  3. Python 对象(type/object/class) 作用域 一等函数 (慕课--Python高级,IO并发 第二章)

    在python中一共有两种作用域:全局作用域和函数作用域全局作用域:在全局都有效,全局作用域在程序执行时创建,在程序执行结束时销毁:所有函数以外的区域都是全局作用域:在全局作用域中定义的变量,都属于全 ...

  4. 转载 SharePoint【Site Definition 系列】– 创建Content Type

    转载原地址:  http://www.cnblogs.com/wsdj-ITtech/archive/2012/09/01/2470274.html Sharepoint本身就是一个丰富的大容器,里面 ...

  5. Python 之 type方法创建类

    type()方法作为元类,用来创建类: type(类名, 父类的元组(针对继承的情况,可以为空),包含属性的字典(名称和值)) 以下代码可以用type()方法来创建: class Myclass(ob ...

  6. 使用python type动态创建类

    使用python type动态创建类 X = type('X', (object,), dict(a=1))  # 产生一个新的类型 X 和下列方法class X(object):    a = 1效 ...

  7. type动态创建类

    在一些特定场合,需要动态创建类,比如创建表单,就会用到type动态创建类,举个例子: class Person(object): def __init__(self,name,age): self.n ...

  8. Django - Form嵌套的Meta类 + 为什么type()能创建类

    Form里面嵌套了一个Meta类 class PostForm(forms.ModelForm): class Meta: model = Post # field to be exposed fie ...

  9. 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 ...

随机推荐

  1. BCP 命令

    bcp 实用工具 我们可以通过如下BCP命令(注意不能有回车)将其导出为XML文件,并保存: BCP "SELECT TOP 30 [bom_no],[LEVEL] FROM [sqladm ...

  2. LR中HTTP协议录制模式选择

    在LR中使用HTML/HTTP协议进行脚本录制时面临正确选择HTTP-based script / URL-base script 录制模式的问题,以下是比较官方的建议:1)基于浏览器的应用程序推荐使 ...

  3. Windows注册表(持续更新)

    HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Zoom 下, 设置DWORD 值 ZoomDisabled  等于 1.

  4. java获取类路径

    String file = MessageTask3.class.getResource("").getFile(); File: public static final Stri ...

  5. c#循环迭代匿名类链表(可迭代的匿名类)

    Main(){ //为什么?object是基类啊!! //报错.不能从List<anonymous>换成List<object>. //var q=(List<objec ...

  6. docker kubernetes--

    http://kubernetes.io/docs/getting-started-guides/docker/ https://hub.docker.com/r/solsson/hyperkube- ...

  7. Chart控件,把Y轴设置成百分比

    这次所有属性设置都用代码(就当整理便于以后查询). 在窗体放置一个Chart控件,未做任何设置:然后编写代码: //设置 chart2.Legends[ ].Enabled = false;//不显示 ...

  8. MYSQL 中常用日期时间函数使用

    MySQL Date 函数 下面的表格列出了 MySQL 中最重要的内建日期函数: 函数 描述 NOW() 返回当前的日期和时间 CURDATE() 返回当前的日期 CURTIME() 返回当前的时间 ...

  9. Magento修改邮件模板内容

    Magento 默认邮件模板 都是带着官方的标志和一些官方的基本信息.为了建立品牌形象我们需要把邮件模板中的所有官方信息换成自己的信息.修改步骤如下: 1.找到Magento的邮件模板文件(这里以 e ...

  10. Spark Programming--Actions II

    saveAsTextFile saveAsTextFile(path, compressionCodecClass=None) aveAsTextFile用于将RDD以文本文件的格式存储到文件系统中, ...