Oracle Basics

 Description: Fishing monarch yesterday found online document a real oracle project, the general content of the rough looked and felt a lot of knowledge solid enough, they followed the document knock again, in addition to the current mechanical code does not achieve, mainly related knowledge: create a table space, to create a user, to empower the user to create a table, the table to add the primary key and foreign key constraints to the table and add comments field, create a function, creation process, create sequence, create a trigger, create packages and the like, is the knowledge summarized as follows:

1. Create a table space


- create a table space CREATE TABLESPACE fund DATAFILE 'e: \ ORADATA \ fundd_file.dbf' SIZE 40M;

2. Create a user



- Create a user ID and password CREATE USER test_userIDENTIFIED BY test123456 DEFAULT TABLESPACE fund;


3. to empower users


- authorize GRANT CONNECT, RESOURCE TO test_user;


4. Create a table

--创建表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. Add primary key and foreign key constraints to the table

 

- Remove the existing primary key --ALTER TABLE Fund DROP CONSTRAINT PK_Fund; - add a primary key constraint ALTER TABLE Fund ADD CONSTRAINT PK_Fund_FundNo PRIMARY KEY (FundNo); - add foreign key constraint ALTER TABLE Fund ADD CONSTRAINT FK_Fund_CompanyId FOREIGN KEY (CompanyId) REFERENCES FundCompany (CompanyId);


6. Add comments to the table and field


 

- add comments to the table COMMENT ON TABLE Fund IS 'funds table'; - Field to add comments COMMENT ON COLUMN Fund.FundNo IS 'Fund Code ID, home key'; COMMENT ON COLUMN Fund.CompanyId IS 'fund company ID, CompanyId FundCompany foreign key references the table '; COMMENT ON COLUMN Fund.FundName iS' fund name '; COMMENT ON COLUMN Fund.Price iS' NAV '; COMMENT ON COLUMN Fund.FundType iS' type fund, 1 denotes open, 2 represents closed '; COMMENT ON COLUMN Fund.Invest IS' investment direction, a stock, bond 2, 3 currency, mixing 4 '; COMMENT ON COLUMN Fund.BuyLimit IS' purchase limit '; COMMENT ON COLUMN Fund.IsChange IS' whether convertible, 0 represents a non-conversion, a conversion may be represented '; COMMENT ON COLUMN Fund.YearRate iS' APR, must be a number between 0-1 '; COMMENT ON COLUMN Fund.ApplyDate iS' filing date '; COMMENT ON COLUMN Fund.State IS 'fund state 0 indicates normal, 1 indicates the freezing';

7. Create function


 

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. Create a sequence


 

- for the fund table (FundCompany) sequence to create the primary key CREATE SEQUENCE SQ_COMPANYIDINCREMENT BY 1 - plus each 1START WITH 1 - 1 from the start NOMAXVALUE - no maximum the NOCYCLE - has not accumulated cycle CACHE 10;

9. Create Trigger


 

CREATE OR REPLACE TRIGGER TR_FundBEFORE INSERT ON FundFOR EACH ROWBEGIN:NEW.FUNDNO := FUNC_NEXTID('V', 'SQ_FundNo.NEXTVAL', 6);END;

10. Create a package (the package comprises a function and procedure)


package:

 

/ * ------------------------------------------------ - create a package instructions - the package name: FundAccountManager_pack-- Description: create a procedure or function are realized, the fund account opening, funds account information inquiries. - Original: Jun Fishing - Date: 2016/3/10 - QQ: 954739353 ------------------------------ -------------------- * / CREATE OR REPLACE PACKAGE FundAccountManager_pack IS-- current account Opening 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; - Banking account Opening 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 ;

Inclusions:


 

CREATE OR REPLACE PACKAGE BODY FundAccountManager_pack IS ---- Current Account Opening / * ---------------------------------- Description ---------------- create a package - the package name: FundAccountManager_pack-- Description: create a procedure or function are realized, the fund account opening, funds account information inquiries. - Original: Jun Fishing - Date: 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); / * unique primary key constraint violation is -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,


Consolidate the foundation, in order to respond to complex development.

 Document addresses  Baidu library, click Open .

If questions, please enlighten me.

 

 

From "ITPUB blog" link: http: //blog.itpub.net/30630063/viewspace-2215487/, For reprint, please indicate the source, otherwise it will be held liable.                                             

Guess you like

Origin www.cnblogs.com/pungwe/p/11080810.html