Advertisement
Guest User

Untitled

a guest
May 19th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1.  
  2. while(exists(select 1 from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where CONSTRAINT_TYPE='FOREIGN KEY'))
  3.  
  4. begin
  5.  
  6.  
  7. declare @sql nvarchar(2000)
  8.  
  9.  
  10. SELECT TOP 1 @sql=('ALTER TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME
  11.  
  12.  
  13. + '] DROP CONSTRAINT [' + CONSTRAINT_NAME + ']')
  14.  
  15.  
  16. FROM information_schema.table_constraints
  17.  
  18.  
  19. WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'
  20.  
  21.  
  22. exec (@sql)
  23.  
  24.  
  25. end
  26.  
  27. IF OBJECT_ID('dbo.Subjectparticipants') IS NOT NULL
  28. DROP TABLE dbo.Subjectparticipants
  29.  
  30.  
  31. IF OBJECT_ID('dbo.StudentsGroup') IS NOT NULL
  32. DROP TABLE dbo.StudentsGroup
  33.  
  34.  
  35.  
  36. IF OBJECT_ID('dbo.Students') IS NOT NULL
  37. DROP TABLE dbo.Students
  38.  
  39. IF OBJECT_ID('dbo.Subjects') IS NOT NULL
  40. DROP TABLE dbo.Subjects
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. create table Students (
  49. StudentID int not null primary key identity(1,1),
  50. EnrollmentID char(7) not null,
  51. Firstname varchar(20) not null,
  52. Lastname varchar(30) not null,
  53. Birthdate date not null,
  54. Sex varchar(1) default 'M' check(Sex = 'M' or Sex = 'K')
  55. )
  56.  
  57. ALTER TABLE dbo.Students WITH CHECK ADD CONSTRAINT [Difference] CHECK (DATEDIFF(year,GetDate(),Students.Birthdate) <= 40);
  58.  
  59.  
  60.  
  61.  
  62. create table Subjects (
  63. SubjectID int primary key not null,
  64. SubjectName varchar(30) Unique not null
  65. )
  66.  
  67.  
  68.  
  69.  
  70. create table Subjectparticipants (
  71. SubjectparticipantID int not null primary key,
  72. Participationbeginning date null,
  73. Participationend date null,
  74. StudentID_FK int not null,
  75. SubjectID_FK int not null,
  76. foreign key (StudentID_FK) references Students(StudentID) ON DELETE CASCADE,
  77. foreign key (SubjectID_FK) references Subjects(SubjectID) ON DELETE CASCADE
  78. )
  79. ALTER TABLE dbo.SUBJECTPARTICIPANTS WITH CHECK ADD CONSTRAINT [ParticipationEnd] CHECK (([ParticipationEnd]>[ParticipationBeginning]));
  80.  
  81.  
  82.  
  83. create table StudentsGroup (
  84. GroupID int not null primary key,
  85. SubjectID_FK int not null,
  86. Foremanname varchar(50),
  87. Weekday varchar(20) not null,
  88. BeginingTime time not null,
  89. foreign key (SubjectID_FK) references Subjects(SubjectID) ON DELETE CASCADE
  90. )
  91.  
  92. IF OBJECT_ID('dbo.GroupParticipants') IS NOT NULL
  93. DROP TABLE dbo.GroupParticipants
  94.  
  95. create table GroupParticipants(
  96. GroupParticipantID int not null primary key,
  97. ParticipationBegining date,
  98. ParticipationEnd date,
  99. GroupID_FK int not null,
  100. StudentID_FK int not null,
  101. foreign key (StudentID_FK) references Students(StudentID) ON DELETE CASCADE,
  102. foreign key (GroupID_FK) references StudentsGroup(GroupID)ON DELETE CASCADE
  103. )
  104.  
  105.  
  106. INSERT Subjects values ('1', 'matematyka'), ('2', 'biologia'), ('3', 'chemia'), ('4', 'historia')
  107.  
  108.  
  109.  
  110. INSERT dbo.Students ( EnrollmentID, Firstname, Lastname, Birthdate, Sex)
  111. values
  112. ('1', 'Katarzyna', 'Kozera', '1998-05-09', 'K'),
  113. ('2', 'Anna', 'Mielczarek', '2001-07-03', 'K'),
  114. ('3', 'Jan', 'Jasiewicz', '1998-04-05', 'M'),
  115. ('4', 'Piotr', 'Wiecha', '1998-05-01', 'M'),
  116. ('5', 'Jakub', 'Grzywna', '1998-01-02', 'M'),
  117. ('6', 'Leszek', 'Sarepski', '1997-11-07', 'M'),
  118. ('7', 'Kamil', 'Laski', '1997-11-07', 'M'),
  119. ('8', 'Mateusz', 'Siekiera', '1997-11-07', 'M'),
  120. ('9', 'Karol', 'Kunicki', '1997-11-07', 'M'),
  121. ('0', 'Igor', 'Czub', '1997-11-07', 'M'),
  122. ('abc', 'Igor', 'Czub', '1997-11-07', 'M');
  123.  
  124.  
  125. select * from Students
  126.  
  127.  
  128. /*
  129. CREATE VIEW dbo.studentgroupparticipation
  130. AS
  131. Select Student.Lastname, Student.Firstname, Subject.SubjectName, Subjectparticipants.Participationbeginning, Subjectparticipants.Participationend
  132. from Student, Subject, Subjectparticipants
  133. */
  134.  
  135.  
  136.  
  137.  
  138.  
  139. INSERT SUBJECTPARTICIPANTS(SubjectParticipantID, ParticipationBeginning, ParticipationEnd, StudentID_FK, SubjectID_FK)
  140. VALUES (1, '2019-04-02', '2019-07-12', 1, 1),
  141. (2, '2019-04-03', '2019-07-13', 1, 3),
  142. (3, '2019-04-04', '2019-07-14', 1, 2),
  143. (4, '2019-04-05', '2019-07-15', 1, 4),
  144. (5, '2015-04-06', '2017-07-10', 2, 1),
  145. (6, '2016-04-06', '2016-07-10', 2, 4),
  146. (7, '2015-04-06', '2015-07-10', 2, 1),
  147. (8, '2017-09-06', '2018-02-10', 3, 1),
  148. (9, '2017-09-06', '2018-02-10', 3, 2),
  149. (10, '2014-04-06', '2015-07-10', 4, 1),
  150. (11, '2015-04-06', '2015-07-10', 4, 2),
  151. (12, '2016-04-06', '2017-07-10', 4, 3),
  152. (13, '2015-04-06', '2015-07-10', 4, 4);
  153.  
  154.  
  155. INSERT StudentsGroup(
  156. GroupID, SubjectID_FK, Foremanname, Weekday, BeginingTime)
  157. values
  158. (0, 1, 'Jakub', 'Poniedzialek', '13:30'),
  159. (1, 2, 'Kamil', 'Sroda', '11:00'),
  160. (2, 3, 'Jakub', 'Piatek', '14:00'),
  161. (3, 1, 'Mateusz', 'Czwartek', '11:00'),
  162. (4, 2, 'Jan', 'Sroda', '10:00'),
  163. (5, 4, 'Karol', 'Wtorek', '08:00'),
  164. (6, 2, 'Jakub', 'Sroda', '15:00'),
  165. (7, 1, 'Katarzyna', 'Czwartek', '17:00'),
  166. (8, 2, 'Anna', 'Poniedzialek', '12:00'),
  167. (9, 4, 'Piotr', 'Sobota', '11:00');
  168.  
  169. INSERT GroupParticipants (GroupParticipantID, ParticipationBegining, ParticipationEnd, GroupID_FK, StudentID_FK)
  170. VALUES
  171. (0, '2019-02-09', '2019-06-03', 0, 5),
  172. (1, '2019-03-29', '2019-06-16', 1, 7),
  173. (2, '2019-02-19', '2019-07-05', 2, 5),
  174. (3, '2019-02-15', '2019-06-18', 3, 8),
  175. (4, '2019-05-07', '2019-08-19', 4, 3),
  176. (5, '2019-04-01', '2019-07-02', 5, 9),
  177. (6, '2019-02-05', '2019-09-08', 6, 5),
  178. (7, '2019-03-09', '2019-08-03', 7, 1),
  179. (8, '2019-05-18', '2019-07-12', 8, 2),
  180. (9, '2019-04-11', '2019-09-04', 9, 4);
  181.  
  182.  
  183. Update StudentsGroup
  184. Set Weekday = 'Poniedzialek'
  185. where Weekday = 'Czwartek'
  186. and SubjectID_FK = 1;
  187.  
  188. Delete from Students
  189. where lastname like 'S%';
  190.  
  191. Delete from StudentsGroup
  192. where Weekday = 'Sobota'
  193. and SubjectID_FK = 4;
  194.  
  195.  
  196.  
  197. Alter Table GroupParticipants
  198. Add Status varchar(20)
  199. constraint Status check(Status in ('A','S','C'))
  200. constraint xx default 'A'
  201. with Values;
  202.  
  203. Alter Table StudentsGroup
  204. add size smallint
  205. constraint size check(size between 10 and 30)
  206. constraint x default 20
  207. with values;
  208.  
  209.  
  210.  
  211. select * from StudentsGroup
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement