Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Adding constraints on MS SQL

  • 15-10-2015 09:01AM
    #1
    Registered Users, Registered Users 2 Posts: 356 ✭✭


    Hi,
    Hoping someone can help me with the following MS SQL problem as I’m fairly new to it:
    I have two tables (Apartments and Tenants).
    In the Apartments table, there is a field called OccupierID which has a Foreign Key connection to TenantID in the Tenants table.



    The rule is that each TenantID can only be entered into the Apartments table OccupierID once so I used a UNIQUE constraint on OccupierID but this is causing a problem now as I need to allow for NULL to be entered if the Appartment is not occupied.



    Is there another check I can use instead of a UNIQUE constraint that will allow for NULL entries but not a repeat of a TenantID?
    (I haven’t learnt about triggers yet but would this be what I need?)



    Thanks in advance.


Comments

  • Moderators, Society & Culture Moderators Posts: 17,642 Mod ✭✭✭✭Graham


    Take a look at this:

    https://www.mssqltips.com/sqlservertip/2380/sql-server-database-design-with-a-one-to-one-relationship/

    Are you sure that's what you want though? What happens when an apartment has more than one tenant?


  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    You need a reference table here. So you have your Apartments and Tenants tables, and then a third table - ApartmentTenants.

    In this table are just two columns - an ApartmentID and the TenantID associated with it. Both are foreign keys, and you have UNIQUE constraint - the TenantID.

    This means that a tenant can only be an occupier of one apartment. If you want each apartment to only have one occupier, you have a second UNIQUE constraint on the ApartmentID.


  • Registered Users, Registered Users 2 Posts: 213 ✭✭shakedown


    Or remodel your tables - have ApartmentID on the Tenants table and get rid of OccupierID on Apartments. Then you don't even need any unique constraints (assuming you have a primary key on Tenants).


  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    all great ideas of which the third table ApartmentTenants is the best idea.

    but what about the case of having a tenant move through multiple apartments over several years ?

    and what about rental periods ?

    Each apartment may be rented multiple times over multiple tenants or even the same time on multiple leases.

    You model needs to cater for all of these cases

    don;t forget landlords who may own multiple properties


  • Closed Accounts Posts: 22,457 ✭✭✭✭beauf


    And tenants who leave and come back to the same property repeatedly. Maybe a "lease" table which would have properties like a start and end. The closer you model it to real life the easier it will be to model.


  • Advertisement
Advertisement