47.创建一个触发器T_sc1,要求每当在stucou表中插入数据时,向客户端显示一条“记录已添加!”的消息。 48.创建一个触发器T_stu,要求每当用户删除student表的记录时,自动显示被删除记录的内容 49.利用IF UPDATE建立一个触发器T_course,在course表上修改课程类别时,显示信息“课程类别已修改”。 50.创建触发器T_stcour,实现当插入、更新和删除stucou表的选课数据行时,能同时更新Course表中相应的选课人数WillNum. 51.若批量插入、更新和删除stucou表的选课数据行时,修改上题的触发器 52.创建一个触发器T_sc2,要求每当在stucou表中插入数据时,向客户端显示一条“记录添加二次触发”的消息。(知识点:多个触发器) 53.创建一个数据库stuxk,将XK数据库中的表导入其中。 54.为某个系创建一个视图V_elec. 55.为数据库stuxk创建三个用户user1,user2和user3 56.将视图V_elec的查询权限给用户user2和user3.
时间: 2024-02-06 09:11:19 浏览: 134
devtoolset-7-elfutils-0.170-1.el7.sc1.x86_64.rpm
对于问题47-52,这些问题涉及到数据库触发器的创建和修改,需要针对具体的数据库系统进行实现,例如MySQL、SQL Server、Oracle等。以下是一些通用的SQL语句,仅供参考:
47. 创建触发器T_sc1:
```sql
CREATE TRIGGER T_sc1
AFTER INSERT ON stucou
FOR EACH ROW
BEGIN
SELECT '记录已添加!';
END;
```
48. 创建触发器T_stu:
```sql
CREATE TRIGGER T_stu
AFTER DELETE ON student
FOR EACH ROW
BEGIN
SELECT CONCAT('被删除的记录内容为:', OLD.col1, ',', OLD.col2, ',', ...);
END;
```
49. 创建触发器T_course:
```sql
CREATE TRIGGER T_course
AFTER UPDATE ON course
FOR EACH ROW
IF UPDATE(category)
THEN
SELECT '课程类别已修改';
END IF;
```
50. 创建触发器T_stcour:
```sql
CREATE TRIGGER T_stcour
AFTER INSERT, UPDATE, DELETE ON stucou
FOR EACH ROW
BEGIN
IF INSERTING THEN
UPDATE Course SET WillNum = WillNum + 1 WHERE CourseID = NEW.CourseID;
ELSIF UPDATING THEN
UPDATE Course SET WillNum = WillNum + 1 WHERE CourseID = NEW.CourseID;
UPDATE Course SET WillNum = WillNum - 1 WHERE CourseID = OLD.CourseID;
ELSIF DELETING THEN
UPDATE Course SET WillNum = WillNum - 1 WHERE CourseID = OLD.CourseID;
END IF;
END;
```
51. 修改触发器T_stcour,支持批量操作:
```sql
CREATE TRIGGER T_stcour
AFTER INSERT, UPDATE, DELETE ON stucou
FOR EACH ROW
BEGIN
DECLARE cnt INT;
IF INSERTING THEN
SELECT COUNT(*) INTO cnt FROM inserted;
UPDATE Course SET WillNum = WillNum + cnt WHERE CourseID = NEW.CourseID;
ELSIF UPDATING THEN
SELECT COUNT(*) INTO cnt FROM inserted;
UPDATE Course SET WillNum = WillNum + cnt WHERE CourseID = NEW.CourseID;
SELECT COUNT(*) INTO cnt FROM deleted;
UPDATE Course SET WillNum = WillNum - cnt WHERE CourseID = OLD.CourseID;
ELSIF DELETING THEN
SELECT COUNT(*) INTO cnt FROM deleted;
UPDATE Course SET WillNum = WillNum - cnt WHERE CourseID = OLD.CourseID;
END IF;
END;
```
52. 创建触发器T_sc2,实现多个触发器:
```sql
CREATE TRIGGER T_sc1
AFTER INSERT ON stucou
FOR EACH ROW
BEGIN
SELECT '记录已添加!';
END;
CREATE TRIGGER T_sc2
AFTER INSERT ON stucou
FOR EACH ROW
BEGIN
SELECT '记录添加二次触发';
END;
```
对于问题53,可以使用以下命令将XK数据库中的表导入到stuxk数据库中:
```sql
USE stuxk;
SOURCE /path/to/XK.sql;
```
其中,/path/to/XK.sql表示XK数据库的SQL脚本文件路径。
对于问题54和55,可以使用以下SQL语句创建视图和用户:
```sql
-- 创建视图V_elec
CREATE VIEW V_elec AS
SELECT col1, col2, ... FROM table WHERE condition;
-- 创建用户user1、user2和user3
CREATE USER 'user1'@'localhost' IDENTIFIED BY 'password';
CREATE USER 'user2'@'localhost' IDENTIFIED BY 'password';
CREATE USER 'user3'@'localhost' IDENTIFIED BY 'password';
```
其中,'password'表示用户的密码,可以根据实际情况进行修改。
对于问题56,可以使用以下命令将视图V_elec的查询权限授予用户user2和user3:
```sql
GRANT SELECT ON stuxk.V_elec TO 'user2'@'localhost';
GRANT SELECT ON stuxk.V_elec TO 'user3'@'localhost';
```
阅读全文