Oracle SQL Lesson (11)

schema(模式)
一个用户下一组对象的集合,一般与用户名一致。

视图

CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view
[(alias[, alias]...)]
AS subquery
[WITH CHECK OPTION [CONSTRAINT constraint]]
[WITH READ ONLY [CONSTRAINT constraint]];
create view empvu80 as select employee_id, last_name, salary from employees where department_id = 80;
desc empvu80;
grant create view to scott;
create view v1 as select * from dept;
select text from user_views where view_name='V1';

使用列别名

CREATE VIEW salvu50 AS SELECT employee_id ID_NUMBER, last_name NAME, salary*12 ANN_SALARY FROM employees WHERE department_id = 50;

复杂视图的操作限制
如果视图包含如下项,则不能删除行
-Group functions
-A GROUP BY clause
-The DISTINCT keyword
-The pseudocolumn ROWNUM keyword

如果视图包含如下项,则不能修改数据
-Group functions
-A GROUP BY clause
-The DISTINCT keyword
-The pseudocolumn ROWNUM keyword
-Columns defined by expressions

如果视图包含如下项,则不能增加数据
-Group functions
-A GROUP BY clause
-The DISTINCT keyword
-The pseudocolumn ROWNUM keyword
-Columns defined by expressions
-NOT NULL columns in the base tables that are not selected by the view

CREATE OR REPLACE VIEW empvu20 AS SELECT * FROM employees WHERE department_id = 20 WITH CHECK OPTION CONSTRAINT empvu20_ck ;

CREATE OR REPLACE VIEW empvu10(employee_number, employee_name, job_title) AS SELECT employee_id, last_name, job_id FROM employees WHERE department_id = 10 WITH READ ONLY ;

create table d as select * from dept;
create view v1 as select * from d where deptno=40 with check option;
insert into v1 values(50,'EDU','CHINA');
insert into v1 values(40,'EDU','CHINA');

ALTER VIEW V1 READ ONLY;
create or replace view v1 as select * from emp;

使用序列(sequence)

CREATE SEQUENCE sequence
[INCREMENT BY n]
[START WITH n]
[{MAXVALUE n | NOMAXVALUE}]
[{MINVALUE n | NOMINVALUE}]
[{CYCLE | NOCYCLE}]
[{CACHE n | NOCACHE}];

NEXTVAL must be issued for that sequence before CURRVAL contains a value.
第一次必须使用NEXTVAL.

CREATE SEQUENCE dept_deptid_seq INCREMENT BY 10 START WITH 120 MAXVALUE 9999 NOCACHE NOCYCLE;

ALTER SEQUENCE dept_deptid_seq INCREMENT BY 20 MAXVALUE 999999 NOCACHE NOCYCLE;

DROP SEQUENCE dept_deptid_seq;

使用索引
B树索引,位图索引
海量数据中查询少量数据。

CREATE [UNIQUE][BITMAP]INDEX index
ON table (column[, column]...);
CREATE INDEX emp_last_name_idx ON employees(last_name);

DROP INDEX emp_last_name_idx;

Index Creation Guidelines

建议创建索引
A column contains a wide range of values
A column contains a large number of null values
One or more columns are frequently used together in a where clause or a join condition
The table is large and most queries are expected to retrieve less than 2% to 4% of the rows in the table

不建议创建索引
The columns are not often used as a condition in the query
The table is small or most queries are expected to retrieve more than 2% to 4% of the rows in the table
The table is updated frequently
The indexed columns are referenced as part of an expression

同义词

CREATE [PUBLIC] SYNONYM synonym
FOR object;

只有sysdba才可以创建公共同义词。

conn scott/tiger;
create synonym e for emp;
select * from e;

conn / as sysdba
create public synonym e1 for scott.emp;
grant select on e1 to public;

conn hr/hr;
select * from e1;

conn / as sysdba;
select index_name from user_indexex where table_name='E';

自动创建的索引。

原文地址:https://www.cnblogs.com/thlzhf/p/3405044.html