Database Code
  Home arrow Database Code arrow Page 3 - 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
     
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry 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 - Key Constraints
    (Page 3 of 8 )

    Keys are the cornerstone concepts of database design and management. There are four different types of common keys used in almost all database endeavors. These are Primary keys, Foreign keys, Alternate keys and Inversion keys. We will be looking at the first three keys in detail today.  Inversion keys are merely alternative ways of sorting the data.

    Primary Key Constraints

    Primary keys are the unique identifiers for each row. They must contain unique values and cannot be null. Due to their importance in relational databases, Primary keys are the most fundamental of all keys and constraints. A table can have only one Primary key. A Primary key ensures uniqueness within the column declared as being part of that Primary key, and that unique value serves as an identifier for each row in that table. There are two ways to create Primary keys: the CREATE TABLE and ALTER TABLE commands. Using a small, integer column as a Primary key is recommended. Each table should have a Primary key.

    Now let us see how to use Primary key constraint in a create table command:

     
    USE Accounting
    CREATE TABLE 
    Employees
    {
     EmployeeID   int   
    IDENTITY   NOT NULL PRIMARY KEY
    ,
     EmployeeName   
    varchar
    (30)   NOT NULL,
     SSN   
    varchar
    (11)   NOTNULL,
     Address1   
    varchar
    (25)   NOT NULL,
     Address2   
    varchar
    (20)   NOT NULL,
     City   
    varchar
    (20)   NOTNULL,
     State   
    varchar
    (2)   NOT NULL,
     Zip   
    varchar
    (10)   NOT NULL,
     Contact   
    varchar
    (25)   NOT NULL,
     Phone   
    char
    (15)   NOT NULL,
     Salary   money   NOT 
    NULL
    ,
     HireDate   smalldatetime   NOT 
    NULL
    ,
     TerminationDate   smalldatetime   
    NULL
    ,
     EmpManagerID   int   NOT 
    NULL
     Department   varchar
    (25)   NOT 
    NULL
    }

    Creating a Primary key on an already existing table, is easy and it looks like follows:


    Use Accounting
      ALTER TABLE Customers
      ADD CONSTRAINT pk_custID 
      
    PRIMARY KEY (CustomerID)

    A table may have more than one combination of columns that could uniquely identify the rows in a table. Each combination is a Candidate key. We pick one of the Candidate keys to be the Primary key. For example, in the Car_Parts table (Example 3), both Part_No and Part_Name could be Candidate keys, but only Part_No is chosen as a Primary key.


    CREATE TABLE 
    Car_Parts
                
    (Part_No   int   PRIMARY 
    KEY
    ,
                
    Part_Name  char
    (30), 

                
    Part_Weight  
    decimal
    (6,2),
                
    Part_Color   char
    (15) )

    The index generated by a Primary key constraint cannot cause the number of indexes on the table to exceed 249 non-clustered indexes and 1 clustered index. If CLUSTERED or NONCLUSTERED is not specified for a Primary key constraint, CLUSTERED is used if there are no clustered indexes specified for UNIQUE constraints.

    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 1 hosted by Hostway