標籤:字元型 內容 平均數 提交 最大值 decode ... next 數字
資料庫的增刪改查: 增:insert into ... values(); 例:insert into p_emp values(sq_emp.nextval,‘小白‘,‘保潔‘,7902,sysdate,600,null,30,0); commit; 注意,表裡邊有多少列,values()裡邊的內容就有多少,一一對應關係。後邊加分號結束語句,然後commit;提交。 自增長序列:右擊Sequences,new一個新的自增序列,起一個name,然後有一個nextval屬性,實現自增長。 查:select ... from ... 例:select t.*,t.rowid from p_emp t(用t代表p_emp),加上t.rowid,可以直接在表裡進行修改。單行語句可以不加分號! 刪除:delete ... where ... 例:delete p_emp e where e.empon = 1; commit; 從一個表裡邊刪除某-行,地址是當empon = 1的那一行。 改:update 表 set 表.屬性 = ... where 表.屬性 = ...; update p_emp e set e.ename = ‘李華‘ where e.empon = 1; commit;資料庫的常用關鍵字: 1.in---:在某個範圍內 例子:select * from p_emp e where e.sal in(400,300,5000); 查表p_emp的工資分別為400,300,5000,的那一行。 2.like---:模糊查詢 %表示任一字元,_表示單個字元。 例子:select * from p_emp e where e.ename like ‘T%‘; 查詢名字以t開頭的,注意like後面必須加 ‘ ’,查詢的為字串的話,開頭要大寫。 select * from p_emp e where e.ename like ‘K___‘; 查詢以字母k開頭的,然後後邊有幾個字母,就加幾個底線。 3.order by--- 排序,預設為正序排序,esc省略了,如果需要倒序排序,則在後面加desc 例子:select * from e_emp order by e.sal desc 查詢工資表倒序排列。 select * from p_emp e order by e.ename,e.sal desc; 這種情況,是先把ename進行正序,然後在每個ename這個獨立的區間內,通過工資進行倒序,desc只管著後邊的。 如果ename也想倒序,就在其後邊加dese: select * from p_emp e order by e.ename desc,e.sal desc; select * from e_emp order by 1,2 ; 按照第一列和第二列排序; 4.group by--- 按照某幾列分組 select e.job from p_emp e group by e.job; 與前邊不同的是,按照哪一列分組,就去查那一列,而不是 * 。 5.having 過濾分組,是和group by 關鍵字一塊用的 select e.job from p_emp e group by e.job having count(*)>3; 6.alter table p_emp add sex number 給表加上一列 7.case when 類似於分支 例子:select e.ename as 姓名, case e.sex when 1 then ‘男‘ when 0 then ‘女‘ else ‘kk‘ end 性別 from p_emp as e; 主要要有end結尾,可以把別名寫在後邊,預設吧as省略了 8.set------設定某些屬性 9.distinct---去除重複 例子:select distinct t.job from p_emp t;查詢去除重複之後的工作 10.between...and---(閉合區間, 即包括前面的數, 也包括後面的數) 例子:select t.* from p_emp t where t.sal between 2000 and 3000; 工資在2000到3000的區間 11.all---表示一個集合中所有的元素 例子:select t.* from p_emp t where t.sal >= all(select t2.sal from p_emp t2); 查工資最高的,all(裡邊裝的是一個集合) 12.any/some---表示一個集合中任意一個元素 例子:select t.* from p_emp t where t.sal >= any(select t2.sal from p_emp t2); 大於任何一個都滿足條件,any(裡邊裝的是一個集合)常用函數: 1.to_char---日期轉換成字元型資料 例子:select to_char(t.hiredate,‘yyyy-mm-dd‘) 日期 from p_emp t; to_date---字元轉換成日期型資料 例子:select to_date(‘1997-12-12‘,‘yyyy-mm-dd‘) 日期 from dual; 傳入一個字串,轉換為日期格式,因為沒有from ,所以提供了一個虛擬表 dual select to_date(‘1997-12-12‘,‘yyyy-mm-dd‘) - sysdate 日期 from dual; 傳入的的時間和系統時間求差,返回的是相差的天數。 select * from p_emp t where t.hiredate > to_date(‘1982-01-12‘,‘yyyy-mm-dd‘); 時間之間做比較 2.分組函數: mod---取餘函數 avg---平均數函數 sum---求和函數 count---計數函數 例子:select t.job,sum(t.sal),avg(t.sal),count(t.sal) from p_emp t group by t.job; 主要是和group by 分組一塊用的; 3.decode---類似於分支語句的函數 例子:select t.ename,decode(t.sex,1,‘男‘,0,‘女‘,‘qqq‘) 性別 from p_emp t; 4.substr---分割字串 例子:select substr(t.ename,1,5) from p_emp t;分割名字,從第一位到第5位, select substr(t.ename,1) from p_emp t;從第一位後面的所有 5.max---取最大值函數 例子:select max(t.sal) from p_emp t; 6.min---取最小值函數 例子:select min(t.sal) from p_emp t; 7.trunc---取整函數 例子:select trunc(12.555) from dual; 8.ceil---向上取整函數 floor---向下取整 例子:select floor(12.555) from dual; 注意,上述3個函數可以用在日期上的 9.nvl---過濾空值函數,傳入2個參數,並且只能寫入數字 nvl2---過濾空值函數2,是nvl的加強版,傳入三個參數,為空白返回第二個,不為空白返回第三個 例子:select nvl(t.comm,0) from p_emp t;如果有的項為空白,那麼就用賦值為0; select nvl2(t.comm,‘為空白‘,‘不為空白‘) from p_emp t; 10.lower---將字母全部改為小寫 upper---改為大寫 例子:select upper(t.ename) from p_emp t; 11.concat ---連結兩字串 例子;select concat(‘tename‘,‘rrrr‘) from dual; 12.wm_concat---列轉行函數(也屬於分組函數) 例子:select wm_concat(t.ename) from p_emp t;返回一行,把所有名字整合成一塊,一個長的字串。不常用
oracle常用關鍵字和函數