Select subquery CHECK constraint DB2
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty{ margin-bottom:0;
}
up vote
1
down vote
favorite
I have 2 tables: TVSHOW and ACTOR. I want to reject records if the inserted actor in TVSHOW does not exist in the ACTOR table using DB2.
db2 "ALTER TABLE TVSHOW ADD CONSTRAINT check_actor CHECK (actor in (select id from ACTOR))"
I keep running into, During SQL processing it returned: SQL0546N The check constraint "check_actor" is invalid.
How can I fix this?
db2 constraint
New contributor
add a comment |
up vote
1
down vote
favorite
I have 2 tables: TVSHOW and ACTOR. I want to reject records if the inserted actor in TVSHOW does not exist in the ACTOR table using DB2.
db2 "ALTER TABLE TVSHOW ADD CONSTRAINT check_actor CHECK (actor in (select id from ACTOR))"
I keep running into, During SQL processing it returned: SQL0546N The check constraint "check_actor" is invalid.
How can I fix this?
db2 constraint
New contributor
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have 2 tables: TVSHOW and ACTOR. I want to reject records if the inserted actor in TVSHOW does not exist in the ACTOR table using DB2.
db2 "ALTER TABLE TVSHOW ADD CONSTRAINT check_actor CHECK (actor in (select id from ACTOR))"
I keep running into, During SQL processing it returned: SQL0546N The check constraint "check_actor" is invalid.
How can I fix this?
db2 constraint
New contributor
I have 2 tables: TVSHOW and ACTOR. I want to reject records if the inserted actor in TVSHOW does not exist in the ACTOR table using DB2.
db2 "ALTER TABLE TVSHOW ADD CONSTRAINT check_actor CHECK (actor in (select id from ACTOR))"
I keep running into, During SQL processing it returned: SQL0546N The check constraint "check_actor" is invalid.
How can I fix this?
db2 constraint
db2 constraint
New contributor
New contributor
New contributor
asked Nov 24 at 19:33
user2251234
82
82
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
5
down vote
accepted
You cannot use a subselect in a check constraint; you can only reference columns from the same table.
However, what you want to accomplish is what referential integrity constraints, also known as foreign keys, are for.
ALTER TABLE TVSHOW ADD CONSTRAINT actor_fk FOREIGN KEY (actor) REFERENCES ACTOR(id)
Presumably you have already defined a primary key constraint on ACTOR.id
; if not, you need to define it, or a unique constraint on that column.
@MarkBarinstein thank you, corrected.
– mustaccio
Nov 25 at 14:17
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
You cannot use a subselect in a check constraint; you can only reference columns from the same table.
However, what you want to accomplish is what referential integrity constraints, also known as foreign keys, are for.
ALTER TABLE TVSHOW ADD CONSTRAINT actor_fk FOREIGN KEY (actor) REFERENCES ACTOR(id)
Presumably you have already defined a primary key constraint on ACTOR.id
; if not, you need to define it, or a unique constraint on that column.
@MarkBarinstein thank you, corrected.
– mustaccio
Nov 25 at 14:17
add a comment |
up vote
5
down vote
accepted
You cannot use a subselect in a check constraint; you can only reference columns from the same table.
However, what you want to accomplish is what referential integrity constraints, also known as foreign keys, are for.
ALTER TABLE TVSHOW ADD CONSTRAINT actor_fk FOREIGN KEY (actor) REFERENCES ACTOR(id)
Presumably you have already defined a primary key constraint on ACTOR.id
; if not, you need to define it, or a unique constraint on that column.
@MarkBarinstein thank you, corrected.
– mustaccio
Nov 25 at 14:17
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
You cannot use a subselect in a check constraint; you can only reference columns from the same table.
However, what you want to accomplish is what referential integrity constraints, also known as foreign keys, are for.
ALTER TABLE TVSHOW ADD CONSTRAINT actor_fk FOREIGN KEY (actor) REFERENCES ACTOR(id)
Presumably you have already defined a primary key constraint on ACTOR.id
; if not, you need to define it, or a unique constraint on that column.
You cannot use a subselect in a check constraint; you can only reference columns from the same table.
However, what you want to accomplish is what referential integrity constraints, also known as foreign keys, are for.
ALTER TABLE TVSHOW ADD CONSTRAINT actor_fk FOREIGN KEY (actor) REFERENCES ACTOR(id)
Presumably you have already defined a primary key constraint on ACTOR.id
; if not, you need to define it, or a unique constraint on that column.
edited Nov 25 at 14:17
answered Nov 24 at 20:03
mustaccio
8,87762135
8,87762135
@MarkBarinstein thank you, corrected.
– mustaccio
Nov 25 at 14:17
add a comment |
@MarkBarinstein thank you, corrected.
– mustaccio
Nov 25 at 14:17
@MarkBarinstein thank you, corrected.
– mustaccio
Nov 25 at 14:17
@MarkBarinstein thank you, corrected.
– mustaccio
Nov 25 at 14:17
add a comment |
user2251234 is a new contributor. Be nice, and check out our Code of Conduct.
user2251234 is a new contributor. Be nice, and check out our Code of Conduct.
user2251234 is a new contributor. Be nice, and check out our Code of Conduct.
user2251234 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f223340%2fselect-subquery-check-constraint-db2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown