ORA-02291: 违反了完整性约束 () - 未找到父键
ORA-02291: integrity constraint () violated - parent key not found
我正在尝试为 admin
和 user
创建会话,这两个 table 都关联到具有相同外键的登录 table - 但是当我创建admin
的问题 PL/SQL: ORA-02291: integrity constraint (login_fk_user) violated - parent key not found
我在这个 , I did not explain in the best way, but got some ideas on using (party-role-relationship model) and this code below his more related to the question 的另一个问题中遇到了错误
创建tableadmin
和主键
/* 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);
创建tableusers
和主键
/* 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);
创建tablelogin
以及主键和外键
/* 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);
使用 table 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;
为 table 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
我做错了什么?
在 table 登录中,我添加了两个 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;
使用 table 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;
为 table 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
可以按我想要的方式工作
我正在尝试为 admin
和 user
创建会话,这两个 table 都关联到具有相同外键的登录 table - 但是当我创建admin
的问题 PL/SQL: ORA-02291: integrity constraint (login_fk_user) violated - parent key not found
我在这个
创建tableadmin
和主键
/* 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);
创建tableusers
和主键
/* 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);
创建tablelogin
以及主键和外键
/* 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);
使用 table 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;
为 table 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
我做错了什么?
在 table 登录中,我添加了两个 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;
使用 table 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;
为 table 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
可以按我想要的方式工作