Π‘ΠΊΡΠΈΠΏΡΡ ΠΈ ΠΈΡ ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅
ΠΠ°Π±Π°Π²Π»ΡΠ΅ΠΌ Π½ΠΎΠ²ΡΠ΅ ΠΏΠΎΠ»Ρ Π² ΡΠ°Π±Π»ΠΈΡΡ ΠΈ ΡΠ²ΡΠ·ΡΠ²Π°Π΅ΠΌ ΠΈΡ Ρ ΡΠ°Π±Π»ΠΈΡΠ΅ΠΉ Π°Π΄ΡΠ΅Ρ*/. INSERT INTO row_order SELECT g. id_goods, o. id_order, o. quantity FROM orders o JOIN goods g USING (id_goods); 6. ΡΠ΄Π΅Π»Π°ΡΡ ΡΠ΅Π½Ρ Π½Π° Π΄ΠΎΠΌΠ΅Π½Π΅ Π΄Π΅Π½ΡΠ³ΠΈ ΠΈ ΡΡΠΎΠΈΠΌΠΎΡΡΡ Π² Π·Π°ΠΊΠ°Π·Π΅*/. Π£Π΄Π°Π»ΡΠ΅ΠΌ ΡΡΠΎΠ»Π±ΡΡ Π³ΠΎΡΠΎΠ΄Π° ΠΈ ΡΠ»ΠΈΡΡ ΠΈΠ· ΡΠ°Π±Π»ΠΈΡ client ΠΈ employee*/. INSERT INTO SPECIALIZATION (SPECIALIZATION, ID_SPECIALIZATION) VALUES ('ΠΠΊΡΠΏΠ΅Π΄ΠΈΡΠΎΡ', 4); WHERE a… Π§ΠΈΡΠ°ΡΡ Π΅ΡΡ >
Π‘ΠΊΡΠΈΠΏΡΡ ΠΈ ΠΈΡ ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ (ΡΠ΅ΡΠ΅ΡΠ°Ρ, ΠΊΡΡΡΠΎΠ²Π°Ρ, Π΄ΠΈΠΏΠ»ΠΎΠΌ, ΠΊΠΎΠ½ΡΡΠΎΠ»ΡΠ½Π°Ρ)
Π‘ΠΊΡΠΈΠΏΡ ΠΌΠΎΠ΄ΠΈΡΠΈΠΊΠ°ΡΠΈΠΈ ΠΠ
Π¦Π΅Π»Ρ ΠΌΠΎΠ΄ΠΈΡΠΈΠΊΠ°ΡΠΈΠΈ — ΡΠ»ΡΡΡΠ΅Π½ΠΈΠ΅ ΡΡΡΡΠΊΡΡΡΡ ΠΠ, ΠΎΠΏΡΠΈΠΌΠΈΠ·Π°ΡΠΈΡ Π΅Π΅ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΡ.
ΠΠ»Ρ ΡΠΎΠ³ΠΎ, ΡΡΠΎΠ±Ρ ΠΌΠΎΠ΄ΠΈΡΠΈΡΠΈΡΠΎΠ²Π°ΡΡ ΠΈΡΡ ΠΎΠ΄Π½ΡΡ Π±Π°Π·Ρ Π΄Π°Π½Π½ΡΡ Π±Π΅Π· ΠΏΠΎΡΠ΅ΡΠΈ ΠΈΠΌΠ΅ΡΡΠ΅ΠΉΡΡ Π² Π½Π΅ΠΉ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΠΈ, Π½Π΅ΠΎΠ±Ρ ΠΎΠ΄ΠΈΠΌΠΎ Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠ΅ Π΄Π΅ΠΉΡΡΠ²ΠΈΡ:
/*Π΄ΠΎΠΌΠ΅Π½ Π΄Π»Ρ Π΄Π΅Π½Π΅Π³*/.
CREATE DOMAIN money AS NUMERIC (15,2);
/*Π΄Π»Ρ ΠΏΠ΅ΡΠ΅ΡΠΈΡΠ»ΡΠ΅ΠΌΡΡ ΡΠΈΠΏΠΎΠ²*/.
CREATE DOMAIN transfer AS INTEGER CHECK (VALUE >= 0);
/**********************/.
/* 1. ΠΠ°ΠΊΠ°Π· — ΡΠΎΠ²Π°Ρ M ΠΊ N */.
/**********************/.
/* Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΡΠ°Π±Π»ΠΈΡΡ row_order (ΡΡΡΠΎΠΊΠ° Π·Π°ΠΊΠ°Π·Π°)*/.
CREATE TABLE Row_order (.
id_goods INTEGER NOT NULL,.
id_order INTEGER NOT NULL,.
quantity transfer NOT NULL);
ALTER TABLE row_order ADD CONSTRAINT XPKRow_order primary key (id_goods, id_order);
ALTER TABLE row_order ADD FOREIGN KEY (id_goods) REFERENCES goods (id_goods), ADD FOREIGN KEY (id_order) REFERENCES orders (id_order);
/*ΠΏΠ΅ΡΠ΅Π½ΠΎΡΠΈΠΌ ΠΊΠΎΠ»-Π²ΠΎ ΠΈ Π΄Π°Π½Π½ΡΠ΅ ΠΈΠ· ΡΠ°Π±Π»ΠΈΡ Π·Π°ΠΊΠ°Π· ΠΈ ΡΠΎΠ²Π°Ρ Π² ΡΠ°Π±Π»ΠΈΡΡ ΡΡΡΠΎΠΊΠ° Π·Π°ΠΊΠ°Π·Π°*/.
INSERT INTO row_order SELECT g. id_goods, o. id_order, o. quantity FROM orders o JOIN goods g USING (id_goods);
/*ΡΠ΄Π°Π»ΡΠ΅ΠΌ ΡΡΠΎΠ»Π±Π΅Ρ ΠΊΠΎΠ»-Π²ΠΎ ΠΈΠ· ΡΠ°Π±Π»ΠΈΡΡ Π·Π°ΠΊΠ°Π·*/.
ALTER TABLE orders DROP quantity;
/*ΡΠ°Π·ΡΡΠ²Π°Π΅ΠΌ ΡΠ²ΡΠ·Ρ*/.
ALTER TABLE orders DROP id_goods;
/************************************************************/.
/*2. ΠΠ²Π΅ΡΡΠΈ ΡΡΡΠ½ΠΎΡΡΡ ΠΠΎΡΡΠ°Π²ΠΊΠ° ΠΊΠΎΡΠΎΡΠ°Ρ ΠΎΠ±ΡΠ΅Π΄ΠΈΠ½ΡΠ΅Ρ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ Π·Π°ΠΊΠ°Π·Π° Π² ΠΎΠ΄Π½Ρ Π΄ΠΎΡΡΠ°Π²ΠΊΡ ΡΠΎΠ²Π°ΡΠ°*/.
/************************************************************/.
/*ΡΠΎΠ·Π΄Π°Π΄ΠΈΠΌ ΡΡΡΠ½ΠΎΡΡΡ ΠΏΠΎΡΡΠ°Π²ΠΊΠ° Delivery*/.
CREATE TABLE delivery (id_delivery INTEGER NOT NULL PRIMARY KEY,.
forwarding_agent INTEGER NOT NULL,.
driver INTEGER NOT NULL.
);
ALTER TABLE delivery ADD FOREIGN KEY (forwarding_agent) REFERENCES employee (id_employee),.
ADD FOREIGN KEY (driver) REFERENCES employee (id_employee);
/*ΡΠΎΠ·Π΄Π°Π΄ΠΈΠΌ ΡΠ°Π±Π»ΠΈΡΡ ΡΡΡΠΎΠΊΠ° ΠΏΠΎΡΡΠ°Π²ΠΊΠΈ (row_delivery)*/.
CREATE TABLE row_delivery (.
id_delivery INTEGER NOT NULL,.
id_order INTEGER NOT NULL);
ALTER TABLE row_delivery ADD CONSTRAINT XPKrow_delivery primary key (id_delivery, id_order);
ALTER TABLE row_delivery ADD FOREIGN KEY (id_delivery) REFERENCES delivery (id_delivery), ADD FOREIGN KEY (id_order) REFERENCES orders (id_order);
/* Π΄ΠΎΠ±Π°Π²ΠΈΠΌ Π½ΠΎΠ²ΡΡ ΡΡΡΠΎΠΊΡ Π² ΡΠ°Π±Π»ΠΈΡΡ ΠΏΠΎΡΡΠ°Π²ΠΊΠ°, ΡΠΏΠ΅ΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΡ ΡΠΊΡΠΏΠ΅Π΄ΠΈΡΠΎΡ ΠΈ Π½Π°Π·Π½Π°ΡΠΈΠΌ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΡΠΊΡΠΏΠ΅Π΄ΠΈΡΠΎΡΠ° Π½Π° ΠΏΠΎΡΡΠ°Π²ΠΊΡ*/.
INSERT INTO SPECIALIZATION (SPECIALIZATION, ID_SPECIALIZATION) VALUES ('ΠΠΊΡΠΏΠ΅Π΄ΠΈΡΠΎΡ', 4);
INSERT INTO EMPLOYEE (FIO, ID_EMPLOYEE, HOUSE_NUMBER, ID_STREET, ID_SPECIALIZATION) VALUES ('ΠΡΠ·Π½Π΅ΡΠΎΠ² Π.', 4, 10, 3, 4);
INSERT INTO DELIVERY (ID_DELIVERY, FORWARDING_AGENT, DRIVER) VALUES (1, 4, 1);
/*Π·Π°Π½Π΅ΡΠ΅ΠΌ Π² ΡΠ°Π±Π»ΠΈΡΡ ΡΡΡΠΎΠΊΠ° ΠΏΠΎΡΡΠ°Π²ΠΊΠΈ Π²ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅ ΠΊΠΎΡΠΎΡΡΠ΅ Π±ΡΠ»ΠΈ Π΄ΠΎ ΡΠ΅ΡΡΡΡΠΊΡΡΡΠΈΠ·Π°ΡΠΈΠΈ Π±Π΄*/.
INSERT INTO row_delivery (id_delivery, id_order) SELECT 1, o. id_order FROM orders o;
/***********************************************************/.
/*3. ΡΠ±ΡΠ°ΡΡ ΡΠ°Π±ΠΎΡΡΡ Π³ΡΡΠΏΠΏΡ ΠΈ Π² ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΠΈΠΈ Ρ ΡΠΎΠ»ΡΠΌΠΈ ΠΏΡΠΎΡΡΠ°Π²ΠΈΡΡ ΠΌΠ΅Π½Π΅Π΄ΠΆΠ΅ΡΠ° ΠΈ ΠΏΡΠΎΠ΄Π°Π²ΡΠ° Π² ΡΠ°Π±Π»ΠΈΡΠ΅ Π·Π°ΠΊΠ°Π·*/.
/***********************************************************/.
/*Π΄ΠΎΠ±Π°Π²ΠΈΠΌ ΡΡΠΎΠ»Π±ΡΡ Π² ΡΠ°Π±Π»ΠΈΡΡ Π·Π°ΠΊΠ°Π·*/.
ALTER TABLE orders.
ADD manager INTEGER,.
ADD seller INTEGER;
ALTER TABLE orders.
ADD FOREIGN KEY (seller) REFERENCES employee (id_employee),.
ADD FOREIGN KEY (manager) REFERENCES employee (id_employee);
/*ΠΠ°ΠΏΠΎΠ»Π½ΡΠ΅ΠΌ Π΄Π°Π½Π½ΡΠΌΠΈ Π½ΠΎΠ²ΡΠ΅ ΡΡΠΎΠ»Π±ΡΡ*/.
UPDATE orders o.
SET o. manager =.
(SELECT FIRST 1 e. id_employee.
FROM composition_group c, employee e, specialization s.
WHERE c. id_employee=e.id_employee AND e. id_specialization=s.id_specialization AND.
o.id_group=c.id_group AND s. specialization='ΠΠ΅Π½Π΅Π΄ΠΆΠ΅Ρ');
UPDATE orders o.
SET o. seller =.
(SELECT FIRST 1 e. id_employee.
FROM composition_group c, employee e, specialization s.
WHERE c. id_employee=e.id_employee AND e. id_specialization=s.id_specialization AND.
o.id_group=c.id_group AND s. specialization='ΠΡΠΎΠ΄Π°Π²Π΅Ρ');
/*ΡΠ΄Π°Π»ΡΠ΅ΠΌ id_group ΠΈ ΡΠ°Π±Π»ΠΈΡΡ composition_grop ΠΈ working_group*/.
ALTER TABLE orders DROP CONSTRAINT integ24,.
DROP id_group;
DROP TABLE composition_group;
DROP TABLE working_group;
/******************************************/.
/*4. ΡΠΏΠ΅ΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΡ ΠΏΠ΅ΡΠ΅ΠΈΠΌΠ΅Π½ΠΎΠ²Π°ΡΡ Π² Π΄ΠΎΠ»ΠΆΠ½ΠΎΡΡΡ*/.
/******************************************/.
ALTER TABLE employee DROP CONSTRAINT integ22;
CREATE TABLE post.
(id_post INTEGER NOT NULL PRIMARY KEY,.
postnm VARCHAR (20).
);
ALTER TABLE employee ALTER id_specialization TO id_post;
INSERT INTO post SELECT s. id_specialization, s. specialization FROM specialization s;
ALTER TABLE employee ADD FOREIGN KEY (id_post) REFERENCES post (id_post);
DROP TABLE specialization;
/***********************/.
/*5. Π²Π²Π΅ΡΡΠΈ ΡΠ°Π±Π»ΠΈΡΡ Π°Π΄ΡΠ΅Ρ*/.
/***********************/.
CREATE TABLE address.
(id_address INTEGER NOT NULL PRIMARY KEY,.
id_city INTEGER,.
id_street INTEGER,.
house_number SMALLINT,.
apartament_number SMALLINT);
ALTER TABLE address ADD FOREIGN KEY (id_city) REFERENCES city (id_city),.
ADD FOREIGN KEY (id_street) REFERENCES street (id_street);
CREATE SEQUENCE GEN_ADDRESS_ID;
ALTER SEQUENCE GEN_ADDRESS_ID RESTART WITH 1;
INSERT INTO address (id_address, id_city, id_street, house_number).
SELECT GEN_ID (gen_address_id, 1), c. id_city, c. id_street, c. house_number FROM client c;
INSERT INTO address (id_address, id_city, id_street, house_number).
SELECT GEN_ID (gen_address_id, 1),(SELECT c. id_city FROM city c WHERE c. city_name = 'Π ΡΠ·Π°Π½Ρ'), e. id_street, e. house_number FROM employee e;
/*Π΄Π°Π±Π°Π²Π»ΡΠ΅ΠΌ Π½ΠΎΠ²ΡΠ΅ ΠΏΠΎΠ»Ρ Π² ΡΠ°Π±Π»ΠΈΡΡ ΠΈ ΡΠ²ΡΠ·ΡΠ²Π°Π΅ΠΌ ΠΈΡ Ρ ΡΠ°Π±Π»ΠΈΡΠ΅ΠΉ Π°Π΄ΡΠ΅Ρ*/.
ALTER TABLE employee ADD id_address INTEGER,.
ADD FOREIGN KEY (id_address) REFERENCES address (id_address);
ALTER TABLE client ADD id_address INTEGER,.
ADD FOREIGN KEY (id_address) REFERENCES address (id_address);
UPDATE client c.
SET c. id_address =.
(SELECT a. id_address FROM address a.
WHERE a. id_city=c.id_city and a. id_street=c.id_street and a. house_number= c. house_number);
UPDATE employee e.
SET e. id_address =.
(SELECT a. id_address FROM address a.
WHERE a. id_city=(SELECT c. id_city FROM city c WHERE c. city_name = 'Π ΡΠ·Π°Π½Ρ').
AND a. id_street=e.id_street AND a. house_number= e. house_number);
/*ΡΠ΄Π°Π»ΡΠ΅ΠΌ ΡΡΠΎΠ»Π±ΡΡ Π³ΠΎΡΠΎΠ΄Π° ΠΈ ΡΠ»ΠΈΡΡ ΠΈΠ· ΡΠ°Π±Π»ΠΈΡ client ΠΈ employee*/.
ALTER TABLE employee DROP CONSTRAINT integ23,.
DROP id_street,.
DROP house_number;
ALTER TABLE client DROP CONSTRAINT integ18,.
DROP CONSTRAINT integ19,.
DROP id_city,.
DROP id_street,.
DROP house_number;
/*************************************************/.
/*6. ΡΠ΄Π΅Π»Π°ΡΡ ΡΠ΅Π½Ρ Π½Π° Π΄ΠΎΠΌΠ΅Π½Π΅ Π΄Π΅Π½ΡΠ³ΠΈ ΠΈ ΡΡΠΎΠΈΠΌΠΎΡΡΡ Π² Π·Π°ΠΊΠ°Π·Π΅*/.
/*************************************************/.
/*Π΄Π°Π±Π°Π²ΠΈΠΌ Π² ΡΠ°Π±Π»ΠΈΡΡ ΠΎrders ΡΡΠΎΠ»Π±Π΅Ρ cost (ΡΡΠΎΠΈΠΌΠΎΡΡΡ)*/.
ALTER TABLE orders ADD cost money ,.
ALTER cost SET DEFAULT 0;
ALTER TABLE goods ALTER price TYPE money;
/******************************************/.
/*7. ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²Π΅Π½Π½ΡΠ΅ Ρ Π°Ρ-ΠΊΠΈ Π½Π° Π΄ΠΎΠΌΠ΅Π½Π΅ Π½Π΅ ΠΎΡΡΠΈΡ.*/.
/******************************************/.
/*ΠΏΠΎΠ»Ρ Π½Π°Π»ΠΈΡΠΈΠ΅, ΠΊΠΎΠ»-Π²ΠΎ, Π½ΠΎΠΌΠ΅Ρ Π΄ΠΎΠΌΠ°, Π½ΠΎΠΌΠ΅Ρ ΠΊΠ²Π°ΡΡΠΈΡΡ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΠΈΠΌ Π½Π° Π΄ΠΎΠΌΠ΅Π½Π΅ Π½Π΅ ΠΎΡΡΠΈΡΠ°ΡΠ΅Π»ΡΠ½ΠΎ*/.
ALTER TABLE goods ALTER presence TYPE transfer;
ALTER TABLE row_order ALTER quantity TYPE transfer;
ALTER TABLE address ALTER house_number TYPE transfer,.
ALTER apartament_number TYPE transfer;