sql 语句 创建UPDATE触发器,当Salary表中的InCome值增加500时,OutCome值则增加50。
时间: 2023-07-11 07:33:59 浏览: 75
可以使用以下 SQL 语句创建 UPDATE 触发器:
```
CREATE TRIGGER update_salary
AFTER UPDATE ON Salary
FOR EACH ROW
IF NEW.InCome > OLD.InCome THEN
UPDATE Salary
SET OutCome = OutCome + 50
WHERE employee_id = NEW.employee_id;
END IF;
```
这个触发器会在 Salary 表中 InCome 值发生变化时被触发,然后根据需要增加 OutCome 值。其中,`NEW.InCome` 表示更新后的收入值,`OLD.InCome` 表示更新前的收入值。如果更新后的收入值比更新前的收入值高,则会将该员工的 OutCome 值增加 50。`WHERE employee_id = NEW.employee_id` 表示仅更新该员工的记录。
相关问题
创建UPDATE触发器,当Salary表中Income值增加500时, outcome值增加50:
以下是创建该触发器的SQL语句:
```
CREATE TRIGGER update_salary_income
AFTER UPDATE ON Salary
FOR EACH ROW
BEGIN
IF NEW.Income = OLD.Income + 500 THEN
UPDATE Salary SET Outcome = Outcome + 50 WHERE EmployeeID = NEW.EmployeeID;
END IF;
END;
```
该触发器会在每次Salary表中的记录被更新后被触发。当Income值增加500时,触发器会将对应的EmployeeID记录的Outcome值增加50。请注意,这个触发器只会在Income值增加500时触发,而不是在任何时候都增加50。
帮我用sql语句完成一下操作:创建一个存储过程,要求一个员工的工作年份大于6年时将其转到经理办公室工作。 创建一个存储过程,根据每个人的学历将收入提高,如大专及以下提高400,本科提高500,硕士提高650,博士提高800。 创建一个存储过程,使用游标计算本科及以上学历的员工在总员工中所占比例。 创建存储过程,使用游标确定一个员工的实际收入是否排在前三名,结果为1表示是,结果为0表示否。 用触发器实现数据库YGGL中的参照完整性 删除Department表中记录的同时删除该记录“DepartmentID”字段值在Employees表中对应的记录。 创建UPDATE触发器,当Salary 表中Income值增加500时, OutCome 值则增加50。 创建触发器,当修改Employees表中员工的工作时间增加1年则将收入增加500,增加2年则增加1000,依次增加。若工作时间减少则无变化。 对于“YGGL”数据库,表Employees的EmployeeID列与表Salary的 EmployeeID列应满足参照完整性,请用触发器实现之。
1. 创建一个存储过程,要求一个员工的工作年份大于6年时将其转到经理办公室工作。
```
CREATE PROCEDURE transfer_to_manager_office
@employee_id INT
AS
BEGIN
IF (SELECT DATEDIFF(YEAR, HireDate, GETDATE()) FROM Employees WHERE EmployeeID = @employee_id) > 6
BEGIN
UPDATE Employees SET Office = 'Manager Office' WHERE EmployeeID = @employee_id
END
END
```
2. 创建一个存储过程,根据每个人的学历将收入提高,如大专及以下提高400,本科提高500,硕士提高650,博士提高800。
```
CREATE PROCEDURE increase_salary_by_education_level
AS
BEGIN
UPDATE Salary SET Income =
CASE Education
WHEN '大专及以下' THEN Income + 400
WHEN '本科' THEN Income + 500
WHEN '硕士' THEN Income + 650
WHEN '博士' THEN Income + 800
ELSE Income
END
END
```
3. 创建一个存储过程,使用游标计算本科及以上学历的员工在总员工中所占比例。
```
CREATE PROCEDURE calculate_percentage_of_bachelor_or_above
AS
BEGIN
DECLARE @total INT, @count INT, @percentage DECIMAL(5,2)
SELECT @total = COUNT(*) FROM Employees
SELECT @count = COUNT(*) FROM Employees WHERE Education IN ('本科','硕士','博士')
SET @percentage = CONVERT(DECIMAL(5,2), @count) / CONVERT(DECIMAL(5,2), @total) * 100
PRINT 'Percentage of employees with Bachelor or above degree: ' + CONVERT(VARCHAR(10), @percentage) + '%'
END
```
4. 创建存储过程,使用游标确定一个员工的实际收入是否排在前三名,结果为1表示是,结果为0表示否。
```
CREATE PROCEDURE check_if_income_ranked_top_three
@employee_id INT
AS
BEGIN
DECLARE @income INT, @rank INT
SELECT @income = Income FROM Salary WHERE EmployeeID = @employee_id
DECLARE cur CURSOR FOR
SELECT Income FROM Salary ORDER BY Income DESC
SET @rank = 1
OPEN cur
FETCH NEXT FROM cur INTO @income
WHILE @@FETCH_STATUS = 0
BEGIN
IF (@income = (SELECT Income FROM Salary WHERE EmployeeID = @employee_id))
BEGIN
IF (@rank <= 3) PRINT '1'
ELSE PRINT '0'
BREAK
END
SET @rank = @rank + 1
FETCH NEXT FROM cur INTO @income
END
CLOSE cur
DEALLOCATE cur
END
```
5. 用触发器实现数据库YGGL中的参照完整性 删除Department表中记录的同时删除该记录“DepartmentID”字段值在Employees表中对应的记录。
```
CREATE TRIGGER trg_delete_department
ON Department
AFTER DELETE
AS
BEGIN
DELETE FROM Employees WHERE DepartmentID IN (SELECT DepartmentID FROM DELETED)
END
```
6. 创建UPDATE触发器,当Salary 表中Income值增加500时, OutCome 值则增加50。
```
CREATE TRIGGER trg_salary_increase
ON Salary
AFTER UPDATE
AS
BEGIN
IF UPDATE(Income)
BEGIN
UPDATE Salary SET Outcome = Outcome + 50 WHERE Income IN (SELECT Income + 500 FROM INSERTED)
END
END
```
7. 创建触发器,当修改Employees表中员工的工作时间增加1年则将收入增加500,增加2年则增加1000,依次增加。若工作时间减少则无变化。
```
CREATE TRIGGER trg_increase_salary_by_work_years
ON Employees
AFTER UPDATE
AS
BEGIN
IF UPDATE(HireDate)
BEGIN
DECLARE @employee_id INT, @hire_date DATE, @years_diff INT
DECLARE cur CURSOR FOR
SELECT EmployeeID, HireDate FROM INSERTED
OPEN cur
FETCH NEXT FROM cur INTO @employee_id, @hire_date
WHILE @@FETCH_STATUS = 0
BEGIN
SET @years_diff = DATEDIFF(YEAR, @hire_date, GETDATE())
IF (@years_diff > 0)
BEGIN
UPDATE Salary SET Income =
CASE
WHEN @years_diff = 1 THEN Income + 500
WHEN @years_diff = 2 THEN Income + 1000
ELSE Income
END
WHERE EmployeeID = @employee_id
END
FETCH NEXT FROM cur INTO @employee_id, @hire_date
END
CLOSE cur
DEALLOCATE cur
END
END
```
8. 对于“YGGL”数据库,表Employees的EmployeeID列与表Salary的 EmployeeID列应满足参照完整性,请用触发器实现之。
```
CREATE TRIGGER trg_ensure_referential_integrity
ON Employees
AFTER INSERT, UPDATE
AS
BEGIN
IF EXISTS (SELECT * FROM INSERTED WHERE EmployeeID NOT IN (SELECT EmployeeID FROM Salary))
BEGIN
RAISERROR('EmployeeID does not exist in Salary table.', 16, 1)
ROLLBACK TRANSACTION
END
END
```
阅读全文