Oracle442個應用情境-----------資料庫邏輯對象管理 應用情境,資料庫邏輯設計

來源:互聯網
上載者:User

Oracle442個應用情境-----------資料庫邏輯對象管理 應用情境,資料庫邏輯設計


應用情境128:建立暫存資料表

建立一個儲存臨時選擇商品資訊的暫存資料表temp_goods

CREATE GLOBAL TEMPORARY TABLE temp_goods
(GoodsId   NUMBER,
GoodsNum  NUMBER,
Price   NUMBER)
ON COMMIT DELETE ROWS;

建立一個暫存資料表空間

CREATE TEMPORARY TABLESPACE tbs_t1
TEMPFILE 'tbs_t1.f' SIZE 50m REUSE AUTOEXTEND ON
MAXSIZE UNLIMITED
EXTENT MANAGEMENT LOCAL UNIFORM SIZE 64K;

在資料表空間tb_t1中建立temp_goods1;

CREATE GLOBAL TEMPORARY TABLE temp_goods1
(GoodsId   NUMBER,
GoodsNum  NUMBER,
Price   NUMBER)
ON COMMIT DELETE ROWS
TABLESPACE tbs_t1;

應用情境129:查看錶資訊的系統檢視表

COL TABLE_NAME FORMAT A10
COL COLUMN_NAME FORMAT A10
COL DATA_TYPE FORMAT A10
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, DATA_LENGTH, LAST_ANALYZED
FROM DBA_TAB_COLUMNS
WHERE TABLE_NAME = 'EMPLOYEES';

應用情境130:在Enterprise Manager中修改表

應用情境131使用DESC語句查看錶結果

查看錶employee結果的語句如下:

DESC SYSTEM.Employee

使用情境132:使用ALTER TABLE語句向表中添加列

ALTER TABLE SYSTEM.Departments ADD (Description VARCHAR2(1000));

應用情境133:使用ALTER TABLE語句修改列名

ALTER TABLE SYSTEM.Departments
RENAME COLUMN Description TO Descript;

應用情境134:使用ALTER TABLE語句刪除隊列

ALTER TABLE HRMAN.Departments
RENAME COLUMN Description TO Descript;

應用情境136:建立和驗證主鍵約束

ALTER TABLE HRMAN.Departments ADD (Description varchar2(1000));
DESC HRMAN.Departments;
ALTER TABLE HRMAN.Departments
SET UNUSED (Description);
DESC HRMAN.Departments;
刪除表中所有不可用列:
ALTER TABLE HRMAN.Departments
DROP UNUSED COLUMNS;


應用情境137:建立和驗證非空約束

建立表user2並指定username列和userpwd列為非空約束

CREATE TABLE HRMAN.Users2
  (UserId    NUMBER,
   UserName       VARCHAR2(40) NOT NULL,
   UserPwd        VARCHAR2(40) NOT NULL,
   CONSTRAINT PK_USERID PRIMARY KEY(UserId)
  );

將約束中的username列設定為 not null

ALTER TABLE HRMAN.Users MODIFY UserName NOT NULL;

使用下面的語句可以將列設定為空白:

ALTER TABLE HRMAN.Users MODIFY UserName NULL;

向表user2中插入資料器username值為null:

INSERT INTO HRMAN.Users2 (UserId, UserPwd) VALUES(1,'123456');


應用情境138:建立驗證唯一約束

在uername上建立唯一約束:

CREATE TABLE Users3
  (UserId  Number Primary Key,
   UserName       Varchar2(40) NOT NULL UNIQUE,
   UserPwd        Varchar2(40) NOT NULL
  );
建立user4,其結構與表user完全相同:
CREATE TABLE HRMAN.Users4
  (UserId    NUMBER PRIMARY KEY,
   UserName       VRCHAR2(40),
   UserPwd        VARCHAR2(40),
   CONSTRAINT UK_USERNAME UNIQUE(UserName)
  );

將表user的username列設定唯一約束:
ALTER TABLE HRMAN. Users
ADD CONSTRAINT UK_USERNAME1
UNIQUE(UserName);

使用insert向表user中插入兩條記錄使他們列值是重複的:username=test-》

INSERT INTO HRMAN.Users VALUES(100, 'test', 'test');
INSERT INTO HRMAN.Users VALUES(101, 'test', '1234');


應用情境139:建立驗證檢查約束

在username列上定義檢查約束:
CREATE TABLE HRMAN.Users5
  (UserId    number PRIMARY KEY,
   UserName       varchar2(40),
   UserPwd        varchar2(40)
   CONSTRAINT CK_USERPWD CHECK(LENGTH(UserPwd)>=6)
  );

使用alter建立檢查約束:

ALTER TABLE HRMAN. Users
ADD CONSTRAINT CK_USERPWD1 CHECK(LENGTH(UserPwd)>=6);

使用insert插入記錄是userpwd的列值得長度小於6:

INSERT INTO HRMAN.Users VALUES(102, 'user', 'pwd');

應用情境140:建立和驗證外鍵約束

為表departments的dep_id列和表employee的dep_id列建立外鍵約束fk_emp_depid:、

ALTER TABLE HRMAN.Employees
ADD CONSTRAINT FK_EMP_DEPID
FOREIGN KEY(Dep_id) REFERENCES HRMAN.Departments(Dep_id);

向表中插入一條記錄:

INSERT INTO HRMAN.Employees (Emp_id, Emp_name, Dep_id)
VALUES (1, 'Johney', 3);

應用情境141:設定DEFAULT列的屬性

建立表users6,設定userpwd的預設值為:11111:

CREATE TABLE HRMAN.Users6
  (UserId         Number Primary Key,
   UserName          Varchar2(40) NOT NULL UNIQUE,
   UserPwd           Varchar2(40) DEFAULT('111111')
   );

向表中插入一條記錄:

INSERT INTO HRMAN.Users6 (UserId, UserName) VALUES(1, 'user');

應用情境142:刪除表

DROP TABLE HRMAN.USERS;

應用情境143:在Enterprise Manager中查看和管理檢視

應用情境144:在Enterprise Manager中建立視圖

SELECT e.Emp_Id, e.Emp_Name, d.Dep_Name
FROM HRMAN.Employees e, HRMAN.Departments d
WHERE e.Dep_Id=d.Dep_Id

應用情境145:使用create view

建立視圖v_emp1

CREATE VIEW HRMAN.V_EMP
AS
SELECT e.Emp_Id, e.Emp_Name, d.Dep_Name
FROM HRMAN.Employees e, HRMAN.Departments d
WHERE e.Dep_Id=d.Dep_Id;

查看使用者hrman的視圖以及其定義的文本資訊:

SELECT VIEW_NAME, TEXT FROM DBA_VIEWS
WHERE OWNER='HRMAN';


應用情境146:修改視圖

修改視圖hrman.v_emp1:

CREATE OR REPLACE VIEW HRMAN.V_EMP
AS
SELECT e.Emp_Id, e.Emp_Name, e.Sex, e.Wage, d.Dep_Name
FROM HRMAN.Employees e, HRMAN.Departments d
WHERE e.Dep_Id=d.Dep_Id;

應用情境147:刪除視圖

DROP view HR.v_emp1;

應用情境148:在Enterprise Manager中的索引管理頁面

應用情境149:在Enterprise Manager中建立索引

應用情境150:

使用資料表空間users儲存索引:

CREATE INDEX HRMAN.IX_EMPNAME ON HRMAN.EMPLOYEES(EMP_NAME)
TABLESPACE Users;

為表HRMAN。Enmployees的列IDCard建立唯一索引:

CREATE INDEX HRMAN. UQ_IDCARD ON HRMAN.EMPLOYEES(IDCARD)
TABLESPACE Users;

查看hrman所擁有的視圖及其定義文本資訊:

SELECT INDEX_NAME, TABLE_NAME FROM DBA_INDEXES
WHERE OWNER='HRMAN';

應用情境151:修改索引

ALTER INDEX HRMAN.IX_EMPNAME UNUSABLE;

ALTER INDEX HRMAN.IX_EMPNAME REBUILD;

ALTER INDEX HRMAN.IX_EMPNAME RENAME TO IX_EMPNAME_1;

應用情境152:刪除索引

DROP INDEX HRMAN.IX_EMPNAME_1;

應用情境153:在Enterprise Manager中查看和管理實體化視圖

應用情境154:在Enterprise Manager中建立視圖

應用情境155:使用CREATE MATERIALIZED VIEW 語句建立實體化視圖

建立實體化mv_emp1:

CREATE MATERIALIZED VIEW HRMAN.MV_EMP1
REFRESH FORCE
ON DEMAND
AS
SELECT e.Emp_Id, e.Emp_Name, d.Dep_Name
FROM HRMAN.Employees e, HRMAN.Departments d
WHERE e.Dep_Id=d.Dep_Id;

應用情境156:修改視圖

將重新整理類型設定為CONPLETE:
ALTER MATERIALIZED VIEW HRMAN.MV_EMP
REFRESH COMPLETE
ON DEMAND;

應用情境157:刪除實體化視圖

DROP MATERIALIZED VIEW HRMAN.MV_EMP;

 

應用情境158:建立簇

CREATE CLUSTER HRMAN.HrCluster (dep_id NUMBER)
    PCTUSED 80            
    PCTFREE 5             
    SIZE 500              
    TABLESPACE users       
    STORAGE (INITIAL 200K
    NEXT 300K             
    MINEXTENTS 2         
    MAXEXTENTS UNLIMITED        
    PCTINCREASE 33);   

應用情境159:建立簇表

在簇HRMAN.HrCluster中建立DeptInfo:

CREATE TABLE HRMAN.DeptInfo
(
   Dep_id   NUMBER  PRIMARY KEY, 
   Dep_name      VARCHAR2(100) NOT NULL
)
CLUSTER HRMAN.HrCluster(Dep_id);


在HRMAN.HrCluster中建立表EmpInfo:

CREATE TABLE HRMAN.EmpInfo
  (
   Emp_id   NUMBER  PRIMARY KEY,
   Emp_name     VARCHAR2(50) NOT NULL,
   Sex        VARCHAR2(2),
   Title        VARCHAR2(50),
   Wage   NUMBER(8, 2),
   IdCard   VARCHAR2(20),
   Dep_id   NUMBER 
  )
CLUSTER HRMAN.HrCluster(Dep_id);

應用情境160:使用DBA_CLUSTERS視圖查看簇的資訊

查看HRMAN中的簇表列資訊:

COL CLUSTER_NAME FORMAT A20
COL OWNER FORMAT A20
COL TABLESPACE_NAME FORMAT A20
SELECT CLUSTER_NAME, OWNER, TABLESPACE_NAME, CLUSTER_TYPE FROM DBA_CLUSTERS;

應用情境161:使用DBA_CLU_COLUMNS視圖查看簇表的列資訊

COL OWNER FORMAT A10
COL CLUSTER_NAME FORMAT A15
COL CLU_COLUMN_NAME FORMAT A10
COL TABLE_NAME FORMAT A10
COL TAB_COLUMN_NAME FORMAT A15
SELECT * FROM DBA_CLU_COLUMNS
WHERE OWNER='HRMAN';

應用情境162:建立簇索引

在村HRMAN.HrCluster上建立簇索引:

CREATE INDEX HRMAN.IX_EMP_DEPT
    ON CLUSTER HRMAN.HrCluster
    TABLESPACE  users
    STORAGE (INITIAL 50K
    NEXT 50K             
    MINEXTENTS 2         
    MAXEXTENTS 10        
    PCTINCREASE 33);   

應用情境163:修改簇

ALTER CLUSTER HRMAN.HrCluster
    PCTUSED 60
    PCTFREE 30;

應用情境164:刪除簇

DROP CLUSTER HRMAN.HrCluster
INCLUDING TABLES
CASCADE CONSTRAINTS;


應用情境165:建立散列簇

在HRMAN方案上建立散列簇HashCluster:

CREATE TABLE HRMAN.Area
(
    AreaId      NUMBER(5, 0)  PRIMARY KEY,
    AreaName     VARCHAR2(200),
    UpperId  NUMBER(5, 0)
)  
CLUSTER HRMAN.HashCluster(AreaId);

在散列簇上建立表area用於儲存地區資訊

CREATE CLUSTER STCluster (Id NUMBER)
SIZE 512 SINGLE TABLE HASHKEYS 500;

應用情境166:控制散列簇的空間使用

應用情境167:估算散列簇所需要的空間大小

應用情境168:修改和刪除散列簇

應用情境169:使用DBA_CLUSTER_HASH_EXPRESSIONS視圖查看散列函數

SELECT * FROM DBA_CLUSTER_HASH_EXPRESSIONS;

應用情境170:建立序列

CREATE SEQUENCE HRMAN.EMP_S
MINVALUE 1
NOMAXVALUE
START WITH 1
INCREMENT BY 1
NOCYCLE
CACHE 20;

應用情境171:修改序列

ALTER SEQUENCE HRMAN.EMP_S
MAXVALUE 10000;

應用情境172:刪除序列

DROP SEQUENCE HRMAN.EMP_S;

應用情境173:序列的使用

CREATE SEQUENCE HRMAN.USER_S
MINVALUE 1
NOMAXVALUE
START WITH 1
INCREMENT BY 1
NOCYCLE
CACHE 20;

INSERT INTO HRMAN.USERS VALUES(HRMAN.USER_S.NEXTVAL, 'admin', 'pass');

應用情境174:建立同義字

CREATE PUBLIC SYNONYM HrEmployees FOR HRMAN.Employees;

應用情境175:使用系統檢視表DBA_SYNONYMS查看同義字的資訊

COL OWNER FORMAT A10
COL SYNONYM_NAME FORMAT A10
COL TABLE_OWNER FORMAT A10
COL TABLE_NAME FORMAT A10
COL DB_LINK FORMAT A10
SELECT * FROM DBA_SYNONYMS
WHERE TABLE_NAME='EMPLOYEES';

應用情境176:在DML語句中使用同義字

COL EMP_NAME FORMAT A20
COL TITLE FORMAT A10
SELECT Emp_name, Title, Wage FROM HrEmployees;


應用情境177:刪除同義字

DROP PUBLIC SYNONYM HrEmployees;

 

著作權聲明:歡迎轉載,希望在你轉載的同時,添加原文地址,謝謝配合

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.