Referential Integrity and Constraint Question

Posted on 2010-01-07
Last Modified: 2012-05-08

I have two tables, "Customer" and "Inactive"

Customer Table

cust_id (PK)       cust_name          status
1                 ABC Company    0
4                 XYG Company    2

Status Table

status(PK)       status_desc
0                   some desc
1                  diff desc
2                  2nd desc

I want to create a constraint between the PK of the Status table and the Status column in the Customer table.   What would be the SQL needed to do this?

I basically want to prevent a user from creating a record in the Customer Table with a value in the status column that doesn't exist in the Status table.

Hope that makes sense.

Question by:Westside2004
    LVL 12

    Accepted Solution

    ALTER TABLE Customer ADD FOREIGN KEY (status) REFERENCES Status(status)

    LVL 1

    Author Closing Comment


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Suggested Solutions

    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
    Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
    Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now