Oracle基础入门
Posted pungwe
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle基础入门相关的知识,希望对你有一定的参考价值。
说明:钓鱼君昨天在网上找到一份oracle项目实战的文档,粗略看了一下大致内容,感觉自己很多知识不够扎实,便跟着文档敲了一遍,目前除了机械性代码没有实现外,主要涉及知识:创建表空间、创建用户、给用户赋权、创建表、给表添加主键及外键约束、给表及字段添加注释、创建函数、创建过程、创建序列、创建触发器、创建程序包等,现将知识整理如下:
1.创建表空间
--创建表空间CREATE TABLESPACE fund DATAFILE ‘e:\ORADATA\fundd_file.dbf‘ SIZE 40M;
2.创建用户
--创建用户及密码CREATE USER test_userIDENTIFIED 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.
给表及字段添加注释
--给表添加注释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表示冻结‘;
7. 创建函数
CREATE OR REPLACE FUNCTION FUNC_NEXTID(I_SQ IN VARCHAR2,I_TITLE IN VARCHAR2,I_LEN IN NUMBER) RETURN VARCHAR2 ASV_SQ VARCHAR2(100);V_KEY VARCHAR2(100);BEGINSELECT 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_COMPANYIDINCREMENT BY 1 --每次加1START WITH 1 --从1开始NOMAXVALUE --没有最大值NOCYCLE --一直累加不循环CACHE 10;
9. 创建触发器
CREATE OR REPLACE TRIGGER TR_FundBEFORE INSERT ON FundFOR EACH ROWBEGIN: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 ISERR_CURRENTACCOUNT EXCEPTION;PRAGMA EXCEPTION_INIT(ERR_CURRENTACCOUNT, -1);/*违反唯一主键约束为-1*/BEGININSERT 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 THENRETURN 1;END IF;EXCEPTIONWHEN ERR_CURRENTACCOUNT THENRETURN 0;WHEN OTHERS THENRETURN - 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 ISERR_FINANCINGACCOUNT EXCEPTION;PRAGMA EXCEPTION_INIT(ERR_FINANCINGACCOUNT, -1);BEGININSERT 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 THENRETURN 1;END IF;EXCEPTIONWHEN ERR_FINANCINGACCOUNT THENRETURN 0;WHEN OTHERS THENRETURN - 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 ISERR_FUNDACCOUNT EXCEPTION;PRAGMA EXCEPTION_INIT(ERR_FUNDACCOUNT, -1);V_COUNT NUMBER;BEGIN--约束条件检查SELECT COUNT(FINANCINGACCOUNT)INTO V_COUNTFROM FUNDACCOUNTWHERE FINANCINGACCOUNT = I_FINANCINGACCOUNTAND COMPANYID = I_COMPANYID;IF V_COUNT = 0 THENINSERT 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 THENRETURN 1;END IF;ELSERETURN - 1;END IF;EXCEPTIONWHEN ERR_FUNDACCOUNT THENRETURN 0;WHEN OTHERS THENRETURN - 1;END FUNC_ADD_FUNDACCOUNT;--基金账户的查询PROCEDURE PRO_QUERY_FUNDACCOUNT(O_RESULT OUT SYS_REFCURSOR,I_FINANCINGACCOUNT IN VARCHAR2) ASBEGINOPEN O_RESULT FORSELECT *FROM FUNDACCOUNT TWHERE T.FINANCINGACCOUNT = I_FINANCINGACCOUNT;END PRO_QUERY_FUNDACCOUNT;END FundAccountManager_pack;
巩固基础、方能应对复杂开发。
文档地址 百度文库,点击打开 。
如有问题,欢迎赐教。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30630063/viewspace-2215487/,如需转载,请注明出处,否则将追究法律责任。
以上是关于Oracle基础入门的主要内容,如果未能解决你的问题,请参考以下文章