SQL Constraints, SQL Tutorial, Global Guide Line Technology.
Forum | Blog |


Basic SQL Guide.           
SQL Introduction.               
 What is Table?                  
 SQL Create Table.            
 SQL Insert Statement.      
 SQL Select Keyword.        
 SQL Order by clause.        
 SQL Count Statement.      
 SQL Group By Clause.      
 SQL Having Clause.          
 SQL Alias Tables.             
 SQL Join Tables.              
 SQL Outer Join.               
 SQL Update Statement.   
 SQL Delete Statement.    
 SQL Distinct Keyword.      
 SQL Where Keyword.       
 SQL And Or Keyword.      
 SQL In Keyword.              
 SQL Between Keyword.   
 SQL Like Keyword.          
 SQL Functions.                
 SQL Concatenate.           
 SQL Substrings.               
 SQL Trim Function.         
 SQL Constraints.
 SQL Primary Key.            
 SQL Foreign Key.            
 SQL Create Views.           
 SQL Create Index.           
 SQL Alter Table.             
 SQL Drop Table.             
 SQL Truncate Table.      
 SQL Summary.                

SQL Tutorial >> SQL Constraints.

     Back             Next     

We can place constraints to limit the type of data that can go into a table. Now in SQL Tutorial section we will see that such constraints can be specified when the table is first created via the SQL CREATE TABLE statement, or after the table is already created via the SQL ALTER TABLE statement.

Common types of constraints include the following:

Types of SQL Constraints

- Primary Key
- Foreign Key

Each type of SQL Constraint is described in below with example.

SQL NOT NULL Constraint.

By default, a column can hold NULL value. If we not want to allow NULL or empty value in a column of our table, then we need to place an SQL Constraint on this column specifying that NULL empty column is now not an allowable value.

For example, in the following statement,

Example of NOT NULL Constraint

(empID integer NOT NULL,
FirstName varchar (30) NOT NULL,
LastName varchar(30));

Columns "empID" and "FirstName" cannot include NULL, while "LastName" can include NULL, while inserting the record in this table we must provide empID and FirstName after this SQL NOT NULL Constraints apply.

SQL UNIQUE Constraint.

The UNIQUE SQL constraint ensures that all values in a column are distinct and unique values repetition will not allow after applying SQL UNIQUE Constraint.

For example, the following statement explains real example of SQL UNIQUE Constraints.

Example of SQL UNIQUE Constraint

(empID integer UNIQUE,
FirstName varchar (30),
LastName varchar(30));

Column "empID" cannot include duplicate values, while such constraint does not hold for columns "FirstName" and "LastName" so these last two can hold duplication.

Please note that a column that is specified as a primary key must also be unique. At the same time, a column that's unique may or may not be a primary key.

SQL CHECK Constraint.

The SQL CHECK constraint ensures that all values in a column satisfy certain conditions, suppose we are wishing to enter some conditional record like age of employ not less then 20 so we need to verify it with SQL CHECK Constraint.

For example, in the following statement explains how to use the SQL CHECK Constraint in our queries.

Example of SQL CHECK Constraint

(empID integer CHECK (empID > 10),
FirstName varchar (30),
LastName varchar(30));

Column "empID" must only include integers greater than 10.  Less then 10 if we try to enter a record then the constraint will force us to enter grater then 10, so we can't enter less then 10 in the "empID" column.

Please note that the SQL CHECK constraint does not get enforced by MySQL database at this time.

Primary Key and Foreign Key are discussed in the next two coming SQL Tutorial sections of PRIMARY Key Constraint and FOREIGN Key Constrint..

     Back             Next     

[ About ] [ Contact ] [ Home ]
[ Links ] [ Site Map ] [ Services ] [ Privacy ]

Copyright 2005 -  2021 www.globalguideline.com All rights reserved. (Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher) 355 visitors are online now