当前位置:网站首页>数据库-同义词
数据库-同义词
2022-06-29 06:37:00 【EbowTang】
基本概念:
同义词是数据库模式对象的⼀个别名,经常⽤于简化对象访问和提⾼对象访问的安全性。
在使⽤同义词时,数据库将它翻译成对应模式对象的名字。
与视图类似,同义词并不占⽤实际存储空间,只有在数据字典中保存了同义词的定义。在数据库中的⼤部分数据库对象,如表、视图、同义词、序列、存储过程等,数据库管理员都可以根据实际情况为他们定义同义词。隐藏对象名称和所有者。
思维导图总结:

测试代码:(KES上实测)
-----------------------------
---------------同义词
-----------------------------
--同义词查询(oracle才有)
select * from user_sysnonyms;
--1,创建student表的同义词
CREATE PUBLIC SYNONYM stu FOR student;
--2,创建以及替换的方式
create OR REPLACE public SYNONYM stu FOR student;
drop synonym public.stu;
drop synonym public.stu1;
--3,创建自定义的模式schm,验证该私有模式下创建共有表的同义词
CREATE SCHEMA schm;
CREATE SYNONYM schm.syn_tab FOR public.tab; --tab不存在
SELECT * FROM schm.syn_tab; --报错ERROR: relation "SCHM.SYN_TAB" does not exist
--SYS_SYNONYM视图中查询同义词信息
SELECT * FROM SYS_SYNONYM WHERE synname = 'SYN_TAB'; --同义词定义信息
--4,私有同义词和共有同义词可以同名
--创建tb表
CREATE TABLE public.tb(id int);
INSERT INTO TB VALUES(1);
select * from tb;
--tabl是不存在的,但是我依然能为其进行创建
--同时私有同义词和共有同义词是可以同名的
CREATE PUBLIC SYNONYM syn_tabl FOR tabl;
CREATE SYNONYM schm.syn_tabl FOR tabl;
--5,同义词的视图查询
--从视图中查询信息
SELECT syn.synname, sp.nspname
FROM SYS_SYNONYM as syn, SYS_NAMESPACE AS sp
WHERE syn.synnamespace = sp.oid;
--6,在私有schm模式中为public模式下的表创建同义词
CREATE SYNONYM schm.syn_tb FOR public.tb; --已经创建了共有tb表,但是这个同义词是私有的
SELECT *
FROM SYS_SYNONYM
WHERE synname = 'syn_tb'; ---syn_t和SYS_TB不一样,变量里是要做区分大小写的!!!
--撒也查不出来啊,表也有,同义词也有
SELECT status
FROM ALL_OBJECTS
WHERE object_name = 'syn_tab';
SELECT * FROM schm.syn_tb; --查询同义词信息(只有一个编号为1)
--删除关联的表后,同义词变更为无效状态,重新创建回来。则有效
DROP TABLE public.tb;
SELECT status
FROM ALL_OBJECTS
WHERE object_name = 'syn_tab';
--删除tb表后,信息也无法再查询到,同义词状态无效(报错关系不存在)
SELECT * FROM schm.syn_tb;
---一些创建上的限制
---a,创建公有同义词不可指定模式,(将报错)
CREATE PUBLIC SYNONYM schm.syn_tt FOR public.tab;
ERROR: PUBLIC can not be used when schema name specified for synonym name
LINE 1: CREATE PUBLIC SYNONYM schm.syn_tt FOR public.tab;
---b,创建私有的,必须指定模式(下列示例未指定同义词名字,将报错)
CREATE SYNONYM sys FOR public.tab;
set SEARCH_PATH to schm,public;
---c,创建私有同义词不可指定为PUBLIC。
--错误原因,这是私有同义词,但是我们却指定到了共有模式下
CREATE SYNONYM public.syn_tab1 FOR public.tab1;
create table schm.company(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
--由于前面执行了这个set SEARCH_PATH to schm,public;
--默认模式路径已变更
show search_path;
--下表直接创建在schm中
create table company1(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
--大清理
drop synonym schm.syn_tb;
drop synonym schm.syn_tab;
drop synonym public.syn_tabl;
--创建表的一个的简单视图,为同义词准备
create force view age_view as
select stu_nmb,stu_name
from student
where age > 20 and gender = '男'
with local check option ;--可选,限定DML语句操作必须满足一定条件
CREATE PUBLIC SYNONYM stu1 FOR age_view;
select * from stu1;
-----------------------------------------------------------
-------------------------------创建实验表-------------------
-----------------------------------------------------------
drop table if exists student; --oracle没有这个语法,mysql和KES存在
CREATE TABLE student (
stu_nmb number(8),
stu_name char(8) not null,
gender varchar2(2),
age number(2), --检查约束
class varchar2 (40) not null,
email varchar2 (30),
sdate DATE,
constraint con_nmb primary key (stu_nmb),
constraint con_check check ( gender in ('男','女')),
constraint ch_age CHECK (age BETWEEN 18 AND 30), --检查约束
constraint uk_student_email UNIQUE (email)
);
--首先插入,正确
INSERT INTO student VALUES(2314,'张德田','男',19,'高三第6班','[email protected]',SYSDATE);
INSERT INTO student VALUES(4324,'吴海峰','男',18,'高三第1班','[email protected]',SYSDATE);
INSERT INTO student VALUES(2614,'章德正','男',20,'高三第8班','[email protected]',SYSDATE);
INSERT INTO student VALUES(2184,'宋义','女',20,'高三第3班','[email protected]',SYSDATE);
INSERT INTO student VALUES(9874,'张华乐','女',19,'高三第4班','[email protected]',SYSDATE);
INSERT INTO student VALUES(1474,'黎文','女',19,'高三第4班','[email protected]',SYSDATE);
INSERT INTO student VALUES(6574,'吉祥','男',21,'高三第7班','[email protected]',SYSDATE);
INSERT INTO student VALUES(8174,'向玲','女',19,'高三第1班','[email protected]',SYSDATE);
INSERT INTO student VALUES(1414,'梅田田','女',21,'高三第7班','[email protected]',SYSDATE);
select * from student;
--测试同义词
select * from stu;
边栏推荐
- Antlr4 recognizes the format of escape string containing quotation marks
- Li Kou today's question -324 Swing sort II
- VerilogA - counter
- Difference between URI and URL
- AIRNET notes 1
- 'only_ full_ group_ The influence of by'sql mode on group by and its treatment
- Go basic data type conversion
- Fault: NetBt log for id4321
- [MySQL technology topic] technical analysis and guide for analyzing the high availability architecture of MySQL
- Mongodb paging method
猜你喜欢

百度小程序自动提交搜索

Servlet version conflict causes page 404

What are the uses of wireless pressure collectors?

National Defense University project summary

Illustrate plug-in -- AI plug-in development -- creative plug-in -- astute graphics -- multi axis mirroring function

How to change the password after forgetting the MySQL password (the latest version of 2022 detailed tutorial nanny level)

Exclusive download. Alibaba cloud native brings 10+ technical experts to bring new possibilities of cloud native and cloud future

Small program large screen adaptation Guide

【OSPF引入直连路由时巧借静态黑洞路由做汇总】

Chapter IV introduction to FPGA development platform
随机推荐
How to combine two byte arrays [repeat] - how to combine two byte arrays [duplicate]
jetson tx2
QT (x): innosetup for software packaging
配置Flutter开发环境
Unity ar shadow shadow
Hyperledger Fabric 2. X custom smart contract
用机器人教育创造新一代生产和服务工具
Sourcetree remote red exclamation point
关于端口转发程序的一点思考
Antd work item memo w3.0
About: deleting unwanted event log lists
Observer mode vs publish subscribe mode
Leetcode simple question: judging the color of a grid on a chess board
Analysis comp122 the Caesar cipher
Single application and microservice application
Servlet version conflict causes page 404
Pointer from beginner to advanced (2)
Analysis on the wave of learning robot education for children
2022.02.14
Configuring the flutter development environment