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 8526667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:20:15+00:00 2026-06-11T08:20:15+00:00

I have two tables, A and B. The structure looks like this: CREATE TABLE

  • 0

I have two tables, A and B. The structure looks like this:

CREATE TABLE A (
    w int NOT NULL,
    x int NOT NULL,
    y int NOT NULL,
    CONSTRAINT PK_A PRIMARY KEY (w, x, y)
)

CREATE TABLE B (
    w int NOT NULL,
    y int NULL,
    z int NOT NULL
)

I want to make sure for any set of values entered in table B, that w and y are in table A. If the y value in table B is null, I only want to make sure that w is in table A.

Some sample data, inserts, and expected results:

Table A
w  x  y
----------
1  1  1
1  1  2
1  2  1
1  3  2
2  1  1

INSERT INTO B (w, y, z) VALUES (1, 1, 3) -- good
INSERT INTO B (w, y, z) VALUES (1, NULL, 3) -- good
INSERT INTO B (w, y, z) VALUES (1, 1, 4) -- good
INSERT INTO B (w, y, z) VALUES (2, NULL, 3) -- good
INSERT INTO B (w, y, z) VALUES (1, 3, 1) -- fail
INSERT INTO B (w, y, z) VALUES (3, NULL, 1) -- fail

Any way for this to work? I’m using SQL Server 2000 if that comes into play.

  • 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-06-11T08:20:17+00:00Added an answer on June 11, 2026 at 8:20 am

    Unfortunately, you cannot use a foreign key constraint on B.w and B.y because they would reference non-unique columns on A. But you can add this check via triggers:

    create trigger check_w on B for insert, update
    as
    if not exists(select * from A join inserted on A.w = inserted.w) 
    begin
      raiserror('W not in A!', 1, 1)
      rollback transaction
    end
    
    GO
    
    create trigger check_y on B for insert, update
    as
    if 
    (select y from inserted) is not null and 
    not exists(select * from A join inserted on A.y = inserted.y) 
    begin
      raiserror('Y not null and not in A!', 1, 1)
      rollback transaction
    end
    
    GO
    

    You can definitely combine these two triggers in one.

    Also note that you’ll need a trigger on A for the delete operation. You either prevent deletion if there’re matching rows on B, or you perform a cascade delete operation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables like of this structure: content (content_id, content_type, user_id, time, comment_count)
I have two database tables with the following structure: actions: action_id int(11) primary key
I have a data structure that looks like this: Model Place primary key id
I have two tables structured like this: table a id title date 1 testing1
I inherited an interesting table structure. This table structure looks like the following: GroupTable
I'm using SQL Server 2008. I have a database table that looks like this
So, I have an app/assets/stylesheets/ directory structure that looks something like this: |-dialogs |-mixins
I have a data-structure (in plist) that looks something like this: What i have
Not sure how to structure this. Model inheritance seems sensible, but it looks like
My table structure (in SQL Server) looks something like this: (D1 is more recent

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.