This topic describes how to create and drop synonyms.
In OceanBase Database, a synonym is an alias for a database object in a tenant. Synonyms facilitate privilege management. You can use synonyms to mask the owner privilege of data objects.
Create a synonym
You can use the CREATE SYNONYM statement to create a synonym.
When you create a synonym, note that:
To create a private synonym for the current user, you must have the
CREATE SYNONYMprivilege.To create a private synonym for other users, you must have the
CREATE ANY SYNONYMprivilege.To create a public synonym, you must have the
CREATE PUBLIC SYNONYMprivilege.You can create a synonym for a non-existent object. You do not need to be authorized to access the object for which you want to create a synonym.
For information about how to query user privileges, see Query user privileges. For information about how to grant user privileges, see Modify user privileges.
To create a synonym, use the following syntax:
CREATE [ OR REPLACE ] [ PUBLIC ] SYNONYM [ schema. ]synonym FOR [ schema. ]object;
For more information about the CREATE SYNONYM statement, see SQL reference (Oracle mode).
Here are some examples:
Create a private synonym.
obclient> CREATE TABLE test(c1 int); Query OK, 0 rows affected (0.18 sec) obclient> CREATE SYNONYM s1 for test; Query OK, 0 rows affected (0.05 sec) obclient> INSERT INTO s1 VALUES(1); Query OK, 1 row affected (0.02 sec) obclient> SELECT * FROM s1; +------+ | c1 | +------+ | 1 | +------+ 1 row in set (0.01 sec)Create a public synonym.
obclient> CREATE PUBLIC SYNONYM syn_pub FOR test; Query OK, 0 rows affected (0.03 sec) obclient> SELECT * FROM syn_pub; +------+ | c1 | +------+ | 1 | +------+ 1 row in set (0.01 sec)
Drop a synonym
You can use the DROP SYNONYM statement to drop a synonym that you no longer need.
When you drop a synonym, note that:
To drop a private synonym, make sure that this private synonym is under the corresponding database or schema, and that you have the
DROP ANY SYNONYMprivilege.To drop a public synonym, you must have the
DROP PUBLIC SYNONYMprivilege.In addition, you must specify the
PUBLICkeyword, and cannot specify the database or schema in the synonym dropping statement.
For information about how to query user privileges, see Query user privileges. For information about how to grant user privileges, see Modify user privileges.
The syntax for dropping a synonym is as follows:
DROP [PUBLIC] SYNONYM [ schema. ]synonym;
For more information about the DROP SYNONYM statement, see OceanBase Database SQL Reference (Oracle Mode).
Here are some examples:
Drop a private synonym.
obclient> DROP SYNONYM test.s1; Query OK, 0 rows affected (0.03 sec)Drop a public synonym.
obclient> DROP PUBLIC SYNONYM syn_pub; Query OK, 0 rows affected (0.02 sec)