What is constrain in sql server?

Posted by Pkanwar under Sql Server on 10/8/2013 | Points: 10 | Views : 1168 | Status : [Member] | Replies : 6
What is constrain in sql server?











Thanks




Responses

Posted by: Bandi on: 10/8/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
SQL Server constraints allow you to enforce rules in your database. These rules may affect business logic, database integrity and/or table structures. Each one plays an important role in your database architecture. The six types of constraints supported by Microsoft SQL Server include:

UNIQUE constraints allow SQL Server administrators to specify that a column may not contain duplicate values.

CHECK constraints allow you to limit the types of data that users may insert in a database.

DEFAULT constraints allow you to specify a value that the database will use to populate fields that are left blank in the input source.

NOT NULL constraints allow you to specify that a column may not contain NULL values.

PRIMARY KEY constraints specify fields that uniquely identify each record in the table.

FOREIGN KEY constraints are fields in a relational database table that match the primary key column of another table. Foreign keys can be used to cross-reference tables.

Each of these constraints plays a significant, unique role when creating a SQL Server database architecture.

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Pkanwar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/8/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
refer
http://technet.microsoft.com/en-us/library/ms189862(v=sql.105).aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Pkanwar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sriramnandha on: 10/10/2013 [Member] Starter | Points: 25

Up
0
Down
Create a Table To Check Conditions of the Table.constraint is used to allow enforce the conditions of the table.

1) domain integraity Constraint
2) Entity Integraity Constraint
3) Referential Integraity Constraint


1) Domain Integraity Constraint
Not Null
Check

2) Entity Integrtiy constraint
Primary Key Constraint
Unique Key Constraint

3) Referential Integrity Constraint

Parent Table Child Table Relation Ship [Primary key and Foreign key]



Hope this will help

Sriram.R


sriram

Pkanwar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/29/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Click on "Mark as Answer " link

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Pkanwar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Samirbhogayta on: 10/29/2013 [Member] Starter | Points: 25

Up
0
Down
hi.. please go through this description

SQL constraints are used to specify rules for the data in a table.

If there is any violation between the constraint and the data action, the action is aborted by the constraint.

Constraints can be specified when the table is created (inside the CREATE TABLE statement) or after the table is created (inside the ALTER TABLE statement).
SQL CREATE TABLE + CONSTRAINT Syntax
CREATE TABLE table_name
(
column_name1 data_type(size) constraint_name,
column_name2 data_type(size) constraint_name,
column_name3 data_type(size) constraint_name,
....
);

In SQL, we have the following constraints:

NOT NULL - Indicates that a column cannot store NULL value
UNIQUE - Ensures that each row for a column must have a unique value
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures that a column (or combination of two or more columns) have an unique identity which helps to find a particular record in a table more easily and quickly
FOREIGN KEY - Ensure the referential integrity of the data in one table to match values in another table
CHECK - Ensures that the value in a column meets a specific condition
DEFAULT - Specifies a default value when specified none for this column


SAMIR
Sr. Software Engineer

Pkanwar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Samirbhogayta on: 11/11/2013 [Member] Starter | Points: 25

Up
0
Down
hi.. this is your answer

A PRIMARY KEY constraint is a unique identifier for a row within a database table. Every table should have a primary key constraint to uniquely identify each row and only one primary key constraint can be created for each table. The primary key constraints are used to enforce entity integrity.

A UNIQUE constraint enforces the uniqueness of the values in a set of columns, so no duplicate values are entered. The unique key constraints are used to enforce entity integrity as the primary key constraints.

A FOREIGN KEY constraint prevents any actions that would destroy links between tables with the corresponding data values. A foreign key in one table points to a primary key in another table. Foreign keys prevent actions that would leave rows with foreign key values when there are no primary keys with that value. The foreign key constraints are used to enforce referential integrity.

A CHECK constraint is used to limit the values that can be placed in a column. The check constraints are used to enforce domain integrity.

A NOT NULL constraint enforces that the column will not accept null values. The not null constraints are used to enforce domain integrity, as the check constraints.

SAMIR
Sr. Software Engineer

Pkanwar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response