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;








0















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!!










share|improve this question






























    0















    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!!










    share|improve this question


























      0












      0








      0








      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!!










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      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






















          3 Answers
          3






          active

          oldest

          votes


















          0














          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'));





          share|improve this answer
































            0














            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






            share|improve this answer






























              0














              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;





              share|improve this answer

























                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
                );



                );













                draft saved

                draft discarded


















                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









                0














                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'));





                share|improve this answer





























                  0














                  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'));





                  share|improve this answer



























                    0












                    0








                    0







                    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'));





                    share|improve this answer















                    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'));






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 22 at 11:54

























                    answered Mar 22 at 11:48









                    Gordon LinoffGordon Linoff

                    800k37321426




                    800k37321426























                        0














                        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






                        share|improve this answer



























                          0














                          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






                          share|improve this answer

























                            0












                            0








                            0







                            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






                            share|improve this answer













                            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







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 22 at 11:50









                            Zaynul Abadin TuhinZaynul Abadin Tuhin

                            19.6k31135




                            19.6k31135





















                                0














                                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;





                                share|improve this answer





























                                  0














                                  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;





                                  share|improve this answer



























                                    0












                                    0








                                    0







                                    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;





                                    share|improve this answer















                                    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;






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Mar 22 at 13:24

























                                    answered Mar 22 at 13:13









                                    VenkatVenkat

                                    447




                                    447



























                                        draft saved

                                        draft discarded
















































                                        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.




                                        draft saved


                                        draft discarded














                                        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





















































                                        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







                                        Popular posts from this blog

                                        SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                                        용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                                        155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해