整理如下:
友情提示:commit是執行完後需要的。當然如果你執行另外的語句更新就不需要了。否則你可能會發現剛才的更新沒有用。
1 以DBA的身份登入資料庫
connect sys/change_on_install@hhxx as sysdba;
2 查看日期等的DUAL表查詢
SELECT SYSDATE FROM DUAL;
3 查看使用者的預設資料表空間
select username,default_tablespace from dba_users;
4 給使用者賦許可權
grant resource to hhsys;
grant connect to hhsys;
grant sysdba to hhsys;
5 建立資料表空間
CREATE TABLESPACE "HHSCINFO"
LOGGING
DATAFILE 'D:/ORACLE/ORADATA/INFOPLAT/HHSCINFO.ora' SIZE 512M
EXTENT MANAGEMENT LOCAL
6 建立表
create table "HHSYS"."xxgntable" (
功能編號 CHAR(8) not null,
功能名稱 CHAR(64) not null,
上級功能編號 CHAR(8) not null,
constraint PK_XXGNTABLE primary key (功能編號 )
);
7 查詢表
select * from HHSYS."xxgntable"
8 插入資料庫
INSERT INTO "HHSYS"."XTGNTABLE" ("功能編號" ,"功能名稱" ,"上級功能編號" ) VALUES ('25' ,'檔案管理' ,'20' )
9 將字元型的按照數值來排序
select * from hhsys.xtgntable order by to_number("功能編號")
例子:
Char型等:1, 10,110,12,排序不是按照你想象的安順排
上面的語句就是可以按順序排的。1,10,12,110
10 擷取表欄位及對應的類型
select column_name,data_type from all_tab_columns where lower(table_name)= 'xitonggongneng'
1 UserName CHAR
2 UserPwd CHAR
3 EMail CHAR
11 選擇表的前n行
select 關鍵字,閱讀次數,SN FROM xinxi WHERE ROWNUM<11
12 查看欄位中是否不含或不包含欄位
select * from hhsys.xinxi where 關鍵詞 NOT LIKE '%演算法%'
select * from hhsys.xinxi where 關鍵詞 LIKE '%演算法%'
13 更新資料庫
update xinxi set 資訊標題='這是個測試', 經辦人='李四', 關鍵字='測試',審批人='張三' where SN=2
14 表的關聯查詢 等值查詢
select * from hhsys.xinxi a, hhsys.xinxishenpi b, hhsys.xinxiquanxian c where a.sn = b.id and a.sn=c.id
由於*號不推薦使用具體點
select a.建立日期,a.資訊標題,a.發布部門,a.經辦人,a.資訊類型,a.關鍵字,c.許可權人員 from hhsys.xinxi a, hhsys.xinxishenpi b, hhsys.xinxiquanxian c where a.Sn = b.id and a.sn=c.id
15 查看資料庫歸檔模式
select log_mode from v$database;
16 查看查詢欄位結果的長度Length和to_char的用法及格式化
select LENGTH(to_char(Hiredate,'fm YYYY-MM-DD ')) from scott.emp
17 等值查詢
select a.*,b.* from hhsys.fawendengji a , hhsys.shouwendengji b
18 從一個表中擷取記錄插入另一個表中
insert into fawendengji values(select * from shouwendengji where id ='1')
19 查看錶空間
SELECT FILE_NAME,TABLESPACE_NAME,BYTES,USER_BLOCKS FROM DBA_DATA_FILES
20 修改資料表空間
alter database datafile 'D:/ORACLE/ORADATA/HHXX/HHNFXP.ORA' resize 8192M;
21 更新表
UPDATE tushu set 目前狀態='在館' WHERE 目前狀態<>'在館'
22 distinct的使用
select distinct 字典名稱 from zidianmingcheng,xitongzidian where 模組類別='1'
23 字串的日期比較函數
select SYSDATE from dual;
select * from hhsys.huiyi where to_date(開始時間,'YYYY-MM-DD')<sysdate
注:其中的開始時間為char型(字串)
可以查詢出比目前時間小的記錄
24 匯出資料表空間
exp "+"'hhsys/*******@hhxx as sysdba'"+" file=d:/t.dat tablespaces=hhnfxp"
25 匯出表
exp hhsys/*******@hhxx tablespaces=hhnfxp file=d:/hhnfxp.dmp"
26 選擇表中出現次數多於1次的資料
select count(*),finger from hash group by finger having count(*)>1