Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Constraints and Rules in SQL

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

like image 581
Divakar Avatar asked Jun 21 '13 04:06

Divakar


2 Answers

There are a lot of difference but One the most notable difference is that we can bind rules to a datatypes whereas constraints are bound only to columns. this feature enable us to create our own data-type with the help of Rules and get the input according to that.

I have found a good example, check the following link. constraints vs rules

like image 200
Dinup Kandel Avatar answered Sep 29 '22 08:09

Dinup Kandel


From Postgres manual:

The PostgreSQL rule system allows one to define an alternate action to be performed on insertions, updates, or deletions in database tables. Roughly speaking, a rule causes additional commands to be executed when a given command on a given table is executed

A constraint, as an example, can be used to ensure that a given number never matches some value. Such a birth year should not be under 1880 for new users registering or above the current year.

like image 22
Pétur Ingi Egilsson Avatar answered Sep 29 '22 06:09

Pétur Ingi Egilsson