Results 1 to 5 of 5

Thread: CHECK constraint problem - SQL Anywhere

  1. #1
    Join Date
    Jun 2005
    Posts
    11

    CHECK constraint problem - SQL Anywhere

    Hello All,

    I could do with a nudge in the right direction with a problem I have. I say nudge as the problem is connected to an assignment and I would like to answer it on my own.

    There is a table which holds details about the different tariffs countries' impose on imports. It has three columns - country, classification (Chemicals, Cereals or Agricultural) and tariff. The check constraint I have to add to the table is to ensure that no country's tariff for a particular classification is more than 15% higher than the average tariff for that classification.

    So far I have this:
    ALTER TABLE import_tariff
    ADD CHECK
    (tariff <=
    (SELECT AVG(tariff) * 1.15
    FROM import_tariff a
    WHERE a.classification = classification));


    This works fine when there is only one type of classification in the table. EG. if there are two countries in the table who are importing Chemicals and their tariffs are 23.00 and 24.00, then no country who imports Chemicals can be added to the table whose tariff is more than 27.02.

    The problem with the code is it will not allow a country to be added to the table with a different classification and a tariff over 27.02 - in other words the CHECK is not distinguising between classifications. I think the problem is the condition in the WHERE clause is not taking the value for classification from the row being inputted and I'm not sure how to fix this. It needs to filter out rows in the table with a different classification than the row being inputted.

    Any hints will be greatly received.

    Thanks.
    Last edited by indigodream; 07-08-2005 at 08:13 AM.

  2. #2
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    Check contraints checks the values before new row is entered. So better alternative is to write a trigger.

  3. #3
    Join Date
    Jun 2005
    Posts
    11
    Quote Originally Posted by skhanal
    Check contraints checks the values before new row is entered. So better alternative is to write a trigger.
    Thanks for that Skhanal, but the question definitely asks for a CHECK constraint. We haven't covered triggers in the course yet.

  4. #4
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    Have you tried this.

    ALTER TABLE import_tariff b
    ADD CHECK
    (b.tariff <=
    (SELECT AVG(tariff) * 1.15
    FROM import_tariff a
    WHERE a.classification = b.classification));

    Does SQL Anywhere support sub query in check constraint?

  5. #5
    Join Date
    Jun 2005
    Posts
    11
    Quote Originally Posted by skhanal
    Have you tried this.

    ALTER TABLE import_tariff b
    ADD CHECK
    (b.tariff <=
    (SELECT AVG(tariff) * 1.15
    FROM import_tariff a
    WHERE a.classification = b.classification));

    Does SQL Anywhere support sub query in check constraint?
    Thanks for trying, but it fails at the alias b on the first line.

    Sub queries are supported.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •