我正在尝试创建“诊断”表(最后一个表),但是创建外键的两行代码有问题。我确信这就是问题所在,因为当我注释掉它们时,我可以创建表。可以毫无错误地创建前4个表,因此它们没有问题。谢谢您阅读我的问题。
我试图找出一些语法错误,但无济于事。
CREATE TABLE nurse(
nurse_ID number(5) not null,
nurse_name varchar2(20),
nurse_number number(10),
nurse_address varchar2(50),
CONSTRAINTS nurse_pk PRIMARY KEY (nurse_ID)
);
CREATE TABLE medicine(
med_ID number(10) not null,
med_name varchar2(30),
med_type varchar2(20),
exp_date date,
dose_lim float,
med_components varchar2(50),
CONSTRAINTS med_pk PRIMARY KEY (med_ID)
);
CREATE TABLE in_patient (
in_pat_ID number(5) not null,
in_pat_name varchar2(20),
in_pat_add varchar2(50),
in_pat_dob date,
in_pat_history varchar2(100),
in_mode_payment varchar2(20),
in_start_date date,
in_end_date date,
CONSTRAINTS in_pat_pk PRIMARY KEY (in_pat_ID)
);
CREATE TABLE out_patient (
out_pat_ID number(5) not null,
out_pat_name varchar2(20),
out_pat_add varchar2(50),
out_pat_dob date,
out_pat_history varchar2(100),
out_mode_payment varchar(20),
out_date_of_visit date,
CONSTRAINTS out_pat_pk PRIMARY KEY (out_pat_ID)
);
CREATE TABLE diagnosis(
dia_ref number(12) not null,
dia_type varchar2(20),
dia_date date,
dia_result varchar2(8), --Either Negative or positive.
CONSTRAINTS dia_pk PRIMARY KEY (dia_ref),
--one of the following is going to be null when you insert rows into this table since one diagnosis can only be associated with a single patient
CONSTRAINTS dia_out_fk FOREIGN KEY (out_pat_ID) REFERENCES out_patient (out_pat_ID),
CONSTRAINTS dia_in_fk FOREIGN KEY (in_pat_ID) REFERENCES in_patient (in_pat_ID)
);
我希望可以正确创建表。
答案 0 :(得分:0)
您的diagnosis
表将out_pat_ID
和in_pat_ID
用作应用作外键的列,但是这些列实际上并不存在于表定义中。尝试在此处添加它们作为一种可能的解决方法:
CREATE TABLE diagnosis (
dia_ref number(12) NOT NULL,
dia_type varchar2(20),
dia_date date,
dia_result varchar2(8),
out_pat_ID number(5),
in_pat_ID number(5)
CONSTRAINT dia_pk PRIMARY KEY (dia_ref),
CONSTRAINT dia_out_fk FOREIGN KEY (out_pat_ID) REFERENCES out_patient (out_pat_ID),
CONSTRAINT dia_in_fk FOREIGN KEY (in_pat_ID) REFERENCES in_patient (in_pat_ID)
);
答案 1 :(得分:0)
诊断表需要添加out_pat_ID和in_pat_ID列
CREATE TABLE nurse( nurse_ID number(5) not null, nurse_name varchar2(20), nurse_number number(10), nurse_address varchar2(50), CONSTRAINTS nurse_pk PRIMARY KEY (nurse_ID) );
✓
CREATE TABLE medicine( med_ID number(10) not null, med_name varchar2(30), med_type varchar2(20), exp_date date, dose_lim float, med_components varchar2(50), CONSTRAINTS med_pk PRIMARY KEY (med_ID) );
✓
CREATE TABLE in_patient ( in_pat_ID number(5) not null, in_pat_name varchar2(20), in_pat_add varchar2(50), in_pat_dob date, in_pat_history varchar2(100), in_mode_payment varchar2(20), in_start_date date, in_end_date date, CONSTRAINTS in_pat_pk PRIMARY KEY (in_pat_ID) );
✓
CREATE TABLE out_patient ( out_pat_ID number(5) not null, out_pat_name varchar2(20), out_pat_add varchar2(50), out_pat_dob date, out_pat_history varchar2(100), out_mode_payment varchar(20), out_date_of_visit date, CONSTRAINTS out_pat_pk PRIMARY KEY (out_pat_ID) );
✓
CREATE TABLE diagnosis( dia_ref number(12) not null, dia_type varchar2(20), dia_date date, dia_result varchar2(8), --Either Negative or positive. out_pat_ID number(5) not null, in_pat_ID number(5) not null, CONSTRAINTS dia_pk PRIMARY KEY (dia_ref), --one of the following is going to be null when you insert rows into this table since one diagnosis can only be associated with a single patient CONSTRAINTS dia_out_fk FOREIGN KEY (out_pat_ID) REFERENCES out_patient (out_pat_ID), CONSTRAINTS dia_in_fk FOREIGN KEY (in_pat_ID) REFERENCES in_patient (in_pat_ID) );
✓
db <>提琴here
答案 2 :(得分:0)
如果每次诊断只允许一名患者,那么您也应该具有lapply()
约束。您的评论和数据结构不同步:
tmps <- list(temp1, temp2)
do.call(cbind,
lapply(tmps,
function(tmp) {
n = length(tmp)
cov_tmp <- cov(tmp)
1 / (2*pi^(n/2)*det(cov_tmp)^(1/2))*exp(-1 / 2 * mahalanobis(data, colMeans(tmp), cov_tmp))
}
)
)
[,1] [,2]
[1,] 4.316396e-13 0.000000e+00
[2,] 6.835553e-15 7.970888e-284
[3,] 8.674921e-21 2.687251e-145
[4,] 5.923777e-19 8.020048e-189
[5,] 5.627127e-16 8.064007e-184
[6,] 2.495667e-17 5.738550e-209
[7,] 6.311390e-22 8.913098e-97
[8,] 1.413893e-12 0.000000e+00
[9,] 5.521715e-15 1.619401e-221
[10,] 5.212091e-17 5.810407e-254
也就是说,用于住院和门诊的单独表格看起来并不正确。或者,至少,您需要一个check
表,其中包含一段时间内有关某人的信息。然后,您可以使用单独的“约会”和“注册”来确定他们是进行门诊预约还是进行住院治疗注册。
答案 3 :(得分:0)
我个人更喜欢将内联的单列约束定义为列定义的一部分。然后,您不必重复列名,并且对于FK约束,您可以让该列继承其父级的数据类型,从而减少出错的可能性。
create table in_patient
( in_pat_id number(5) constraint in_pat_pk primary key
, in_pat_name varchar2(20)
, in_pat_add varchar2(50)
, in_pat_dob date
, in_pat_history varchar2(100)
, in_mode_payment varchar2(20)
, in_start_date date
, in_end_date date
);
create table out_patient
( out_pat_id number(5) constraint out_pat_pk primary key
, out_pat_name varchar2(20)
, out_pat_add varchar2(50)
, out_pat_dob date
, out_pat_history varchar2(100)
, out_mode_payment varchar(20)
, out_date_of_visit date
);
create table diagnosis
( dia_ref number(12) constraint dia_pk primary key
, dia_type varchar2(20)
, dia_date date
, dia_result varchar2(8)
, dia_out_fk references out_patient (out_pat_id)
, dia_in_fk references in_patient (in_pat_id)
);
如果dia_result
只能是'POSITIVE'
或'NEGATIVE'
,那么我建议添加一个检查约束以强制执行此操作。
可能更多的列可以定义为not null
。最好定义您可能想到的所有约束。