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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:10:19+00:00 2026-05-22T19:10:19+00:00

I’m working in SQLAzure at the moment. I’m setting up a design where each

  • 0

I’m working in SQLAzure at the moment.

I’m setting up a design where each User has a number of Address’s

When the user then places an Order, then I want to link that Order to both the User and to a couple of Addresses.

So my tables look like:

User

  • Id
  • Name
  • etc

Address

  • Id
  • UserId (Foreign Key)
  • Street
  • etc

Order

  • Id
  • UserId (Foreign Key)
  • DeliveryAddressId (Foreign Key)
  • BillingAddressId (Foreign Key)
  • etc

Is there a way I can I set up a check within SQL Server so that a user can’t under any circumstances (e.g. by hacking an HTML POST) submit an Order with an AddressId which is not linked to the same as the submitted UserId. I’ve looked at “foreign key constraints” in the docs, but this doesn’t seem to be quite what I’m looking for.

Any suggestions of what to try – or what tutorials to read – would be most appreciated.

  • 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-22T19:10:20+00:00Added an answer on May 22, 2026 at 7:10 pm

    In addition to your primary key in the Address table (on Id), you should also declare another key constraint, a UNIQUE constraint, on (Id,UserId).

    ALTER TABLE Address ADD CONSTRAINT UQ_Address_UserCheck UNIQUE (Id,UserID)
    

    You can then either replace your existing FKs from Order to address, or add additional ones, that check both columns

    ALTER TABLE Order ADD CONSTRAINT
         FK_Order_DeliveryAddress_UserCheck FOREIGN KEY (DeliveryAddressID,UserID)
         references Address (Id,UserId)
    

    As I say, you can add these all as additional constraints, if you want to.


    So, with some slight naming tweaks, your tables are rendered as this:

    create table Users (
        UserID int IDENTITY(1,1) not null,
        Name varchar(30) not null,
        /* Other columns */
        constraint PK_Users PRIMARY KEY (UserID),
        constraint UQ_User_Names UNIQUE (Name)
    )
    go
    create table Addresses (
        AddressID int IDENTITY(1,1) not null,
        UserID int not null,
        Street varchar(35) not null,
        /* Other columns */
        constraint PK_Addresses PRIMARY KEY (AddressID),
        constraint FK_Addresses_Users FOREIGN KEY (UserID) references Users (UserID),
        constraint UQ_Addresses_UserCheck UNIQUE (UserID,AddressID)
    )
    go
    create table Orders (
        OrderID int IDENTITY (1,1) not null,
        UserID int not null,
        DeliveryAddressID int not null,
        BillingAddressID int not null,
        /* Other columns - there may be other nullability concerns above */
        constraint PK_Orders PRIMARY KEY (OrderID),
        constraint FK_Orders_Users FOREIGN KEY (UserID) references Users (UserID),
        constraint FK_Orders_DeliveryAddresses FOREIGN KEY (DeliveryAddressID) references Addresses (AddressID),
        constraint FK_Orders_BillingAddresses FOREIGN KEY (BillingAddressID) references Addresses (AddressID),
        /* Further constraints - ensure UserID -> AddressID match */
        constraint FK_Orders_DeliveryAddress_UserCheck FOREIGN KEY (UserID,DeliveryAddressID) references Addresses (UserID,AddressID),
        constraint FK_Orders_BillingAddress_UserCheck FOREIGN KEY (UserID,BillingAddressID) references Addresses (UserID,AddressID)
    )
    

    And trying it our with some inserts that should work, except for the last (where there’s a user/address mismatch), it works:

    declare @UID1 int
    declare @UID2 int
    declare @AID1_1 int
    declare @AID1_2 int
    declare @AID2_1 int
    declare @AID2_2 int
    insert into Users (Name)
    select 'User1'
    set @UID1 = SCOPE_IDENTITY()
    insert into Users (Name)
    select 'User2'
    set @UID2 = SCOPE_IDENTITY()
    
    insert into Addresses (UserID,Street)
    select @UID1,'Street1'
    set @AID1_1 = SCOPE_IDENTITY()
    insert into Addresses (UserID,Street)
    select @UID1,'Street2'
    set @AID1_2 = SCOPE_IDENTITY()
    insert into Addresses (UserID,Street)
    select @UID2,'Street1'
    set @AID2_1 = SCOPE_IDENTITY()
    insert into Addresses (UserID,Street)
    select @UID2,'Street2'
    set @AID2_2 = SCOPE_IDENTITY()
    
    insert into Orders (UserID,DeliveryAddressID,BillingAddressID)
    select @UID1,@AID1_1,@AID1_2 union all
    select @UID2,@AID2_1,@AID2_1
    
    insert into Orders (UserID,DeliveryAddressID,BillingAddressID)
    select @UID1,@AID1_1,@AID2_1
    

    Results:

    (1 row(s) affected)
    
    (1 row(s) affected)
    
    (1 row(s) affected)
    
    (1 row(s) affected)
    
    (1 row(s) affected)
    
    (1 row(s) affected)
    
    (2 row(s) affected)
    Msg 547, Level 16, State 0, Line 31
    The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Orders_BillingAddress_UserCheck". The conflict occurred in database "Test", table "dbo.Addresses".
    The statement has been terminated.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I need to clean up various Word 'smart' characters in user input, including but
i want to parse a xhtml file and display in UITableView. what is the

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.