SQL_Add constraint and check the string using LIKE Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to concatenate text from multiple rows into a single text string in SQL server?How to query MongoDB with “like”?SQL for Oracle to check if a constraint existsthe alter command conflicted with the check constraint (but no rows violate constraint)Check constraint on datecheck for valid date which is declared in varchar2I keep getting errors in my procedure that help add new course and i have put requirements in descriptionOracle SQL add check constraint at multiple table levelConstraints interdependent tablesChange datatype without changing “not null” constraint
Did Mueller's report provide an evidentiary basis for the claim of Russian govt election interference via social media?
How were pictures turned from film to a big picture in a picture frame before digital scanning?
What is the difference between CTSS and ITS?
Flight departed from the gate 5 min before scheduled departure time. Refund options
Omitting the following parentheses
In musical terms, what properties are varied by the human voice to produce different words / syllables?
What is the origin of 落第?
As a dual citizen, my US passport will expire one day after traveling to the US. Will this work?
Project Euler #1 in C++
Is multiple magic items in one inherently imbalanced?
Tannaka duality for semisimple groups
Did any compiler fully use 80-bit floating point?
Random body shuffle every night—can we still function?
How many morphisms from 1 to 1+1 can there be?
Nose gear failure in single prop aircraft: belly landing or nose-gear up landing?
Special flights
Why do early math courses focus on the cross sections of a cone and not on other 3D objects?
How would a mousetrap for use in space work?
Universal covering space of the real projective line?
How to ternary Plot3D a function
Do I really need to have a message in a novel to appeal to readers?
Is it dangerous to install hacking tools on my private linux machine?
How can I prevent/balance waiting and turtling as a response to cooldown mechanics
Drawing a ribbon graph
SQL_Add constraint and check the string using LIKE
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to concatenate text from multiple rows into a single text string in SQL server?How to query MongoDB with “like”?SQL for Oracle to check if a constraint existsthe alter command conflicted with the check constraint (but no rows violate constraint)Check constraint on datecheck for valid date which is declared in varchar2I keep getting errors in my procedure that help add new course and i have put requirements in descriptionOracle SQL add check constraint at multiple table levelConstraints interdependent tablesChange datatype without changing “not null” constraint
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am using Oracle SQL.
Here is the table rental, and the CC_Type column means the credit card type. After creating the table, I want to add a constraint to make sure the credit card is either 'credit' or 'debit'
CREATE TABLE rental
( Rental_Num VARCHAR2(5) CONSTRAINT rental_PK PRIMARY KEY,
Rent_Date DATE DEFAULT SYSDATE,
Credit_Card_Num CHAR(16),
CC_Type CHAR(7),
Member_ID VARCHAR2(5)
);
Therefore, I try to write:
ALTER TABLE RENTAL
2 ADD CONSTRAINT CC_TYPE_CK
3 CHECK(CC_TYPE LIKE 'Credit' OR CC_TYPE LIKE 'Debit');
But SQL yield: cannot validate (SYSTEM.CC_TYPE_CK) - check constraint violated
I don't understand it violated what? And how to fix it?
Thanks!!
sql oracle
add a comment |
I am using Oracle SQL.
Here is the table rental, and the CC_Type column means the credit card type. After creating the table, I want to add a constraint to make sure the credit card is either 'credit' or 'debit'
CREATE TABLE rental
( Rental_Num VARCHAR2(5) CONSTRAINT rental_PK PRIMARY KEY,
Rent_Date DATE DEFAULT SYSDATE,
Credit_Card_Num CHAR(16),
CC_Type CHAR(7),
Member_ID VARCHAR2(5)
);
Therefore, I try to write:
ALTER TABLE RENTAL
2 ADD CONSTRAINT CC_TYPE_CK
3 CHECK(CC_TYPE LIKE 'Credit' OR CC_TYPE LIKE 'Debit');
But SQL yield: cannot validate (SYSTEM.CC_TYPE_CK) - check constraint violated
I don't understand it violated what? And how to fix it?
Thanks!!
sql oracle
add a comment |
I am using Oracle SQL.
Here is the table rental, and the CC_Type column means the credit card type. After creating the table, I want to add a constraint to make sure the credit card is either 'credit' or 'debit'
CREATE TABLE rental
( Rental_Num VARCHAR2(5) CONSTRAINT rental_PK PRIMARY KEY,
Rent_Date DATE DEFAULT SYSDATE,
Credit_Card_Num CHAR(16),
CC_Type CHAR(7),
Member_ID VARCHAR2(5)
);
Therefore, I try to write:
ALTER TABLE RENTAL
2 ADD CONSTRAINT CC_TYPE_CK
3 CHECK(CC_TYPE LIKE 'Credit' OR CC_TYPE LIKE 'Debit');
But SQL yield: cannot validate (SYSTEM.CC_TYPE_CK) - check constraint violated
I don't understand it violated what? And how to fix it?
Thanks!!
sql oracle
I am using Oracle SQL.
Here is the table rental, and the CC_Type column means the credit card type. After creating the table, I want to add a constraint to make sure the credit card is either 'credit' or 'debit'
CREATE TABLE rental
( Rental_Num VARCHAR2(5) CONSTRAINT rental_PK PRIMARY KEY,
Rent_Date DATE DEFAULT SYSDATE,
Credit_Card_Num CHAR(16),
CC_Type CHAR(7),
Member_ID VARCHAR2(5)
);
Therefore, I try to write:
ALTER TABLE RENTAL
2 ADD CONSTRAINT CC_TYPE_CK
3 CHECK(CC_TYPE LIKE 'Credit' OR CC_TYPE LIKE 'Debit');
But SQL yield: cannot validate (SYSTEM.CC_TYPE_CK) - check constraint violated
I don't understand it violated what? And how to fix it?
Thanks!!
sql oracle
sql oracle
edited Mar 22 at 12:05
a_horse_with_no_name
309k46471574
309k46471574
asked Mar 22 at 11:46
Shin Yu WuShin Yu Wu
1478
1478
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You apparently have data in the table that violates the constraint. Your code basically works; here is a db<>fiddle (this fixes the extra comma before the closing paren in the create table statement).
So, look for the bad data:
select r.*
from rental r
where r.cc_type not in ('Credit', 'Debit');
I would also write the constraint using in . . . it is simpler:
ALTER TABLE RENTAL
ADD CONSTRAINT CC_TYPE_CK
CHECK (CC_TYPE IN ('Credit', 'Debit'));
add a comment |
just remove , after Member_ID VARCHAR2(5) other wise your query working fine
CREATE TABLE rental
( Rental_Num VARCHAR2(5) CONSTRAINT rental_PK PRIMARY KEY,
Rent_Date DATE DEFAULT SYSDATE,
Credit_Card_Num CHAR(16),
CC_Type CHAR(7),
Member_ID VARCHAR2(5)
);
ALTER TABLE RENTAL
ADD CONSTRAINT CC_TYPE_CK
CHECK(CC_TYPE LIKE 'Credit' OR CC_TYPE LIKE 'Debit');
demo link
add a comment |
I agree with Gordon Linoff, Check for the data in cc_type.
select Distinct r.cc_type
from rental r;
If you still want to create the constraint even when you have the other CC_TYPEs(in the other words, without validating the existing data) then try this
ALTER TABLE RENTAL
ADD CONSTRAINT CC_TYPE_CK
CHECK (CC_TYPE IN ('Credit', 'Debit')) NOVALIDATE;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2fstackoverflow.com%2fquestions%2f55298914%2fsql-add-constraint-and-check-the-string-using-like%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You apparently have data in the table that violates the constraint. Your code basically works; here is a db<>fiddle (this fixes the extra comma before the closing paren in the create table statement).
So, look for the bad data:
select r.*
from rental r
where r.cc_type not in ('Credit', 'Debit');
I would also write the constraint using in . . . it is simpler:
ALTER TABLE RENTAL
ADD CONSTRAINT CC_TYPE_CK
CHECK (CC_TYPE IN ('Credit', 'Debit'));
add a comment |
You apparently have data in the table that violates the constraint. Your code basically works; here is a db<>fiddle (this fixes the extra comma before the closing paren in the create table statement).
So, look for the bad data:
select r.*
from rental r
where r.cc_type not in ('Credit', 'Debit');
I would also write the constraint using in . . . it is simpler:
ALTER TABLE RENTAL
ADD CONSTRAINT CC_TYPE_CK
CHECK (CC_TYPE IN ('Credit', 'Debit'));
add a comment |
You apparently have data in the table that violates the constraint. Your code basically works; here is a db<>fiddle (this fixes the extra comma before the closing paren in the create table statement).
So, look for the bad data:
select r.*
from rental r
where r.cc_type not in ('Credit', 'Debit');
I would also write the constraint using in . . . it is simpler:
ALTER TABLE RENTAL
ADD CONSTRAINT CC_TYPE_CK
CHECK (CC_TYPE IN ('Credit', 'Debit'));
You apparently have data in the table that violates the constraint. Your code basically works; here is a db<>fiddle (this fixes the extra comma before the closing paren in the create table statement).
So, look for the bad data:
select r.*
from rental r
where r.cc_type not in ('Credit', 'Debit');
I would also write the constraint using in . . . it is simpler:
ALTER TABLE RENTAL
ADD CONSTRAINT CC_TYPE_CK
CHECK (CC_TYPE IN ('Credit', 'Debit'));
edited Mar 22 at 11:54
answered Mar 22 at 11:48
Gordon LinoffGordon Linoff
800k37321426
800k37321426
add a comment |
add a comment |
just remove , after Member_ID VARCHAR2(5) other wise your query working fine
CREATE TABLE rental
( Rental_Num VARCHAR2(5) CONSTRAINT rental_PK PRIMARY KEY,
Rent_Date DATE DEFAULT SYSDATE,
Credit_Card_Num CHAR(16),
CC_Type CHAR(7),
Member_ID VARCHAR2(5)
);
ALTER TABLE RENTAL
ADD CONSTRAINT CC_TYPE_CK
CHECK(CC_TYPE LIKE 'Credit' OR CC_TYPE LIKE 'Debit');
demo link
add a comment |
just remove , after Member_ID VARCHAR2(5) other wise your query working fine
CREATE TABLE rental
( Rental_Num VARCHAR2(5) CONSTRAINT rental_PK PRIMARY KEY,
Rent_Date DATE DEFAULT SYSDATE,
Credit_Card_Num CHAR(16),
CC_Type CHAR(7),
Member_ID VARCHAR2(5)
);
ALTER TABLE RENTAL
ADD CONSTRAINT CC_TYPE_CK
CHECK(CC_TYPE LIKE 'Credit' OR CC_TYPE LIKE 'Debit');
demo link
add a comment |
just remove , after Member_ID VARCHAR2(5) other wise your query working fine
CREATE TABLE rental
( Rental_Num VARCHAR2(5) CONSTRAINT rental_PK PRIMARY KEY,
Rent_Date DATE DEFAULT SYSDATE,
Credit_Card_Num CHAR(16),
CC_Type CHAR(7),
Member_ID VARCHAR2(5)
);
ALTER TABLE RENTAL
ADD CONSTRAINT CC_TYPE_CK
CHECK(CC_TYPE LIKE 'Credit' OR CC_TYPE LIKE 'Debit');
demo link
just remove , after Member_ID VARCHAR2(5) other wise your query working fine
CREATE TABLE rental
( Rental_Num VARCHAR2(5) CONSTRAINT rental_PK PRIMARY KEY,
Rent_Date DATE DEFAULT SYSDATE,
Credit_Card_Num CHAR(16),
CC_Type CHAR(7),
Member_ID VARCHAR2(5)
);
ALTER TABLE RENTAL
ADD CONSTRAINT CC_TYPE_CK
CHECK(CC_TYPE LIKE 'Credit' OR CC_TYPE LIKE 'Debit');
demo link
answered Mar 22 at 11:50
Zaynul Abadin TuhinZaynul Abadin Tuhin
19.6k31135
19.6k31135
add a comment |
add a comment |
I agree with Gordon Linoff, Check for the data in cc_type.
select Distinct r.cc_type
from rental r;
If you still want to create the constraint even when you have the other CC_TYPEs(in the other words, without validating the existing data) then try this
ALTER TABLE RENTAL
ADD CONSTRAINT CC_TYPE_CK
CHECK (CC_TYPE IN ('Credit', 'Debit')) NOVALIDATE;
add a comment |
I agree with Gordon Linoff, Check for the data in cc_type.
select Distinct r.cc_type
from rental r;
If you still want to create the constraint even when you have the other CC_TYPEs(in the other words, without validating the existing data) then try this
ALTER TABLE RENTAL
ADD CONSTRAINT CC_TYPE_CK
CHECK (CC_TYPE IN ('Credit', 'Debit')) NOVALIDATE;
add a comment |
I agree with Gordon Linoff, Check for the data in cc_type.
select Distinct r.cc_type
from rental r;
If you still want to create the constraint even when you have the other CC_TYPEs(in the other words, without validating the existing data) then try this
ALTER TABLE RENTAL
ADD CONSTRAINT CC_TYPE_CK
CHECK (CC_TYPE IN ('Credit', 'Debit')) NOVALIDATE;
I agree with Gordon Linoff, Check for the data in cc_type.
select Distinct r.cc_type
from rental r;
If you still want to create the constraint even when you have the other CC_TYPEs(in the other words, without validating the existing data) then try this
ALTER TABLE RENTAL
ADD CONSTRAINT CC_TYPE_CK
CHECK (CC_TYPE IN ('Credit', 'Debit')) NOVALIDATE;
edited Mar 22 at 13:24
answered Mar 22 at 13:13
VenkatVenkat
447
447
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2fstackoverflow.com%2fquestions%2f55298914%2fsql-add-constraint-and-check-the-string-using-like%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