orcle 數(shù)據(jù)庫名是orcl 有system scott hr 等多個用戶商叹,其中system為管理員賬戶沪哺,每個賬戶都有各自不同的table. oracle 會自動開啟事務(wù)
常用語法總結(jié):
? ?oracle 常用的命令
? ? ? ? ? ? 1.host cls 清除屏幕酌儒;
? ? ? ? ? ? 2. spool D:\abc.text 在D盤創(chuàng)建一個text文件
? ? ? ? ? ? ? ? spool off? 結(jié)束儲存
? ? ? ? ? ? 3. / 重復(fù)上次命令
? ? ? ? ? ? 4. -- 代表注釋
? ? ? ? ? ? 5.ed 用于修改以保存的sql語句
? ?oracle 中的數(shù)據(jù)類型
? ? ? ? ? ? char varchar2 number date
?各種小知識
? ? ?1. 常用關(guān)鍵字
? ? ? ? ? ?create table tname (colname type 約束);創(chuàng)建表
? ? ? ? ? ?create table tabname1 as select * from tabname2; 復(fù)制一張表(包括數(shù)據(jù))
? ? ? ? ? ?create table tabname1 as select * from tabname2 where 1=2;復(fù)制表結(jié)構(gòu)
? ? ? ? ? ?select * from tab; 查看當(dāng)前賬戶所有的表
? ? ? ? ? ?desc tabname;查看表結(jié)構(gòu)
? ? ? ? ? ?show user; 顯示當(dāng)前用戶
? ? ? ? ? ?show linesize 顯示行寬
? ? ? ? ? ?et linesize 150 設(shè)置行寬
? ? ? ? ? ?col col_mgr for a10(9999);? 設(shè)置列寬10個字符的寬度(4個數(shù)字的寬度)
? ? ? ? ? ?set pagesize 20; 設(shè)置一頁行數(shù)
? ? ? ? 2.? 約束
? ? ? ? ? ? not null籍滴, nuique , primary key, foreign key, check(條件表達式)
? ? ? ? ? ? ?例:age number check(age>0 and age <120);
? ? ? ? ?3.偽表
? ? ? ? ? ? ? dual? 為了語句完整性而提供
? ? ? ? ? ? ? select sysdate from dual;
? ? ? ? ? 4.偽列
? ? ? ? ? ? ? ? rownum 在查詢結(jié)果中加入的一列從一開始的序列號?
? ? ? ? ? ? ? ?可以用于orale 中的分頁查詢 rownum只能用于小于表達式 不支持(between and)
? ? ? ? ? ? ? ?select * from (select rownum r,t,*fom tabname t where ????????????????????rownum<currpage*pagecount)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? where r>(currpage-1)*pagecount;
? ? ? ? ? ? ? ?rowid 基于64位編碼的18個字符來唯一標識一條記錄的物理位置的一個ID,類似于JAVA ????????????????????中的哈希碼,不支持增刪改操作
? ? ? ? ? ?5.外連接
? ? ? ? ? ? ? ?(+)代表此字段所在的表為子表
? ? ? ? ? ? ? ? ? select? * from emp,dept where emp.deptno=dept,deptno(+);
? ? ? ? ? ?6.||連接符
? ? ? ? ? ? ? ? ? select '姓名:'||name||'工作'||job from tabname;
? ? ? ? ? ? ? ? ? 可將結(jié)果顯示為字符串
? ? ? ? ? ?7.常用函數(shù)
? ? ? ? ? ? ? ? sysdate 當(dāng)前日期
? ? ? ? ? ? ? ? lower() 變小寫 upper()變大寫 initcap() 首字母大寫()
? ? ? ? ? ? ? ? ?substr(str,num,count) 截取字符 從num開始長度為count num和count 可省略
? ? ? ? ? ? ? ? ?length() 字符數(shù)量
? ? ? ? ? ? ? ? ?lpad(str,num,str1) str左邊填充num個str1 rpad為右邊
? ? ? ? ? ? ? ? ?trim(char from str) 去掉str中的char
? ? ? ? ? ? ? ? ?replace(str,'oldchar','newchar')? 將字符str中的oldchar替換為newchar
? ? ? ? ? ? ? ? ?round(num,length)四舍五入 length 為小數(shù)位數(shù)? 負數(shù)(-1代表四舍五入舍掉個位數(shù))
? ? ? ? ? ? ? ? ? trunc(num,length)舍去? length 為小數(shù)位數(shù)
? ? ? ? ? ? ? ? ? ?ceil()向上取整floor()向下取整
? ? ? ? ? ? ? ? ? ?nvl(a,0) 如果a的值為空 則賦值為0
? ? ? ? ? ? ? ? ? ?nvl2(a,b,default)? 等同于 a==null?default:b
? ? ? ? ? ? ? ? ? ?nullif(a,b)如果a==b返回null否則返回a
? ? ? ? ? ? ? ? ? ?coalesce(列1,列2,列3) 從左到右返回第一個不為null的值
? ? ? ? ? ? ?8.關(guān)于日期
? ? ? ? ? ? ? ? ? ? ? ? 日期格式
? ? ? ? ? ? ? ? ? ? ? ? ? ? yyyy ||? 2011
? ? ? ? ? ? ? ? ? ? ? ? ? ? year || twenty ninteen
'? ? ? ? ? ? ? ? ? ? ? ? ? ? MM || 04
? ? ? ? ? ? ? ? ? ? ? ? ? ? MONTH || 4月
? ? ? ? ? ? ? ? ? ? ? ? ? ? DY || 星期一
? ? ? ? ? ? ? ? ? ? ? ? ? ? DAY || 星期一
? ? ? ? ? ? ? ? ? ? ? ? ? ? DD ||? 02
? ? ? ? ? ? ? ? ?完整日期格式 ‘fm yyyy-mm-dd hh24:mi:ss’ 其中fm 可以去掉月和天前面的0 ????????????????????????????????24代表24小時制?
? ? ? ? ? ? ? ? ? sysdate支持=-*/運算
? ? ? ? ? ? ? ? ? months_between(date1,date2)? 計算相差幾月
? ? ? ? ? ? ? ? ? ?add_months(date,num) date 添加num月
? ? ? ? ? ? ? ? ? last_day(sysdate)? 當(dāng)前月份最后一天
? ? ? ? ? ? ? ? ? next_day(sysdate,num(str)) 下一個周幾 num只能為1-7 只能為星期一到星期日?
? ? ? ? ? ?9.類型轉(zhuǎn)換函數(shù)
? ? ? ? ? ? ? ? ? ? ? to_date(string,'yyyy')? 轉(zhuǎn)化字符串為日期格式
? ? ? ? ? ? ? ? ? ? ? to_number(char) 將一段數(shù)字字符轉(zhuǎn)化為number格式
? ? ? ? ? ? ? ? ? ? ? ?to_char(num/date,'fromat') 將日期轉(zhuǎn)化為字符串
? ? ? ? ? ? ? ? ? ? ? ?select to_char(sysdate,’fmyyyy-mm-dd hh24:mi:ss“ today is “dd') from dual;?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 結(jié)果: 2019-8-28 15:58:34 today is 28? ?format中可拼接字符串
? ? ? ? ? ? ? ? ? ? ? ? ?select to_char(88.8,'L9,999.99') from dual;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 結(jié)果:? ¥88.88
? ? ? ? ? ? ? ? ? ? ? ? 10.占位符:&
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? select * from tabnaem where name = '&name'? 可從鍵盤接收name的值