api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

blog_sqlserver.sql 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. IF (OBJECT_ID('FK_barcodes_products', 'F') IS NOT NULL)
  2. BEGIN
  3. ALTER TABLE [barcodes] DROP CONSTRAINT [FK_barcodes_products]
  4. END
  5. GO
  6. IF (OBJECT_ID('FK_posts_users', 'F') IS NOT NULL)
  7. BEGIN
  8. ALTER TABLE [posts] DROP CONSTRAINT [FK_posts_users]
  9. END
  10. GO
  11. IF (OBJECT_ID('FK_posts_categories', 'F') IS NOT NULL)
  12. BEGIN
  13. ALTER TABLE [posts] DROP CONSTRAINT [FK_posts_categories]
  14. END
  15. GO
  16. IF (OBJECT_ID('FK_post_tags_tags', 'F') IS NOT NULL)
  17. BEGIN
  18. ALTER TABLE [post_tags] DROP CONSTRAINT [FK_post_tags_tags]
  19. END
  20. GO
  21. IF (OBJECT_ID('FK_post_tags_posts', 'F') IS NOT NULL)
  22. BEGIN
  23. ALTER TABLE [post_tags] DROP CONSTRAINT [FK_post_tags_posts]
  24. END
  25. GO
  26. IF (OBJECT_ID('FK_comments_posts', 'F') IS NOT NULL)
  27. BEGIN
  28. ALTER TABLE [comments] DROP CONSTRAINT [FK_comments_posts]
  29. END
  30. GO
  31. IF (OBJECT_ID('barcodes', 'U') IS NOT NULL)
  32. BEGIN
  33. DROP TABLE [barcodes]
  34. END
  35. GO
  36. IF (OBJECT_ID('products', 'U') IS NOT NULL)
  37. BEGIN
  38. DROP TABLE [products]
  39. END
  40. GO
  41. IF (OBJECT_ID('events', 'U') IS NOT NULL)
  42. BEGIN
  43. DROP TABLE [events]
  44. END
  45. GO
  46. IF (OBJECT_ID('countries', 'U') IS NOT NULL)
  47. BEGIN
  48. DROP TABLE [countries]
  49. END
  50. GO
  51. IF (OBJECT_ID('users', 'U') IS NOT NULL)
  52. BEGIN
  53. DROP TABLE [users]
  54. END
  55. GO
  56. IF (OBJECT_ID('tags', 'U') IS NOT NULL)
  57. BEGIN
  58. DROP TABLE [tags]
  59. END
  60. GO
  61. IF (OBJECT_ID('posts', 'U') IS NOT NULL)
  62. BEGIN
  63. DROP TABLE [posts]
  64. END
  65. GO
  66. IF (OBJECT_ID('post_tags', 'U') IS NOT NULL)
  67. BEGIN
  68. DROP TABLE [post_tags]
  69. END
  70. GO
  71. IF (OBJECT_ID('comments', 'U') IS NOT NULL)
  72. BEGIN
  73. DROP TABLE [comments]
  74. END
  75. GO
  76. IF (OBJECT_ID('categories', 'U') IS NOT NULL)
  77. BEGIN
  78. DROP TABLE [categories]
  79. END
  80. GO
  81. IF (OBJECT_ID('tag_usage', 'V') IS NOT NULL)
  82. BEGIN
  83. DROP VIEW [tag_usage]
  84. END
  85. GO
  86. CREATE TABLE [categories](
  87. [id] [int] IDENTITY,
  88. [name] [nvarchar](max) NOT NULL,
  89. [icon] [varbinary](max) NULL,
  90. PRIMARY KEY CLUSTERED
  91. (
  92. [id] ASC
  93. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  94. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  95. GO
  96. SET ANSI_NULLS ON
  97. GO
  98. SET QUOTED_IDENTIFIER ON
  99. GO
  100. CREATE TABLE [comments](
  101. [id] [int] IDENTITY,
  102. [post_id] [int] NOT NULL,
  103. [message] [nvarchar](max) NOT NULL,
  104. PRIMARY KEY CLUSTERED
  105. (
  106. [id] ASC
  107. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  108. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  109. GO
  110. SET ANSI_NULLS ON
  111. GO
  112. SET QUOTED_IDENTIFIER ON
  113. GO
  114. CREATE TABLE [post_tags](
  115. [id] [int] IDENTITY,
  116. [post_id] [int] NOT NULL,
  117. [tag_id] [int] NOT NULL,
  118. PRIMARY KEY CLUSTERED
  119. (
  120. [id] ASC
  121. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  122. ) ON [PRIMARY]
  123. GO
  124. SET ANSI_NULLS ON
  125. GO
  126. SET QUOTED_IDENTIFIER ON
  127. GO
  128. CREATE TABLE [posts](
  129. [id] [int] IDENTITY,
  130. [user_id] [int] NOT NULL,
  131. [category_id] [int] NOT NULL,
  132. [content] [nvarchar](max) NOT NULL,
  133. PRIMARY KEY CLUSTERED
  134. (
  135. [id] ASC
  136. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  137. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  138. GO
  139. SET ANSI_NULLS ON
  140. GO
  141. SET QUOTED_IDENTIFIER ON
  142. GO
  143. CREATE TABLE [tags](
  144. [id] [int] IDENTITY,
  145. [name] [nvarchar](max) NOT NULL,
  146. PRIMARY KEY CLUSTERED
  147. (
  148. [id] ASC
  149. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  150. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  151. GO
  152. SET ANSI_NULLS ON
  153. GO
  154. SET QUOTED_IDENTIFIER ON
  155. GO
  156. CREATE TABLE [users](
  157. [id] [int] IDENTITY,
  158. [username] [nvarchar](max) NOT NULL,
  159. [password] [nvarchar](max) NOT NULL,
  160. [location] [geometry] NULL,
  161. CONSTRAINT [PK_users] PRIMARY KEY CLUSTERED
  162. (
  163. [id] ASC
  164. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  165. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  166. GO
  167. SET ANSI_NULLS ON
  168. GO
  169. SET QUOTED_IDENTIFIER ON
  170. GO
  171. CREATE TABLE [countries](
  172. [id] [int] IDENTITY,
  173. [name] [nvarchar](max) NOT NULL,
  174. [shape] [geometry] NOT NULL,
  175. CONSTRAINT [PK_countries] PRIMARY KEY CLUSTERED
  176. (
  177. [id] ASC
  178. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  179. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  180. GO
  181. SET ANSI_NULLS ON
  182. GO
  183. SET QUOTED_IDENTIFIER ON
  184. GO
  185. CREATE TABLE [events](
  186. [id] [int] IDENTITY,
  187. [name] [nvarchar](max) NOT NULL,
  188. [datetime] [datetime2](0) NOT NULL,
  189. [visitors] [int] NOT NULL,
  190. CONSTRAINT [PK_events] PRIMARY KEY CLUSTERED
  191. (
  192. [id] ASC
  193. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  194. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  195. GO
  196. SET ANSI_NULLS ON
  197. GO
  198. SET QUOTED_IDENTIFIER ON
  199. GO
  200. CREATE VIEW [tag_usage]
  201. AS
  202. SELECT top 100 PERCENT name, COUNT(name) AS [count] FROM tags, post_tags WHERE tags.id = post_tags.tag_id GROUP BY name ORDER BY [count] DESC, name
  203. GO
  204. SET ANSI_NULLS ON
  205. GO
  206. SET QUOTED_IDENTIFIER ON
  207. GO
  208. CREATE TABLE [products](
  209. [id] [int] IDENTITY,
  210. [name] [nvarchar](max) NOT NULL,
  211. [price] [decimal](10,2) NOT NULL,
  212. [properties] [xml] NOT NULL,
  213. [created_at] [datetime2](0) NOT NULL,
  214. [deleted_at] [datetime2](0) NULL,
  215. CONSTRAINT [PK_products] PRIMARY KEY CLUSTERED
  216. (
  217. [id] ASC
  218. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  219. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  220. GO
  221. SET ANSI_NULLS ON
  222. GO
  223. SET QUOTED_IDENTIFIER ON
  224. GO
  225. CREATE TABLE [barcodes](
  226. [id] [int] IDENTITY,
  227. [product_id] [int] NOT NULL,
  228. [hex] [nvarchar](max) NOT NULL,
  229. [bin] [varbinary](max) NOT NULL,
  230. CONSTRAINT [PK_barcodes] PRIMARY KEY CLUSTERED
  231. (
  232. [id] ASC
  233. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  234. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  235. GO
  236. SET IDENTITY_INSERT [categories] ON
  237. GO
  238. INSERT [categories] ([id], [name], [icon]) VALUES (1, N'announcement', NULL)
  239. GO
  240. INSERT [categories] ([id], [name], [icon]) VALUES (2, N'article', NULL)
  241. GO
  242. SET IDENTITY_INSERT [categories] OFF
  243. GO
  244. SET IDENTITY_INSERT [comments] ON
  245. GO
  246. INSERT [comments] ([id], [post_id], [message]) VALUES (1, 1, N'great')
  247. GO
  248. INSERT [comments] ([id], [post_id], [message]) VALUES (2, 1, N'fantastic')
  249. GO
  250. INSERT [comments] ([id], [post_id], [message]) VALUES (3, 2, N'thank you')
  251. GO
  252. INSERT [comments] ([id], [post_id], [message]) VALUES (4, 2, N'awesome')
  253. GO
  254. SET IDENTITY_INSERT [comments] OFF
  255. GO
  256. SET IDENTITY_INSERT [post_tags] ON
  257. GO
  258. INSERT [post_tags] ([id], [post_id], [tag_id]) VALUES (1, 1, 1)
  259. GO
  260. INSERT [post_tags] ([id], [post_id], [tag_id]) VALUES (2, 1, 2)
  261. GO
  262. INSERT [post_tags] ([id], [post_id], [tag_id]) VALUES (3, 2, 1)
  263. GO
  264. INSERT [post_tags] ([id], [post_id], [tag_id]) VALUES (4, 2, 2)
  265. GO
  266. SET IDENTITY_INSERT [post_tags] OFF
  267. GO
  268. SET IDENTITY_INSERT [posts] ON
  269. GO
  270. INSERT [posts] ([id], [user_id], [category_id], [content]) VALUES (1, 1, 1, N'blog started')
  271. GO
  272. INSERT [posts] ([id], [user_id], [category_id], [content]) VALUES (2, 1, 2, N'It works!')
  273. GO
  274. SET IDENTITY_INSERT [posts] OFF
  275. GO
  276. SET IDENTITY_INSERT [tags] ON
  277. GO
  278. INSERT [tags] ([id], [name]) VALUES (1, N'funny')
  279. GO
  280. INSERT [tags] ([id], [name]) VALUES (2, N'important')
  281. GO
  282. SET IDENTITY_INSERT [tags] OFF
  283. GO
  284. SET IDENTITY_INSERT [users] ON
  285. GO
  286. INSERT [users] ([id], [username], [password], [location]) VALUES (1, N'user1', N'pass1', NULL)
  287. GO
  288. INSERT [users] ([id], [username], [password], [location]) VALUES (2, N'user2', N'pass2', NULL)
  289. GO
  290. SET IDENTITY_INSERT [users] OFF
  291. GO
  292. SET IDENTITY_INSERT [countries] ON
  293. GO
  294. INSERT [countries] ([id], [name], [shape]) VALUES (1, N'Left', N'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))')
  295. GO
  296. INSERT [countries] ([id], [name], [shape]) VALUES (2, N'Right', N'POLYGON ((70 10, 80 40, 60 40, 50 20, 70 10))')
  297. GO
  298. SET IDENTITY_INSERT [countries] OFF
  299. GO
  300. SET IDENTITY_INSERT [events] ON
  301. GO
  302. INSERT [events] ([id], [name], [datetime], [visitors]) VALUES (1, N'Launch', N'2016-01-01 13:01:01', 0)
  303. GO
  304. SET IDENTITY_INSERT [events] OFF
  305. GO
  306. SET IDENTITY_INSERT [products] ON
  307. GO
  308. INSERT [products] ([id], [name], [price], [properties], [created_at]) VALUES (1, N'Calculator', N'23.01', N'<root type="object"><depth type="boolean">false</depth><model type="string">TRX-120</model><width type="number">100</width><height type="null" /></root>', '1970-01-01 01:01:01')
  309. GO
  310. SET IDENTITY_INSERT [products] OFF
  311. GO
  312. SET IDENTITY_INSERT [barcodes] ON
  313. GO
  314. INSERT [barcodes] ([id], [product_id], [hex], [bin]) VALUES (1, 1, N'00ff01', 0x00ff01)
  315. GO
  316. SET IDENTITY_INSERT [barcodes] OFF
  317. GO
  318. ALTER TABLE [comments] WITH CHECK ADD CONSTRAINT [FK_comments_posts] FOREIGN KEY([post_id])
  319. REFERENCES [posts] ([id])
  320. GO
  321. ALTER TABLE [comments] CHECK CONSTRAINT [FK_comments_posts]
  322. GO
  323. ALTER TABLE [post_tags] WITH CHECK ADD CONSTRAINT [FK_post_tags_posts] FOREIGN KEY([post_id])
  324. REFERENCES [posts] ([id])
  325. GO
  326. ALTER TABLE [post_tags] CHECK CONSTRAINT [FK_post_tags_posts]
  327. GO
  328. ALTER TABLE [post_tags] WITH CHECK ADD CONSTRAINT [FK_post_tags_tags] FOREIGN KEY([tag_id])
  329. REFERENCES [tags] ([id])
  330. GO
  331. ALTER TABLE [post_tags] CHECK CONSTRAINT [FK_post_tags_tags]
  332. GO
  333. ALTER TABLE [posts] WITH CHECK ADD CONSTRAINT [FK_posts_categories] FOREIGN KEY([category_id])
  334. REFERENCES [categories] ([id])
  335. GO
  336. ALTER TABLE [posts] CHECK CONSTRAINT [FK_posts_categories]
  337. GO
  338. ALTER TABLE [posts] WITH CHECK ADD CONSTRAINT [FK_posts_users] FOREIGN KEY([user_id])
  339. REFERENCES [users] ([id])
  340. GO
  341. ALTER TABLE [posts] CHECK CONSTRAINT [FK_posts_users]
  342. GO
  343. ALTER TABLE [barcodes] WITH CHECK ADD CONSTRAINT [FK_barcodes_products] FOREIGN KEY([product_id])
  344. REFERENCES [products] ([id])
  345. GO
  346. ALTER TABLE [barcodes] CHECK CONSTRAINT [FK_barcodes_products]
  347. GO