模式管理
更新時間 2025-02-14 10:22:24
最近更新時間: 2025-02-14 10:22:24
分享文章
本頁主要介紹天翼云TeleDB數據庫模式管理。
模式本質上是一個名字空間,Oracle里一般叫用戶,SQL Server中叫框架,MySQL 中叫數據庫,模式里面包含表、數據類型、函數以及操作符,對象名稱可以與在其他模式中存在的對象重名,訪問某個模式中的對象時可以使用模式名.對象名。
創建模式
標準語法
teledb=# create schema teledb_schema; CREATE SCHEMA擴展語法,不存在時才創建
teledb=# create schema if not exists teledb_schema; NOTICE: schema "teledb_schema" already exists, skipping CREATE SCHEMA指定所屬用戶
teledb=# create schema teledb_schema_owner AUTHORIZATION teledb_user; CREATE SCHEMA teledb=# \dn teledb_schema_owner List of schemas Name | Owner ----------------------+-------------- teledb_schema_owner | teledb_user (1 row)
修改模式屬性
修改模式名
teledb=# alter schema teledb_schema rename to teledb_schema_new; ALTER SCHEMA修改所有者
teledb=# \dn teledb_schema_new List of schemas Name | Owner --------------------+--------- teledb_schema_new | teledb (1 row) teledb=# alter schema teledb_schema_new owner to teledb_user; ALTER SCHEMA teledb=# \dn teledb_schema_new List of schemas Name | Owner --------------------+-------------- teledb_schema_new | teledb_user (1 row)
刪除模式
teledb=# drop schema teledb_schema_owner;
DROP SCHEMA當模式中存在對象時,則會刪除失敗,提示如下。
teledb=# create table teledb_schema_new.test(id int);
CREATE TABLE
teledb=# drop schema teledb_schema_new;
ERROR: cannot drop schema teledb_schema_new because other objects depend on it
DETAIL: table teledb_schema_new.test depends on schema teledb_schema_new
HINT: Use DROP ... CASCADE to drop the dependent objects too.參考如下強制刪除,會將模式中的對象也級聯刪除掉。
teledb=# drop schema teledb_schema_new cascade;
NOTICE: drop cascades to table teledb_schema_new.test
DROP SCHEMA配置用戶訪問模式權限
普通用戶對于某個模式下的對象訪問除了訪問對象要授權外,模式也需要授權。
[teledb@localhost bin]$ ./telesql -p 11111 -U teledb -d teledb
Password for user teledb:
telesql (TeleDB V6)
Type "help" for help.
-- 創建shema
teledb=# create schema test;
CREATE SCHEMA
-- 創建表
teledb=# create table test.t1(id int);
CREATE TABLE
-- 創建用戶user1
teledb=# create role user1 with login password 'User@184';
CREATE ROLE
-- 授權表訪問權限給用戶user1
teledb=# grant select on test.t1 to user1;
GRANT切換到普通用戶user1,嘗試訪問test.t1表,由于沒有授權test模式,仍然無法訪問。
teledb=# \c - user1
Password for user user1:
You are now connected to database "teledb" as user "user1".
teledb=> select * from test.t1;
ERROR: permission denied for schema test
LINE 1: select * from test.t1;
^
切換到teledb用戶,將test模式的訪問權限授權給user1,重新用user1訪問t1表
teledb=> \c - teledb
Password for user teledb:
You are now connected to database "teledb" as user "teledb".
-- 授權schema
teledb=# grant usage on schema test to user1;
GRANT
teledb=# \c - user1
Password for user user1:
You are now connected to database "teledb" as user "user1".
teledb=> select * from test.t1;
id
----
(0 rows)配置訪問模式的順序
TeleDB有一個運行變量叫search_path,其值為模式名列表,用于配置訪問數據對象的順序,如下所示。
當前連接用戶。
teledb=# select current_user;
current_user
--------------
teledb
(1 row)顯示當前search_path,搜索路徑只配置為$user, public,其中 $user為當前用戶名,即上面的current_user 值teledb。
teledb=# show search_path;
search_path
-----------------
"$user", public
(1 row)不指定模式創建數據表,則該表存放于第一個搜索模式下面。第一個模式找不到的情況下選擇第二個,依次順序查找。
teledb=# create table t2(id int, content text);
CREATE TABLE
teledb=# \dt t2
List of relations
Schema | Name | Type | Owner
--------+------+-------+---------
public | t2 | table | teledb
(1 row)
--由于沒有teledb模式,所以表默認放到了public模式下
-- 創建teledb模式后再次建表,表建到了teledb模式下
teledb=# create schema teledb;
CREATE SCHEMA
teledb=# create table t3(id int, content text);
CREATE TABLE
teledb=# \dt t3
List of relations
Schema | Name | Type | Owner
---------+------+-------+---------
teledb | t3 | table | teledb
(1 row)指定表位于某個模式下,不同模式下表名可以相同。在public模式下創建t3表
teledb=# create table public.t3(id int, content text);
CREATE TABLE
teledb=# \dt public.t3
List of relations
Schema | Name | Type | Owner
--------+------+-------+---------
public | t3 | table | teledb
(1 row)同名的表查詢時,沒有指定shema時,始終只會查到前面模式的表數據。
-- 插入數據到public.t3表
teledb=# insert into public.t3 values(1,'test');
INSERT 0 1
-- 由于teledb模式在第一順位,且存在同名表,所以始終是teledb模式下的空表
teledb=# select * from t3;
id | content
----+---------
(0 rows)
-- 指定public模式去查詢可以查到插入的數據
teledb=# select * from public.t3;
id | content
----+---------
1 | test
(1 row)訪問不在搜索路徑的對象時,需要寫全路徑。
teledb=# create table test1.t4 (id int, name char);
CREATE TABLE
teledb=# select * from t4;
ERROR: relation "t4" does not exist
LINE 1: select * from t4;
^
teledb=# select * from test1.t4;
id | name
----+------
(0 rows)上面出錯是因為模式test1沒有配置在search_path搜索路徑中。