环境系统平台:microsoft windows (64-bit) 10版本:4.5

瀚高数据库中支持使用以下语句创建用户定义的数据类型:

  • ​create domain​:它创建了一个用户定义的数据类型,可以有可选的约束,基于其他基本类型,实质是定义一个域。
  • ​create type​:它通常用于使用存储过程创建复合类型(两种或多种数据类型混合的数据类型)。

一、domain用法及示例

假如有以下表结构:

create table test_domain (id varchar,md5 text not null check(length(md5)=32));

其中md5列的类型及约束,可以定义一个domain来抽象,如下:

highgo=# create domain md5 as

highgo-# text not null

highgo-# check (

highgo(#     length(value) = 32

highgo(# );

create domain

highgo=# 

highgo=# \dd md5

                                  list of domains

 schema | name | type | collation | nullable | default |           check            

--------+------+------+-----------+----------+---------+----------------------------

 public | md5  | text |           | not null |         | check (length(value) = 32)

(1 row)

highgo=# create table test_domain (id varchar,md5 md5);

create table

highgo=# insert into test_domain values('1','2');

error:  value for domain md5 violates check constraint "md5_check"

highgo=# insert into test_domain values('2','76a2173be6393254e72ffa4d6df1030a');

insert 0 1

二、创建mysql中datetime类型

highgo=# create domain datetime as timestamp without time zone;

highgo=# create table t_time (id int,create_time datetime);

create table

highgo=# \d+ t_time

                                     table "public.t_time"

   column    |   type   | collation | nullable | default | storage | stats target | description 

-------------+----------+-----------+----------+---------+---------+--------------+-------------

 id          | integer  |           |          |         | plain   |              | 

 create_time | datetime |           |          |         | plain   |              | 

access method: heap

highgo=# insert into t_time values (1,now()),(2,now());

insert 0 2

highgo=# 

highgo=# select * from t_time;

 id |        create_time         

----+----------------------------

  1 | 2021-08-03 19:28:11.207324

  2 | 2021-08-03 19:28:11.207324

(2 rows)

三、create type用法及示例

create type name as

    ( [ attribute_name data_type [ collate collation ] [, ... ] ] )

create type name as enum

    ( [ 'label' [, ... ] ] )

create type name as range (

    subtype = subtype

    [ , subtype_opclass = subtype_operator_class ]

    [ , collation = collation ]

    [ , canonical = canonical_function ]

    [ , subtype_diff = subtype_diff_function ]

)

create type name (

    input = input_function,

    output = output_function

    [ , receive = receive_function ]

    [ , send = send_function ]

    [ , typmod_in = type_modifier_input_function ]

    [ , typmod_out = type_modifier_output_function ]

    [ , analyze = analyze_function ]

    [ , internallength = { internallength | variable } ]

    [ , passedbyvalue ]

    [ , alignment = alignment ]

    [ , storage = storage ]

    [ , like = like_type ]

    [ , category = category ]

    [ , preferred = preferred ]

    [ , default = default ]

    [ , element = element ]

    [ , delimiter = delimiter ]

    [ , collatable = collatable ]

)

create type name

创建示例:

create type compfoo as (f1 int, f2 text);

create function getfoo() returns setof compfoo as $$

    select fooid, fooname from foo

$$ language sql;

create type bug_status as enum ('new', 'open', 'closed');

create table bug (

    id serial,

    description text,

    status bug_status

);

create type float8_range as range (subtype = float8, subtype_diff = float8mi);

到此这篇关于如何创建一个创建mysql数据库中的datetime类型的文章就介绍到这了,更多相关创建datetime类型内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!