diff --git a/1012.md b/1012.md new file mode 100644 index 0000000000000000000000000000000000000000..e6fae6156a4af004451bcdb7953e35ae8f841420 --- /dev/null +++ b/1012.md @@ -0,0 +1,167 @@ +存储函数 + +``` +create function 函数名(参数名 参数类型,……) +returns 返回值类型 #返回值类型只能有一个 +deterministic contains sql [no sql]#明确包含【不包含sql语句】 +[characterustics……] +begin +函数体 #函数体中肯定有return语句,结果只能是一行一列或者某个确定值 +end +``` + +游标 + +``` +#1.声明游标 +declare 游标名 cursor for select * from student; +#2.打开游标 +open 游标名; +#3.使用游标 +fetch 游标名 into 变量1,变量...; +# 注意: +# 变量的个数是根据定义游标时查询的字段个数决定 +# 每 fetch 一次取一行,依次向下取 + +#4.关闭游标 +close 游标名; +``` + +异常处理 + +``` + /* + 1.异常处理完之后程序该怎么执行 + continue : 继续执行剩余的代码 + exit : 直接终止程序 + undo :不支持 + 2.触发条件 + 条件码:1329 等 + 条件名:SQLWARNING NOT FOUND SQLEXCEPTION + 3.异常触发之后执行什么代码 + 设置 flag 的值为 0 + * +``` + +##### 作业 + +```mysql +-- -- 部门表 + create table dept( + deptno int primary key auto_increment, -- 部门编号 + dname varchar(14) , -- 部门名字 + loc varchar(13) -- 地址 + ) ; + -- 员工表 + create table emp( + empno int primary key auto_increment,-- 员工编号 + ename varchar(10), -- 员工姓名 - + job varchar(9), -- 岗位 + mgr int, -- 直接领导编号 + hiredate date, -- 雇佣日期,入职日期 + sal int, -- 薪水 + comm int, -- 提成 + deptno int not null, -- 部门编号 + foreign key (deptno) references dept(deptno) + ); + insert into dept values(10,'财务部','北京'); + insert into dept values(20,'研发部','上海'); + insert into dept values(30,'销售部','广州'); + insert into dept values(40,'行政部','深圳'); + insert into emp values(7369,'刘一','职员',7902,'1980-12-17',800,null,20); + insert into emp values(7499,'陈二','推销员',7698,'1981-02-20',1600,300,30); + insert into emp values(7521,'张三','推销员',7698,'1981-02-22',1250,500,30); + insert into emp values(7566,'李四','经理',7839,'1981-04-02',2975,null,20); + insert into emp values(7654,'王五','推销员',7698,'1981-09-28',1250,1400,30); + insert into emp values(7698,'赵六','经理',7839,'1981-05-01',2850,null,30); + insert into emp values(7782,'孙七','经理',7839,'1981-06-09',2450,null,10); + insert into emp values(7788,'周八','分析师',7566,'1987-06-13',3000,null,20); + insert into emp values(7839,'吴九','总裁',null,'1981-11-17',5000,null,10); + insert into emp values(7844,'郑十','推销员',7698,'1981-09-08',1500,0,30); + insert into emp values(7876,'郭十一','职员',7788,'1987-06-13',1100,null,20); + insert into emp values(7900,'钱多多','职员',7698,'1981-12-03',950,null,30); + insert into emp values(7902,'大锦鲤','分析师',7566,'1981-12-03',3000,null,20); + insert into emp values(7934,'木有钱','职员',7782,'1983-01-23',1300,null,10); + + +-- 1.利用游标,查询并计算出emp表中全公司的总薪资(salary和comm) +drop procedure if exists pzy; + +delimiter // + +create procedure pzy(out all_salary int) +begin +-- 1.定义变量 +declare usal int; +declare com int; +-- 5.定义一个变量,用于循环使用 +declare usum int default 0; +-- 6.定义一个初始值的循环值 +declare i int default 1; +-- 2.声明游标 +declare ub cursor for select sal,ifnull(comm,0) from emp; +-- 9.异常处理 +declare continue handler for 1329 set i=2; +-- 3.打开游标 +open ub; +-- 4.使用游标,循环 + while i=1 do +-- 获取salary,com的值 + fetch ub into usal,com; +-- 将每一次的usum与salay com进行累加 + set usum=usum+usal+com; +-- 结束循环 + end while; +-- 7.关闭游标 +close ub; +-- 赋值给总工资 +set all_salary=usum; +end// +delimiter ; +call pzy(@alll); +select @alll; + + + +-- 2.利用游标修改表格,如果sal<1000,则删除工资为此值的员工,如果1000 < sal <= 3000,该员工薪资涨100,并将复制到新表,否则扣工资100 +drop procedure if exists p11; +delimiter // +create procedure p11() +begin +-- 定义变量 +declare uename varchar(20); +declare usal int; +declare i int default 1; + +-- 声明游标 +declare ub cursor for select ename,sal from emp; +-- 打开游标 +open ub; + # 创建一个新表 + drop table if exists new_emp; + create table new_emp( + empno int primary key auto_increment,-- 员工编号 + ename varchar(10), -- 员工姓名 + sal int, -- 薪水 + ); +-- 使用游标 +while i=1 do +fetch ub into uename,usal; +-- 如果sal<1000,则删除工资为此值的员工 +if sal <1000 then delete from emp where ename=uename;select concat(e_name,'员工薪资低于1000已被删除') 已删除员工; +-- 如果1000 < sal <= 3000,该员工薪资涨100,并将复制到新表,否则扣工资100 +elseif 1000 < sal <= 3000 then +set usal=usal+100; +insert into new_emp values(e_id,e_name,salary); +else +# 否则大于3000,扣工资100 +set salary = salary - 100; +insert into new_emp values(e_id,e_name,salary); +end if; +end while; +#关闭游标 +close ub; +end// +delimiter ; +call p11(); +``` \ No newline at end of file diff --git a/1013.md b/1013.md new file mode 100644 index 0000000000000000000000000000000000000000..ecce1da2b4aa35da2f67115e98d6ab558c6df1ea --- /dev/null +++ b/1013.md @@ -0,0 +1,165 @@ +触发器(trigger)是与表有关的数据库对象,指在insert/update/delete之前(BEFORE)或之后(AFTER),触发并执行触发器中定义的SQL语句集合。 + +事件A 对user表新增一条数据 姓名name 年龄age 性别sex + +事件B 对userlogs记录一条user表的操作 new.name,new.age + +触发器的这种特性可以协助应用在数据库端确保数据的完整性 , 日志记录 , 数据校验等操作。 + +使用别名OLD和NEW来引用触发器中发生变化的记录内容,这与其他的数据库是相似的。**触发器只支持行级触发** + +### 触发器的类型 + +| 类型 | NEW和OLD | +| ------ | ---------------------------------------- | +| insert | new代表将要新增或已新增的数据 | +| update | old代表更新前的数据、new代表更新后的数据 | +| delete | old代表将要删除或已删除的数据 | + +### 语法 + +创建触发器 + +``` +create trigger 触发器名称 +before/after(触发时机) insert/update/delete(触发类型) +on 表名 for each row -- 行级触发器 +begin + 触发的语句... +end; +``` + +查看 + +``` +show triggers; +``` + +删除 + +``` +drop trigger 触发器名称; +``` + +### 案例 + +通过触发器记录 tb_user 表的数据变更日志,将变更日志插入到日志表user_logs中, 包含增、删、改 ; + +user_logs表结构 + +``` +create table user_logs( + id int(11) primary key auto_increment, + operation varchar(20) not null comment '操作类型, insert/update/delete', + operate_time datetime not null comment '操作时间', + operate_id int(11) not null comment '操作的ID', + operate_params varchar(500) comment '操作参数' +) +``` + +A.插入数据触发器 + +B.更新数据触发器 + +C.删除数据触发器 + +### 练习 + +模拟一个食品库存表,当食品采购时,记录采购信息并更改库存。 + +##### 食品库存表 (Food): + +food_id: 食品ID (主键) food_name: 食品名称 quantity: 食品数量 + +##### 食品采购记录表 (DeliveryLog): + +log_id: 采购记录ID (主键) food_id: 食品ID (外键) quantity: 采购数量 delivery_date: 采购日期 + +``` +drop table food; +create table food( +food_id int primary key auto_increment,-- 食品ID +food_name varchar(20),-- 食品名称 +quantity int-- 食品数量 +); + +insert into food values (null,'蛋糕','20'),(null,'冰淇淋','40'); +select * from food; +drop table DeliveryLog; +create table DeliveryLog( +log_id int primary key auto_increment,-- 采购记录ID (主键) +quantity int, -- 采购数量 +delivery_date date, -- 采购日期 +id int,-- 食品ID +foreign key (id) references food(food_id) -- 食品ID (外键) +); + +-- 模拟一个食品库存表,当食品采购时,记录采购信息并更改库存。 + +drop trigger test_1; +delimiter // +create trigger test_1 +after insert on food +for each row +begin +-- 定义num,cou 两个目标表 +declare num int; +declare cou int; +-- 将指定值插入目标表中,count是计算出food表的id数 quantity是查出food id关联表 对应的数量 +select count(*) into cou from food; +select quantity into num from food where food_id=cou; +-- 将查出的id 和quantity分别插入DeliveryLog表中 +insert into DeliveryLog values(null,num,now(),cou); + end// + delimiter ; +insert into food values (null,'糖果','20') +``` + +课上练习 + +```mysql +create database zy charset utf8; +use zy; +-- 1 创建两个表a,b + +drop table if exists a; +create table a( +id int primary key auto_increment, +namea varchar(20) +); +drop table if exists b; +create table b( +id int primary key auto_increment, +nameb varchar(20) + +); +-- 2 分别创建三个触发器,监控a表对其增加,删除,修改动作,相应在b表生成一条监控数据 +drop trigger test_1; +delimiter // +create trigger test_1 +after insert on a +for each row +begin +insert into b (nameb) values ('生成一条监控数据'); +end// +delimiter; +insert into a (namea) values('反效果'); +insert into a (namea) values('大米恩本'); +insert into a (namea) values('发美女吧'); + + +select * from b; + +drop trigger test_2; +delimiter // +create trigger test_2 +after delete on a +for each row +begin +insert into b (nameb) values ('shan生成一条监控数据'); +end// +delimiter; +delete from a where namea='发美女吧'; +select * from a; +select * from b; +``` \ No newline at end of file diff --git "a/907\344\275\234\344\270\232.md" "b/907\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..aa942ea99eb8c4be4d995843e7730a84233c1dae --- /dev/null +++ "b/907\344\275\234\344\270\232.md" @@ -0,0 +1,94 @@ +##### 第一范式:要求字段的内容不可再分割,为的是保证数据的原子性 + +##### 2.第二范式:要求在满足第一范式的基础上,要求非主键字段完全依赖主键(非主键要依赖整个联合主键)而不能只依赖部分 + +##### 3.第三范式:满足第二范式的前提上,要求非主键属性要直接依赖于主键 + +```mysql +drop database if exists mxdx; +create database mxdx charset utf8; +use mxdx; + +#院系表 +create table college ( +col_id int primary key auto_increment, +col_name char(10) +); + +#专业表 +create table major ( +m_id int primary key auto_increment, +m_name char(10), +col_id int, +foreign key (col_id) references college (col_id) +); + +#班级表 +create table clazz ( +cl_id int primary key auto_increment, +cl_name char(10), +m_id int, +foreign key (m_id) references major (m_id) +); + +#课程信息 +create table course ( +cou_id int primary key auto_increment, +cou_name char(10) +); + + + +#教室 +create table classroom ( +cr_id int primary key auto_increment, +cr_address char(10) +); + + + +#课程表 +create table timetable ( +tt_id int primary key auto_increment, +tt_time char(3), +tt_order char(3), +cou_id int, +foreign key (cou_id) references course (cou_id), +cr_id int, +foreign key (cr_id) references classroom (cr_id) +); + + +#教师表 +create table teacher ( +te_id int primary key auto_increment, +te_name char(3), +te_sex char(1), +cou_id int, +foreign key (cou_id) references course (cou_id) +); + + + +#学生表 +create table student ( +st_id int primary key auto_increment, +st_name char(10), +st_sex char(1), +cl_id int, +foreign key (cl_id) references clazz (cl_id), +tt_id int, +foreign key (tt_id) references timetable (tt_id) +); + + +#选修表 +create table choose( +ch_id int primary key auto_increment, +ch_socre char(3), +tt_id int, +foreign key (tt_id) references timetable (tt_id), +st_id int, +foreign key (st_id) references student (st_id) +); +``` \ No newline at end of file diff --git "a/908\344\275\234\344\270\232.md" "b/908\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..1c070dc5249ed9982be2d974439c9a45d3dbdb2e --- /dev/null +++ "b/908\344\275\234\344\270\232.md" @@ -0,0 +1,220 @@ +.一对一 + +将任一表的主键放入另外一个表当外键 + +2.一对多 + +将一的主键当多的外键 + +3.多对多 + +引入第三张表(关系表)将前面两个表的主键当该表的外键 + +### 2.E_R图(实体关系图) + +实体、属性、关系 + +### 3.范式 + +1.第一范式 + +要求字段的内容,不可再分割,为的是保证数据的原子性 + +2.第二范式 + +要求在第一范式的基础上,要求非主键字段属性完全依赖主键(非主键要依赖整个联合主键)而不是只依赖部分(联合主键) + +完全函数依赖 定义:设X,Y是关系R的两个属性集合,X’是X的真子集,存在X→Y,但对每一个X’都有X’!→Y,则称Y完全函数依赖于X。 + +比如通过学号->姓名 + +部分函数依赖 定义:设X,Y是关系R的两个属性集合,存在X→Y,若X’是X的真子集,存在X’→Y,则称Y部分函数依赖于X。 + +需要借用知乎刘慰教师的例子用一下,自己也理解了很长时间。 + +![img](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAAAAACIM/FCAAACh0lEQVR4Ae3ch5W0OgyG4dt/mQJ2xgQPzJoM1m3AbALrxzrf28FzsoP0HykJEEAAAUQTBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEkKK0789+GK/I2ezfQB522PnS1qc8pGgXvr4tE4aY0XOUWlGImThWgyCk6DleixzE7qwBkg/MGiDPlVVAyp1VQGrPKiACDhFI6VkF5LmzCki+sg7IwDoglnVAil0IMkeG9CyUiwsxLFUVFzJJOQaKCjFCDN9RXMjIX7W6ztZXZDKKCyn8sWJvH+nca7WHDN9lROlAliPH9iRKCPI4cswFJQWxB46toLQgQ9jhn5QYZA9DOkoMUoQde5YapAxDWkoNYsOQR3KQd9CxUnIQF4S49CB9ENKlBxmDEKsFUgMCCCCAAHIrSF61f6153Ajy8nyiPr8L5MXnmm4CyT2fzN4DUvHZ+ntA2tOQBRBAAAEEEEAAAQQQ7ZBaC6TwSiDUaYHQ2yuB0MN+ft+43whyrs4rgVCjBUKTFshLC6TUAjGA3AxSaYFYLZBOC2RUAsk8h5qTg9QcbEoOsoQhQ2qQhsO5xCD5dgB5JQaZ+KBKGtKecvR81Ic0ZDjByKdDx0rSEDZ/djQbH+bkIdvfJFm98BfV8hD2zprfVdlu9PxVeyYAkciREohRAplJCaRSAplJCcQogTjSAdlyHRBvSAekJR0QRzogA+mADJkOiCPSAPEtqYBshlRAXC43hxix2QiOuEZkVERykGyNo9idIZKE0HO7XrG6OiMShlDWjstVzdPgXtUH9v0CEidAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQP4HgjZxTpdEii0AAAAASUVORK5CYII=) + +[https://img-blog.csdnimg.cn/img_convert/0099ee963b99b2a08f78af70bbcab552.png](https://gitee.com/link?target=https%3A%2F%2Fimg-blog.csdnimg.cn%2Fimg_convert%2F0099ee963b99b2a08f78af70bbcab552.png) + +码用(学号+课程),为什么要加课程呢?因为不同课程成绩是通过学号查不出来的。 + +不过用(学号+课程)当作码是不是有些问题? + +(学号+课程)->姓名,但是学号->姓名 + +(学号+课程)->系名,但是学号->系名 + +(学号+课程)->系主任,但是学号->系主任 + +这个就是部分依赖,说实话我看定义一脸懵逼。 + +要是上面那张表符合第二范式。需要将表拆分为两张表。 + +一张是 学号、课程、分数表 + +另外一张是 学号、姓名、系名、系主任表 + +![img](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAAAAACIM/FCAAACh0lEQVR4Ae3ch5W0OgyG4dt/mQJ2xgQPzJoM1m3AbALrxzrf28FzsoP0HykJEEAAAUQTBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEkKK0789+GK/I2ezfQB522PnS1qc8pGgXvr4tE4aY0XOUWlGImThWgyCk6DleixzE7qwBkg/MGiDPlVVAyp1VQGrPKiACDhFI6VkF5LmzCki+sg7IwDoglnVAil0IMkeG9CyUiwsxLFUVFzJJOQaKCjFCDN9RXMjIX7W6ztZXZDKKCyn8sWJvH+nca7WHDN9lROlAliPH9iRKCPI4cswFJQWxB46toLQgQ9jhn5QYZA9DOkoMUoQde5YapAxDWkoNYsOQR3KQd9CxUnIQF4S49CB9ENKlBxmDEKsFUgMCCCCAAHIrSF61f6153Ajy8nyiPr8L5MXnmm4CyT2fzN4DUvHZ+ntA2tOQBRBAAAEEEEAAAQQQ7ZBaC6TwSiDUaYHQ2yuB0MN+ft+43whyrs4rgVCjBUKTFshLC6TUAjGA3AxSaYFYLZBOC2RUAsk8h5qTg9QcbEoOsoQhQ2qQhsO5xCD5dgB5JQaZ+KBKGtKecvR81Ic0ZDjByKdDx0rSEDZ/djQbH+bkIdvfJFm98BfV8hD2zprfVdlu9PxVeyYAkciREohRAplJCaRSAplJCcQogTjSAdlyHRBvSAekJR0QRzogA+mADJkOiCPSAPEtqYBshlRAXC43hxix2QiOuEZkVERykGyNo9idIZKE0HO7XrG6OiMShlDWjstVzdPgXtUH9v0CEidAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQP4HgjZxTpdEii0AAAAASUVORK5CYII=) + +[https://img-blog.csdnimg.cn/img_convert/94a4230520a842bf949eb83d490ec24e.png](https://gitee.com/link?target=https%3A%2F%2Fimg-blog.csdnimg.cn%2Fimg_convert%2F94a4230520a842bf949eb83d490ec24e.png) + +3.第三范式 + +满足第二范式的前提下,要求非主键属性要直接依赖于主键(不能出现传递性依赖,被依赖可单独弄个表) + +传递函数依赖 设X,Y,Z是关系R中互不相同的属性集合,存在X→Y(Y !→X),Y→Z,则称Z传递函数依赖于X。 + +[https://blog.csdn.net/rl529014/article/details/48391465](https://gitee.com/link?target=https%3A%2F%2Fblog.csdn.net%2Frl529014%2Farticle%2Fdetails%2F48391465) + +采用这位大佬的例子 在关系R(学号 ,宿舍, 费用)中,(学号)->(宿舍),宿舍!=学号,(宿舍)->(费用),费用!=宿舍,所以符合传递函数的要求 + +第三范式 满足第二范式的条件下不存在传递函数依赖。 + +要满足第三范式,在分成两张表的时候第二张表还是有问题? + +学号->系名,系名->系主任 传递依赖。 + +需要将系名和系主任另外新建一张表。 + +![img](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAAAAACIM/FCAAACh0lEQVR4Ae3ch5W0OgyG4dt/mQJ2xgQPzJoM1m3AbALrxzrf28FzsoP0HykJEEAAAUQTBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEkKK0789+GK/I2ezfQB522PnS1qc8pGgXvr4tE4aY0XOUWlGImThWgyCk6DleixzE7qwBkg/MGiDPlVVAyp1VQGrPKiACDhFI6VkF5LmzCki+sg7IwDoglnVAil0IMkeG9CyUiwsxLFUVFzJJOQaKCjFCDN9RXMjIX7W6ztZXZDKKCyn8sWJvH+nca7WHDN9lROlAliPH9iRKCPI4cswFJQWxB46toLQgQ9jhn5QYZA9DOkoMUoQde5YapAxDWkoNYsOQR3KQd9CxUnIQF4S49CB9ENKlBxmDEKsFUgMCCCCAAHIrSF61f6153Ajy8nyiPr8L5MXnmm4CyT2fzN4DUvHZ+ntA2tOQBRBAAAEEEEAAAQQQ7ZBaC6TwSiDUaYHQ2yuB0MN+ft+43whyrs4rgVCjBUKTFshLC6TUAjGA3AxSaYFYLZBOC2RUAsk8h5qTg9QcbEoOsoQhQ2qQhsO5xCD5dgB5JQaZ+KBKGtKecvR81Ic0ZDjByKdDx0rSEDZ/djQbH+bkIdvfJFm98BfV8hD2zprfVdlu9PxVeyYAkciREohRAplJCaRSAplJCcQogTjSAdlyHRBvSAekJR0QRzogA+mADJkOiCPSAPEtqYBshlRAXC43hxix2QiOuEZkVERykGyNo9idIZKE0HO7XrG6OiMShlDWjstVzdPgXtUH9v0CEidAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQP4HgjZxTpdEii0AAAAASUVORK5CYII=) + +[https://img-blog.csdnimg.cn/img_convert/dfe5d8f3dcb41d4e1c7eebd4ee8f3335.png](https://gitee.com/link?target=https%3A%2F%2Fimg-blog.csdnimg.cn%2Fimg_convert%2Fdfe5d8f3dcb41d4e1c7eebd4ee8f3335.png) + +总结: 第一范式:简单说 列不能再分 + +第二范式:简单说 建立在第一范式基础上,消除部分依赖 + +第三范式:简单说 建立在第二范式基础上,消除传递依赖。 ———————————————— 版权声明:本文为CSDN博主「萝卜头LJW」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:[https://blog.csdn.net/u013164931/article/details/79692402](https://gitee.com/link?target=https%3A%2F%2Fblog.csdn.net%2Fu013164931%2Farticle%2Fdetails%2F79692402) + +### 概念模型 + +常见单词意思: + +seral 自增 + +Charachers 字符串 + +cocle字段名英文 + +integer 数字 + +variable char 可变长度 + +decimal 小数 + +float 整数 + +number 数字 + +#### 概念模型 + +##### 1.创建概念模型(类似ER图) + +以用户为角度(CDM) + +记得弄关系,关系表可以不弄,会自动生成 + +##### 2.转换成逻辑模型 + +以计算机的角度(LDM) + +在CDM中点tools,下一步点GLDM + +##### 3.转换物理模型 + +以数据库角度(PDM) + +tools,下一步点GPDM + +##### 4.生成DDL + +database+generation(记得改成对应mysql的版本,否则报错) + +```mysql +/*==============================================================*/ +/* DBMS name: MySQL 5.0 */ +/* Created on: 2023-09-11 20:34:27 */ +/*==============================================================*/ + + +drop table if exists Relationship_1; + +drop table if exists book; + +drop table if exists librarian; + +drop table if exists library; + +drop table if exists reader; + +/*==============================================================*/ +/* Table: Relationship_1 */ +/*==============================================================*/ +create table Relationship_1 +( + re_id int not null, + book_id int not null, + primary key (re_id, book_id) +); + +/*==============================================================*/ +/* Table: book */ +/*==============================================================*/ +create table book +( + book_id int not null, + li_id int not null, + book_name char(15) not null, + press char(10) not null, + time national varchar(15) not null, + primary key (book_id) +); + +/*==============================================================*/ +/* Table: librarian */ +/*==============================================================*/ +create table librarian +( + id int not null, + li_id int not null, + name char(5) not null, + primary key (id) +); + +/*==============================================================*/ +/* Table: library */ +/*==============================================================*/ +create table library +( + li_id int not null, + id int not null, + li_name char(10) not null, + li_address char(10) not null, + primary key (li_id) +); + +/*==============================================================*/ +/* Table: reader */ +/*==============================================================*/ +create table reader +( + re_id int not null, + re_name char(4) not null, + primary key (re_id) +); + +alter table Relationship_1 add constraint FK_Relationship_1 foreign key (re_id) + references reader (re_id) on delete restrict on update restrict; + +alter table Relationship_1 add constraint FK_Relationship_2 foreign key (book_id) + references book (book_id) on delete restrict on update restrict; + +alter table book add constraint FK_Relationship_5 foreign key (li_id) + references library (li_id) on delete restrict on update restrict; + +alter table librarian add constraint FK_Relationship_3 foreign key (li_id) + references library (li_id) on delete restrict on update restrict; + +alter table library add constraint FK_Relationship_4 foreign key (id) + references librarian (id) on delete restrict on update restrict; +``` \ No newline at end of file diff --git "a/912\344\275\234\344\270\232.md" "b/912\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..b0049daf6189e9329fd57b1140d597cec491952e --- /dev/null +++ "b/912\344\275\234\344\270\232.md" @@ -0,0 +1,218 @@ +```mysql +/*==============================================================*/ +/* DBMS name: MySQL 5.0 */ +/* Created on: 2023-09-12 11:26:47 */ +/*==============================================================*/ +drop database if exists zy; +create database zy charset utf8; +use zy; + +drop table if exists artist; + +drop table if exists country; + +drop table if exists director; + +drop table if exists language; + +drop table if exists movie; + +drop table if exists protagonist; + +drop table if exists rating; + +drop table if exists review; + +drop table if exists scriptwriter; + +drop table if exists type; + +drop table if exists user; + +/*==============================================================*/ +/* Table: artist */ +/*==============================================================*/ +create table artist +( + art_id int not null, + movie_id int not null, + pro_id int not null, + dir_id int not null, + scr_id int not null, + art_name char(10) not null, + art_work char(10) not null, + primary key (art_id) +); + +/*==============================================================*/ +/* Table: country */ +/*==============================================================*/ +create table country +( + cou_id int not null, + movie_id int not null, + cou_name char(20) not null, + primary key (cou_id) +); + + +insert into country values +(1,1,"美国,韩国"); +/*==============================================================*/ +/* Table: director */ +/*==============================================================*/ +create table director +( + dir_id int not null, + dir_name char(10) not null, + primary key (dir_id) +); + +insert into director values +(1,'席琳·宋'); +/*==============================================================*/ +/* Table: language */ +/*==============================================================*/ +create table `language` +( + lang_id int not null, + movie_id int not null, + lang char(10) not null, + primary key (lang_id) +); +insert into `language` values +(1,1,'英语,韩语'); + +/*==============================================================*/ +/* Table: movie */ +/*==============================================================*/ +create table movie +( + movie_id int not null, + movie_name char(20) not null, + release_date date not null, + length int not null, + primary key (movie_id) +); +insert into movie values +(1,'过往人生','2023-01-21',160); + +/*==============================================================*/ +/* Table: protagonist */ +/*==============================================================*/ +create table protagonist +( + pro_id int not null, + pro_name char(10) not null, + primary key (pro_id) +); +insert into protagonist values +(1,' 格蕾塔·李'), +(2,' 刘台午'), +(3,' 约翰·马加罗'), +(4,'文胜雅'), +(5,'尹智慧 '); + +/*==============================================================*/ +/* Table: rating */ +/*==============================================================*/ +create table rating +( + movie_id int not null, + user_id int not null, + user_rating char(10) not null, + primary key (movie_id, user_id) +); + +insert into rating values +(1,1,'90分'); + +/*==============================================================*/ +/* Table: review */ +/*==============================================================*/ +create table review +( + re_id int not null, + user_id int not null, + movie_id int not null, + re_name char(20) not null, + re_text char(100) not null, + re_appraise int not null, + primary key (re_id) +); +insert into review values +(1,1,1,'男人总是留守过去,女人总是面向未来','在柏林的映后采访中,一个记者提了这样一个问题:为什么在这部电影里,男人总是承受、感性、饱含爱的那一方,而女人则冷静、选择活在当下、坚持走在自己的路上',5); + + +/*==============================================================*/ +/* Table: scriptwriter */ +/*==============================================================*/ +create table scriptwriter +( + scr_id int not null, + scr_name char(10) not null, + primary key (scr_id) +); +insert into scriptwriter values +(1,'席琳·宋'); + + +/*==============================================================*/ +/* Table: type */ +/*==============================================================*/ +create table type +( + type_id int not null, + movie_id int not null, + type_name char(10) not null, + primary key (type_id) +); +insert into type values +(1,1,'剧情 / 爱情'); + + +/*==============================================================*/ +/* Table: user */ +/*==============================================================*/ +create table `user` +( + user_id int not null, + user_name char(10) not null, + primary key (user_id) +); +insert into `user` values +(1,'陈梦梦'); + +alter table artist add constraint FK_Relationship_4 foreign key (movie_id) + references movie (movie_id) on delete restrict on update restrict; + +alter table artist add constraint FK_Relationship_5 foreign key (pro_id) + references protagonist (pro_id) on delete restrict on update restrict; + +alter table artist add constraint FK_Relationship_6 foreign key (dir_id) + references director (dir_id) on delete restrict on update restrict; + +alter table artist add constraint FK_Relationship_7 foreign key (scr_id) + references scriptwriter (scr_id) on delete restrict on update restrict; + +alter table country add constraint FK_Relationship_3 foreign key (movie_id) + references movie (movie_id) on delete restrict on update restrict; + +alter table language add constraint FK_Relationship_2 foreign key (movie_id) + references movie (movie_id) on delete restrict on update restrict; + +alter table rating add constraint FK_Relationship_11 foreign key (movie_id) + references movie (movie_id) on delete restrict on update restrict; + +alter table rating add constraint FK_Relationship_12 foreign key (user_id) + references user (user_id) on delete restrict on update restrict; + +alter table review add constraint FK_Relationship_10 foreign key (movie_id) + references movie (movie_id) on delete restrict on update restrict; + +alter table review add constraint FK_Relationship_9 foreign key (user_id) + references user (user_id) on delete restrict on update restrict; + +alter table type add constraint FK_Relationship_1 foreign key (movie_id) + references movie (movie_id) on delete restrict on update restrict; +``` \ No newline at end of file diff --git "a/913\344\275\234\344\270\232.md" "b/913\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..dac35dbe7a0669cade560fc02c713eae7d1b7f6e --- /dev/null +++ "b/913\344\275\234\344\270\232.md" @@ -0,0 +1,124 @@ +```mysql +drop table if exists docter; + +drop table if exists drug; + +drop table if exists nurse; + +drop table if exists patient; + +drop table if exists shoushushi_id; + +drop table if exists sickbed; + +drop table if exists sickperson; + +/*==============================================================*/ +/* Table: docter */ +/*==============================================================*/ +create table docter +( + docter_id int not null auto_increment, + docter_name varchar(10) not null, + docter_sex char(1) not null, + docter_degree varchar(15) not null, + docter_tel numeric(8,0) not null, + primary key (docter_id) +); + +/*==============================================================*/ +/* Table: drug */ +/*==============================================================*/ +create table drug +( + drug_id int not null auto_increment, + drug_name varchar(20) not null, + drug_medication varchar(50) not null, + drug_price int not null, + primary key (drug_id) +); + +/*==============================================================*/ +/* Table: nurse */ +/*==============================================================*/ +create table nurse +( + nurse_id int not null auto_increment, + nurse_name varchar(10) not null, + primary key (nurse_id) +); + +/*==============================================================*/ +/* Table: patient */ +/*==============================================================*/ +create table patient +( + patient_id int not null auto_increment, + patient_name varchar(10) not null, + patient_address varchar(20) not null, + patient_tel numeric(20,0) not null, + primary key (patient_id) +); + +/*==============================================================*/ +/* Table: shoushushi_id */ +/*==============================================================*/ +create table shoushushi_id +( + shoushushi_id_id int not null auto_increment, + sickbed_id int not null, + sic_sickperson_id int not null, + patient_id int not null, + docter_id int not null, + shoushushi_idname varchar(10) not null, + sickperson_id int not null, + primary key (shoushushi_id_id) +); + +/*==============================================================*/ +/* Table: sickbed */ +/*==============================================================*/ +create table sickbed +( + sickbed_id int not null, + nurse_id int not null, + primary key (sickbed_id) +); + +/*==============================================================*/ +/* Table: sickperson */ +/*==============================================================*/ +create table sickperson +( + sickperson_id int not null, + patient_id int not null, + docter_id int not null, + drug_id int not null, + shoushushi_id_id int not null, + allergy char(1) not null, + sympton varchar(50) not null, + primary key (sickperson_id, patient_id, docter_id) +); + +alter table shoushushi_id add constraint FK_inhospital foreign key (sickbed_id) + references sickbed (sickbed_id) on delete restrict on update restrict; + +alter table shoushushi_id add constraint FK_operation foreign key (sic_sickperson_id, patient_id, docter_id) + references sickperson (sickperson_id, patient_id, docter_id) on delete restrict on update restrict; + +alter table sickbed add constraint FK_nursing foreign key (nurse_id) + references nurse (nurse_id) on delete restrict on update restrict; + +alter table sickperson add constraint FK_booking foreign key (patient_id) + references patient (patient_id) on delete restrict on update restrict; + +alter table sickperson add constraint FK_operation2 foreign key (shoushushi_id_id) + references shoushushi_id (shoushushi_id_id) on delete restrict on update restrict; + +alter table sickperson add constraint FK_see foreign key (docter_id) + references docter (docter_id) on delete restrict on update restrict; + +alter table sickperson add constraint FK_takedrug foreign key (drug_id) + references drug (drug_id) on delete restrict on update restrict; +![2e14b2d498fac95d7a02c2cd3d014a9.png](https://s2.loli.net/2023/09/14/PoB1vy2ENardRG5.png) +``` \ No newline at end of file