怎麼建立一個建立MySQL資料庫中的datetime類型

WBOY
發布: 2023-06-02 16:55:04
轉載
1783 人瀏覽過

環境系統平台: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類型的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!