oracle 小專案實戰總結
阿新 • • 發佈:2019-01-26
說明:釣魚君昨天在網上找到一份oracle專案實戰的文件,粗略看了一下大致內容,感覺自己很多知識不夠紮實,便跟著文件敲了一遍,目前除了機械性程式碼沒有實現外,主要涉及知識:建立表空間、建立使用者、給使用者賦權、建立表、給表新增主鍵及外來鍵約束、給表及欄位添加註釋、建立函式、建立過程、建立序列、建立觸發器、建立程式包等,現將知識整理如下:
1.建立表空間
--建立表空間
CREATE TABLESPACE fund DATAFILE 'e:\ORADATA\fundd_file.dbf' SIZE 40M;
2.建立使用者
--建立使用者及密碼 CREATE USER test_user IDENTIFIED BY test123456 DEFAULT TABLESPACE fund;
3.給使用者賦權
--授權
GRANT CONNECT,RESOURCE TO test_user;
4.建立表
--建立表 CREATE TABLE Fund( FundNo VARCHAR2(20), CompanyId VARCHAR2(20), FundName VARCHAR2(20), Price NUMBER(10,2), FundType NUMBER(1,0), Invest NUMBER(1,0), BuyLimit NUMBER(5,0), IsChange NUMBER(1,0), YearRate NUMBER(6,5), ApplyDate DATE, State NUMBER(1,0) );
5.給表新增主鍵及外來鍵約束
--刪除已有的主鍵
--ALTER TABLE Fund DROP CONSTRAINT PK_Fund;
--新增主鍵約束
ALTER TABLE Fund ADD CONSTRAINT PK_Fund_FundNo PRIMARY KEY(FundNo);
--新增外來鍵約束
ALTER TABLE Fund ADD CONSTRAINT FK_Fund_CompanyId FOREIGN KEY(CompanyId) REFERENCES FundCompany(CompanyId);
6.給表及欄位添加註釋
7.建立函式--給表添加註釋 COMMENT ON TABLE Fund IS '基金錶'; --給表字段添加註釋 COMMENT ON COLUMN Fund.FundNo IS '基金程式碼ID,主鍵'; COMMENT ON COLUMN Fund.CompanyId IS '基金公司ID,外來鍵引用FundCompany表的CompanyId'; COMMENT ON COLUMN Fund.FundName IS '基金名稱'; COMMENT ON COLUMN Fund.Price IS '基金淨值'; COMMENT ON COLUMN Fund.FundType IS '基金型別,1表示開放式,2表示封閉式'; COMMENT ON COLUMN Fund.Invest IS '投資方向,1股票,2債券,3貨幣,4混合'; COMMENT ON COLUMN Fund.BuyLimit IS '購買下限'; COMMENT ON COLUMN Fund.IsChange IS '是否可轉換,0表示不可轉換,1表示可轉換'; COMMENT ON COLUMN Fund.YearRate IS '年利率,必須是0-1之間的數字'; COMMENT ON COLUMN Fund.ApplyDate IS '申請日期'; COMMENT ON COLUMN Fund.State IS '基金狀態,0表示正常,1表示凍結';
CREATE OR REPLACE FUNCTION FUNC_NEXTID(I_SQ IN VARCHAR2,
I_TITLE IN VARCHAR2,
I_LEN IN NUMBER) RETURN VARCHAR2 AS
V_SQ VARCHAR2(100);
V_KEY VARCHAR2(100);
BEGIN
SELECT I_SQ || '.NEXTVAL' INTO V_SQ FROM DUAL;
SELECT I_TITLE || LPAD(V_SQ, I_LEN, 0) INTO V_KEY FROM DUAL;
RETURN V_KEY;
END;
8.建立序列
--為基金公司表(FundCompany)主鍵建立序列
CREATE SEQUENCE SQ_COMPANYID
INCREMENT BY 1 --每次加1
START WITH 1 --從1開始
NOMAXVALUE --沒有最大值
NOCYCLE --一直累加不迴圈
CACHE 10;
9.建立觸發器
CREATE OR REPLACE TRIGGER TR_Fund
BEFORE INSERT ON Fund
FOR EACH ROW
BEGIN
:NEW.FUNDNO := FUNC_NEXTID('V', 'SQ_FundNo.NEXTVAL', 6);
END;
10.建立程式包(包體中包括了函式及過程)
包:
/*------------------------------------------------
--建立包說明
--包名:FundAccountManager_pack
--功能描述:建立過程或函式分別實現,基金賬戶開戶、基金賬戶資訊查詢。
--原創:釣魚君
--日期:2016/3/10
--QQ:954739353
--
------------------------------------------------*/
CREATE OR REPLACE PACKAGE FundAccountManager_pack IS
--活期賬戶開戶
FUNCTION FUNC_ADD_CURRENTACCOUNT(I_CURRENTPASSWORD VARCHAR2,
I_DEPOSITSUM NUMBER,
I_CARDTYPE NUMBER,
I_CARDNO VARCHAR2,
I_NAME VARCHAR2,
I_ADDRESS VARCHAR2,
I_PHONE VARCHAR2,
I_SEX NUMBER,
I_OPENACCDATE DATE,
I_STATE NUMBER) RETURN NUMBER;
--理財賬戶開戶
FUNCTION FUNC_ADD_FINANCINGACCOUNT(I_FINANCEPASSWORD VARCHAR2,
I_MONEYTYPE NUMBER,
I_ACCOUNTBALANCE NUMBER,
I_ENABLEBALANCE NUMBER,
I_CONGEALFUND NUMBER,
I_STATE NUMBER,
I_CURRENTACCOUNT VARCHAR2)
RETURN NUMBER;
--基金賬戶開戶
FUNCTION FUNC_ADD_FUNDACCOUNT(I_FINANCINGACCOUNT VARCHAR2,
I_COMPANYID VARCHAR2,
I_CARDTYPE NUMBER,
I_CARDNO VARCHAR2,
I_NAME VARCHAR2,
I_SEX NUMBER,
I_ADDRESS VARCHAR2,
I_PHONE VARCHAR2,
I_POSTNUM VARCHAR2,
I_EMAIL VARCHAR2,
I_CREATEDATE DATE,
I_CONGEALSTATE NUMBER) RETURN NUMBER;
--基金賬戶資訊查詢
PROCEDURE PRO_QUERY_FUNDACCOUNT(O_RESULT OUT SYS_REFCURSOR,
I_FINANCINGACCOUNT IN VARCHAR2);
END FundAccountManager_pack;
包體:
CREATE OR REPLACE PACKAGE BODY FundAccountManager_pack IS
----活期賬戶開戶
/*------------------------------------------------
--建立包說明
--包名:FundAccountManager_pack
--功能描述:建立過程或函式分別實現,基金賬戶開戶、基金賬戶資訊查詢。
--原創:釣魚君
--日期:2016/3/10
--QQ:954739353
--
------------------------------------------------*/
FUNCTION FUNC_ADD_CURRENTACCOUNT(I_CURRENTPASSWORD VARCHAR2,
I_DEPOSITSUM NUMBER,
I_CARDTYPE NUMBER,
I_CARDNO VARCHAR2,
I_NAME VARCHAR2,
I_ADDRESS VARCHAR2,
I_PHONE VARCHAR2,
I_SEX NUMBER,
I_OPENACCDATE DATE,
I_STATE NUMBER) RETURN NUMBER IS
ERR_CURRENTACCOUNT EXCEPTION;
PRAGMA EXCEPTION_INIT(ERR_CURRENTACCOUNT, -1);
/*違反唯一主鍵約束為-1*/
BEGIN
INSERT INTO CURRENTACCOUNT
(CURRENTPASSWORD,
DEPOSITSUM,
CARDTYPE,
CARDNO,
NAME,
ADDRESS,
PHONE,
SEX,
OPENACCDATE,
STATE)
VALUES
(I_CURRENTPASSWORD,
I_DEPOSITSUM,
I_CARDTYPE,
I_CARDNO,
I_NAME,
I_ADDRESS,
I_PHONE,
I_SEX,
I_OPENACCDATE,
I_STATE);
IF SQL%FOUND THEN
RETURN 1;
END IF;
EXCEPTION
WHEN ERR_CURRENTACCOUNT THEN
RETURN 0;
WHEN OTHERS THEN
RETURN - 1;
END FUNC_ADD_CURRENTACCOUNT;
--理財賬戶開戶
FUNCTION FUNC_ADD_FINANCINGACCOUNT(I_FINANCEPASSWORD VARCHAR2,
I_MONEYTYPE NUMBER,
I_ACCOUNTBALANCE NUMBER,
I_ENABLEBALANCE NUMBER,
I_CONGEALFUND NUMBER,
I_STATE NUMBER,
I_CURRENTACCOUNT VARCHAR2)
RETURN NUMBER IS
ERR_FINANCINGACCOUNT EXCEPTION;
PRAGMA EXCEPTION_INIT(ERR_FINANCINGACCOUNT, -1);
BEGIN
INSERT INTO FINANCINGACCOUNT
(FINANCEPASSWORD,
MONEYTYPE,
ACCOUNTBALANCE,
ENABLEBALANCE,
CONGEALFUND,
STATE,
CURRENTACCOUNT)
VALUES
(I_FINANCEPASSWORD,
I_MONEYTYPE,
I_ACCOUNTBALANCE,
I_ENABLEBALANCE,
I_CONGEALFUND,
I_STATE,
I_CURRENTACCOUNT);
IF SQL%FOUND THEN
RETURN 1;
END IF;
EXCEPTION
WHEN ERR_FINANCINGACCOUNT THEN
RETURN 0;
WHEN OTHERS THEN
RETURN - 1;
END FUNC_ADD_FINANCINGACCOUNT;
--基金賬戶開戶
FUNCTION FUNC_ADD_FUNDACCOUNT(I_FINANCINGACCOUNT VARCHAR2,
I_COMPANYID VARCHAR2,
I_CARDTYPE NUMBER,
I_CARDNO VARCHAR2,
I_NAME VARCHAR2,
I_SEX NUMBER,
I_ADDRESS VARCHAR2,
I_PHONE VARCHAR2,
I_POSTNUM VARCHAR2,
I_EMAIL VARCHAR2,
I_CREATEDATE DATE,
I_CONGEALSTATE NUMBER) RETURN NUMBER IS
ERR_FUNDACCOUNT EXCEPTION;
PRAGMA EXCEPTION_INIT(ERR_FUNDACCOUNT, -1);
V_COUNT NUMBER;
BEGIN
--約束條件檢查
SELECT COUNT(FINANCINGACCOUNT)
INTO V_COUNT
FROM FUNDACCOUNT
WHERE FINANCINGACCOUNT = I_FINANCINGACCOUNT
AND COMPANYID = I_COMPANYID;
IF V_COUNT = 0 THEN
INSERT INTO FUNDACCOUNT
(FINANCINGACCOUNT,
COMPANYID,
CARDTYPE,
CARDNO,
NAME,
SEX,
ADDRESS,
PHONE,
POSTNUM,
EMAIL,
CREATEDATE,
CONGEALSTATE)
VALUES
(I_FINANCINGACCOUNT,
I_COMPANYID,
I_CARDTYPE,
I_CARDNO,
I_NAME,
I_SEX,
I_ADDRESS,
I_PHONE,
I_POSTNUM,
I_EMAIL,
I_CREATEDATE,
I_CONGEALSTATE);
IF SQL%FOUND THEN
RETURN 1;
END IF;
ELSE
RETURN - 1;
END IF;
EXCEPTION
WHEN ERR_FUNDACCOUNT THEN
RETURN 0;
WHEN OTHERS THEN
RETURN - 1;
END FUNC_ADD_FUNDACCOUNT;
--基金賬戶的查詢
PROCEDURE PRO_QUERY_FUNDACCOUNT(O_RESULT OUT SYS_REFCURSOR,
I_FINANCINGACCOUNT IN VARCHAR2) AS
BEGIN
OPEN O_RESULT FOR
SELECT *
FROM FUNDACCOUNT T
WHERE T.FINANCINGACCOUNT = I_FINANCINGACCOUNT;
END PRO_QUERY_FUNDACCOUNT;
END FundAccountManager_pack;
鞏固基礎、方能應對複雜開發。
如有問題,歡迎賜教。