Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
17
Добавлен:
05.06.2015
Размер:
385.02 Кб
Скачать

Раздел 6 Другие объекты базы данных

6.1. Последовательности SEQUENCE

Q6_1

SQL> create sequence DEPTNO_seq

increment by 10

start with 10

maxvalue 10000;

Sequencecreated.

Q6_2

Генерация очередного значения последовательности

SQL> select DEPTNO_seq.nextval from dual;

NEXTVAL

---------

10

Q6_3

Выбор очередного и текущего значения последовательности в одной SQLкоманде

SQL> select DEPTNO_seq.nextval,

DEPTNO_seq.currval from dual;

NEXTVAL CURRVAL

--------- ---------

  1. 20

Q6_4

Изменение параметров последовательности

SQL> alter sequence DEPTNO_seq

increment by 5

maxvalue 1000;

Sequence altered.

Q6_5

Удаление последовательности

SQL> drop sequence DEPTNO_seq;

Sequencedropped.

6.2. Создание представления с ограничением применяемых к нему команд INSERT и UPDATE

Q6_6

SQL> create view d10emp as

select EMPNO,ename,DEPTNO

from EMP

where DEPTNO=10

with check option constraint d10emp_invalid;

View created.

SQL> select * from d10emp;

EMPNO ENAME DEPTNO

--------- ------------------------- ----------

7839 KING 10

7782 CLARK 10

7934 MILLER10

Q6_7

Создание представления с запретом изменений данных через него

SQL> create view emp_DEPTNO as

select EMPNO,ename,job,mgr,sal,d.DEPTNO,dname

from EMP e, DEPTNO d

where e.DEPTNO(+) = d.DEPTNO

with read only;

View created.

SQL> select * from emp_DEPTNO;

EMPNO ENAME JOB MGR SAL DEPTNO DNAME

--------- ------------------------- --------- --------- --------- ---------- --------------

7839 KING PRESIDENT 5000 10 ACCOUNTING

7782 CLARK MANAGER 7839 2450 10 ACCOUNTING

7934 MILLER CLERK 7782 1300 10 ACCOUNTING

7566 JONES MANAGER 7839 2975 20 RESEARCH

7788 SCOTT ANALYST 7566 3000 20 RESEARCH

7876 ADAMS CLERK 7788 1100 20 RESEARCH

7369 SMITH CLERK 7902 800 20 RESEARCH

7902 FORD ANALYST 7566 3000 20 RESEARCH

7658 BUGSON ANALYST 7566 3000 20 RESEARCH

7698 BLAKE MANAGER 7839 2850 30 SALES

7654 MARTIN SALESMAN 7698 1250 30 SALES

7499 ALLEN SALESMAN 7698 1600 30 SALES

7844 TURNER SALESMAN 7698 1500 30 SALES

7900 JAMES CLERK 7698 950 30 SALES

7521 WARD SALESMAN 7698 1250 30 SALES

8000 COOPER MANAGER 2300 40 OPERATIONS

16 rows selected.

Q6_8

Удаление представлений

SQL> drop view emp_DEPTNO;

View dropped.

SQL> drop view d10emp;

View dropped.

6.3.Создание индексов

Q6_9

SQL> create index i_emp_sal_comm on EMP (sal,comm);

Index created.

SQL> create unique index i_DEPTNO_unq_dname on DEPTNO (dname);

Indexcreated.

Q6_10

Запрос с использованием индекса i_DEPTNO_unq_dname

SQL> select * from DEPTNO

where dname like 'S%';

DEPTNO DNAME LOC

---------- -------------- -------------

30 SALES CHICAGO

Q6_11

Проверка структуры идекса

SQL> analyze index i_emp_sal_comm validate structure;

Index analyzed.

Q6_12

Удаление индексов

SQL> drop index i_emp_sal_comm;

Index dropped.

SQL> drop index i_DEPTNO_unq_dname;

Indexdropped.

6.4. Кластеры Clusters

Q6_13

Создание кластера, содержащего таблицы EMPиDEPTNO

SQL> create cluster personnel (dep_number number(2))

storage (initial 100k next 50k pctincrease 10);

Cluster created.

SQL> create table DEPT_kl(

DEPTNO number(2) not null,

dname varchar2(13),

loc varchar2(9),

constraint DEPTNO_pk primary key (DEPTNO))

cluster personnel(DEPTNO);

Table created.

SQL> create table emp_kl (

empno number(4) not null,

ename varchar2(10),

job varchar2(9),

mgr number(4) constraint emp_self_key references emp_kl(empno),

hiredate date,

sal number(7,2),

comm number(7,2),

DEPTNO number(2) not null,

constraint emp_fk foreign key (DEPTNO) references DEPT_kl(DEPTNO),

constraint emp_pk primary key (empno))

cluster personnel (DEPTNO);

Table created.

Q6_14

Создание индекса кластера

SQL> create index idx_personnel on cluster personnel;

Index created.

scott@8i> insert into dept_kl

select * from dept;

4 rows created.

scott@8i> insert into emp_kl

select * from emp;

14 rows created.

Q6_15

Удаление кластера, всех таблиц, принадлежащих кластеру и всех ограничений целостности, не принадлежащих ему

SQL> drop cluster personnel

including tables cascade constraints;

Cluster dropped.

6.5. Синонимы SYNONYMS

Q6_16

Создание синонима к таблице COMM

SQL> create synonym commission for comm;

Synonym created.

SQL> select * from commission;

EMPNO COMM

--------- ---------

7499 1100

7654 500

7844 500

7844 1500

Q6_17

Удаление синонима

SQL> drop synonym commission;

Synonym dropped.

6.6. Создание снимка и выборка данных из него через представление

Q6_18

SQL> create snapshot snap_DEPTNO as

select * from DEPT;

Snapshot created.

SQL> create view DEPTNO as

select * from snap_DEPTNO

with read only;

View created.

SQL> select * from DEPTNO;

DEPTNO DNAME LOC

---------- -------------- -------------

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

40 OPERATIONS BOSTON