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

Примеры

SQL> -- СОЗДАНИЕ ТАБЛИЦЫ

SQL> --

SQL> CREATE TABLE product (

2 product_id INT,

3 name VARCHAR2(25),

4 price NUMBER(6,2),

5 vat_tax NUMBER(2,2),

6 quantity INT);

Table created.

SQL>

SQL> --

SQL> -- ВСТАВКА ДАННЫХ

SQL> --

SQL> INSERT INTO product VALUES (1, 'CD-RW Disk', 20.00, 0.20, 100);

1 row created.

SQL> INSERT INTO product VALUES (2, 'CD-R Disk', 15.00, 0.20, 200);

1 row created.

SQL> INSERT INTO product VALUES (3, '3.5" Floppy Disk', 10.00, 0.20, 30);

1 row created.

SQL> INSERT INTO product VALUES (4, 'DVD-R Disk', 80.00, 0.20, 10);

1 row created.

SQL> INSERT INTO product VALUES (5, 'DVD+R Disk', 80.00, 0.20, 10);

1 row created.

SQL> INSERT INTO product VALUES (6, 'DVD-RW Disk', 300.00, 0.20, 5);

1 row created.

SQL> INSERT INTO product VALUES (7, 'DVD+RW Disk', 300.00, 0.20, 5);

1 row created.

SQL> INSERT INTO product VALUES (8, 'Red Ink Marker', 60.00, 0.20, 10);

1 row created.

SQL> INSERT INTO product VALUES (9, 'Green Ink Marker', 60.00, 0.20, 10);

1 row created.

SQL> INSERT INTO product VALUES (10, 'Blue Ink Marker', 60.00, 0.20, 10);

1 row created.

SQL> INSERT INTO product VALUES (11, 'Blue Ink Marker', 80.00, 0.20, NULL);

1 row created.

SQL> INSERT INTO product VALUES (12, 'Milk 1 lt.', 20.00, 0.12, NULL);

1 row created.

SQL> INSERT INTO product VALUES (13, 'Milk 3 lt.', 60.00, 0.12, NULL);

1 row created.

SQL> INSERT INTO product VALUES (14, 'Milk 3 lt.', 65.00, 0.12, 5);

1 row created.

SQL>

SQL> --

SQL> -- ФОРМАТИРОВАНИЕ РЕЗУЛЬТАТОВ РАБОТЫ SELECT

SQL> --

SQL>

SQL> -- использование констант для задания значений столбцов

SQL> SELECT name, '$', 10 FROM product;

NAME ' 10

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

CD-RW Disk $ 10

CD-R Disk $ 10

3.5" Floppy Disk $ 10

DVD-R Disk $ 10

DVD+R Disk $ 10

DVD-RW Disk $ 10

DVD+RW Disk $ 10

Red Ink Marker $ 10

Green Ink Marker $ 10

Blue Ink Marker $ 10

Blue Ink Marker $ 10

NAME ' 10

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

Milk 1 lt. $ 10

Milk 3 lt. $ 10

Milk 3 lt. $ 10

14 rows selected.

SQL>

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

SQL> SELECT name "Product Name", price * (1 + vat_tax) "Full Price" FROM product;

Product Name Full Price

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

CD-RW Disk 24

CD-R Disk 18

3.5" Floppy Disk 12

DVD-R Disk 96

DVD+R Disk 96

DVD-RW Disk 360

DVD+RW Disk 360

Red Ink Marker 72

Green Ink Marker 72

Blue Ink Marker 72

Blue Ink Marker 96

Product Name Full Price

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

Milk 1 lt. 22.4

Milk 3 lt. 67.2

Milk 3 lt. 72.8

14 rows selected.

SQL>

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

SQL> SELECT name || '-' || name, price FROM product;

NAME||'-'||NAME PRICE

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

CD-RW Disk-CD-RW Disk 20

CD-R Disk-CD-R Disk 15

3.5" Floppy Disk-3.5" Floppy Disk 10

DVD-R Disk-DVD-R Disk 80

DVD+R Disk-DVD+R Disk 80

DVD-RW Disk-DVD-RW Disk 300

DVD+RW Disk-DVD+RW Disk 300

Red Ink Marker-Red Ink Marker 60

Green Ink Marker-Green Ink Marker 60

Blue Ink Marker-Blue Ink Marker 60

Blue Ink Marker-Blue Ink Marker 80

NAME||'-'||NAME PRICE

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

Milk 1 lt.-Milk 1 lt. 20

Milk 3 lt.-Milk 3 lt. 60

Milk 3 lt.-Milk 3 lt. 65

14 rows selected.

SQL> SELECT name || '-' || name "Product-Product", price FROM product;

Product-Product PRICE

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

CD-RW Disk-CD-RW Disk 20

CD-R Disk-CD-R Disk 15

3.5" Floppy Disk-3.5" Floppy Disk 10

DVD-R Disk-DVD-R Disk 80

DVD+R Disk-DVD+R Disk 80

DVD-RW Disk-DVD-RW Disk 300

DVD+RW Disk-DVD+RW Disk 300

Red Ink Marker-Red Ink Marker 60

Green Ink Marker-Green Ink Marker 60

Blue Ink Marker-Blue Ink Marker 60

Blue Ink Marker-Blue Ink Marker 80

Product-Product PRICE

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

Milk 1 lt.-Milk 1 lt. 20

Milk 3 lt.-Milk 3 lt. 60

Milk 3 lt.-Milk 3 lt. 65

14 rows selected.

SQL> --

SQL> -- ВЫБОРКА ДАННЫХ (СТРОК) ИЗ ТАБЛИЦЫ СООТВЕТСТВУЮЩИХ НЕКОМУ КРИТЕРИЮ

SQL> --

SQL>

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

SQL> SELECT name, price FROM product WHERE price=10;

NAME PRICE

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

3.5" Floppy Disk 10

SQL> SELECT name, price FROM product WHERE price<10;

no rows selected

SQL> SELECT name, price FROM product WHERE price>10;

NAME PRICE

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

CD-RW Disk 20

CD-R Disk 15

DVD-R Disk 80

DVD+R Disk 80

DVD-RW Disk 300

DVD+RW Disk 300

Red Ink Marker 60

Green Ink Marker 60

Blue Ink Marker 60

Blue Ink Marker 80

Milk 1 lt. 20

NAME PRICE

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

Milk 3 lt. 60

Milk 3 lt. 65

Соседние файлы в папке lab2
  • #
    16.04.2013246.78 Кб63lab2.doc
  • #
    16.04.201365.37 Кб64lab2_sample.lst
  • #
    16.04.20139.29 Кб66lab2_sample.sql