Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6766527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:50:31+00:00 2026-05-26T14:50:31+00:00

I wonder how to solve this problem. I got two tables Table1 ( t1_prim

  • 0

I wonder how to solve this problem.

I got two tables

Table1 (t1_prim, t1_int)
with t1_prim as primary key and t1_int not null.

Table2 (t2_prim1, t2prim2, t2_int)
with t2_prim1 and t2_prim2 as primary key and foreign key constraint t2_prim1 references t1_prim.

how can I define a constraint that makes sure, that nobody enters an integer in t2_int that is bigger than the corresponding entry in t1_int?

I tried it like this (doesn’t work because you can’t enter subqueries in check constraint):

CREATE TABLE table2 
  (t2_prim1 TEXT,
  t2_prim2 INTEGER,
  t2_int INTEGER NOT NULL,
  PRIMARY KEY (t2_prim1, t2_prim2),
  FOREIGN KEY (t2_prim1) REFERENCES table1(t1_prim),
  CHECK (t2_int2 <= (SELECT t1_int2 FROM table1 WHERE t1_int1=t2_int1)));

And I think there would be another problem if it would work like this. How do I check, that this constraint is still fullfilled when changing t1_int?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:50:31+00:00Added an answer on May 26, 2026 at 2:50 pm

    Finally found an answer to my question. You can solve the problem by using triggers. Here’s a working example:

    CREATE TABLE table1
      (t1_prim TEXT PRIMARY KEY,
       t1_int INTEGER NOT NULL);
    
    CREATE TABLE table2
      (t2_prim1 TEXT,
      t2_prim2 INTEGER,
      t2_int INTEGER NOT NULL,
      PRIMARY KEY (t2_prim1, t2_prim2),
      FOREIGN KEY (t2_prim1) REFERENCES table1(t1_prim));
    

    Creating the triggers for updating and inserting:

    CREATE TRIGGER t1_int_update_constraint
    BEFORE UPDATE OF t1_int ON table1
      BEGIN
        SELECT CASE 
          WHEN new.t1_int < (SELECT max(t2_int) FROM table2 WHERE t2_prim1=old.t1_prim) 
            THEN (SELECT RAISE(ABORT, 
              'Input smaller than maximum of all values for t2_int in database!'))
          END;
      END;
    
    CREATE TRIGGER t2_int_update_constraint
    BEFORE UPDATE OF t2_int ON table2
      BEGIN
        SELECT CASE 
          WHEN new.t2_int > (SELECT t1_int FROM table1 WHERE t1_prim=old.t2_prim1) 
            THEN (SELECT RAISE(ABORT, 
             'Input bigger than value in t1_int!'))
          END;
        END;
    
    CREATE TRIGGER t2_int_insert_constraint
    BEFORE INSERT ON table2
      BEGIN
        SELECT CASE 
          WHEN new.t2_int > (SELECT t1_int FROM table1 WHERE t1_prim=new.t2_prim1) 
            THEN (SELECT RAISE(ABORT, 
             'Input bigger than value in t1_int!'))
          END;
      END;
    

    If you have now the following tables:

    table1

    t1_prim     t1_int
    ----------  ----------
    one         5         
    two         10   
    

    table2

    t2_prim1    t2_prim2    t2_int    
    ----------  ----------  ----------
    one         1           5         
    one         2           4         
    two         1           7         
    two         2           5         
    two         3           1         
    

    You get this output:

    UPDATE table1 SET t1_int=4 WHERE t1_prim='one';
    

    Error: Input smaller than maximum of all values for t2_int in database!

    UPDATE table1 SET t1_int=6 WHERE t1_prim='two';
    

    Error: Input smaller than maximum of all values for t2_int in database!

    UPDATE table2 SET t2_int=8 WHERE t2_prim1='one' AND t2_prim2=1;
    

    Error: Input bigger than value in t1_int!

    UPDATE table2 SET t2_int=11 WHERE t2_prim1='two' AND t2_prim2=2;
    

    Error: Input bigger than value in t1_int!

    INSERT INTO table2 VALUES ('one', 3, 6);
    

    Error: Input bigger than value in t1_int!

    While those work perfectly well:

    INSERT INTO table2 VALUES ('one', 3, 6);
    UPDATE table2 SET t2_int=1 WHERE t2_prim1='one' AND t2_prim2=1;
    UPDATE table1 SET t1_int=8 WHERE t1_prim='two';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder if there is any trick to solve this problem. I have my
By trying to solve this problem , something made me wonder. Consider the following
I'm trying to understand how to solve this trivial problem in C, in the
i wonder how i can solve the following problem. i have a horizontal scrollbar
I've tried all sorts of design approaches to solve this problem, but I just
wonder if I could get some advice on the best way to solve this
I wonder if there any better ideas to solve the problem below, I have
im having this problem...i wonder if any of you have any ideas how to
I wonder if someone knows if there is a pre-made solution for this: I
I am looking for an algorithm that will solve my problem in the most

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.