I2DB Ch7.sql
I2DB Ch7.sql
----------------------------------------------------------
/* Add some reference constraints */
ALTER TABLE tblEmployee
ADD CONSTRAINT FK_tblEmployee_tblDepartment
FOREIGN KEY(depNum) REFERENCES tblDepartment(depNum)
----------------------------------------------------------
/* Insert some Employees tuples */
SELECT depNum FROM tblDepartment
----------------------------------------------------------
/* Update some values */
SELECT depNum FROM tblDepartment;
SELECT empSSN, depNum, supervisorSSN FROM tblEmployee WHERE empSSN=30121050004;
UPDATE tblEmployee
SET depNum=100
WHERE empSSN=30121050004
UPDATE tblEmployee
SET depNum=2
WHERE empSSN=30121050004
UPDATE tblEmployee
SET depNum=1
WHERE empSSN=30121050004
--
DELETE tblDepartment
WHERE depNum=1
DELETE tblDepartment
WHERE depNum=123
----------------------------------------------------------
/*7.2*/
alter table tblEmployee
drop constraint NN_empName
alter table tblEmployee
add constraint NN_empName check (empName is not null)
--
alter table tblEmployee
drop constraint NN_empSalary
alter table tblEmployee
add constraint NN_empSalary check (empSalary is not null)
UPDATE tblEmployee
SET empName=NULL
WHERE empSSN=30121050004;
----------------------------------------------------------
/*7.3*/
alter table tblEmployee
drop constraint CHK_empSex
UPDATE tblEmployee
SET empSex='A'
WHERE empSSN=30121050004;
UPDATE tblEmployee
SET empSex='M'
WHERE empSSN=30121050004;
UPDATE tblEmployee
SET empSex='F'
WHERE empSSN=30121050004;
----------------------------------------------------------
/**/
alter table tblEmployee
add constraint CHK_SexAge
check (
(empSex='F' and (YEAR(GETDATE()) - YEAR(empBirthdate)) <=55)
or
(empSex='M' AND (YEAR(GETDATE()) - YEAR(empBirthdate)) <= 60)
)
update tblEmployee
set empSex='F', empBirthdate='1955-01-01'
where empSSn=30121050004;
--
update tblEmployee
set empSex='F', empBirthdate='1975-01-01'
where empSSn=30121050004;
----------------------------------------------------------
/* modifing constraints */
alter table tblEmployee
drop constraint CHK_SexAge
alter table tblEmployee
add constraint CHK_SexAge
check (
(empSex='F' and (YEAR(GETDATE()) - YEAR(empBirthdate)) <=55)
or
(empSex='M' AND (YEAR(GETDATE()) - YEAR(empBirthdate)) <= 65)
)
----------------------------------------------------------
/* Short introduction to T-SQL programming */
----------------------------------------------------------
-- Assign a value into a variable : using SET or SELECT
DECLARE @empName NVARCHAR(20), @empSSN AS DECIMAL, @empSalary DECIMAL=1000
SET @empName=N'Mai Duy An'
SET @empSalary=2000
PRINT @empName
PRINT @empSalary
UPDATE tblEmployee
SET @empName=empName, @empSalary=empSalary
WHERE empName=N'Mai Duy An'
PRINT @empName
PRINT @empSalary
-- Converts an expression from one data type to a different data type : using CAST
or CONVERT function
DECLARE @empName NVARCHAR(20), @empSalary DECIMAL
SET @empName=N'Mai Duy An'
SET @empSalary=1000
PRINT @empName + '''s salary is ' + CAST(@empSalary AS VARCHAR)
PRINT @empName + '''s salary is ' + CONVERT(VARCHAR, @empSalary)
-- using IF statement
DECLARE @workHours DECIMAL, @bonus DECIMAL
SELECT @workHours=SUM(workHours)
FROM tblWorksOn
WHERE empSSN=30121050027
GROUP BY empSSN
-- WHILE statement
DECLARE @factorial INT, @n INT
SET @n=5
SET @factorial=1
BEGIN TRANSACTION
INSERT INTO tblDepartment(depNum,depName)
VALUES(6, N'Phòng Kế Toán');
IF @@ERROR<>0
BEGIN
ROLLBACK TRANSACTION
PRINT @@ERROR
END
COMMIT TRANSACTION
----------------------------------------------------------
/* Implement Trigger with T-SQL */
----------------------------------------------------------
-- create trigger
IF OBJECT_ID('Tr_Employee_Insert', 'TR') is not null
drop trigger Tr_Employee_Insert
go
CREATE TRIGGER Tr_Employee_Insert ON tblEmployee
AFTER INSERT
AS
RAISERROR('Insert trigger is awakened',16,1)
go
--test
INSERT INTO tblEmployee(empSSN, empName, empSalary, depNum, supervisorSSN)
VALUES (30121050345, N'Nguyễn Văn Tý', 10000, 1, 30121050037);
SELECT * FROM tblEmployee WHERE empSSN=30121050345
--delete trigger
IF OBJECT_ID('Tr_Employee_Delete', 'TR') is not null
drop trigger Tr_Employee_Delete
go
CREATE TRIGGER Tr_Employee_Delete ON tblEmployee
AFTER DELETE
AS
RAISERROR('Delete trigger is awakened',16,1)
go
--test
DELETE FROM tblEmployee WHERE empSSN=30121050345
--update trigger
IF OBJECT_ID('Tr_Employee_Update', 'TR') is not null
drop trigger Tr_Employee_Update
go
CREATE TRIGGER Tr_Employee_Update ON tblEmployee
AFTER Update
AS
RAISERROR('Update trigger is awakened',16,1)
go
--test
UPDATE tblEmployee
SET empName=N'Trần Văn X'
WHERE empSSN=30121050345
----------------------------------------------------------
/* Abort Transaction */
--Có thể hủy bỏ các tác động trên dữ liệu của lệnh kích hoạt trigger thông qua lệnh
ROLLBACK
--insert trigger
IF OBJECT_ID('Tr_Employee_Insert', 'TR') is not null
drop trigger Tr_Employee_Insert
go
CREATE TRIGGER Tr_Employee_Insert ON tblEmployee
AFTER INSERT
AS
RAISERROR('Insert trigger is awakened',16,1)
ROLLBACK TRANSACTION
go
--test
INSERT INTO tblEmployee(empSSN, empName, empSalary, depNum, supervisorSSN)
VALUES (30121050345, N'Nguyễn Văn Tý', 10000, 1, 30121050037);
--not found employee whose empSSN is 30121050345
SELECT * FROM tblEmployee WHERE empSSN=30121050345
----------------------------------------------------------
/* Hai bảng đặc biệt INSERTED and DELETED
Các bộ được thêm mới sẽ lưu trong bảng INSERTED
Các bộ bị xóa sẽ lưu trong bảng DELETED
Trường hợp cập nhật: các bộ ứng với giá trị cũ sẽ lưu trong DELETED và giá trị mới
sẽ lưu trong INSERTED
*/
IF OBJECT_ID('Tr_Employee_Insert', 'TR') is not null
drop trigger Tr_Employee_Insert
go
CREATE TRIGGER Tr_Employee_Insert ON tblEmployee
AFTER INSERT
AS
DECLARE @vEmpSSN DECIMAL, @vEmpName NVARCHAR(50)
SELECT @vEmpSSN=empSSN FROM inserted
SELECT @vEmpName=empName FROM inserted
PRINT 'new tuple:'
PRINT 'empSSN=' + CAST(@vEmpSSN AS nvarchar(11)) + ' empName=' + @vEmpName
go
--test
delete tblEmployee where empSSN=30121050345
INSERT INTO tblEmployee(empSSN, empName, empSalary, depNum, supervisorSSN)
VALUES (30121050345, N'Nguyễn Văn Tý', 10000, 1, 30121050037);
----------------------------------------------------------
/* Không ký hợp đồng với nhân viên dưới 18 tuổi */
IF OBJECT_ID('Tr_Employee_Under18', 'TR') is not null
drop trigger Tr_Employee_Under18
go
----------------------------------------------------------
/*
Mỗi nhân viên chỉ cho phép tối đa 2 người phụ thuộc
*/
IF OBJECT_ID('Tr_Dependent_AtMost2', 'TR') is not null
drop trigger Tr_Dependent_AtMost2
go
--test
select * from tblDependent where empSSN=30121050004;
insert into tblDependent(depName, empSSn, depRelationship)
values(N'Vương Thị A', 30121050004, 'Em');
insert into tblDependent(depName, empSSn, depRelationship)
values(N'Vương Thị B', 30121050004, 'Em');