我正在尝试为admin
创建会话,并且user
这两个表都与具有相同外键的登录表相关联 - 但是当我为admin
创建了一个“我”时,我收到错误PL/SQL: ORA-02291: integrity constraint (login_fk_user) violated - parent key not found
,在我link中的另一个问题中,我没有以最好的方式解释,但得到了一些关于使用(派对 - 角色 - 关系模型)的想法,而这个代码在他的下面更多与已解决的问题link相关
创建表admin
和主键
/* table admin*/
create table admin (id_admin number(10) not null,
admin_name varchar(30) not null);
/* primary key */
alter table admin add constraint admin_pk primary key (id_admin);
创建表users
和主键
/* table user*/
create table users (id_user number(10) not null,
user_name varchar(30) not null);
/* primary key */
alter table users add constraint user_pk primary key (id_user);
创建表login
以及主键和外键
/* table login*/
create table login(id_login number(10) not null,
id_admin_user_login number(10) not null,
email_login varchar(20) not null,
password_login varchar(10) not null);
/* primary key */
alter table login add constraint login_pk primary key (id_login);
/* foreign key reference to admin*/
alter table login add constraint login_fk_admin foreign key (id_admin_user_login)
reference admin(id_admin);
/* foreign key reference to user*/
alter table login add constraint login_fk_user foreign key (id_admin_user_login)
reference users(id_user);
使用表admin/login
为管理员创建会话的过程:
PROCEDURE create_admin_session( p_name IN VARCHAR2,
p_email IN VARCHAR2,
p_pass IN VARCHAR2,
p_error OUT NUMBER,
p_msg_error OUT VARCHAR2)
IS
BEGIN
p_error := 0;
INSERT ALL
INTO admin (id_admin, admin_name) VALUES(seq_admin.NEXTVAL, p_name)
INTO login(id_login, id_admin_user_login, email_login, pass_login)
VALUES(seq_login.NEXTVAL, seq_admin.CURRVAL, p_email, p_pass)
SELECT * FROM DUAL;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
p_error := 1;
p_msg_error := 'Error!'||SQLERRM;
END create_admin_session;
为具有表users/login
的用户创建会话的过程:
PROCEDURE create_user_session( p_name IN VARCHAR2,
p_email IN VARCHAR2,
p_pass IN VARCHAR2,
p_error OUT NUMBER,
p_msg_error OUT VARCHAR2)
IS
BEGIN
p_error := 0;
INSERT ALL
INTO users (id_user, user_name) VALUES(seq_user.NEXTVAL, p_name)
INTO login(id_login, id_admin_user_login, email_login, pass_login)
VALUES(seq_login.NEXTVAL, seq_user.CURRVAL, p_email, p_pass)
SELECT * FROM DUAL;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
p_error := 1;
p_msg_error := 'Error!'||SQLERRM;
END create_user_session;
当我创建管理会话时,收到错误ORA-02291: integrity constraint (login_fk_user) violated - parent key not found
我做错了什么?
答案 0 :(得分:1)
在表格登录中,我为用户id not null
添加了两个id_user_login number(10) null
,为管理员id_admin_login number(10) null
添加了第二个
/* table login*/
create table login(id_login number(10) not null,
id_admin_login number(10) null,
id_user_login number(10) null,
email_login varchar(20) not null,
password_login varchar(10) not null);
/* primary key */
alter table login add constraint login_pk primary key (id_login);
/* foreign key reference to admin*/
alter table login add constraint login_fk_admin foreign key (id_admin_login)
reference admin(id_admin) ON DELETE CASCADE;
/* foreign key reference to user*/
alter table login add constraint login_fk_user foreign key (id_user_login)
reference users(id_user) ON DELETE CASCADE;
使用表admin/login
为管理员创建会话的过程:
PROCEDURE create_admin_session( p_name IN VARCHAR2,
p_email IN VARCHAR2,
p_pass IN VARCHAR2,
p_error OUT NUMBER,
p_msg_error OUT VARCHAR2)
IS
BEGIN
p_error := 0;
INSERT ALL
INTO admin (id_admin, admin_name) VALUES(seq_admin.NEXTVAL, p_name)
INTO login(id_login, id_admin_login, id_user_login, email_login, pass_login)
VALUES(seq_login.NEXTVAL, seq_admin.CURRVAL, NULL, p_email, p_pass)
SELECT * FROM DUAL;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
p_error := 1;
p_msg_error := 'Error!'||SQLERRM;
END create_admin_session;
为具有表users/login
的用户创建会话的过程:
PROCEDURE create_user_session( p_name IN VARCHAR2,
p_email IN VARCHAR2,
p_pass IN VARCHAR2,
p_error OUT NUMBER,
p_msg_error OUT VARCHAR2)
IS
BEGIN
p_error := 0;
INSERT ALL
INTO users (id_user, user_name) VALUES(seq_user.NEXTVAL, p_name)
INTO login(id_login, id_admin_login, id_user_login, email_login, pass_login)
VALUES(seq_login.NEXTVAL, NULL, seq_user.CURRVAL, p_email, p_pass)
SELECT * FROM DUAL;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
p_error := 1;
p_msg_error := 'Error!'||SQLERRM;
END create_user_session;
通过这个决议,一切都在完美,如果我需要drop user/login
就像我想要的那样工作