简体   繁体   English

如何在 Oracle 中查找模式名称? 当您使用只读用户在 sql 会话中连接时

[英]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:为避免这种情况,您基本上有两种选择:

  1. Set the current schema in your session:在会话中设置当前模式
ALTER SESSION SET CURRENT_SCHEMA=XYZ
  1. Create synonyms for all tables:为所有表创建同义词:
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:最后一个应该总是有效的:

  1. Query the current schema setting:查询当前架构设置:
SELECT SYS_CONTEXT('USERENV','CURRENT_SCHEMA') FROM DUAL
  1. List your synonyms:列出你的同义词:
SELECT * FROM ALL_SYNONYMS WHERE OWNER = USER
  1. Investigate all tables (with the exception of the some well-known standard schemas):调查所有表(除了一些众所周知的标准模式):
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.

相关问题 如何在SQL(Oracle)中使用给定数字查找代号 - How to find Code name from given numbers using in SQL(Oracle) 使用双引号将表名括起来时,只能在 Oracle SQL 中查找表。 为什么? - Can only find table in Oracle SQL when surrounding the table name with double quotes. Why? 有表名时如何获取模式名-Oracle - How to get the schema name when I have table name - Oracle 如何在 Oracle 模式中查找没有“WHERE”的特定名称? - How to find a specific name without "WHERE" in an Oracle schema? 如何使用替换删除属于 sys 中模式用户的表 Oracle sql - how to drop a table that belong to a schema user in sys using substitution Oracle sql 在Oracle SQL中,我们如何避免必须添加模式名称? - In Oracle SQL how do we avoid having to prepend the schema name? 如何在Oracle SQL查询中不写模式名称? - How to don't write schema name in oracle sql query? 尝试使用SQL脚本删除架构/用户时列名无效 - Invalid column name when trying to drop schema/user using SQL script 在Oracle数据库中查询表/字段时,如何查看查询结果中的表模式? (使用SQuirreL SQL) - When querying tables/fields in Oracle database, how to see table schema in query results? (using SQuirreL SQL) SQL:使用 INFORMATION_SCHEMA.TABLES 时如何指定数据库名称和服务器名称? - SQL: When using INFORMATION_SCHEMA.TABLES How do I specify a database name and a server name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM