数据仓库服务 GAUSSDB(DWS)-CREATE SYNONYM:示例

时间:2024-12-06 15:12:41

示例

创建模式ot:

1
CREATE SCHEMA ot;

创建表ot.t1及其同义词t1:

1
2
CREATE TABLE ot.t1(id int, name varchar2(10)) DISTRIBUTE BY hash(id);
CREATE OR REPLACE SYNONYM t1 FOR ot.t1;

使用同义词t1:

1
2
3
SELECT * FROM t1;
INSERT INTO t1 VALUES (1, 'ada'), (2, 'bob');
UPDATE t1 SET t1.name = 'cici' WHERE t1.id = 2;

创建同义词v1及其关联视图ot.v_t1:

1
2
CREATE SYNONYM v1 FOR ot.v_t1;
CREATE VIEW ot.v_t1 AS SELECT * FROM ot.t1;

使用同义词v1:

1
SELECT * FROM v1;

创建重载函数ot.add及其同义词add:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
CREATE OR REPLACE FUNCTION ot.add(a integer, b integer) RETURNS integer AS
$$
SELECT $1 + $2
$$
LANGUAGE sql;

CREATE OR REPLACE FUNCTION ot.add(a decimal(5,2), b decimal(5,2)) RETURNS decimal(5,2) AS
$$
SELECT $1 + $2
$$
LANGUAGE sql;

CREATE OR REPLACE SYNONYM add FOR ot.add;

使用同义词add:

1
2
SELECT add(1,2);
SELECT add(1.2,2.3);

创建存储过程ot.register及其同义词register:

1
2
3
4
5
6
7
8
9
CREATE PROCEDURE ot.register(n_id integer, n_name varchar2(10))
SECURITY INVOKER
AS
BEGIN
    INSERT INTO ot.t1 VALUES(n_id, n_name);
END;
/

CREATE OR REPLACE SYNONYM register FOR ot.register;

使用同义词register,调用存储过程:

1
CALL register(3,'mia');
support.huaweicloud.com/sqlreference-910-dws/dws_06_0176.html