-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delivery.sql
123 lines (105 loc) · 3.83 KB
/
Delivery.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
-- phpMyAdmin SQL Dump
-- version 4.0.10deb1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Mar 31, 2016 at 04:44 PM
-- Server version: 5.5.47-0ubuntu0.14.04.1
-- PHP Version: 5.6.19-1+deb.sury.org~trusty+1
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `Delivery`
--
-- --------------------------------------------------------
--
-- Table structure for table `Delivery_Points`
--
CREATE TABLE IF NOT EXISTS `Delivery_Points` (
`Point_ID` int(11) NOT NULL AUTO_INCREMENT,
`Note` text COLLATE utf8_unicode_ci,
`Total_Cost` double NOT NULL DEFAULT '0',
`identifier_order` text COLLATE utf8_unicode_ci,
`Street` text COLLATE utf8_unicode_ci NOT NULL,
`House` text COLLATE utf8_unicode_ci NOT NULL,
`Entry` text COLLATE utf8_unicode_ci,
`floor` int(11) DEFAULT NULL,
`flat` int(11) DEFAULT NULL,
`Latitude` double NOT NULL,
`Longitude` double NOT NULL,
`phone_number` bigint(14) NOT NULL,
`time_start` time DEFAULT NULL,
`time_end` time DEFAULT NULL,
`Delivery_Date` date NOT NULL,
`Order_Date` date NOT NULL,
PRIMARY KEY (`Point_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2 ;
--
-- Dumping data for table `Delivery_Points`
--
INSERT INTO `Delivery_Points` (`Point_ID`, `Note`, `Total_Cost`, `identifier_order`, `Street`, `House`, `Entry`, `floor`, `flat`, `Latitude`, `Longitude`, `phone_number`, `time_start`, `time_end`, `Delivery_Date`, `Order_Date`) VALUES
(1, NULL, 250, '20160321#1', 'улица Рафиева', '113', '5', 4, 159, 53.856689, 27.43383, 375297768637, '18:00:00', '20:15:00', '2016-03-22', '2016-03-21');
-- --------------------------------------------------------
--
-- Table structure for table `Orders`
--
CREATE TABLE IF NOT EXISTS `Orders` (
`Order_ID` int(11) NOT NULL AUTO_INCREMENT,
`Point_ID` int(11) NOT NULL,
`Description` text COLLATE utf8_unicode_ci NOT NULL,
`Cost` double NOT NULL,
PRIMARY KEY (`Order_ID`),
KEY `Point_ID` (`Point_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=22 ;
--
-- Dumping data for table `Orders`
--
INSERT INTO `Orders` (`Order_ID`, `Point_ID`, `Description`, `Cost`) VALUES
(2, 1, 'Товар №3', 352.14);
--
-- Triggers `Orders`
--
DROP TRIGGER IF EXISTS `Add_Order`;
DELIMITER //
CREATE TRIGGER `Add_Order` AFTER INSERT ON `Orders`
FOR EACH ROW BEGIN
UPDATE Delivery_Points SET Total_Cost = (Total_Cost + New.Cost) WHERE Point_Id = New.Point_id;
END
//
DELIMITER ;
DROP TRIGGER IF EXISTS `Delete_Order`;
DELIMITER //
CREATE TRIGGER `Delete_Order` AFTER DELETE ON `Orders`
FOR EACH ROW BEGIN
SET @total_cost := (Select Total_cost From Delivery_Points Where Point_Id = Old.Point_id);
IF ((@total_cost- Old.Cost) >= 0) THEN
UPDATE Delivery_Points SET Total_Cost = (Total_Cost-Old.Cost) WHERE Point_Id = Old.Point_id;
Else
UPDATE Delivery_Points SET Total_Cost = 0 WHERE Point_Id = Old.Point_id;
END IF;
END
//
DELIMITER ;
DROP TRIGGER IF EXISTS `Update_Order`;
DELIMITER //
CREATE TRIGGER `Update_Order` AFTER UPDATE ON `Orders`
FOR EACH ROW BEGIN
UPDATE Delivery_Points SET Total_Cost = (Total_Cost- Old.Cost + New.Cost) WHERE Point_Id = New.Point_id;
END
//
DELIMITER ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `Orders`
--
ALTER TABLE `Orders`
ADD CONSTRAINT `Orders_ibfk_1` FOREIGN KEY (`Point_ID`) REFERENCES `Delivery_Points` (`Point_ID`) ON DELETE CASCADE ON UPDATE CASCADE;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;