[英]How to find schema name in Oracle ? when you are connected in sql session using read only user
I am connected to a oracle database with a read only user and i used service name while Setting up connection in sql developer hence i dont know SID ( schema ).我使用只读用户连接到 oracle 数据库,并且在 sql developer 中设置连接时使用了服务名称,因此我不知道 SID(架构)。
How can i find out schema name which i am connected to ?如何找出我连接到的架构名称?
I am looking for this because i want to generate ER diagram and in that process at one step it asks to select schema.我正在寻找这个,因为我想生成 ER 图,并且在该过程中的一个步骤中它要求选择模式。 When i tried to select my user name , i dint get any tables as i guess all tables are mapped with schema user.当我尝试选择我的用户名时,我没有得到任何表,因为我猜所有表都与模式用户映射。
Edit: I got my answer partially by the below sql Frank provided in comment , it gave me owner name which is schema in my case.编辑:我通过评论中提供的以下 sql Frank 部分得到了我的答案,它给了我所有者名称,在我的情况下是架构。 But I am not sure if it is generic solution applicable for all cases.但我不确定它是否适用于所有情况的通用解决方案。
select owner, table_name from all_tables.
Edit : I think above sql is correct solution in all cases because schema is owner of all db objects.编辑:我认为上面的 sql 在所有情况下都是正确的解决方案,因为架构是所有 db 对象的所有者。 So either i get schema or owner both are same.所以要么我得到架构要么所有者都是相同的。 Earlier my understanding about schema was not correct and i gone through another question and found schema is also a user.早些时候我对架构的理解不正确,我通过另一个问题发现架构也是一个用户。
Frank / a_horse_with_no_name Put this in answer so that i can accept it. Frank / a_horse_with_no_name回答这个问题,这样我就可以接受了。
Call SYS_CONTEXT
to get the current schema.调用SYS_CONTEXT
以获取当前模式。 From Ask Tom "How to get current schema :来自Ask Tom“如何获取当前架构:
select sys_context( 'userenv', 'current_schema' ) from dual;
To create a read-only user, you have to setup a different user than the one owning the tables you want to access.要创建只读用户,您必须设置与拥有要访问的表的用户不同的用户。
If you just create the user and grant SELECT permission to the read-only user, you'll need to prepend the schema name to each table name.如果您只是创建用户并向只读用户授予 SELECT 权限,则需要在每个表名前添加架构名称。 To avoid this, you have basically two options:为避免这种情况,您基本上有两种选择:
ALTER SESSION SET CURRENT_SCHEMA=XYZ
CREATE SYNONYM READER_USER.TABLE1 FOR XYZ.TABLE1
So if you haven't been told the name of the owner schema, you basically have three options.因此,如果您没有被告知所有者架构的名称,您基本上有三个选择。 The last one should always work:最后一个应该总是有效的:
SELECT SYS_CONTEXT('USERENV','CURRENT_SCHEMA') FROM DUAL
SELECT * FROM ALL_SYNONYMS WHERE OWNER = USER
SELECT * FROM ALL_TABLES WHERE OWNER NOT IN ('SYS', 'SYSTEM', 'CTXSYS', 'MDSYS');
How about the following 3 statements?以下3个说法如何?
-- change to your schema -- 更改为您的架构
ALTER SESSION SET CURRENT_SCHEMA=yourSchemaName;
-- check current schema -- 检查当前模式
SELECT SYS_CONTEXT('USERENV','CURRENT_SCHEMA') FROM DUAL;
-- generate drop table statements -- 生成删除表语句
SELECT 'drop table ', table_name, 'cascade constraints;' FROM ALL_TABLES WHERE OWNER = 'yourSchemaName';
COPY the RESULT and PASTE and RUN .复制结果并粘贴和运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.