Advertisement
Guest User

db lab#2

a guest
Nov 16th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 10.60 KB | None | 0 0
  1. USE master
  2. GO
  3.  
  4. IF EXISTS (
  5.     SELECT name
  6.     FROM sys.databases
  7.     WHERE name = N'Nagaev' )
  8. ALTER DATABASE [Nagaev] set single_user with rollback immediate
  9. GO
  10.  
  11. IF EXISTS (
  12.     SELECT name
  13.     FROM sys.databases
  14.     WHERE name = N'Nagaev' )
  15. DROP DATABASE [Nagaev]
  16. GO
  17.  
  18. CREATE DATABASE [Nagaev]
  19. GO
  20.  
  21. USE [Nagaev]
  22. GO
  23.  
  24. CREATE TABLE region (
  25.     id TINYINT,
  26.     name NVARCHAR(30),
  27.     CONSTRAINT PK_region_id PRIMARY KEY (id)
  28. )
  29. GO
  30.  
  31. CREATE TABLE rcode (
  32.     region_id tinyint,
  33.     code tinyint,
  34.     CONSTRAINT PK_region_code PRIMARY KEY (code),
  35.     CONSTRAINT FK_region_id FOREIGN KEY (region_id) REFERENCES region(id) ON UPDATE CASCADE,
  36. )
  37. GO
  38.  
  39. CREATE TABLE color
  40. (
  41.     id TINYINT,
  42.     name NVARCHAR(20),
  43.     CONSTRAINT PK_color_id PRIMARY KEY (id)
  44. )
  45. GO
  46.  
  47. CREATE TABLE post (
  48.     id tinyint NOT NULL,
  49.     name varchar(20) NULL,
  50.     CONSTRAINT PK_post_id PRIMARY KEY (id),
  51. )
  52. GO
  53.  
  54. CREATE FUNCTION CorrectNumber (@num VARCHAR(30), @rcode tinyint)
  55. RETURNS int
  56. AS
  57. BEGIN
  58.     DECLARE @str_rcode tinyint
  59.     select @str_rcode = CONVERT(varchar, @rcode)
  60.     if (UPPER(@num) like '[АВЕКМНОРСТУХABEKMHOPCTYX][0-9][0-9][0-9][АВЕКМНОРСТУХABEKMHOPCTYX][АВЕКМНОРСТУХABEKMHOPCTYX]'
  61.         and @num not like '%000%'
  62.         and (@str_rcode like '[0-9][0-9]' or @str_rcode like '[127][0-9][0-9]'))
  63.         return 0
  64.     return -1
  65. END
  66. GO
  67.  
  68. CREATE FUNCTION CorrectRecord (@date DATETIME, @dir VARCHAR(1), @auto_number nvarchar(7), @auto_rcode tinyint, @id tinyint)
  69. RETURNS int
  70. AS
  71. BEGIN
  72.     if NOT EXISTS(
  73.         SELECT * from record
  74.         WHERE record.id != @id and record.auto_number = @auto_number and
  75.               record.auto_rcode = @auto_rcode
  76.         )
  77.         return 0
  78.  
  79.     if EXISTS(
  80.         SELECT * from record
  81.         INNER JOIN
  82.             (SELECT MAX(catchtime) as max_date, auto_number FROM record
  83.                 WHERE catchtime < @date and @auto_number = auto_number and @auto_rcode = auto_rcode
  84.                 GROUP BY auto_number) r
  85.         ON record.catchtime = r.max_date and record.auto_number = r.auto_number
  86.         WHERE @dir != record.direction
  87.         )
  88.         return 0
  89.  
  90.     return -1
  91. END
  92. GO
  93.  
  94. CREATE TABLE auto (
  95.     color_id tinyint NOT NULL,
  96.     number NVARCHAR(7) NOT NULL,
  97.     rcode tinyint NOT NULL,
  98.     owner_name NVARCHAR(100) NOT NULL,
  99.     CONSTRAINT PK_number PRIMARY KEY CLUSTERED
  100.     (
  101.         number ASC,
  102.         rcode ASC
  103.     ),
  104.     CONSTRAINT FK_region_code FOREIGN KEY (rcode) REFERENCES rcode(code) ON UPDATE CASCADE,
  105.     CONSTRAINT FK_color_id FOREIGN KEY (color_id) REFERENCES color(id) ON UPDATE CASCADE
  106. )
  107. GO
  108.  
  109. CREATE TABLE record (
  110.     id tinyint NOT NULL,
  111.     post_id tinyint NOT NULL,
  112.     auto_number nvarchar(7) NOT NULL,
  113.     auto_rcode tinyint,
  114.     catchtime DATETIME NOT NULL,
  115.     direction CHAR NOT NULL,
  116.     CONSTRAINT CK_direction CHECK (direction = '>' or direction = '<'),
  117.     CONSTRAINT CK_record CHECK (dbo.CorrectRecord(catchtime, direction, auto_number, auto_rcode, id) = 0),
  118.     CONSTRAINT PK_record_id PRIMARY KEY (id),
  119.     CONSTRAINT FK_post_id FOREIGN KEY (post_id) REFERENCES post(id) ON UPDATE CASCADE,
  120.     CONSTRAINT FK_auto_id FOREIGN KEY (auto_number, auto_rcode) REFERENCES auto(number, rcode) ON UPDATE CASCADE,
  121. )
  122. GO
  123.  
  124. CREATE TRIGGER on_insert_auto ON auto FOR INSERT AS
  125. BEGIN
  126.     IF EXISTS (
  127.     SELECT *
  128.     FROM inserted WHERE dbo.CorrectNumber(number, rcode) = -1
  129.     )
  130.     BEGIN
  131.         PRINT 'Некорректная запись'
  132.         ROLLBACK TRANSACTION
  133.     END
  134. END
  135. GO
  136.  
  137. CREATE FUNCTION GetAutoType (
  138.     @fromPost tinyint,
  139.     @toPost tinyint,
  140.     @fromRegion tinyint
  141. )
  142. RETURNS VARCHAR(30)
  143. AS
  144. BEGIN
  145.     if (@fromPost != @toPost AND @fromRegion != 1)
  146.         RETURN 'Транзитный'
  147.     if (@fromPost = @toPost AND @fromRegion != 1)
  148.         RETURN 'Иногородний'
  149.     if (@fromRegion = 1)
  150.         RETURN 'Местный'
  151.     RETURN 'Прочий'
  152. END
  153. GO
  154.  
  155. INSERT INTO region(id, name) VALUES
  156. (1, N'Свердловская область'),
  157. (2, N'Самарская область'),
  158. (3, N'Московская область'),
  159. (4, N'Челябинская область'),
  160. (5, N'Краснодарский край')
  161. GO
  162.  
  163. INSERT INTO rcode(region_id, code) VALUES
  164. (1, 66), (1, 96),
  165. (2, 63), (2, 163),
  166. (3, 50), (3, 150),
  167. (4, 74), (4, 174),
  168. (5, 23), (5, 93)
  169. GO
  170.  
  171. INSERT INTO color(id, name) VALUES
  172. (1, N'Красный'),
  173. (2, N'Белый'),
  174. (3, N'Синий'),
  175. (4, N'Черный'),
  176. (5, N'Серый')
  177. GO
  178.  
  179. INSERT INTO post(id, name) VALUES
  180. (1, 'Северный')
  181. ,(2, 'Западный')
  182. ,(3, 'Востоный')
  183. ,(4, 'Южный')
  184. ,(5, 'Резервный')
  185. GO
  186.  
  187. INSERT INTO auto (color_id, number, rcode, owner_name) VALUES
  188. (1, 'А123АА', 66, N'Артемов'),
  189. (1, 'В224АА', 63, N'Боровиков'),
  190. (1, 'С114АА', 50, N'Волгин'),
  191. (1, 'А213АА', 63, N'Грузде'),
  192. (1, 'В124АА', 163, N'Домашних'),
  193. (2, 'С223АА', 50, N'Жигалов'),
  194. (2, 'А113АА', 23, N'Зейналов'),
  195. (2, 'В214АА', 93, N'Клепинин'),
  196. (2, 'С123АА', 150, N'Ливанов'),
  197. (3, 'А214АА', 50, N'Миронов'),
  198. (3, 'В113АА', 63, N'Наймин'),
  199. (3, 'С224АА', 163, N'Проскурнин'),
  200. (3, 'А124АА', 96, N'Реванов'),
  201. (3, 'В213АА', 163, N'Сивой'),
  202. (4, 'С124АА', 66, N'Тарабанкин'),
  203. (4, 'А224АА', 174, N'Гайнанов'),
  204. (4, 'Н114АА', 163, N'Хоронякин'),
  205. (4, 'Т223АА', 150, N'Чалганов'),
  206. (5, 'Х123АА', 50, N'Вайс'),
  207. (5, 'А213АА', 74, N'Епифанов')
  208. GO
  209.  
  210. INSERT INTO record (id, post_id, auto_number, auto_rcode, catchtime, direction) VALUES
  211. (1, 1, 'А123АА', 66, N'20120618 10:34:08', N'>')
  212. ,(2, 2, 'А123АА', 66, N'20120618 10:54:09', N'<')
  213. ,(3, 2, 'В224АА', 63, N'20120618 10:10:00', N'>')
  214. ,(4, 1, 'В224АА', 63, N'20120618 10:34:09', N'<')
  215. ,(5, 3, 'С114АА', 50, N'20120618 11:12:13', N'>')
  216. ,(6, 4, 'С114АА', 50, N'20120618 11:59:12', N'<')
  217. ,(7, 4, 'А213АА', 63, N'20120618 10:34:08', N'>')
  218. ,(8, 3, 'А213АА', 63, N'20120618 13:37:15', N'<')
  219. ,(9, 5, 'В124АА', 163, N'20120618 09:01:05', N'>')
  220. ,(10, 1, 'В124АА', 163, N'20120618 10:13:49', N'<')
  221. ,(11, 1, 'С223АА', 50, N'20120618 12:34:08', N'>')
  222. ,(12, 1, 'С223АА', 50, N'20120618 12:44:09', N'<')
  223. ,(13, 2, 'А113АА', 23, N'20120618 13:34:08', N'>')
  224. ,(14, 2, 'А113АА', 23, N'20120618 15:21:09', N'<')
  225. ,(15, 3, 'В214АА', 93, N'20120618 10:11:18', N'>')
  226. ,(16, 3, 'В214АА', 93, N'20120618 21:56:21', N'<')
  227. ,(17, 4, 'С123АА', 150, N'20120618 12:34:08', N'>')
  228. ,(18, 5, 'С123АА', 150, N'20120618 13:34:09', N'<')
  229. ,(19, 5, 'А214АА', 50, N'20120618 14:14:14', N'>')
  230. ,(20, 1, 'А214АА', 50, N'20120618 14:34:14', N'<')
  231. ,(21, 2, 'В113АА', 63, N'20120618 15:34:08', N'>')
  232. ,(22, 2, 'В113АА', 63, N'20120618 16:37:38', N'<')
  233. ,(23, 1, 'С224АА', 163, N'20120618 10:34:08', N'>')
  234. ,(24, 2, 'С224АА', 163, N'20120618 11:36:09', N'<')
  235. ,(25, 1, 'А124АА', 96, N'20120618 14:39:59', N'>')
  236. ,(26, 3, 'А124АА', 96, N'20120618 15:34:59', N'<')
  237. ,(27, 1, 'В213АА', 163, N'20120618 17:34:08', N'>')
  238. ,(28, 3, 'В213АА', 163, N'20120618 23:34:09', N'<')
  239. ,(29, 5, 'С124АА', 66, N'20120618 18:24:00', N'>')
  240. ,(30, 1, 'С124АА', 66, N'20120618 19:34:03', N'<')
  241. ,(31, 2, 'А224АА', 174, N'20120618 16:22:08', N'>')
  242. ,(32, 1, 'А224АА', 174, N'20120618 20:51:09', N'<')
  243. ,(33, 2, 'Н114АА', 163, N'20120618 11:03:08', N'>')
  244. ,(34, 1, 'Н114АА', 163, N'20120618 19:33:09', N'<')
  245. ,(35, 3, 'Т223АА', 150, N'20120618 19:28:17', N'>')
  246. ,(36, 3, 'Т223АА', 150, N'20120618 19:54:49', N'<')
  247. ,(37, 1, 'Х123АА', 50, N'20120618 23:00:00', N'>')
  248. ,(38, 4, 'Х123АА', 50, N'20120618 23:59:59', N'<')
  249. ,(39, 5, 'А213АА', 74, N'20120618 20:20:18', N'>')
  250. ,(40, 4, 'А213АА', 74, N'20120618 22:28:41', N'<')
  251. --,(41, 5, 'А213АА', 74, N'20120618 10:34:20', N'<')
  252. GO
  253.  
  254. DECLARE @Temp TABLE
  255. (
  256.     from_city tinyint,
  257.     from_post tinyint,
  258.     to_post tinyint
  259. );
  260.  
  261. INSERT INTO
  262. @Temp
  263. SELECT rcode.region_id, rr1.post_id, rr2.post_id FROM auto a
  264.     JOIN record rr1 ON rr1.auto_number = a.number and rr1.auto_rcode = a.rcode AND rr1.direction = N'>'
  265.     JOIN record rr2 ON rr2.auto_number = a.number and rr2.auto_rcode = a.rcode AND rr2.direction = N'<'
  266.     JOIN rcode ON rcode.code = a.rcode
  267. GROUP BY a.owner_name, a.number, rr1.post_id, rr2.post_id, rcode.region_id
  268.  
  269. SELECT dbo.GetAutoType(from_post, to_post, from_city) as 'Тип', COUNT(*) as 'Количество' FROM @Temp
  270. GROUP BY dbo.GetAutoType(from_post, to_post, from_city)
  271. GO
  272.  
  273. DECLARE @Temp TABLE
  274. (
  275.     auto_number nvarchar(7),
  276.     auto_rcode tinyint,
  277.     from_city tinyint,
  278.     from_post tinyint,
  279.     to_post tinyint
  280. );
  281.  
  282. INSERT INTO
  283. @Temp
  284. SELECT a.number, a.rcode, rr3.region_id, rr1.post_id, rr2.post_id FROM auto a
  285.     JOIN record rr1 ON rr1.auto_number = a.number and rr1.auto_rcode = a.rcode AND rr1.direction = N'>'
  286.     JOIN record rr2 ON rr2.auto_number = a.number AND rr2.auto_rcode = a.rcode and rr2.direction = N'<'
  287.     JOIN rcode rr3 ON rr3.code = a.rcode
  288. GROUP BY a.owner_name, rr1.post_id, rr2.post_id, rr3.region_id, a.number, a.rcode
  289.  
  290. SELECT
  291.        a.owner_name as 'Владелец',
  292.        a.number + CAST(a.rcode AS VARCHAR) as 'Номер авто',
  293.        FORMAT (r1.catchtime, 'HH\:mm\:ss', 'ru-RU' ) as 'Время въезда',
  294.        FORMAT (r2.catchtime, 'HH\:mm\:ss', 'ru-RU' ) as 'Время выезда',
  295.        dbo.GetAutoType(from_post, to_post, from_city) as 'Тип',     
  296.        region.name as 'Родной регион'
  297. FROM @Temp t
  298.     JOIN auto a ON a.number = t.auto_number and a.rcode  = t.auto_rcode
  299.     JOIN record r1 ON r1.direction = '>' and r1.auto_number = t.auto_number and r1.auto_rcode = t.auto_rcode
  300.     JOIN record r2 ON r2.direction = '<' and r2.auto_number = t.auto_number and r2.auto_rcode = t.auto_rcode
  301.     JOIN rcode rcode ON rcode.code = a.rcode
  302.     JOIN region ON rcode.region_id = region.id
  303. WHERE dbo.GetAutoType(from_post, to_post, from_city) like '%'
  304. GROUP BY a.owner_name, r1.catchtime,  r2.catchtime, from_post, to_post,
  305.          from_city, region.name, a.number, a.rcode
  306. ORDER BY a.owner_name
  307. GO
  308.  
  309.  
  310.  
  311.  
  312. USE [Nagaev]
  313. GO
  314.  
  315. INSERT INTO auto (color_id, number, rcode, owner_name) VALUES
  316. (3, 'А21АА', 74, N'Сбиванов')
  317. GO
  318.  
  319. INSERT INTO auto (color_id, number, rcode, owner_name) VALUES
  320. (3, 'А212АG', 74, N'Сбиванов')
  321. GO
  322.  
  323. INSERT INTO auto (color_id, number, rcode, owner_name) VALUES
  324. (3, 'А000АA', 74, N'Сбиванов')
  325. GO
  326.  
  327. INSERT INTO auto (color_id, number, rcode, owner_name) VALUES
  328. (3, 'А100АA', 215, N'Сбиванов')
  329. GO
  330.  
  331. INSERT INTO record (id, post_id, auto_number, auto_rcode, catchtime, direction) VALUES
  332.  (41, 5, 'А213BC', 74, N'20120618 20:34:10', N'<')
  333. ,(42, 5, 'А213BC', 74, N'20120618 20:35:20', N'<')
  334. GO
  335.  
  336. INSERT INTO record (id, post_id, auto_number, auto_rcode, catchtime, direction) VALUES
  337.  (41, 5, 'А213BC', 74, N'20120618 20:34:10', N'>')
  338. ,(42, 5, 'А213BC', 74, N'20120618 20:35:20', N'>')
  339. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement