diff --git a/Lab11_Vadim/ex1.sql b/Lab11_Vadim/ex1.sql new file mode 100644 index 0000000..2f60b10 --- /dev/null +++ b/Lab11_Vadim/ex1.sql @@ -0,0 +1,14 @@ +use AdventureWorks + +go + +create table HumanResources.JobCandidateHistory( + JobCandidateID int not null unique, + Resume xml null, + Rating int not null constraint DF_JobCandidateHistory_Rating default 5, + RejectedDate datetime not null, + ContactID int null, + constraint FK_JobCandidateHistory_Contact_ContactID + foreign key(ContactID) references Person.Contact(ContactID), + constraint CK_JobCandidateHistory_Rating check (Rating >=0 and Rating<=10) +) on [PRIMARY] \ No newline at end of file diff --git a/Lab11_Vadim/ex2.sql b/Lab11_Vadim/ex2.sql new file mode 100644 index 0000000..51d356d --- /dev/null +++ b/Lab11_Vadim/ex2.sql @@ -0,0 +1,10 @@ +use AdventureWorks + +go +create trigger dJobCandidate +on HumanResources.JobCandidate +after delete +as +insert into HumanResources.JobCandidateHistory (JobCandidateID, Resume, RejectedDate, ContactID) +select JobCandidateID, Resume, GETDATE(), null +from deleted \ No newline at end of file diff --git a/Lab11_Vadim/ex3.sql b/Lab11_Vadim/ex3.sql new file mode 100644 index 0000000..9c95237 --- /dev/null +++ b/Lab11_Vadim/ex3.sql @@ -0,0 +1,18 @@ +use AdventureWorks + +go +/* +select * from HumanResources.JobCandidateHistory -- просмотр содержрания таблицы перед удалением + +go +delete from HumanResources.JobCandidate +where JobCandidateID = + ( + select min(JobCandidateID) + from HumanResources.JobCandidate + ) + +go +select * from HumanResources.JobCandidateHistory -- просмотр содержрания таблицы после удаления +*/ +truncate table HumanResources.JobCandidateHistory \ No newline at end of file diff --git a/Lab11_Vadim/ex4.sql b/Lab11_Vadim/ex4.sql new file mode 100644 index 0000000..9f8083d --- /dev/null +++ b/Lab11_Vadim/ex4.sql @@ -0,0 +1,34 @@ +use AdventureWorks + +--go + +select * from Production.Product +select * from Sales.SalesOrderDetail +/* +go +create trigger OrderDetailNotDiscontinued +on Sales.SalesOrderDetail +instead of insert, update +as +begin + declare @SelectProduct int + if (exists (select * from deleted)) -- если deleted не пустая (для update) + set @SelectProduct = (select ProductID from deleted) + else + set @SelectProduct = (select ProductID from inserted) + + if (exists (select * from Production.Product where ProductID = @SelectProduct and DiscontinuedDate is not null)) + Raiserror('Error',16,1) +end +*/ +go +select ProductID, Name +from Production.Product +where DiscontinuedDate is not null + +update Production.Product +set DiscontinuedDate = GETDATE() +where ProductID = 680 + +insert Sales.SalesOrderDetail (SalesOrderID, OrderQty, ProductID, SpecialOfferID, UnitPrice, UnitPriceDiscount) +values(43660, 5,680,1,1431,0) \ No newline at end of file