Database Code
  Home arrow Database Code arrow Page 7 - Constraints In Microsoft SQL Server 2000
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE CODE

Constraints In Microsoft SQL Server 2000
By: Gayathri Gokul
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 116
    2004-02-11

    Table of Contents:
  • Constraints In Microsoft SQL Server 2000
  • Types of Constraints
  • Key Constraints
  • Foreign Key Constraints
  • Cascading Updates And Deletes
  • Unique Constraints
  • Check Constraints
  • Default Constraints

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Constraints In Microsoft SQL Server 2000 - Check Constraints
    (Page 7 of 8 )

    The nice thing about Check constraints is that we are not restricted to a particular column. We can also relate them to a column, but in essence they are table-related, which means they can check one column against another as long as all the columns belong to the same table and the values must be for the same row being updated or inserted.  Here are some typical cases where Check constraint will come in handy:

    1. Limit Month column - use SQL command “BETWEEN 1 AND 12”
    2. SSN  format - use SQL command LIKE ‘[0-9][0-9][0-9]- [0-9][0-9][0-9]- [0-9][0-9][0-9][0-9]'
    3. For a city column check against a specified list, use CHECK(city) IN (“NY”, “FL”, “CA”, “MA”, )

    In the following example (11) we are using Check constraint to check if the column min_lvl is greater than or equal to 10 and max_lvl less than or equal to 250.

    CREATE TABLE Jobs
    (job_id smallint IDENTITY(1,1
    PRIMARY KEY CLUSTERED,job_desc varchar(50)NOT NULLmin_lvl tinyint NOT NULL 
    CHECK 
    (min_lvl >= 10),max_lvl tinyint NOT NULL CHECK (max_lvl <= 
    250))

    So while we try to insert any values in the min_lvl column less than 10, it will show an error. Similarly for max_lvl column any value greater than 250 will raise an error.

    Creating A Check Constraint on an Existing Table  

    We have a table called Customer and it has a field already defined as “DateInSystem GETDATE()” this field gets its date based on the current system date. Alter it to include a check constraint.

    Example 12: If we try to enter any date less than the current date it will show an error.


    Use Accounting
    ALTER TABLE Customers
    ADD CONSTRAINT 
    Chk_DateSys
    CHECK
    (DateInSystem <= GETDATE())

    A column can have any number of Check constraints, and the condition can include multiple logical expressions combined with AND and OR. Multiple Check constraints for a column are validated in the order created. The search condition must evaluate to a Boolean expression and cannot reference another table. A column-level Check constraint can reference only the constrained column, and a table-level Check constraint can reference only columns in the same table. Check constraints and rules serve the same function of validating the data during Insert and Delete statements. When a rule and one or more Check constraints exist for a column or columns, all restrictions are evaluated.

    More Database Code Articles
    More By Gayathri Gokul


     

    DATABASE CODE ARTICLES

    - Deployment of the MobiLink Synchronization M...
    - MobiLink Synchronization Wizard in SQL Anywh...
    - Finding Matching Records in Data Access Pages
    - Using the AccessDataSource Control in VS 2005
    - A Closer Look at ADO.NET: The Command Object
    - A Closer Look at ADO.NET: The Connection Obj...
    - Using ADO to Communicate with the Database, ...
    - Code Snippets: Counting Records
    - Constraints In Microsoft SQL Server 2000
    - Multilingual entries into a DB and to be dis...
    - Getting A List of Tables From SQL Server
    - SQL Server Database Creator - .NET Version
    - ADO Recordset Paging
    - Two combos, one textbox example
    - Discussion & Listserv Module by Mike Eck...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway