diff --git "a/\345\271\262\347\272\257\346\254\243/2024.1021 \346\225\260\346\215\256\345\272\223\351\253\230\347\272\247\345\272\224\347\224\250\347\237\245\350\257\206\346\200\273\347\273\223\345\222\214\347\273\274\345\220\210\347\273\203\344\271\240.md" "b/\345\271\262\347\272\257\346\254\243/2024.1021 \346\225\260\346\215\256\345\272\223\351\253\230\347\272\247\345\272\224\347\224\250\347\237\245\350\257\206\346\200\273\347\273\223\345\222\214\347\273\274\345\220\210\347\273\203\344\271\240.md" index 80f91b733947cc7d1f9597aea60ee200eccb03f9..36dd178b42e09093cfa37991cefb5c032a0c9dc2 100644 --- "a/\345\271\262\347\272\257\346\254\243/2024.1021 \346\225\260\346\215\256\345\272\223\351\253\230\347\272\247\345\272\224\347\224\250\347\237\245\350\257\206\346\200\273\347\273\223\345\222\214\347\273\274\345\220\210\347\273\203\344\271\240.md" +++ "b/\345\271\262\347\272\257\346\254\243/2024.1021 \346\225\260\346\215\256\345\272\223\351\253\230\347\272\247\345\272\224\347\224\250\347\237\245\350\257\206\346\200\273\347\273\223\345\222\214\347\273\274\345\220\210\347\273\203\344\271\240.md" @@ -220,7 +220,7 @@ delete 事件只对旧数据进行删除,所以只用old来表示,要被删 ```sql signal sqlstate `可以为5个纯数字,纯英文(大写)` -set ,message_text = `提示信息` +set message_text = `提示信息` ``` ### 八、事物 @@ -534,16 +534,13 @@ delimiter; call pro_patient(103); 2.3. (10分)创建触发器实现:当医生离职时,检查其是否还有未完成医治的病人,如果还存在病人则不予离职,否则可以离职。 -create trigger tri_leave before delete on doctor -for each ROW -begin -declare patient_count int; -select cout(*) into patient_count from patient where doctor_name = old.doctor_name ; -IF patient_count > 0 then - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = '无法离职:仍有未完成的病人!'; -end if; -end ; -错误 +drop trigger if exists tri_help; +create trigger tri_help before insert on userapply +for each row +BEGIN +if (new.help !='已申请') then +signal sqlstate '45000' set message_text = '重新个性受理标志为:已申请!'; +end if; +END; ``` diff --git "a/\345\271\262\347\272\257\346\254\243/2024.1022 \347\273\274\345\220\210\351\242\2302.md" "b/\345\271\262\347\272\257\346\254\243/2024.1022 \347\273\274\345\220\210\351\242\2302.md" new file mode 100644 index 0000000000000000000000000000000000000000..8c3d1aff18362af1ea17072d0e218c1e6f82f36c --- /dev/null +++ "b/\345\271\262\347\272\257\346\254\243/2024.1022 \347\273\274\345\220\210\351\242\2302.md" @@ -0,0 +1,285 @@ +```sql + +#1022综合练习2 +二、综合题 +1. 描述(10分) +针对如下表Author结构创建索引,写出创建索引的SQL语句: +CREATE TABLE Author +( + Id int PRIMARY KEY , + FirstName varchar(45) NOT NULL, + LastName varchar(45) NOT NULL, + UpdatedTime datetime NOT NULL + ) + 对FirstName创建唯一索引uniq_idx_firstname,对LastName创建普通索引idx_lastname + # 答题区: +-- ----------------------------------------------------------------- +create unique index uniq_idx_firstname on Author(FirstName); +create index idx_lastname on Author(LastName); +-- ----------------------------------------------------------------- +2. 描述(15分) +构造一个触发器trg_AuditLog,在向Employees表中插入一条数据的时候,触发插入相关的数据到AuditLog中。 +-- -- 职员表 +CREATE TABLE Employees +( + Id INT PRIMARY KEY auto_increment, + Name varchar(80) NOT NULL, + Age INT NOT NULL, + Address varchar(50), + SALARY decimal(18,2) +); +-- -- 审计日志表 +CREATE TABLE AuditLog +( + Id int primary key auto_increment, + NAME TEXT NOT NULL, + Salary decimal(18,2) +); +写好触发器后,往Employees插入一条数据: +INSERT INTO Employees (NAME,AGE,ADDRESS,SALARY)VALUES ('Paul', 32, 'California', 20000.00 ); +然后从AuditLog里面使用查询语句: +select NAME,Salary from AuditLog; +会输出: +mysql> select NAME,Salary from AuditLog; ++------+----------+ +| NAME | Salary | ++------+----------+ +| Paul | 20000.00 | ++------+----------+ + +# 答题区: +-- ----------------------------------------------------------------- +DROP TRIGGER tri_aud; +create trigger tri_aud before insert on Employees +for each ROW +begin +INSERT INTO AuditLog (NAME,SALARY) VALUES (new.NAME, new.SALARY); +end; +INSERT INTO employees (NAME,AGE,ADDRESS,SALARY)VALUES ('Paul', 32, 'California', 20000.00 ); +select NAME,Salary from AuditLog; +-- ----------------------------------------------------------------- + +-- 3. 描述(10分) +-- 针对Author表创建视图vw_Author,只包含FirstName以及LastName两列,并对这两列重新命名,FirstName为v_FirstName,LastName修改为v_LastName: +-- 先往Author表插入2条测试数据: +insert into Author values (1,'PENELOPE', 'GUINESS', '2006-02-15 12:34:33'), (2,'NICK', 'WAHLBERG', '2006-02-15 12:34:33'); +-- 查询视图:select * from vw_Author; +-- 输出: ++-------------+------------+ +| v_FirstName | v_LastName | ++-------------+------------+ +| PENELOPE | GUINESS | +| NICK | WAHLBERG | ++-------------+------------+ +# 答题区: +-- ----------------------------------------------------------------- +insert into Author values (1,'PENELOPE', 'GUINESS', '2006-02-15 12:34:33'), (2,'NICK', 'WAHLBERG', '2006-02-15 12:34:33'); +drop if exists pro_author; +delimiter// +create procedure pro_author() +begin +select FirstName v_FirstName,LastName v_LastName from author; +end// +delimiter; +call pro_author(); +-- ----------------------------------------------------------------- + +-- 4. 描述(10分) +-- 使用以下数据,完成相关操作 +-- -- 新建表 +CREATE TABLE user +( + Id int primary key auto_increment, + UserCode varchar(80), + Birthday date NOT NULL, + FirstName varchar(14) NOT NULL, + LastName varchar(16) NOT NULL, + Gender char(1) NOT NULL, + HireDate date NOT NULL +); +# 插入一些数据: +INSERT INTO `user` (UserCode,Birthday,FirstName,LastName,Gender,HireDate) VALUES('10001','1953-09-02','Georgi','Facello','M','1986-06-26'); +INSERT INTO `user` (UserCode,Birthday,FirstName,LastName,Gender,HireDate) VALUES('10002','1964-06-02','Bezalel','Simmel','F','1985-11-21'); +INSERT INTO `user` (UserCode,Birthday,FirstName,LastName,Gender,HireDate) VALUES('10003','1959-12-03','Parto','Bamford','M','1986-08-28'); +INSERT INTO `user` (UserCode,Birthday,FirstName,LastName,Gender,HireDate) VALUES('10004','1954-05-01','Chirstian','Koblick','M','1986-12-01'); +INSERT INTO `user` (UserCode,Birthday,FirstName,LastName,Gender,HireDate) VALUES('10005','1955-01-21','Kyoichi','Maliniak','M','1989-09-12'); +INSERT INTO `user` (UserCode,Birthday,FirstName,LastName,Gender,HireDate) VALUES('10006','1953-04-20','Anneke','Preusig','F','1989-06-02'); +INSERT INTO `user` (UserCode,Birthday,FirstName,LastName,Gender,HireDate) VALUES('10007','1957-05-23','Tzvetan','Zielinski','F','1989-02-10'); +INSERT INTO `user` (UserCode,Birthday,FirstName,LastName,Gender,HireDate) VALUES('10008','1958-02-19','Saniya','Kalloufi','M','1994-09-15'); +INSERT INTO `user` (UserCode,Birthday,FirstName,LastName,Gender,HireDate) VALUES('10009','1952-04-19','Sumant','Peac','F','1985-02-18'); +INSERT INTO `user` (UserCode,Birthday,FirstName,LastName,Gender,HireDate) VALUES('10010','1963-06-01','Duangkaew','Piveteau','F','1989-08-24'); +INSERT INTO `user` (UserCode,Birthday,FirstName,LastName,Gender,HireDate) VALUES('10011','1953-11-07','Mary','Sluis','F','1990-01-22'); +-- +-- 查询user表,实现以下显示效果: ++----------+------------+-----------+-----------+--------+------------+ +| UserCode | Birthday | FirstName | LastName | Gender | HireDate | ++----------+------------+-----------+-----------+--------+------------+ +| 10001 | 1953-09-02 | Georgi | Facello | 女 | 1986-06-26 | +| 10002 | 1964-06-02 | Bezalel | Simmel | 男 | 1985-11-21 | +| 10003 | 1959-12-03 | Parto | Bamford | 女 | 1986-08-28 | +| 10004 | 1954-05-01 | Chirstian | Koblick | 女 | 1986-12-01 | +| 10005 | 1955-01-21 | Kyoichi | Maliniak | 女 | 1989-09-12 | +| 10006 | 1953-04-20 | Anneke | Preusig | 男 | 1989-06-02 | +| 10007 | 1957-05-23 | Tzvetan | Zielinski | 男 | 1989-02-10 | +| 10008 | 1958-02-19 | Saniya | Kalloufi | 女 | 1994-09-15 | +| 10009 | 1952-04-19 | Sumant | Peac | 男 | 1985-02-18 | +| 10010 | 1963-06-01 | Duangkaew | Piveteau | 男 | 1989-08-24 | +| 10011 | 1953-11-07 | Mary | Sluis | 男 | 1990-01-22 | ++----------+------------+-----------+-----------+--------+------------+ + +# 答题区: +-- ----------------------------------------------------------------- +select UserCode,Birthday,FirstName,LastName,Gender,HireDate from user; +-- ----------------------------------------------------------------- + + +-- 5. 描述(30分) +开发组收到M公司的开发邀约,拟开发一套OA办公自动化系统,该系统的部分功能及初步需求分析的结果如下 : +部门:部门号、部门名、主管、联系电话、邮箱 +员工:工号、姓名、职位、手机号码、薪资 +用户:用户号、用户名、银行账号、手机号码、联系地址 +用户申请:申请号、用户号、会议日期、天数、参会人数、地点、预算和受理标志 +(1)M公司旗下有业务部、策划部和其他部门。每个部门只有一名主管,只负责管理本部门的工作,且主管参照员工关系的员工号;一个部门有多名员工,每名员工属于且仅属于一个部门。 +(2)员工职位包括主管、业务员、 策划员等。业务员负责受理用户申请,设置受理标志。一名业务员可以受理多个用户申请,但一个用户申请只能由一名业务员受理。 +(3)用户号唯一标识用户信息中的每一个用户。 +(4)用户申请信息的受理标志有:已申请(默认)、已拒绝、已受理、延期受理、已过期。申请号唯一标识用户申请信息中的每一个申请,且一个用户可以提交多个申请,但一个用户申请只对应一个用户号。 +准备数据: + + +-- 題目1(10分):使用PowerDesigner,设计可以使用的数据库物理模型,要求中文名称和英文名称清楚(英文不知道的可以借助翻译软件),表关系准确;利用设计好的模型生成sql语句,创建一个名为OADatabase的数据库,备用; +drop database if exists db_emp; +create database db_emp; +use db_emp; +/*==============================================================*/ +/* DBMS name: MySQL 5.0 */ +/* Created on: 2024/10/22 16:30:44 */ +/*==============================================================*/ + + +drop table if exists depart; + +drop table if exists emp; + +drop table if exists user; + +drop table if exists userapply; + +/*==============================================================*/ +/* Table: depart */ +/*==============================================================*/ +create table depart +( + depart_id int not null, + depart_name varchar(25), + manager varchar(25), + de_phone int, + email varchar(40), + primary key (depart_id) +); + +/*==============================================================*/ +/* Table: emp */ +/*==============================================================*/ +create table emp +( + emp_id int not null, + depart_id int not null, + emp_name varchar(25), + emp_job varchar(25), + emp_phone int, + sal decimal, + primary key (emp_id) +); + +/*==============================================================*/ +/* Table: user */ +/*==============================================================*/ +create table user +( + user_id int not null, + user_name varchar(25), + bank_account int, + user_phone int, + address varchar(40), + primary key (user_id) +); + +/*==============================================================*/ +/* Table: userapply */ +/*==============================================================*/ +create table userapply +( + apply_id int not null, + emp_id int not null, + user_id int not null, + apply_user_id int, + me_date varchar(40), + day_num int, + people_num int, + point varchar(40), + bud_sal decimal, + help varchar(40), + primary key (apply_id) +); + +alter table emp add constraint FK_depart_emp foreign key (depart_id) + references depart (depart_id) on delete restrict on update restrict; + +alter table userapply add constraint FK_emp_userapply foreign key (emp_id) + references emp (emp_id) on delete restrict on update restrict; + +alter table userapply add constraint FK_user_userappply2 foreign key (user_id) + references user (user_id) on delete restrict on update restrict; +INSERT INTO `depart` VALUES (101, '资源部', '韦大力', 1885654, 'weidali@qq.com'); +INSERT INTO `depart` VALUES (102, '业务部', '叶业', 147258, 'yeye@qq.com'); +INSERT INTO `depart` VALUES (103, '策划部', '孙隼', 175993, 'sunsun@qq.com'); +INSERT INTO `depart` VALUES (104, '财务部', '张三', 159357, 'zhangsan@qq.com'); + +INSERT INTO `emp` VALUES (10, 101, '资源部', '主管', 1885654, 40000); +INSERT INTO `emp` VALUES (20, 102, '业务部', '业务员', 147258, 10000); +INSERT INTO `emp` VALUES (30, 103, '策划部', '策划员', 175993, 15000); +INSERT INTO `emp` VALUES (40, 104, '财务部', '会计', 159357, 8000); + +INSERT INTO `user` VALUES (23440, '李无', 123456, 11111, '福建省'); +INSERT INTO `user` VALUES (23441, '钱奕', 456789, 22222, '安徽省'); +INSERT INTO `user` VALUES (23442, '李斯', 789456, 33333, '江苏省'); +INSERT INTO `user` VALUES (23443, '王说', 456123, 44444, '江西省'); + +INSERT INTO `userapply` VALUES (123123, 10, 23440, 234, '2024-10-19', 5, 10, 'aaa', 10000, '已申请'); +INSERT INTO `userapply` VALUES (123124, 20, 23441, 235, '2024-10-20', 6, 11, 'bbb', 5000, '已受理'); +INSERT INTO `userapply` VALUES (123125, 30, 23442, 236, '2024-10-21', 8, 12, 'ccc', 9000, '已拒绝'); +INSERT INTO `userapply` VALUES (123126, 40, 23443, 237, '2024-10-22', 9, 5, 'ddd', 8000, '已过期'); + + +-- 题目2(10分):为了保证当用户申请会议,受理标志为 已申请,请设计一个触发器,当用户申请信息表插入数据的时候,可以判断其默认受理标志是否为:已申请,如果是,则无需处理允许正常插入,如果不是则重新个性受理标志为:已申请; + +# 答题区: +-- ----------------------------------------------------------------- +drop trigger if exists tri_help; +create trigger tri_help before insert on userapply +for each row +BEGIN +if (new.help !='已申请') then +signal sqlstate '45000' set message_text = '重新个性受理标志为:已申请!'; +end if; +END; +-- ----------------------------------------------------------------- + +-- 题目3(10分):当业务员受理用户申请的时候,需要将当前处理的记录中受理标志修改为:已受理,同时将其它所有状态为:已申请,且会议日期已超过的记录中的受理标志个性为:已过期,请设计一个存储过程来处理此业务; + +# 答题区: +-- ----------------------------------------------------------------- +drop procedure if exists pro_add(); +create procedure pro_add(in applyid int) +begin +declare currrent_date varchar(40); +set currrent_date = now(); +update userapply set help = '已受理' where apply_id= applyid; +update userapply set help = '已过期' where help = '已申请' and me_date < currrent_date; +end ; +call pro_add(23440); +select * from userapply; +-- ----------------------------------------------------------------- + + +``` \ No newline at end of file