Click here to Skip to main content
15,886,080 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the difference between using constraints and rules in SQL, preferably SQL server 2008 R2. Can anyone explain and demonstrate with a real time example
Posted

Rules are a backward-compatibility feature that performs some of the same functions as CHECK constraints.
CHECK constraints are the preferred, standard way to restrict the values in a column.
CHECK constraints are also more concise than rules; there can only be one rule applied to a column, but multiple CHECK constraints can be applied. CHECK constraints are specified as part of the CREATE TABLE statement, while rules are created as separate objects and then bound to the column.

Important about RULE:
This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.
Refer: http://technet.microsoft.com/en-us/library/ms188064(v=sql.105).aspx[^]
 
Share this answer
 
Comments
Rajesh Anuhya 21-Jun-13 3:27am    
Have my +5 for formatting and Good answer
--RA
RULE

1. The first one:
I think that Rule is one of constrains.
2. Rule can assign more than one fields in table, objects

CONSTRAINTS

Constraints allow you to define the way Microsoft® SQL Server™ automatically enforces the integrity of a database. Constraints define rules regarding the values allowed in columns and are the standard mechanism for enforcing integrity.

See link to get more details

http://msdn.microsoft.com/en-us/library/aa174546%28v=sql.80%29.aspx[^]

Hope that is useful.

Trong Anh
 
Share this answer
 
Check here,

Contraints_vs_rules.
 
Share this answer
 
The major difference between rule and Check is re usability. Check constraint is associated with columns in a Table. So these can't be re-used. Rules are defined with in a database and can be applied to any number of columns.

Eg.
CREATE TABLE Persons
(
P_Id int NOT NULL CHECK (P_Id>0),
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900