Oracle中索引名稱的唯一性

來源:互聯網
上載者:User

標籤:nis   --   creat   別名   proc   不能   structure   table   connect   

資料庫索引處理是遇到的一點問題,簡單記錄下

 

oracle的規定,在同一個SCHEMA下的對象是不能用相同的名字命名的,一般建立索引名用“表名_欄位名”,這樣能很快知道這個索引,是屬於哪個表的。

col_index 在ORACLE中都叫對象,不能命名相同

例:

  1. SQL> create table emp1 as select * from scott.emp;  
  2.    
  3. Table created  
  4.    
  5. SQL> create table emp2 as select * from scott.emp;  
  6.    
  7. Table created  
  8.    
  9. SQL> create index emp1_name_idx on emp1(ename);  
  10.    
  11. Index created  
  12.    
  13. SQL> create index emp1_name_idx on emp2(ename);  
  14.    
  15. create index emp1_name_idx on emp2(ename)  
  16.    
  17. ORA-00955: 名稱已由現有對象使用 

轉載下schema的解析,源自   http://blog.csdn.net/kimsoft/article/details/4627520

 

我們先來看一下他們的定義:
A schema is a collection of database objects (used by a user.).
Schema objects are the logical structures that directly refer to the database’s data.
A user is a name defined in the database that can connect to and access objects.
Schemas and users help database administrators manage database security.

從定義中我們可以看出schema為資料庫物件的集合,為了區分各個集合,我們需要給這個集合起個名字,這些名字就是我們在企業管理器的方案下看到的許多類似使用者名稱的節點,這些類似使用者名稱的節點其實就是一個schema,schema裡麵包含了各種對象如tables, views, sequences, stored procedures, synonyms, indexes, clusters, and database links。

一個使用者一般對應一個schema,該使用者的schema名等於使用者名稱,並作為該使用者預設schema。這也就是我們在企業管理器的方案下看到schema名都為資料庫使用者名稱的原因。Oracle資料庫中不能新建立一個schema,要想建立一個schema,只能通過建立一個使用者的方法解決(Oracle中雖然有create schema語句,但是它並不是用來建立一個schema的),在建立一個使用者的同時為這個使用者建立一個與使用者名稱同名的schem並作為該使用者的預設shcema。即schema的個數同user的個數相同,而且schema名字同user名字一一 對應並且相同,所有我們可以稱schema為user的別名,雖然這樣說並不準確,但是更容易理解一些。

一個使用者有一個預設的schema,其schema名就等於使用者名稱,當然一個使用者還可以使用其他的schema。如果我們訪問一個表時,沒有指明該表屬於哪一個schema中的,系統就會自動給我們在表上加上預設的sheman名。比如我們在訪問資料庫時,訪問scott使用者下的emp表,通過select * from emp; 其實,這sql語句的完整寫法為select * from scott.emp。在資料庫中一個對象的完整名稱為schema.object,而不屬user.object。類似如果我們在建立對象時不指定該對象的schema,在該對象的schema為使用者的預設schema。這就像一個使用者有一個預設的資料表空間,但是該使用者還可以使用其他的資料表空間,如果我們在建立對象時不指定資料表空間,則Object Storage Service在預設資料表空間中,要想讓Object Storage Service在其他資料表空間中,我們需要在建立對象時指定該對象的資料表空間。

咳,說了這麼多,給大家舉個例子,否則,一切枯燥無味!
SQL> Gruant dba to scott

SQL> create table test(name char(10));
Table created.

SQL> create table system.test(name char(10));
Table created.

SQL> insert into test values(‘scott‘);
1 row created.

SQL> insert into system.test values(‘system‘);
1 row created.

SQL> commit;
Commit complete.

SQL> conn system/manager
Connected.

SQL> select * from test;
NAME
----------
system

SQL> ALTER SESSION SET CURRENT_SCHEMA = scott; --改變使用者預設schema名
Session altered.

SQL> select * from test;
NAME
----------
scott

SQL> select owner ,table_name from dba_tables where table_name=upper(‘test‘);
OWNER TABLE_NAME
------------------------------ ------------------------------
SCOTT TEST
SYSTEM TEST
--上面這個查詢就是我說將schema作為user的別名的依據。實際上在使用上,shcema與user完全一樣,沒有什麼區別,在出現schema名的地方也可以出現user名。

Oracle中索引名稱的唯一性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.