Advertisement
rajrao

get-records-that-have-all-sub-records

Aug 18th, 2015
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 7.02 KB | None | 0 0
  1. USE [TestDb]
  2. GO
  3. CREATE TABLE [dbo].[Medication_Symptom](
  4.     [Id] [INT] NOT NULL,
  5.     [MedicineId] [INT] NOT NULL,
  6.     [SymptomId] [INT] NOT NULL,
  7.  CONSTRAINT [PK_MedicineSymptom] PRIMARY KEY CLUSTERED
  8. (
  9.     [Id] ASC
  10. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
  11. ) ON [PRIMARY]
  12.  
  13. GO
  14. CREATE TABLE [dbo].[Medicine](
  15.     [Id] [INT] NOT NULL,
  16.     [Name] [NCHAR](10) NOT NULL,
  17.  CONSTRAINT [PK_Medicine] PRIMARY KEY CLUSTERED
  18. (
  19.     [Id] ASC
  20. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
  21. ) ON [PRIMARY]
  22.  
  23. GO
  24. CREATE TABLE [dbo].[Patient](
  25.     [Id] [INT] NOT NULL,
  26.     [Name] [NCHAR](10) NOT NULL,
  27.  CONSTRAINT [PK_Patient] PRIMARY KEY CLUSTERED
  28. (
  29.     [Id] ASC
  30. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
  31. ) ON [PRIMARY]
  32.  
  33. GO
  34. CREATE TABLE [dbo].[Patient_Symptom](
  35.     [Id] [INT] NOT NULL,
  36.     [PatientId] [INT] NOT NULL,
  37.     [SymptomId] [INT] NOT NULL,
  38.  CONSTRAINT [PK_PatientSymptom] PRIMARY KEY CLUSTERED
  39. (
  40.     [Id] ASC
  41. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
  42. ) ON [PRIMARY]
  43.  
  44. GO
  45. CREATE TABLE [dbo].[Symptom](
  46.     [Id] [INT] NOT NULL,
  47.     [Name] [NCHAR](10) NOT NULL,
  48.  CONSTRAINT [PK_Symptom] PRIMARY KEY CLUSTERED
  49. (
  50.     [Id] ASC
  51. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
  52. ) ON [PRIMARY]
  53.  
  54. GO
  55. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (1, 1, 1)
  56. GO
  57. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (2, 1, 2)
  58. GO
  59. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (3, 2, 3)
  60. GO
  61. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (4, 2, 4)
  62. GO
  63. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (5, 3, 5)
  64. GO
  65. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (6, 4, 1)
  66. GO
  67. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (7, 4, 3)
  68. GO
  69. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (8, 4, 4)
  70. GO
  71. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (9, 5, 6)
  72. GO
  73. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (10, 5, 7)
  74. GO
  75. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (11, 6, 1)
  76. GO
  77. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (12, 6, 3)
  78. GO
  79. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (13, 7, 1)
  80. GO
  81. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (14, 7, 2)
  82. GO
  83. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (15, 7, 3)
  84. GO
  85. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (16, 7, 4)
  86. GO
  87. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (17, 7, 5)
  88. GO
  89. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (18, 7, 6)
  90. GO
  91. INSERT [dbo].[Medication_Symptom] ([Id], [MedicineId], [SymptomId]) VALUES (19, 7, 7)
  92. GO
  93. INSERT [dbo].[Medicine] ([Id], [Name]) VALUES (1, N'MedA      ')
  94. GO
  95. INSERT [dbo].[Medicine] ([Id], [Name]) VALUES (2, N'MedB      ')
  96. GO
  97. INSERT [dbo].[Medicine] ([Id], [Name]) VALUES (3, N'MedC      ')
  98. GO
  99. INSERT [dbo].[Medicine] ([Id], [Name]) VALUES (4, N'MedD      ')
  100. GO
  101. INSERT [dbo].[Medicine] ([Id], [Name]) VALUES (5, N'MedE      ')
  102. GO
  103. INSERT [dbo].[Medicine] ([Id], [Name]) VALUES (6, N'MedF      ')
  104. GO
  105. INSERT [dbo].[Medicine] ([Id], [Name]) VALUES (7, N'MedAll    ')
  106. GO
  107. INSERT [dbo].[Patient] ([Id], [Name]) VALUES (1, N'PA        ')
  108. GO
  109. INSERT [dbo].[Patient] ([Id], [Name]) VALUES (2, N'PB        ')
  110. GO
  111. INSERT [dbo].[Patient] ([Id], [Name]) VALUES (3, N'PC        ')
  112. GO
  113. INSERT [dbo].[Patient] ([Id], [Name]) VALUES (4, N'PD        ')
  114. GO
  115. INSERT [dbo].[Patient] ([Id], [Name]) VALUES (5, N'PE        ')
  116. GO
  117. INSERT [dbo].[Patient] ([Id], [Name]) VALUES (6, N'PF        ')
  118. GO
  119. INSERT [dbo].[Patient] ([Id], [Name]) VALUES (7, N'PAll      ')
  120. GO
  121. INSERT [dbo].[Patient_Symptom] ([Id], [PatientId], [SymptomId]) VALUES (1, 1, 1)
  122. GO
  123. INSERT [dbo].[Patient_Symptom] ([Id], [PatientId], [SymptomId]) VALUES (2, 1, 2)
  124. GO
  125. INSERT [dbo].[Patient_Symptom] ([Id], [PatientId], [SymptomId]) VALUES (3, 2, 3)
  126. GO
  127. INSERT [dbo].[Patient_Symptom] ([Id], [PatientId], [SymptomId]) VALUES (4, 2, 4)
  128. GO
  129. INSERT [dbo].[Patient_Symptom] ([Id], [PatientId], [SymptomId]) VALUES (5, 3, 5)
  130. GO
  131. INSERT [dbo].[Patient_Symptom] ([Id], [PatientId], [SymptomId]) VALUES (6, 5, 1)
  132. GO
  133. INSERT [dbo].[Patient_Symptom] ([Id], [PatientId], [SymptomId]) VALUES (7, 5, 3)
  134. GO
  135. INSERT [dbo].[Patient_Symptom] ([Id], [PatientId], [SymptomId]) VALUES (8, 6, 7)
  136. GO
  137. INSERT [dbo].[Patient_Symptom] ([Id], [PatientId], [SymptomId]) VALUES (9, 7, 1)
  138. GO
  139. INSERT [dbo].[Patient_Symptom] ([Id], [PatientId], [SymptomId]) VALUES (10, 7, 2)
  140. GO
  141. INSERT [dbo].[Patient_Symptom] ([Id], [PatientId], [SymptomId]) VALUES (11, 7, 3)
  142. GO
  143. INSERT [dbo].[Patient_Symptom] ([Id], [PatientId], [SymptomId]) VALUES (12, 7, 4)
  144. GO
  145. INSERT [dbo].[Patient_Symptom] ([Id], [PatientId], [SymptomId]) VALUES (13, 7, 5)
  146. GO
  147. INSERT [dbo].[Patient_Symptom] ([Id], [PatientId], [SymptomId]) VALUES (14, 7, 6)
  148. GO
  149. INSERT [dbo].[Patient_Symptom] ([Id], [PatientId], [SymptomId]) VALUES (15, 7, 7)
  150. GO
  151. INSERT [dbo].[Symptom] ([Id], [Name]) VALUES (1, N'SA        ')
  152. GO
  153. INSERT [dbo].[Symptom] ([Id], [Name]) VALUES (2, N'SB        ')
  154. GO
  155. INSERT [dbo].[Symptom] ([Id], [Name]) VALUES (3, N'SC        ')
  156. GO
  157. INSERT [dbo].[Symptom] ([Id], [Name]) VALUES (4, N'SD        ')
  158. GO
  159. INSERT [dbo].[Symptom] ([Id], [Name]) VALUES (5, N'SE        ')
  160. GO
  161. INSERT [dbo].[Symptom] ([Id], [Name]) VALUES (6, N'SF        ')
  162. GO
  163. INSERT [dbo].[Symptom] ([Id], [Name]) VALUES (7, N'SG        ')
  164. GO
  165. ALTER TABLE [dbo].[Medication_Symptom]  WITH CHECK ADD  CONSTRAINT [FK_MedicineSymptom_Medicine] FOREIGN KEY([MedicineId])
  166. REFERENCES [dbo].[Medicine] ([Id])
  167. GO
  168. ALTER TABLE [dbo].[Medication_Symptom] CHECK CONSTRAINT [FK_MedicineSymptom_Medicine]
  169. GO
  170. ALTER TABLE [dbo].[Medication_Symptom]  WITH CHECK ADD  CONSTRAINT [FK_MedicineSymptom_Symptom] FOREIGN KEY([SymptomId])
  171. REFERENCES [dbo].[Symptom] ([Id])
  172. GO
  173. ALTER TABLE [dbo].[Medication_Symptom] CHECK CONSTRAINT [FK_MedicineSymptom_Symptom]
  174. GO
  175. ALTER TABLE [dbo].[Patient_Symptom]  WITH CHECK ADD  CONSTRAINT [FK_PatientSymptom_Patient] FOREIGN KEY([PatientId])
  176. REFERENCES [dbo].[Patient] ([Id])
  177. GO
  178. ALTER TABLE [dbo].[Patient_Symptom] CHECK CONSTRAINT [FK_PatientSymptom_Patient]
  179. GO
  180. ALTER TABLE [dbo].[Patient_Symptom]  WITH CHECK ADD  CONSTRAINT [FK_PatientSymptom_Symptom] FOREIGN KEY([SymptomId])
  181. REFERENCES [dbo].[Symptom] ([Id])
  182. GO
  183. ALTER TABLE [dbo].[Patient_Symptom] CHECK CONSTRAINT [FK_PatientSymptom_Symptom]
  184. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement