Database Code
  Home arrow Database Code arrow Page 8 - Constraints In Microsoft SQL Server 2000
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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 131
    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


    Constraints In Microsoft SQL Server 2000 - Default Constraints


    (Page 8 of 8 )

    Like all constraints, Default constraints become an integral part of the table definition. It defines what to do when a row is inserted with data for the column on which you have not determined a Default constraint. We can set the default value to be either as a literal string say default salary $3000 or as a one of the several system values such as GETDATE().

    Look at Example 13:


    USE Accounting
    CREATE TABLE Shippers
    (
      
    ShipperID  int  IDENTITY NOT NULL PRIMARY KEY
    ,
      
    ShipperName  varchar
    (30)  NOT NULL,
      Address  
    varchar
    (25)  NOT NULL,
      DateInSystem  smalldatetime  NOT 
    NULL
     
    DEFAULT GETDATE()
    )

    Insert values into Shippers like follows:


    Insert into Shippers (shipperName)
      
    Values
         
    (‘UPS’)

    When we run a select statement on Shippers:


    Select from Shippers;

    The default value is generated for DateInSystem column we if we did not supply the value because its default value is set to current system date.

    ShipperID       ShipperName       DateInSystem

    1                  UPS                    2003-12-25:23:26:00

    Creating Default Constraint on an Existing Table  

     

    The Jobs table is altered so that, a character string default supplies a description (column job_desc) when the actual description is not entered explicitly. (Example 14)


    ALTER TABLE Jobs
    ADD CONSTRAINT 
    CnDef_jobs
    DEFAULT 'New Position - title yet to be assigned’ FOR 
    job_desc;

    Let us write a sample table (example 15) which looks more like the Employee table in the pubs database to test the skills we have learned so far.


    CREATE TABLE 
    employee2
    (emp_id varchar(7CONSTRAINT PK_emp_id PRIMARY KEY NONCLUSTERED 
    CONSTRAINT
    CK_emp_id CHECK 
    (emp_id LIKE'[A-Z][A-Z][A-Z][1-9][0-9][0-9][FM]' 
    or emp_id LIKE '[A-Z]-[A-Z][1-9][0-9][0-9][FM]',fname varchar(20NOT NULL
    minit char(1NULLlname varchar(30NOT NULLjob_id smallint NOT NULL DEFAULT 

    REFERENCES jobs 
    (job_id), job_lvl tinyint DEFAULT 10
    hire_date 
    datetime NOT NULL 
    DEFAULT (getdate())
    )


    In the above table employee2 emp_id is the Primary key column. Each employee ID consists of three characters that represent the employee's initials, followed by a three-digit number ranging from 100 through 999 and then the employee's gender (M or F). A (hyphen) - is acceptable for the middle initial. The job_id column of the employee2 table for new hires refers to the job_id column of the Jobs table, so a Foreign key constraint is set up here. Default constraint is used for job_lvl column to set its value to 10 if no value is supplied at the time of insert. Similarly by default, the current system date is entered for the hire_date column.

     

    So we have dealt with types of constraints in SQL Server 2000, its sub classes, creating tables with specific constraints, and modifying existing tables. Now the job is up to you to experiment with a few codes of your own to master this basic yet essential part of database designing and data integrity.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · The article is well informative. It has placed various attributes with well designed...
     

    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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT