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

1 Row created.

SQL>

SQL> -- разрешение существующих ограничений (в таблице есть запись

SQL> -- нарушающая это ограничение!)

SQL> ALTER TABLE salesperson ENABLE CONSTRAINT salesperson_unique_hire_date;

ALTER TABLE salesperson ENABLE CONSTRAINT salesperson_unique_hire_date

*

ERROR at line 1:

ORA-02293: cannot validate (SYSTEM.SALESPERSON_UNIQUE_HIRE_DATE) - check

constraint violated

SQL>

SQL> -- удалим строки с ошибками

SQL> DELETE salesperson WHERE hire_date <= TO_DATE('31/12/2002', 'DD/MM/YYYY');

1 row deleted.

SQL>

SQL> -- снова разрешим ограничение

SQL> ALTER TABLE salesperson ENABLE CONSTRAINT salesperson_unique_hire_date;

Table altered.

SQL>

SQL>

SQL> --

SQL> -- определение внешних ключей таблицы

SQL> --

SQL>

SQL> -- определение первичного ключа таблицы по полю salesperson_id

SQL> ALTER TABLE salesperson ADD PRIMARY KEY (salesperson_id);

Table altered.

SQL>

SQL> -- добавим в таблицу purchase опредение внешнего ключа - ссылку

SQL> -- на поле в таблице salesperson

SQL> ALTER TABLE purchase

2 ADD CONSTRAINT fk_purchase_to_salesperson

3 FOREIGN KEY (salesperson_id)

4 REFERENCES salesperson (salesperson_id);

ADD CONSTRAINT fk_purchase_to_salesperson

*

ERROR at line 2:

ORA-02298: cannot validate (SYSTEM.FK_PURCHASE_TO_SALESPERSON) - parent keys

not found

SQL>

SQL> -- удалим противоречивые записи из таблицы purchase

SQL> DELETE purchase

2 WHERE salesperson_id NOT IN (SELECT salesperson_id FROM salesperson);

1 row deleted.

SQL>

SQL> -- снова попробуем добавить внешний ключ

SQL> ALTER TABLE purchase

2 ADD CONSTRAINT fk_purchase_to_salesperson

3 FOREIGN KEY (salesperson_id)

4 REFERENCES salesperson (salesperson_id);

Table altered.

SQL>

SQL> --

SQL> -- ВЫБОРКА ДАННЫХ

SQL> --

SQL>

SQL> -- выборка из нескольких таблиц (декартово произведение таблиц)

SQL> SELECT * FROM salesperson, purchase;

SALESPERSON_ID FIRST_NAME LAST_NAME HIRE_DATE SALESPERSON_ID

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

PRODUCT_ID PURCHASE_D QUANTITY

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

10 Ivan Ivanov 2003-02-01 10

1 2004-06-15 9

11 Peter Petrov 2003-10-21 10

1 2004-06-15 9

12 Fedor Fedorov 2003-06-17 10

1 2004-06-15 9

SALESPERSON_ID FIRST_NAME LAST_NAME HIRE_DATE SALESPERSON_ID

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

PRODUCT_ID PURCHASE_D QUANTITY

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

13 John Johnson 2004-09-01 10

1 2004-06-15 9

10 Ivan Ivanov 2003-02-01 10

5 2004-06-15 1

11 Peter Petrov 2003-10-21 10

5 2004-06-15 1

SALESPERSON_ID FIRST_NAME LAST_NAME HIRE_DATE SALESPERSON_ID

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

PRODUCT_ID PURCHASE_D QUANTITY

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

12 Fedor Fedorov 2003-06-17 10

5 2004-06-15 1

13 John Johnson 2004-09-01 10

5 2004-06-15 1

10 Ivan Ivanov 2003-02-01 10

6 2004-06-16 2

SALESPERSON_ID FIRST_NAME LAST_NAME HIRE_DATE SALESPERSON_ID

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

PRODUCT_ID PURCHASE_D QUANTITY

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

11 Peter Petrov 2003-10-21 10

6 2004-06-16 2

12 Fedor Fedorov 2003-06-17 10

6 2004-06-16 2

13 John Johnson 2004-09-01 10

6 2004-06-16 2

SALESPERSON_ID FIRST_NAME LAST_NAME HIRE_DATE SALESPERSON_ID

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

PRODUCT_ID PURCHASE_D QUANTITY

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

10 Ivan Ivanov 2003-02-01 11

8 2004-07-14 3

11 Peter Petrov 2003-10-21 11

8 2004-07-14 3

12 Fedor Fedorov 2003-06-17 11

8 2004-07-14 3

SALESPERSON_ID FIRST_NAME LAST_NAME HIRE_DATE SALESPERSON_ID

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

PRODUCT_ID PURCHASE_D QUANTITY

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

13 John Johnson 2004-09-01 11

8 2004-07-14 3

10 Ivan Ivanov 2003-02-01 12

7 2004-04-07 5

11 Peter Petrov 2003-10-21 12

7 2004-04-07 5

SALESPERSON_ID FIRST_NAME LAST_NAME HIRE_DATE SALESPERSON_ID

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

PRODUCT_ID PURCHASE_D QUANTITY

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

12 Fedor Fedorov 2003-06-17 12

7 2004-04-07 5

13 John Johnson 2004-09-01 12

7 2004-04-07 5

10 Ivan Ivanov 2003-02-01 10

8 2004-09-25 1

SALESPERSON_ID FIRST_NAME LAST_NAME HIRE_DATE SALESPERSON_ID

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

PRODUCT_ID PURCHASE_D QUANTITY

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

11 Peter Petrov 2003-10-21 10

8 2004-09-25 1

12 Fedor Fedorov 2003-06-17 10

8 2004-09-25 1

13 John Johnson 2004-09-01 10

8 2004-09-25 1

SALESPERSON_ID FIRST_NAME LAST_NAME HIRE_DATE SALESPERSON_ID

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

PRODUCT_ID PURCHASE_D QUANTITY

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

10 Ivan Ivanov 2003-02-01 13

5 2004-07-08 7

11 Peter Petrov 2003-10-21 13

5 2004-07-08 7

12 Fedor Fedorov 2003-06-17 13

5 2004-07-08 7

SALESPERSON_ID FIRST_NAME LAST_NAME HIRE_DATE SALESPERSON_ID

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

PRODUCT_ID PURCHASE_D QUANTITY

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

13 John Johnson 2004-09-01 13

5 2004-07-08 7

10 Ivan Ivanov 2003-02-01 13

6 2004-07-08 1

11 Peter Petrov 2003-10-21 13

6 2004-07-08 1

SALESPERSON_ID FIRST_NAME LAST_NAME HIRE_DATE SALESPERSON_ID

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

PRODUCT_ID PURCHASE_D QUANTITY

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

12 Fedor Fedorov 2003-06-17 13

6 2004-07-08 1

13 John Johnson 2004-09-01 13

6 2004-07-08 1

32 rows selected.

SQL> SELECT * FROM salesperson, salesperson;

SELECT * FROM salesperson, salesperson

*

ERROR at line 1:

ORA-00918: column ambiguously defined

SQL>

SQL> -- применение псевдонимов таблиц

SQL> SELECT S.last_name, P.quantity FROM salesperson S, purchase P;

LAST_NAME QUANTITY

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

Ivanov 9

Petrov 9

Fedorov 9

Johnson 9

Ivanov 1

Petrov 1

Fedorov 1

Johnson 1

Ivanov 2

Petrov 2

Fedorov 2

LAST_NAME QUANTITY

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

Johnson 2

Ivanov 3

Petrov 3

Fedorov 3

Johnson 3

Ivanov 5

Petrov 5

Fedorov 5

Johnson 5

Ivanov 1

Petrov 1

LAST_NAME QUANTITY

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

Fedorov 1

Johnson 1

Ivanov 7

Petrov 7

Fedorov 7

Johnson 7

Ivanov 1

Petrov 1

Fedorov 1

Johnson 1

32 rows selected.

SQL>

SQL> -- "свяжем" таблицы по значению поля salesperson_id

SQL> SELECT *

2 FROM salesperson, purchase P

3 WHERE salesperson.salesperson_id = P.salesperson_id;

SALESPERSON_ID FIRST_NAME LAST_NAME HIRE_DATE SALESPERSON_ID

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

PRODUCT_ID PURCHASE_D QUANTITY

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

10 Ivan Ivanov 2003-02-01 10

1 2004-06-15 9

10 Ivan Ivanov 2003-02-01 10

5 2004-06-15 1

10 Ivan Ivanov 2003-02-01 10

6 2004-06-16 2

SALESPERSON_ID FIRST_NAME LAST_NAME HIRE_DATE SALESPERSON_ID

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

PRODUCT_ID PURCHASE_D QUANTITY

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

11 Peter Petrov 2003-10-21 11

8 2004-07-14 3

12 Fedor Fedorov 2003-06-17 12

7 2004-04-07 5

10 Ivan Ivanov 2003-02-01 10

8 2004-09-25 1

SALESPERSON_ID FIRST_NAME LAST_NAME HIRE_DATE SALESPERSON_ID

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

PRODUCT_ID PURCHASE_D QUANTITY

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

13 John Johnson 2004-09-01 13

5 2004-07-08 7

13 John Johnson 2004-09-01 13

6 2004-07-08 1

8 rows selected.

SQL> -- покажем все сделанные покупки

SQL> SELECT * FROM purchase;

SALESPERSON_ID PRODUCT_ID PURCHASE_D QUANTITY

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

10 1 2004-06-15 9

10 5 2004-06-15 1

10 6 2004-06-16 2

11 8 2004-07-14 3

12 7 2004-04-07 5

10 8 2004-09-25 1

13 5 2004-07-08 7

13 6 2004-07-08 1

8 rows selected.

SQL>

SQL> -- покажем все покупки и названия товаров

SQL> SELECT P.*, PRD.product_name

2 FROM purchase P, product PRD

3 WHERE P.product_id = PRD.product_id;

SALESPERSON_ID PRODUCT_ID PURCHASE_D QUANTITY PRODUCT_NAME

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

10 5 2004-06-15 1 Moloko

13 5 2004-07-08 7 Moloko

10 6 2004-06-16 2 Kefir

13 6 2004-07-08 1 Kefir

12 7 2004-04-07 5 Tvorog

11 8 2004-07-14 3 Sok

10 8 2004-09-25 1 Sok

7 rows selected.

SQL> -- покажем все покупки и названия товаров,

SQL> -- причем включим в результат и те товары,

SQL> -- покупки которых не осуществлялись (+)

SQL> SELECT P.*, PRD.product_name

2 FROM purchase P, product PRD

3 WHERE P.product_id = PRD.product_id (+);

SALESPERSON_ID PRODUCT_ID PURCHASE_D QUANTITY PRODUCT_NAME

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

10 1 2004-06-15 9

10 5 2004-06-15 1 Moloko

13 5 2004-07-08 7 Moloko

10 6 2004-06-16 2 Kefir

13 6 2004-07-08 1 Kefir

12 7 2004-04-07 5 Tvorog

11 8 2004-07-14 3 Sok

10 8 2004-09-25 1 Sok

8 rows selected.

SQL> DROP TABLE purchase;

Table dropped.

SQL> DROP TABLE salesperson;

Table dropped.

SQL> DROP TABLE product;

Table dropped.

SQL>

Соседние файлы в папке lab4
  • #
    16.04.2013137.73 Кб47lab4.doc
  • #
    16.04.201330.7 Кб46lab4_sample.lst
  • #
    16.04.20135.19 Кб45lab4_sample.sql
  • #
    16.04.20131.82 Кб46lab4_schema.sql