user_XXX all_XXX dba_XXX --資料字典儲存的是oracle系統資訊,分為這三個。-------------------------------------系統使用者、表、索引資訊-----------------------------------select sum(bytes)/(1024*1024) as "size(M)" from user_segments where segment_name=upper('&table_name');—— 查看某表的大小
select table_name,cache from user_tables where instr(cache,'Y')>0;——查看放在ORACLE的記憶體區裡的表select * from user_synonyms;——查看同義字的名稱查看函數和過程的狀態 SQL>select object_name,status from user_objects where object_type='FUNCTION'; SQL>select object_name,status from user_objects where object_type='PROCEDURE';select text from all_source where owner=user and name=upper('&plsql_name');——查看函數和過程的原始碼select * from dba_objects; 資料對象select * from user_objects;select username from dba_users;--查看系統的全部使用者了,注意,在表裡面都變成了大寫。select table_name from user_tables;--查看該使用者的表select table_name from all_tables;--查看該使用者可以訪問的表select * from user_tablespaces;--查看錶空間select * from dba_tablespaces;--查看錶空間select * from user_indexes;--查看索引 dba_indexs is not existselect * from dba_users;--88A2B2C183431F00 查看系統使用者 崔鐘select length(t.password) from dba_users t where rownum <2;--16,not 32.select distinct(t.GRANTEE) from dba_sys_privs t;select * from dba_role_privs t where t.GRANTEE=upper('lele');--查看樂樂擁有的角色select * from dba_roles;--oracle rolesselect * from global_name;--全域資料庫名==sid==主機字串==執行個體名-------------------------------------資料表空間與資料檔案-----------------------------------create tablespace test02 datafile 'd:\ykdatefile03.dbf' size 100m uniform size 64k; --資料檔案1g,區大小64k。error,是因為語法錯誤,datafile、size丟失。。。select table_name from dba_tables where tablespace_name=upper('users');--查看屬於某個資料表空間的表drop tablespace 'biaomingzi' including contents and datafiles;--刪除資料表空間及其對象--alter tablespace datefileyk add datafile 'd:\adddatafile02.dbf' size 50m uniform size 128k;--擴充資料表空間方法一:增加資料檔案(error by 'uniform size 128k')alter tablespace datefileyk add datafile 'd:\adddatafile02.dbf' size 50m;--擴充資料表空間方法一:增加資料檔案alter tablespace test01 'd:\ykdatefile02.dbf' resize 150m;--擴充資料表空間方法二:增大資料檔案--erroralter database datafile 'd:\ykdatefile02.dbf' resize 150m;--擴充資料表空間方法二:增大資料檔案--right!alter database datafile 'd:\ykdatefile02.dbf' autoextend on next 100m maxsize 200m;--擴充資料表空間方法三:設定資料檔案自動成長--righit!alter database datafile '/u01/app/oracle/oradata/cmsdb/users01.dbf' autoextend on next 100M MAXSIZE UNLIMITED;--擴充資料表空間四,不限制增長。該方法不宜使用!魔屏資訊、users。fchenalter tablespace users online;--offline,離線狀態,當移動資料檔案時執行離線。--修改資料檔案,不需要指明資料表空間,因為資料檔案肯定屬於一個特定的資料表空間,而建立、增加資料檔案的時候,則必須指明這些檔案是屬於哪個tablespace的。--datafileselect * from dba_data_files;--查看錶空間和它的資料檔案的位置!!!--執行命令mv 移動資料檔案,執行命令rename 修改datafile名字。SELECT tablespace_name 資料表空間,sum(blocks*8/1024) 剩餘空間M FROM dba_free_space GROUP BY tablespace_name;--查看剩餘資料表空間select tablespace_name,sum(bytes)/1024/1024 from dba_data_files group by tablespace_name;------------------------------------oracle中的約束-----------------------------------
not null,primary key,foreign key,check,unique.---------index---------1.大表上建立索引才有意義2.在where子句或者是串連條件上經常使用的列上建立索引3.索引不要超過4層--4.單索引,即建立在單列上的索引,一個表可以建立多個索引5.符合索引,建立在兩個列或者更多列上的索引,但要求組合(排列)必須不同,如create index ind_dpixuqiu on dpixuqiu(id,name);create index ind_dpixuqiu on dpixuqiu(name,id);這是兩個不同的索引,建議寫sql的時候,記住列上的索引是如何建立的!6.修改表結構,維護成本高。。。---SQL> conn sys/zhangle as sysdba;--使用sys登入,必須as sysdba或者sysoper,不能是normal!!!搞了一天,也問了浩偉,原來是這個情況。。。what a fucking things!grant update on dpixuqiu(name) to xiaoshuai;--select delete update insert和alter,可以具體到某個欄位。-------------------------------------拜神測試--------------------------------------select * from (select * from tadmin where rownum < 10) t where rownum > 2;select rownum rn, t.* from tadmin t where rownum < 5;select * from (select t.*, rownum rn from tadmin t where rownum < 5) t where rn > 2;--insert into test2 values (1, '測試資料1....1', sysdate, 'y');insert into test2 values (2, '百勝', sysdate, 'N');insert into test2 values (5, '測試資料1....3', sysdate, 'Y');insert into test2 values (11, '測試資料1....3', sysdate, 'Y');commit;update test2 set testname='百勝凱哥' where id=1;select * from test2;--update test1 set testname =test2.testname where exists (select 1 from test2 where test1.id=test2.id); (select s.testname from test1 t, test2 s where t.id = s.id); --update test1 t set t.testname = (select s.testname from test1 t, test2 s where t.id = s.id) where t.id in (select t.id from test1 t, test2 s where t.id = s.id);----正確的update test1 t set testname = (select s.testname from test2 s where t.id = s.id) where t.id in (select t.id from test1 t, test2 s where t.id = s.id); ---- commit; rollback; where t.id = 2;select * from test1;
update test1 t set t.testname = test2.testname where t.id = test2.id;select lower(t.cadduser) from tadmin t;--upper lowerselect upper(substr(t.cadduser,1,1))||lower(substr(t.cadduser,2,length(t.cadduser)-1)) from tadmin t;create table addr02(iid2 number(2),cname2 char(8));alter table addr02 modify iid2 number(2) primary key;alter table addr01 add ttime date;insert into addr02 values ('1','kai01');insert into addr02 values ('2','kai02');insert into addr02 values ('4','kai03');insert into addr01 values ('4','kai01',sysdate);select * from addr01;commit;rollback;update addr01,addr02 set addr01.cname1=addr02.cname2 where addr01.iid=addr02.iid2;--mysql下可以update addr01 t set t.cname1=(select s.cname2 from addr02 s where t.iid=s.iid2) where t.iid in (select s.iid2 from addr02 s where t.iid=s.iid2);create table addr02(iid2 number(2) primary key,cname2 char(8));