sql plus及SQL語句


1.sql plus

1,SQL plus的定義 sql plus是用來和oracle進行交互的工具。可電腦端使用,也可以在服務器上使用 2,啟動SQLplus  在cmd界面直接輸入SQLplus就可進入 3,常用命令   show 顯示SQL plus中的信息  connect (縮寫conn) 先無條件斷開數據庫用戶的鏈接,再次輸入可再次建立鏈接 disconnect (縮寫desc) 斷開當前連接  注意:desc 為描述命令,只能在命令窗口執行 語法:desc 表名 查詢表的表頭名字及類 set 設置SQLplus中的相關信息  help 獲取SQLplus命令幫助  clear scree 清屏 exit或quit  退出 4,創建用戶  第一部分:創建一個普通用戶  語法:create user 用戶名 identified by 密碼;  第二部分:授權此用戶 授權角色在oracle中有3個 DBA 系統管理權限  connect 創建會話鏈接權限 resource 操作數據庫對象的權限 語法:grant 權限,權限..to 用戶名;  修改密碼:alert user 用戶名 identified 新密碼;  刪除用戶:drop user 用戶名; 賬戶解鎖:alert user 用戶名 account unlock; 最后導入SQL腳本:第一,把腳本內容復制黏貼到SQLplus; 第二,@文件路徑 文件名.后綴名

 

2.sql查詢語句

 1,SQL條件查詢  where字句用來過濾查詢的數據,它對字面量大小寫是敏感 語法: select 列名1,列名2... from 表名 where 篩選的條件; 2. --select后面是*,代表查列表的所有的內容  select * from 表名;

 

3.SQL運算符

a.比較運算符 >, <, =, <=, >=, !=(^=,<>) --不等於 --查詢在41部門的員工名字,工資 select first_name,salary --(此處布局為select語句的書寫格式) from s_emp where dept_id=41; --找出工資大於1200的員工的全名、工資、職位(把兩個名字連起來用||' '|| 在引號中間可放符號 漢字等根據客戶需要) select first_name||' '||last_name 全名,salary 工資,title 職位 --漢字代表前面的別名(salary顯示出來的列名是工資) from s_emp where salary >1200;  b.邏輯運算符  and ,or --and優先級高於or --查出41部門中工資高於1200的員工名字,工資; select first_name, salary ,dept_id from s_emp where dept_id = 41 and salary > 1200; --查出41,50,42部門的員工名字,薪水; select first_name,salary from s_emp where dept_id=41 or dept_id=42  or dept_id=50;  c.其它運算符 1.in() 相當於or 。 取多個數值,多個值之間使用逗號隔開 not in() 相當於and。 顯示出來的意思為不包含括號里面的內容 例:deot_id in(41,42,50)相當於dept_id=41 or dept_id=42  or dept_id=50; --查出在41,42,50部門的員工名字,薪水; select first_name,salary from s_emp where dept_id in(41,42,50); 例:dept_id between 2 and 6 在指定的范圍之內,是全閉空間;相當於dept_id>=2 and                      dept_id<=6 dept_id>2 and dept_id<6 非全閉空間 --找出工資在1200到1500之間的員工名字;(全閉和不全閉) 全閉: select first_name,salary from s_emp where salary between 1200 and 1500; 非全閉: select first_name,salary from s_emp where salary >1200 and salary <1500; 2.is null
          is not null 注意:查詢數據時條件是否為null,我們使用關鍵字is null 或者is not null ,千萬不能使                用=(即等號) --找出工資大於1500並且沒有提成的員工; select * from s_emp where salary > 1500 and commission_pct is null; 3.模糊查詢:like '' not like '' 引號里面用通配符: _ 英語狀態下的下划線,代表任意單個字符 % 代表任意多個字符 --查詢名字是以M打頭的員工; select * from s_emp where first_name like 'M%'; --查出姓名中第三個字母是e的員工; select * from s_emp where first_name like '__e%'; --查出姓名不帶a的員工; select * from s_emp where first_name not like'%a%'; 

 

4.order   by排序

 

排序字句: select 列名1,列名2... from 表名 where 查詢條件  order by 列名 asc(升序,默認可以不寫)|desc(降序) --找出查詢職位是Stock Clerk的員工全名、工資,並按照工資的降序排序 select first_name||' '||last_name 全名,salary 工資 from s_emp where title = 'Stock Clerk' order by salary desc; --查詢職位中帶vp(大寫)的員工名字,工資,並按照工資降序排序; select first_name,salary,title  from s_emp where title like '%VP%' order by salary desc; --查詢出年薪(包含提成)低於25000的員工名稱,職位,年薪,並按照年薪升序排序; select first_name 名字,title 職務,salary 薪資,salary*12*(1+nvl(commission_pct/100,0)) 年薪 from s_emp where salary*12*(1+nvl(commission_pct/100,0))<25000 order by salary*12 asc;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM