应用场景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;