程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> Oracle教程 >> sql: Oracle simple table,oraclesimple

sql: Oracle simple table,oraclesimple

編輯:Oracle教程

sql: Oracle simple table,oraclesimple


--Oracle 9i 實例數據腳本地址:$oracle_home/rdbms/admin/utlsampl.sql

CREATE TABLE DEPT
       (DEPTNO NUMBER(2) CONSTRAINT PK_DEPT PRIMARY KEY,
    DNAME VARCHAR2(14) ,
    LOC VARCHAR2(13) ) ;
CREATE TABLE EMP
       (EMPNO NUMBER(4) CONSTRAINT PK_EMP PRIMARY KEY,
    ENAME VARCHAR2(10),
    JOB VARCHAR2(9),
    MGR NUMBER(4),
    HIREDATE DATE,
    SAL NUMBER(7,2),
    COMM NUMBER(7,2),
    DEPTNO NUMBER(2) CONSTRAINT FK_DEPTNO REFERENCES DEPT);
INSERT INTO DEPT VALUES
    (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT VALUES
    (30,'SALES','CHICAGO');
INSERT INTO DEPT VALUES
    (40,'OPERATIONS','BOSTON');
INSERT INTO EMP VALUES
(7369,'SMITH','CLERK',7902,to_date('17-12-1980','dd-mm-yyyy'),800,NULL,20);
INSERT INTO EMP VALUES
(7499,'ALLEN','SALESMAN',7698,to_date('20-2-1981','dd-mm-yyyy'),1600,300,30);
INSERT INTO EMP VALUES
(7521,'WARD','SALESMAN',7698,to_date('22-2-1981','dd-mm-yyyy'),1250,500,30);
INSERT INTO EMP VALUES
(7566,'JONES','MANAGER',7839,to_date('2-4-1981','dd-mm-yyyy'),2975,NULL,20);
INSERT INTO EMP VALUES
(7654,'MARTIN','SALESMAN',7698,to_date('28-9-1981','dd-mm-yyyy'),1250,1400,30);
INSERT INTO EMP VALUES
(7698,'BLAKE','MANAGER',7839,to_date('1-5-1981','dd-mm-yyyy'),2850,NULL,30);
INSERT INTO EMP VALUES
(7782,'CLARK','MANAGER',7839,to_date('9-6-1981','dd-mm-yyyy'),2450,NULL,10);
INSERT INTO EMP VALUES
(7788,'SCOTT','ANALYST',7566,to_date('13-7-87','dd-mm-rr')-85,3000,NULL,20);
INSERT INTO EMP VALUES
(7839,'KING','PRESIDENT',NULL,to_date('17-11-1981','dd-mm-yyyy'),5000,NULL,10);
INSERT INTO EMP VALUES
(7844,'TURNER','SALESMAN',7698,to_date('8-9-1981','dd-mm-yyyy'),1500,0,30);
INSERT INTO EMP VALUES
(7876,'ADAMS','CLERK',7788,to_date('13-7-87', 'dd-mm-rr')-51,1100,NULL,20);
INSERT INTO EMP VALUES
(7900,'JAMES','CLERK',7698,to_date('3-12-1981','dd-mm-yyyy'),950,NULL,30);
INSERT INTO EMP VALUES
(7902,'FORD','ANALYST',7566,to_date('3-12-1981','dd-mm-yyyy'),3000,NULL,20);
INSERT INTO EMP VALUES
(7934,'MILLER','CLERK',7782,to_date('23-1-1982','dd-mm-yyyy'),1300,NULL,10);
CREATE TABLE BONUS
    (
    ENAME VARCHAR2(10)    ,
    JOB VARCHAR2(9)  ,
    SAL NUMBER,
    COMM NUMBER
    ) ;
CREATE TABLE SALGRADE
      ( GRADE NUMBER,
    LOSAL NUMBER,
    HISAL NUMBER );
INSERT INTO SALGRADE VALUES (1,700,1200);
INSERT INTO SALGRADE VALUES (2,1201,1400);
INSERT INTO SALGRADE VALUES (3,1401,2000);
INSERT INTO SALGRADE VALUES (4,2001,3000);
INSERT INTO SALGRADE VALUES (5,3001,9999);
COMMIT;
EXIT



select rowid,emp.* from emp;
SELECT emp.empno,dept.deptno from emp,dept where emp.deptno=dept.deptno and dept.deptno>=20;

SELECT * FROM EMP;


DECLARE
 v_EmpName VARCHAR2(50);
BEGIN
 SELECT EName Into v_EmpName FROM Scott.EMP WHERE empNo=&EmpNo;
 DBMS_OUTPUT.PUT_LINE('當前查詢的員工編號為:'||&EmpNo||'員工名稱為:'||v_EmpName);
END;


--開啟屏幕輸出顯示 
--SET SERVEROUTPUT ON;
--顯示當前日期與時間
BEGIN
  DBMS_OUTPUT.PUT_LINE('現在的日期時間:');
  --顯示信息不換行
  DBMS_OUTPUT.PUT('今天是:');
  --顯示信息並換行
  DBMS_OUTPUT.PUT_LINE(TO_CHAR(SYSDATE,'DAY'));
  DBMS_OUTPUT.PUT('現在時間是: ');
  DBMS_OUTPUT.PUT_LINE(TO_CHAR(SYSDATE,'YYYY-MM-DD HH24:MI:SS'));  
END;


--分支結構示例,演示為員工加薪
DECLARE
 --定義加薪比率常量
 c_Manager CONSTANT NUMBER:=0.15;
 c_SalesMan CONSTANT NUMBER:=0.12;
 c_Clerk CONSTANT NUMBER:=0.10;
 --定義工種變量
 v_Job VARCHAR(100);
 empNo1 NUMBER(4):=7369;
BEGIN
 --查詢指定員工編碼的員工信息
 SELECT job INTO v_Job FROM emp WHERE empno=empNo1;
--執行分支判斷
 IF v_Job='CLERK' THEN
   UPDATE emp SET SAL=SAL*(1+c_Clerk) WHERE empno=empNo1;
 ELSIF v_Job='SALESMAN' THEN
   UPDATE emp SET SAL=SAL*(1+c_SalesMan) WHERE empno=empNo1;   
 ELSIF v_Job='MANAGER' THEN
   UPDATE emp SET SAL=SAL*(1+c_Manager) WHERE empno=empNo1;   
 END IF;
 --顯示完成信息
 DBMS_OUTPUT.PUT_LINE('已經為員工'||empNo1||'成功加薪!');  
 EXCEPTION
 --處理PL/SQL預定義異常
 WHEN NO_DATA_FOUND THEN
   DBMS_OUTPUT.PUT_LINE('沒有找到員工數據');
END; 

--循環結構示例,演示循環為所有員工加薪
DECLARE
 --定義加薪比率常量
 c_Manager CONSTANT NUMBER:=0.15;
 c_SalesMan CONSTANT NUMBER:=0.12;
 c_Clerk CONSTANT NUMBER:=0.10;
 v_Job VARCHAR(100);                         --定義職位變量
 v_EmpNo VARCHAR(20);                        --定義員工編號變量
 v_Ename VARCHAR(60);                        --定義員工名稱變量
 CURSOR c_Emp IS SELECT job,empno,ename from emp FOR UPDATE;
BEGIN
 OPEN c_Emp;   --打開游標
 LOOP          --循環游標
   FETCH c_Emp INTO v_Job,v_EmpNo,v_Ename;   --提取游標數據
   EXIT WHEN c_Emp%NOTFOUND;                 --如果無數據可提取退出游標
 IF v_Job='CLERK' THEN                       --如果為職員,加薪10%
   UPDATE emp SET SAL=SAL*(1+c_Clerk) WHERE CURRENT OF c_Emp;
 ELSIF v_Job='SALESMAN' THEN                 --如果為銷售職員,加薪12%
   UPDATE emp SET SAL=SAL*(1+c_SalesMan) WHERE CURRENT OF c_Emp; 
 ELSIF v_Job='MANAGER' THEN                  --如果為經理,加薪15%
   UPDATE emp SET SAL=SAL*(1+c_Manager) WHERE CURRENT OF c_Emp;  
 END IF;
 --顯示完成信息
 DBMS_OUTPUT.PUT_LINE('已經為員工'||v_EmpNo||':'||v_Ename||'成功加薪!'); 
 END LOOP;
 CLOSE c_Emp;                --關閉游標 
 EXCEPTION
 WHEN NO_DATA_FOUND THEN     --處理PL/SQL預定義異常
   DBMS_OUTPUT.PUT_LINE('沒有找到員工數據');
END;

 
CREATE OR REPLACE FUNCTION GetAddSalaryRatio(p_Job VARCHAR2)
RETURN NUMBER AS
  v_Result NUMBER(7,2);
BEGIN
 IF p_Job='CLERK' THEN                       --如果為職員,加薪10%
   v_Result:=0.10;
 ELSIF p_Job='SALESMAN' THEN                 --如果為銷售職員,加薪12%
   v_Result:=0.12;    
 ELSIF p_Job='MANAGER' THEN                  --如果為經理,加薪15%
   v_Result:=0.15;
 END IF;
 RETURN v_Result;   
END;

CREATE OR REPLACE FUNCTION GetAddSalaryRatioCASE(p_Job VARCHAR2)
RETURN NUMBER AS
  v_Result NUMBER(7,2);
BEGIN  
  CASE p_Job                     --使用CASE WHEN語句進行條件判斷
  WHEN 'CLERK' THEN              --職員
    v_Result:=0.10;
  WHEN 'SALESMAN' THEN           --銷售
    v_Result:=0.12;
  WHEN 'MANAGER' THEN            --經理
    v_Result:=0.15;
  END CASE; 
 RETURN v_Result;                --返回值
END;



BEGIN
  DBMS_OUTPUT.PUT_LINE(GetAddSalaryRatio('bc'));
END;

  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved