diff --git "a/08 \345\256\230\346\226\207\350\257\232/20230913 \345\214\273\347\224\237\344\275\234\344\270\232.md" "b/08 \345\256\230\346\226\207\350\257\232/20230913 \345\214\273\347\224\237\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..018280c89e4ff83714d9076b41fb25f99bffe3dd --- /dev/null +++ "b/08 \345\256\230\346\226\207\350\257\232/20230913 \345\214\273\347\224\237\344\275\234\344\270\232.md" @@ -0,0 +1,266 @@ +## 笔记 + +如果一个主体的属性有多个值那这个属性就可以拆成一个新的主体 + +## 作业 + +![image-20230914001728162](https://s2.loli.net/2023/09/14/c67CgKyaSt8Bz9Q.png) + +~~~ mysql +/*==============================================================*/ +/* DBMS name: MySQL 5.0 */ +/* Created on: 2023/9/13 22:17:22 */ +/*==============================================================*/ +CREATE DATABASE yy charset utf8; +use yy; + +drop table if exists cases; + +drop table if exists department; + +drop table if exists diagnosis; + +drop table if exists doctor; + +drop table if exists hospital; + +drop table if exists medicine; + +drop table if exists nurse; + +drop table if exists outpatient; + +drop table if exists patient; + +drop table if exists payment; + +drop table if exists pharmacy; + +drop table if exists ward; + +/*==============================================================*/ +/* Table: cases */ +/*==============================================================*/ +create table cases +( + pat_id char(10) not null, + doc_id char(10) not null, + primary key (pat_id, doc_id) +); + +INSERT into cases VALUES +('1','3'), +('1','5'); + +/*==============================================================*/ +/* Table: department */ +/*==============================================================*/ +create table department +( + dep_id char(10) not null, + hos_id char(10), + dep_name char(20) not null, + primary key (dep_id) +); + +INSERT into department VALUES +('1','3','内科'), +('2','1','外科'); + +/*==============================================================*/ +/* Table: diagnosis */ +/*==============================================================*/ +create table diagnosis +( + pat_id char(10) not null, + nurse_id char(10) not null, + phar_id char(10) not null, + primary key (pat_id, nurse_id) +); + +INSERT into diagnosis VALUES +('1','3','2'), +('3','2','1'); + +/*==============================================================*/ +/* Table: doctor */ +/*==============================================================*/ +create table doctor +( + doc_id char(10) not null, + dep_id char(10), + out_id char(10), + doc_name char(20) not null, + doc_sex char(10) not null, + doc_age int not null, + doc_introduce varchar(255) not null, + doc_tel char(11) not null, + primary key (doc_id) +); + +INSERT into doctor VALUES +('3','1','1','周富','男',30,'帅','13121516141'), +('1','3','3','叶子豪','女',40,'好','16141519175'); + +/*==============================================================*/ +/* Table: hospital */ +/*==============================================================*/ +create table hospital +( + hos_id char(10) not null, + hos_name char(20) not null, + primary key (hos_id) +); + +INSERT into hospital VALUES +('1','1号医院'), +('3','2号医院'); + +/*==============================================================*/ +/* Table: medicine */ +/*==============================================================*/ +create table medicine +( + med_id char(10) not null, + phar_id char(10), + med_name char(20) not null, + med_time char(10) not null, + primary key (med_id) +); + +INSERT into medicine VALUES +('1','3','2','2020-2-3'), +('1','3','2','2020-3-5'); +/*==============================================================*/ +/* Table: nurse */ +/*==============================================================*/ +create table nurse +( + nurse_id char(10) not null, + phar_id char(10), + nurse_name char(20) not null, + nurse_sex char(2) not null, + nurse_age int not null, + primary key (nurse_id) +); + +INSERT into nurse VALUES +('1','2','小叶','女',30),('3','3','小涛','男',28); + + + +/*==============================================================*/ +/* Table: outpatient */ +/*==============================================================*/ +create table outpatient +( + out_id char(10) not null, + hos_id char(10), + out_name char(20) not null, + primary key (out_id) +); + +INSERT into outpatient VALUES +('1','3','发热科'), +('3','1','内科'); + +/*==============================================================*/ +/* Table: patient */ +/*==============================================================*/ +create table patient +( + pat_id char(10) not null, + ward_id char(10), + pay_id char(10), + pat_name char(10) not null, + pat_sex char(10) not null, + pat_age int not null, + pat_identity char(20) not null, + pat_tel char(11) not null, + primary key (pat_id) +); + +INSERT into patient VALUES +('1','3','2','子豪','男',20,'3500000','13658945168'),('3','1','2','小富','女',30,'31333333','16151418195'); + +/*==============================================================*/ +/* Table: payment */ +/*==============================================================*/ +create table payment +( + pay_id char(10) not null, + pay_name char(20) not null, + pay_amount numeric(8,1) not null, + primary key (pay_id) +); + +INSERT into payment VALUES +('1','120',130.8), +('2','220',1400.3); + +/*==============================================================*/ +/* Table: pharmacy */ +/*==============================================================*/ +create table pharmacy +( + phar_id char(10) not null, + phar_name char(20) not null, + primary key (phar_id) +); + +INSERT into pharmacy VALUES +('1','头孢房'), +('2','999感冒灵房'); + +/*==============================================================*/ +/* Table: ward */ +/*==============================================================*/ +create table ward +( + ward_id char(10) not null, + ward_name char(20) not null, + primary key (ward_id) +); + +INSERT into ward VALUES +('1','210'), +('2','410'); + +/* ====================================== */ +alter table cases add constraint FK_cases foreign key (pat_id) + references patient (pat_id) on delete restrict on update restrict; + +alter table cases add constraint FK_cases2 foreign key (doc_id) + references doctor (doc_id) on delete restrict on update restrict; + +alter table department add constraint FK_Relationship_8 foreign key (hos_id) + references hospital (hos_id) on delete restrict on update restrict; + +alter table diagnosis add constraint FK_diagnosis foreign key (pat_id) + references patient (pat_id) on delete restrict on update restrict; + +alter table diagnosis add constraint FK_diagnosis2 foreign key (nurse_id) + references nurse (nurse_id) on delete restrict on update restrict; + +alter table doctor add constraint FK_Relationship_1 foreign key (dep_id) + references department (dep_id) on delete restrict on update restrict; + +alter table doctor add constraint FK_Relationship_10 foreign key (out_id) + references outpatient (out_id) on delete restrict on update restrict; + +alter table medicine add constraint FK_Relationship_4 foreign key (phar_id) + references pharmacy (phar_id) on delete restrict on update restrict; + +alter table nurse add constraint FK_Relationship_7 foreign key (phar_id) + references pharmacy (phar_id) on delete restrict on update restrict; + +alter table outpatient add constraint FK_Relationship_9 foreign key (hos_id) + references hospital (hos_id) on delete restrict on update restrict; + +alter table patient add constraint FK_Relationship_3 foreign key (ward_id) + references ward (ward_id) on delete restrict on update restrict; + +alter table patient add constraint FK_Relationship_6 foreign key (pay_id) + references payment (pay_id) on delete restrict on update restrict; +~~~ +