diff --git "a/\345\210\230\346\226\207\350\276\211/RBAC-\346\225\260\346\215\256\345\272\223-2024-09-08.md" "b/\345\210\230\346\226\207\350\276\211/RBAC-\346\225\260\346\215\256\345\272\223-2024-09-08.md" index c66162cffd7790ba320891e8c6b3bb5a99bdcabe..1191535792a24d27116736a99808d3a642840983 100644 --- "a/\345\210\230\346\226\207\350\276\211/RBAC-\346\225\260\346\215\256\345\272\223-2024-09-08.md" +++ "b/\345\210\230\346\226\207\350\276\211/RBAC-\346\225\260\346\215\256\345\272\223-2024-09-08.md" @@ -1,202 +1,217 @@ -# 学生管理系统(数据库) - -## 1.需求 - -通用:个人信息(信息,密码等修改) -学生端:成绩(查询) -教师端:成绩(添加,修改,查询) -班主任:班级信息管理,考试成绩管理 - -## 2.模型 - -###C-D - -![image-20240908145511417](https://gitee.com/yuwuchuji_0/open-source-image-library/raw/master/image/202409081455482.png) - -###L-D - -![image-20240908145600886](https://gitee.com/yuwuchuji_0/open-source-image-library/raw/master/image/202409081456963.png) - -###P-D - -![image-20240908145628568](https://gitee.com/yuwuchuji_0/open-source-image-library/raw/master/image/202409081456646.png) - -## 3.SQL代码 - -```sql -/*==============================================================*/ -/* DBMS name: MySQL 5.0 */ -/* Created on: 2024/9/8 15:07:14 */ -/*==============================================================*/ - - -drop table if exists class; - -drop table if exists class_01; - -drop table if exists class_and; - -drop table if exists infomation; - -drop table if exists over1; - -drop table if exists root; - -drop table if exists root2; - -drop table if exists student; - -drop table if exists techer; - -drop table if exists techer1; - -/*==============================================================*/ -/* Table: class */ -/*==============================================================*/ -create table class -( - ccid int not null, - class varchar(225), - primary key (ccid) -); - -/*==============================================================*/ -/* Table: class_01 */ -/*==============================================================*/ -create table class_01 -( - cid int not null, - class varchar(225), - primary key (cid) -); - -/*==============================================================*/ -/* Table: class_and */ -/*==============================================================*/ -create table class_and -( - ccid int not null, - cid int not null, - primary key (ccid, cid) -); - -/*==============================================================*/ -/* Table: infomation */ -/*==============================================================*/ -create table infomation -( - ID int not null, - sex varchar(10), - address varchar(225), - phone bigint, - primary key (ID) -); - -/*==============================================================*/ -/* Table: over1 */ -/*==============================================================*/ -create table over1 -( - ccid int not null, - sid int not null, - over char(10), - primary key (ccid, sid) -); - -/*==============================================================*/ -/* Table: root */ -/*==============================================================*/ -create table root -( - zid int not null, - tec varchar(255), - primary key (zid) -); - -/*==============================================================*/ -/* Table: root2 */ -/*==============================================================*/ -create table root2 -( - rid int not null, - zid int not null, - root varchar(225), - primary key (rid) -); - -/*==============================================================*/ -/* Table: student */ -/*==============================================================*/ -create table student -( - sid int not null, - ID int not null, - cid int not null, - zid int not null, - sname varchar(225), - primary key (sid) -); - -/*==============================================================*/ -/* Table: techer */ -/*==============================================================*/ -create table techer -( - tid int not null, - ID int not null, - zid int not null, - tname varchar(225), - primary key (tid) -); - -/*==============================================================*/ -/* Table: techer1 */ -/*==============================================================*/ -create table techer1 -( - cid int not null, - tid int not null, - primary key (cid, tid) -); - -alter table class_and add constraint FK_Relationship_11 foreign key (cid) - references class_01 (cid) on delete restrict on update restrict; - -alter table class_and add constraint FK_Relationship_5 foreign key (ccid) - references class (ccid) on delete restrict on update restrict; - -alter table over1 add constraint FK_Relationship_13 foreign key (sid) - references student (sid) on delete restrict on update restrict; - -alter table over1 add constraint FK_Relationship_8 foreign key (ccid) - references class (ccid) on delete restrict on update restrict; - -alter table root2 add constraint FK_Relationship_2 foreign key (zid) - references root (zid) on delete restrict on update restrict; - -alter table student add constraint FK_Relationship_10 foreign key (zid) - references root (zid) on delete restrict on update restrict; - -alter table student add constraint FK_Relationship_3 foreign key (ID) - references infomation (ID) on delete restrict on update restrict; - -alter table student add constraint FK_Relationship_6 foreign key (cid) - references class_01 (cid) on delete restrict on update restrict; - -alter table techer add constraint FK_Relationship_4 foreign key (ID) - references infomation (ID) on delete restrict on update restrict; - -alter table techer add constraint FK_Relationship_9 foreign key (zid) - references root (zid) on delete restrict on update restrict; - -alter table techer1 add constraint FK_Relationship_12 foreign key (tid) - references techer (tid) on delete restrict on update restrict; - -alter table techer1 add constraint FK_Relationship_7 foreign key (cid) - references class_01 (cid) on delete restrict on update restrict; - - -``` - -## 4.小结 +# 学生管理系统(数据库) + +## 1.分析 + +### 需求 + +通用:个人信息(信息,密码等修改) +学生端:成绩(查询) +教师端:成绩(添加,修改,查询) +班主任:班级信息管理,考试成绩管理 + +### 数据来源 + +```sql +无 +``` + + + +## 2.模型 + +### C-D + +![image-20240908145511417](https://gitee.com/yuwuchuji_0/open-source-image-library/raw/master/image/202409081455482.png) + +### L-D + +![image-20240908145600886](https://gitee.com/yuwuchuji_0/open-source-image-library/raw/master/image/202409081456963.png) + +### P-D + +![image-20240908145628568](https://gitee.com/yuwuchuji_0/open-source-image-library/raw/master/image/202409081456646.png) + +## 3.SQL代码 + +### 任务I + +```sql +/*==============================================================*/ +/* DBMS name: MySQL 5.0 */ +/* Created on: 2024/9/8 15:07:14 */ +/*==============================================================*/ + + +drop table if exists class; + +drop table if exists class_01; + +drop table if exists class_and; + +drop table if exists infomation; + +drop table if exists over1; + +drop table if exists root; + +drop table if exists root2; + +drop table if exists student; + +drop table if exists techer; + +drop table if exists techer1; + +/*==============================================================*/ +/* Table: class */ +/*==============================================================*/ +create table class +( + ccid int not null, + class varchar(225), + primary key (ccid) +); + +/*==============================================================*/ +/* Table: class_01 */ +/*==============================================================*/ +create table class_01 +( + cid int not null, + class varchar(225), + primary key (cid) +); + +/*==============================================================*/ +/* Table: class_and */ +/*==============================================================*/ +create table class_and +( + ccid int not null, + cid int not null, + primary key (ccid, cid) +); + +/*==============================================================*/ +/* Table: infomation */ +/*==============================================================*/ +create table infomation +( + ID int not null, + sex varchar(10), + address varchar(225), + phone bigint, + primary key (ID) +); + +/*==============================================================*/ +/* Table: over1 */ +/*==============================================================*/ +create table over1 +( + ccid int not null, + sid int not null, + over char(10), + primary key (ccid, sid) +); + +/*==============================================================*/ +/* Table: root */ +/*==============================================================*/ +create table root +( + zid int not null, + tec varchar(255), + primary key (zid) +); + +/*==============================================================*/ +/* Table: root2 */ +/*==============================================================*/ +create table root2 +( + rid int not null, + zid int not null, + root varchar(225), + primary key (rid) +); + +/*==============================================================*/ +/* Table: student */ +/*==============================================================*/ +create table student +( + sid int not null, + ID int not null, + cid int not null, + zid int not null, + sname varchar(225), + primary key (sid) +); + +/*==============================================================*/ +/* Table: techer */ +/*==============================================================*/ +create table techer +( + tid int not null, + ID int not null, + zid int not null, + tname varchar(225), + primary key (tid) +); + +/*==============================================================*/ +/* Table: techer1 */ +/*==============================================================*/ +create table techer1 +( + cid int not null, + tid int not null, + primary key (cid, tid) +); + +alter table class_and add constraint FK_Relationship_11 foreign key (cid) + references class_01 (cid) on delete restrict on update restrict; + +alter table class_and add constraint FK_Relationship_5 foreign key (ccid) + references class (ccid) on delete restrict on update restrict; + +alter table over1 add constraint FK_Relationship_13 foreign key (sid) + references student (sid) on delete restrict on update restrict; + +alter table over1 add constraint FK_Relationship_8 foreign key (ccid) + references class (ccid) on delete restrict on update restrict; + +alter table root2 add constraint FK_Relationship_2 foreign key (zid) + references root (zid) on delete restrict on update restrict; + +alter table student add constraint FK_Relationship_10 foreign key (zid) + references root (zid) on delete restrict on update restrict; + +alter table student add constraint FK_Relationship_3 foreign key (ID) + references infomation (ID) on delete restrict on update restrict; + +alter table student add constraint FK_Relationship_6 foreign key (cid) + references class_01 (cid) on delete restrict on update restrict; + +alter table techer add constraint FK_Relationship_4 foreign key (ID) + references infomation (ID) on delete restrict on update restrict; + +alter table techer add constraint FK_Relationship_9 foreign key (zid) + references root (zid) on delete restrict on update restrict; + +alter table techer1 add constraint FK_Relationship_12 foreign key (tid) + references techer (tid) on delete restrict on update restrict; + +alter table techer1 add constraint FK_Relationship_7 foreign key (cid) + references class_01 (cid) on delete restrict on update restrict; + + +``` + +## 4.小结 + + + 一切都是为了更好的工作。 \ No newline at end of file diff --git "a/\345\210\230\346\226\207\350\276\211/\347\273\274\345\220\210\347\273\203\344\271\2402-2024-10-22.md" "b/\345\210\230\346\226\207\350\276\211/\347\273\274\345\220\210\347\273\203\344\271\2402-2024-10-22.md" new file mode 100644 index 0000000000000000000000000000000000000000..f7c77426681d78a3408b7bba0056d2ea74b1bae8 --- /dev/null +++ "b/\345\210\230\346\226\207\350\276\211/\347\273\274\345\220\210\347\273\203\344\271\2402-2024-10-22.md" @@ -0,0 +1,512 @@ +# 综合练习2 + +## 1.分析 + +### 需求 + +```sql +# 二、综合题 +-- 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 +# 答题区: +-- ----------------------------------------------------------------- + +-- ----------------------------------------------------------------- + +-- 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 | ++------+----------+ + +# 答题区: +-- ----------------------------------------------------------------- + +-- ----------------------------------------------------------------- + +-- 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 | ++-------------+------------+ +# 答题区: +-- ----------------------------------------------------------------- + +-- ----------------------------------------------------------------- + +-- 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 | ++----------+------------+-----------+-----------+--------+------------+ + +# 答题区: +-- ----------------------------------------------------------------- + +-- ----------------------------------------------------------------- + + +-- 5. 描述(30分) +-- 开发组收到M公司的开发邀约,拟开发一套OA办公自动化系统,该系统的部分功能及初步需求分析的结果如下 : +-- +-- ``` +-- 部门:部门号、部门名、主管、联系电话、邮箱 +-- 员工:工号、姓名、职位、手机号码、薪资 +-- 用户:用户号、用户名、银行账号、手机号码、联系地址 +-- 用户申请:申请号、用户号、会议日期、天数、参会人数、地点、预算和受理标志 +-- ``` +-- +-- (1)M公司旗下有业务部、策划部和其他部门。每个部门只有一名主管,只负责管理本部门的工作,且主管参照员工关系的员工号;一个部门有多名员工,每名员工属于且仅属于一个部门。 +-- +-- (2)员工职位包括主管、业务员、 策划员等。业务员负责受理用户申请,设置受理标志。一名业务员可以受理多个用户申请,但一个用户申请只能由一名业务员受理。 +-- +-- (3)用户号唯一标识用户信息中的每一个用户。 +-- +-- (4)用户申请信息的受理标志有:已申请(默认)、已拒绝、已受理、延期受理、已过期。申请号唯一标识用户申请信息中的每一个申请,且一个用户可以提交多个申请,但一个用户申请只对应一个用户号。 +-- +-- 題目1(10分):使用PowerDesigner,设计可以使用的数据库物理模型,要求中文名称和英文名称清楚(英文不知道的可以借助翻译软件),表关系准确;利用设计好的模型生成sql语句,创建一个名为OADatabase的数据库,备用; +-- 题目2(10分):为了保证当用户申请会议,受理标志为 已申请,请设计一个触发器,当用户申请信息表插入数据的时候,可以判断其默认受理标志是否为:已申请,如果是,则无需处理允许正常插入,如果不是则重新个性受理标志为:已申请; + +# 答题区: +-- ----------------------------------------------------------------- + +-- ----------------------------------------------------------------- + +-- 题目3(10分):当业务员受理用户申请的时候,需要将当前处理的记录中受理标志修改为:已受理,同时将其它所有状态为:已申请,且会议日期已超过的记录中的受理标志个性为:已过期,请设计一个存储过程来处理此业务; + +# 答题区: +-- ----------------------------------------------------------------- + +-- ----------------------------------------------------------------- + +``` + +### 数据来源 + +``` + +``` + +## 2.SQL + +### 任务I + +```sql +DROP DATABASE IF EXISTS case_db; +CREATE DATABASE IF NOT EXISTS case_db; +USE case_db; +# 二、综合题 +-- 1. 描述(10分) +-- 针对如下表Author结构创建索引,写出创建索引的SQL语句: +DROP TABLE IF EXISTS Author; +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中。 +-- -- 职员表 +DROP TABLE IF EXISTS Employees; +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 | ++------+----------+ + +# 答题区: +-- ----------------------------------------------------------------- +CREATE TRIGGER trg_AuditLog AFTER INSERT ON Employees FOR EACH ROW INSERT INTO AuditLog (NAME,Salary)VALUES (new.NAME,new.SALARY); +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 | ++-------------+------------+ +# 答题区: +-- ----------------------------------------------------------------- +DROP VIEW IF EXISTS vw_Author; +CREATE VIEW vw_Author AS SELECT FirstName v_FirstName,LastName v_LastName FROM Author; +select * from vw_Author +-- ----------------------------------------------------------------- + +-- 4. 描述(10分) +-- 使用以下数据,完成相关操作 +-- -- 新建表 +DROP TABLE IF EXISTS user; + +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,CASE + WHEN Gender = 'M' THEN + '女' + ELSE + '男' +END AS Gender +,HireDate FROM user; +-- ----------------------------------------------------------------- + +``` + +### 任务II + +```sql +-- 5. 描述(30分) +-- 开发组收到M公司的开发邀约,拟开发一套OA办公自动化系统,该系统的部分功能及初步需求分析的结果如下 : +-- +-- ``` +-- 部门:部门号、部门名、主管、联系电话、邮箱 +-- 员工:工号、姓名、职位、手机号码、薪资 +-- 用户:用户号、用户名、银行账号、手机号码、联系地址 +-- 用户申请:申请号、用户号、会议日期、天数、参会人数、地点、预算和受理标志 +-- ``` +-- +-- (1)M公司旗下有业务部、策划部和其他部门。每个部门只有一名主管,只负责管理本部门的工作,且主管参照员工关系的员工号;一个部门有多名员工,每名员工属于且仅属于一个部门。 +-- +-- (2)员工职位包括主管、业务员、 策划员等。业务员负责受理用户申请,设置受理标志。一名业务员可以受理多个用户申请,但一个用户申请只能由一名业务员受理。 +-- +-- (3)用户号唯一标识用户信息中的每一个用户。 +-- +-- (4)用户申请信息的受理标志有:已申请(默认)、已拒绝、已受理、延期受理、已过期。申请号唯一标识用户申请信息中的每一个申请,且一个用户可以提交多个申请,但一个用户申请只对应一个用户号。 + +-- 題目1(10分):使用PowerDesigner,设计可以使用的数据库物理模型,要求中文名称和英文名称清楚(英文不知道的可以借助翻译软件),表关系准确;利用设计好的模型生成sql语句,创建一个名为OADatabase的数据库,备用; +``` + + + +#### 模型 + +C-D![cd_2024-10-22_15-43-22](https://gitee.com/yuwuchuji_0/open-source-image-library/raw/master/image/upgit_20241022_1729583068.png) + +L-D![ld_2024-10-22_15-43-44](https://gitee.com/yuwuchuji_0/open-source-image-library/raw/master/image/upgit_20241022_1729583076.png) + +P-D![pd_2024-10-22_15-43-55](https://gitee.com/yuwuchuji_0/open-source-image-library/raw/master/image/upgit_20241022_1729583090.png) + +#### 模型sql + +``` +DROP DATABASE IF EXISTS OADatabase; +CREATE DATABASE IF NOT EXISTS OADatabase; +USE OADatabase; + +drop table if exists User_Apply; + +drop table if exists department; + +drop table if exists employee; + +drop table if exists job; + +drop table if exists user; + +drop table if exists user_accept_sign; + +/*==============================================================*/ +/* Table: User_Apply */ +/*==============================================================*/ +create table User_Apply +( + usap_id int not null, + user_id int not null, + usap_date date, + usap_daynum int, + usap_toll int, + usap_adress varchar(255), + usap_budget decimal(10,2), + sign_id int not null, + primary key (usap_id) +); + +/*==============================================================*/ +/* Table: department */ +/*==============================================================*/ +create table department +( + depa_id int not null, + depa_name varchar(255), + mgrempl_id int not null, + depa_phone bigint, + depa_eamail varchar(255), + primary key (depa_id) +); + +/*==============================================================*/ +/* Table: employee */ +/*==============================================================*/ +create table employee +( + empl_id int not null, + empl_name varchar(255), + job_id int not null, + empl_phone bigint, + empl_salary decimal(10,2), + depa_id int not null, + primary key (empl_id) +); + +/*==============================================================*/ +/* Table: job */ +/*==============================================================*/ +create table job +( + job_id int not null, + job_name varchar(255), + primary key (job_id) +); + +/*==============================================================*/ +/* Table: user */ +/*==============================================================*/ +create table user +( + user_id int not null, + user_name varchar(255), + user_bank_id bigint, + user_phone bigint, + user_adress varchar(255), + job_id int not null, + primary key (user_id) +); + +/*==============================================================*/ +/* Table: user_accept_sign */ +/*==============================================================*/ +create table user_accept_sign +( + sign_id int not null, + sign_name varchar(255), + primary key (sign_id) +); + +alter table User_Apply add constraint FK_accept_sign foreign key (sign_id) + references user_accept_sign (sign_id) on delete restrict on update restrict; + +alter table User_Apply add constraint FK_user_apply foreign key (user_id) + references user (user_id) on delete restrict on update restrict; + +alter table department add constraint FK_depa_mgrempl_id foreign key (mgrempl_id) + references employee (empl_id) on delete restrict on update restrict; + +alter table employee add constraint FK_Relationship_1 foreign key (job_id) + references job (job_id) on delete restrict on update restrict; + +alter table employee add constraint FK_empl_depa foreign key (depa_id) + references department (depa_id) on delete restrict on update restrict; + +alter table user add constraint FK_job_user foreign key (job_id) + references job (job_id) on delete restrict on update restrict; +``` + +#### sql2 + +```sql + +-- 题目2(10分):为了保证当用户申请会议,受理标志为 已申请,请设计一个触发器,当用户申请信息表插入数据的时候,可以判断其默认受理标志是否为:已申请,如果是,则无需处理允许正常插入,如果不是则重新个性受理标志为:已申请; + +# 答题区: +-- ----------------------------------------------------------------- +INSERT INTO user_accept_sign (sign_name) VALUES('已申请'),('已拒绝'),('已受理'),('延期受理'),('已过期'); +INSERT INTO job (job_name)VALUES('主管'),('业务员'),('策划员');-- 1主管、2业务员、3策划员 + +DROP TRIGGER IF EXISTS IF_apply; + +CREATE TRIGGER IF NOT EXISTS IF_apply BEFORE INSERT ON user_apply FOR EACH ROW +BEGIN + DECLARE sign_sta INT DEFAULT(0); + SELECT sign_id INTO sign_sta FROM user_apply; + + IF sign_sta <> 1 THEN -- 1为'已申请'的状态ID(1已申请(默认)、2已拒绝、3已受理、4延期受理、5已过期) + SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '只允许添加已申请的数据'; + END IF; + +END; + + +INSERT INTO user (user_name,user_bank_id,user_phone,user_adress,job_id)VALUES('user1',12354647,12345678912,'龙岩',3) + +INSERT INTO user_apply (user_id,usap_date,usap_daynum,usap_toll,usap_adress,usap_budget,sign_id)VALUES(1,NOW(),1,1,'龙岩',6000,1); +INSERT INTO user_apply (user_id,usap_date,usap_daynum,usap_toll,usap_adress,usap_budget,sign_id)VALUES(1,NOW(),1,1,'龙岩',700,5); +-- ----------------------------------------------------------------- + +-- 题目3(10分):当业务员受理用户申请的时候,需要将当前处理的记录中受理标志修改为:已受理,同时将其它所有状态为:已申请,且会议日期已超过的记录中的受理标志个性为:已过期,请设计一个存储过程来处理此业务; + +# 答题区: +-- ----------------------------------------------------------------- +DROP PROCEDURE IF EXISTS deal_transaction; + +CREATE PROCEDURE IF NOT EXISTS deal_transaction(IN userid INT) +BEGIN + DECLARE date_day DATETIME; + SELECT usap_date INTO date_day FROM user_apply; + + IF date_day < NOW() THEN + UPDATE user_apply SET sign_id = 5 WHERE usap_id = userid;-- 5为'已申请'的状态ID(1已申请(默认)、2已拒绝、3已受理、4延期受理、5已过期) +ELSE + UPDATE user_apply SET sign_id = 3 WHERE usap_id = userid;-- 3为'已申请'的状态ID(1已申请(默认)、2已拒绝、3已受理、4延期受理、5已过期) +END IF; + +END; +-- ----------------------------------------------------------------- +``` + +## 3.末尾 + + + +一切为了更好的就业---Lyy \ No newline at end of file