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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

blog_sqlserver.sql 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. IF (OBJECT_ID('FK_posts_users', 'F') IS NOT NULL)
  2. BEGIN
  3. ALTER TABLE [posts] DROP CONSTRAINT [FK_posts_users]
  4. END
  5. GO
  6. IF (OBJECT_ID('FK_posts_categories', 'F') IS NOT NULL)
  7. BEGIN
  8. ALTER TABLE [posts] DROP CONSTRAINT [FK_posts_categories]
  9. END
  10. GO
  11. IF (OBJECT_ID('FK_post_tags_tags', 'F') IS NOT NULL)
  12. BEGIN
  13. ALTER TABLE [post_tags] DROP CONSTRAINT [FK_post_tags_tags]
  14. END
  15. GO
  16. IF (OBJECT_ID('FK_post_tags_posts', 'F') IS NOT NULL)
  17. BEGIN
  18. ALTER TABLE [post_tags] DROP CONSTRAINT [FK_post_tags_posts]
  19. END
  20. GO
  21. IF (OBJECT_ID('FK_comments_posts', 'F') IS NOT NULL)
  22. BEGIN
  23. ALTER TABLE [comments] DROP CONSTRAINT [FK_comments_posts]
  24. END
  25. GO
  26. IF (OBJECT_ID('events', 'U') IS NOT NULL)
  27. BEGIN
  28. DROP TABLE [events]
  29. END
  30. GO
  31. IF (OBJECT_ID('countries', 'U') IS NOT NULL)
  32. BEGIN
  33. DROP TABLE [countries]
  34. END
  35. GO
  36. IF (OBJECT_ID('users', 'U') IS NOT NULL)
  37. BEGIN
  38. DROP TABLE [users]
  39. END
  40. GO
  41. IF (OBJECT_ID('tags', 'U') IS NOT NULL)
  42. BEGIN
  43. DROP TABLE [tags]
  44. END
  45. GO
  46. IF (OBJECT_ID('posts', 'U') IS NOT NULL)
  47. BEGIN
  48. DROP TABLE [posts]
  49. END
  50. GO
  51. IF (OBJECT_ID('post_tags', 'U') IS NOT NULL)
  52. BEGIN
  53. DROP TABLE [post_tags]
  54. END
  55. GO
  56. IF (OBJECT_ID('comments', 'U') IS NOT NULL)
  57. BEGIN
  58. DROP TABLE [comments]
  59. END
  60. GO
  61. IF (OBJECT_ID('categories', 'U') IS NOT NULL)
  62. BEGIN
  63. DROP TABLE [categories]
  64. END
  65. GO
  66. IF (OBJECT_ID('tag_usage', 'V') IS NOT NULL)
  67. BEGIN
  68. DROP VIEW [tag_usage]
  69. END
  70. GO
  71. CREATE TABLE [categories](
  72. [id] [int] IDENTITY,
  73. [name] [nvarchar](max) NOT NULL,
  74. [icon] [varbinary](max) NULL,
  75. PRIMARY KEY CLUSTERED
  76. (
  77. [id] ASC
  78. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  79. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  80. GO
  81. SET ANSI_NULLS ON
  82. GO
  83. SET QUOTED_IDENTIFIER ON
  84. GO
  85. CREATE TABLE [comments](
  86. [id] [int] IDENTITY,
  87. [post_id] [int] NOT NULL,
  88. [message] [nvarchar](max) NOT NULL,
  89. PRIMARY KEY CLUSTERED
  90. (
  91. [id] ASC
  92. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  93. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  94. GO
  95. SET ANSI_NULLS ON
  96. GO
  97. SET QUOTED_IDENTIFIER ON
  98. GO
  99. CREATE TABLE [post_tags](
  100. [id] [int] IDENTITY,
  101. [post_id] [int] NOT NULL,
  102. [tag_id] [int] NOT NULL,
  103. PRIMARY KEY CLUSTERED
  104. (
  105. [id] ASC
  106. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  107. ) ON [PRIMARY]
  108. GO
  109. SET ANSI_NULLS ON
  110. GO
  111. SET QUOTED_IDENTIFIER ON
  112. GO
  113. CREATE TABLE [posts](
  114. [id] [int] IDENTITY,
  115. [user_id] [int] NOT NULL,
  116. [category_id] [int] NOT NULL,
  117. [content] [nvarchar](max) 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] TEXTIMAGE_ON [PRIMARY]
  123. GO
  124. SET ANSI_NULLS ON
  125. GO
  126. SET QUOTED_IDENTIFIER ON
  127. GO
  128. CREATE TABLE [tags](
  129. [id] [int] IDENTITY,
  130. [name] [nvarchar](max) NOT NULL,
  131. PRIMARY KEY CLUSTERED
  132. (
  133. [id] ASC
  134. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  135. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  136. GO
  137. SET ANSI_NULLS ON
  138. GO
  139. SET QUOTED_IDENTIFIER ON
  140. GO
  141. CREATE TABLE [users](
  142. [id] [int] IDENTITY,
  143. [username] [nvarchar](max) NOT NULL,
  144. [password] [nvarchar](max) NOT NULL,
  145. [location] [geometry] NULL,
  146. CONSTRAINT [PK_users] 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 [countries](
  157. [id] [int] IDENTITY,
  158. [name] [nvarchar](max) NOT NULL,
  159. [shape] [geometry] NOT NULL,
  160. CONSTRAINT [PK_countries] PRIMARY KEY CLUSTERED
  161. (
  162. [id] ASC
  163. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  164. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  165. GO
  166. SET ANSI_NULLS ON
  167. GO
  168. SET QUOTED_IDENTIFIER ON
  169. GO
  170. CREATE TABLE [events](
  171. [id] [int] IDENTITY,
  172. [name] [nvarchar](max) NOT NULL,
  173. [datetime] [datetime2](3) NOT NULL,
  174. CONSTRAINT [PK_events] PRIMARY KEY CLUSTERED
  175. (
  176. [id] ASC
  177. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  178. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  179. GO
  180. SET ANSI_NULLS ON
  181. GO
  182. SET QUOTED_IDENTIFIER ON
  183. GO
  184. CREATE VIEW [tag_usage]
  185. AS
  186. 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
  187. GO
  188. SET IDENTITY_INSERT [categories] ON
  189. GO
  190. INSERT [categories] ([id], [name], [icon]) VALUES (1, N'announcement', NULL)
  191. GO
  192. INSERT [categories] ([id], [name], [icon]) VALUES (2, N'article', NULL)
  193. GO
  194. SET IDENTITY_INSERT [categories] OFF
  195. GO
  196. SET IDENTITY_INSERT [comments] ON
  197. GO
  198. INSERT [comments] ([id], [post_id], [message]) VALUES (1, 1, N'great')
  199. GO
  200. INSERT [comments] ([id], [post_id], [message]) VALUES (2, 1, N'fantastic')
  201. GO
  202. INSERT [comments] ([id], [post_id], [message]) VALUES (3, 2, N'thank you')
  203. GO
  204. INSERT [comments] ([id], [post_id], [message]) VALUES (4, 2, N'awesome')
  205. GO
  206. SET IDENTITY_INSERT [comments] OFF
  207. GO
  208. SET IDENTITY_INSERT [post_tags] ON
  209. GO
  210. INSERT [post_tags] ([id], [post_id], [tag_id]) VALUES (1, 1, 1)
  211. GO
  212. INSERT [post_tags] ([id], [post_id], [tag_id]) VALUES (2, 1, 2)
  213. GO
  214. INSERT [post_tags] ([id], [post_id], [tag_id]) VALUES (3, 2, 1)
  215. GO
  216. INSERT [post_tags] ([id], [post_id], [tag_id]) VALUES (4, 2, 2)
  217. GO
  218. SET IDENTITY_INSERT [post_tags] OFF
  219. GO
  220. SET IDENTITY_INSERT [posts] ON
  221. GO
  222. INSERT [posts] ([id], [user_id], [category_id], [content]) VALUES (1, 1, 1, N'blog started')
  223. GO
  224. INSERT [posts] ([id], [user_id], [category_id], [content]) VALUES (2, 1, 2, N'It works!')
  225. GO
  226. SET IDENTITY_INSERT [posts] OFF
  227. GO
  228. SET IDENTITY_INSERT [tags] ON
  229. GO
  230. INSERT [tags] ([id], [name]) VALUES (1, N'funny')
  231. GO
  232. INSERT [tags] ([id], [name]) VALUES (2, N'important')
  233. GO
  234. SET IDENTITY_INSERT [tags] OFF
  235. GO
  236. SET IDENTITY_INSERT [users] ON
  237. GO
  238. INSERT [users] ([id], [username], [password], [location]) VALUES (1, N'user1', N'pass1', NULL)
  239. GO
  240. INSERT [users] ([id], [username], [password], [location]) VALUES (2, N'user2', N'pass2', NULL)
  241. GO
  242. SET IDENTITY_INSERT [users] OFF
  243. GO
  244. SET IDENTITY_INSERT [countries] ON
  245. GO
  246. INSERT [countries] ([id], [name], [shape]) VALUES (1, N'Left', N'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))')
  247. GO
  248. INSERT [countries] ([id], [name], [shape]) VALUES (2, N'Right', N'POLYGON ((70 10, 80 40, 60 40, 50 20, 70 10))')
  249. GO
  250. SET IDENTITY_INSERT [countries] OFF
  251. GO
  252. SET IDENTITY_INSERT [events] ON
  253. GO
  254. INSERT [events] ([id], [name], [datetime]) VALUES (1, N'Launch', N'2016-01-01 13:01:01.111')
  255. GO
  256. SET IDENTITY_INSERT [events] OFF
  257. GO
  258. ALTER TABLE [comments] WITH CHECK ADD CONSTRAINT [FK_comments_posts] FOREIGN KEY([post_id])
  259. REFERENCES [posts] ([id])
  260. GO
  261. ALTER TABLE [comments] CHECK CONSTRAINT [FK_comments_posts]
  262. GO
  263. ALTER TABLE [post_tags] WITH CHECK ADD CONSTRAINT [FK_post_tags_posts] FOREIGN KEY([post_id])
  264. REFERENCES [posts] ([id])
  265. GO
  266. ALTER TABLE [post_tags] CHECK CONSTRAINT [FK_post_tags_posts]
  267. GO
  268. ALTER TABLE [post_tags] WITH CHECK ADD CONSTRAINT [FK_post_tags_tags] FOREIGN KEY([tag_id])
  269. REFERENCES [tags] ([id])
  270. GO
  271. ALTER TABLE [post_tags] CHECK CONSTRAINT [FK_post_tags_tags]
  272. GO
  273. ALTER TABLE [posts] WITH CHECK ADD CONSTRAINT [FK_posts_categories] FOREIGN KEY([category_id])
  274. REFERENCES [categories] ([id])
  275. GO
  276. ALTER TABLE [posts] CHECK CONSTRAINT [FK_posts_categories]
  277. GO
  278. ALTER TABLE [posts] WITH CHECK ADD CONSTRAINT [FK_posts_users] FOREIGN KEY([user_id])
  279. REFERENCES [users] ([id])
  280. GO
  281. ALTER TABLE [posts] CHECK CONSTRAINT [FK_posts_users]
  282. GO