IMAGE类型
功能描述
Vastbase G100在SQL Server兼容模式下支持image数据类型,表示长度可变的二进制数据,大小从 0 到 231-1 (即2147483647)个字节。
注意事项
该特性仅在数据库兼容模式为SQL Server时支持(即数据库实例初始化时指定DBCOMPATIBILITY='MSSQL')。
示例
示例1: image数据类型结合DDL语言使用。
1、创建一个存在字段类型为image的表tb_1,并查看表的详细结构。
CREATE TABLE tb_1
(
id INT PRIMARY KEY,
image_col IMAGE
);
\d+ tb_1;
查询结果显示为:
Table "public.tb_1"
Column | Type | Modifiers | Storage | Stats target | Description
-----------+---------+-----------+----------+--------------+-------------
id | integer | not null | plain | |
image_col | "image" | | extended | |
Indexes:
"tb_1_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
Has OIDs: no
Options: orientation=row, compression=no, fillfactor=80
2、删除表tb_1的image类型的字段,并查看表的详细结构。
ALTER TABLE tb_1 DROP COLUMN image_col;
\d+ tb_1;
查询结果显示为如下,即成功删除了表中image类型的字段:
Table "public.tb_1"
Column | Type | Modifiers | Storage | Stats target | Description
--------+---------+-----------+---------+--------------+-------------
id | integer | not null | plain | |
Indexes:
"tb_1_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
Has OIDs: no
Options: orientation=row, compression=no, fillfactor=80
3、向表tb_1中增加image类型的列,并查看表的详细结构。
ALTER TABLE tb_1 ADD image_col IMAGE;
\d+ tb_1;
查询结果显示为如下,即成功向表中新增了image类型的字段:
Table "public.tb_1"
Column | Type | Modifiers | Storage | Stats target | Description
-----------+---------+-----------+----------+--------------+-------------
id | integer | not null | plain | |
image_col | "image" | | extended | |
Indexes:
"tb_1_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
Has OIDs: no
Options: orientation=row, compression=no, fillfactor=80
4、向表tb_1中增加image类型的列增加注释,并查看表的详细结构。
COMMENT ON COLUMN tb_1.image_col IS 'image column';
\d+ tb_1;
查询结果显示为如下,即成功对表中image类型的字段增加了注释:
Table "public.tb_1"
Column | Type | Modifiers | Storage | Stats target | Description
-----------+---------+-----------+----------+--------------+--------------
id | integer | not null | plain | |
image_col | "image" | | extended | | image column
Indexes:
"tb_1_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
Has OIDs: no
Options: orientation=row, compression=no, fillfactor=80
5、重命名表tb_1的image类型的字段名为img_col。
ALTER TABLE tb_1 RENAME COLUMN image_col TO img_col;
\d+ tb_1;
查询结果显示为如下,即成功对表中image类型的字段重命名:
Table "public.tb_1"
Column | Type | Modifiers | Storage | Stats target | Description
---------+---------+-----------+----------+--------------+--------------
id | integer | not null | plain | |
img_col | "image" | | extended | | image column
Indexes:
"tb_1_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
Has OIDs: no
Options: orientation=row, compression=no, fillfactor=80
6、删除表tb_1。
DROP TABLE tb_1;
select * from tb_1;
查询结果显示为如下,表已不存在,则表中image类型的列数据已被清除。
ERROR: relation "tb_1" does not exist on node1
示例2: image数据类型与其他类型的转化。
1、其他类型转化为image类型。
SELECT CAST(N'0x43564545' AS IMAGE);
SELECT N'0x43564545'::IMAGE;
SELECT CAST('2020-12-22 13:00:00'::DATETIME AS IMAGE);
上述前两条命令能成功转化类型,第三句不能转化。转化结果展示为:
image
------------------------
0x30783433353634353435
(1 row)
image
------------------------
0x30783433353634353435
(1 row)
ERROR: cannot cast type datetime to "image"
LINE 1: SELECT CAST('2020-12-22 13:00:00'::DATETIME AS IMAGE);
^
CONTEXT: referenced column: image
2、image类型转化为其他类型。
SELECT CAST('0x41560120'::image AS VARCHAR);
SELECT '0x43564545'::image::VARBINARY(50);
SELECT CAST('0x43564545'::image AS DECIMAL(10,2));
上述前两条命令能成功转化类型,第三句不能转化。转化结果展示为:
varchar
---------
AV\x01
(1 row)
varbinary
------------
0x43564545
(1 row)
ERROR: invalid length in external "numeric" value
CONTEXT: referenced column: numeric