A SQL Server Database could have multiple tables and each table that has to be stored based on some rules. These rules are the possible checks & conditions which allows you to ensure the data validations before it is being inserted into a specific table.
SQL Server Constraints Types
SQL server constraints can be divided primarily into :
- SQL Server Table level constraints
- SQL Server Column level constraints
S. No | SQL Server Constraint | Table / Column Level | Description |
1 | NOT NULL | Column Level | Column value cannot be specified as NULL |
2 | UNIQUE | Column Level | all values in the column need to be UNIQUE |
3 | CHECK | Column Level | all values in a column satisfies a specific condition |
4 | DEFAULT | Column Level | specifies a DEFAULT value for the column |
5 | INDEX | Table Level | apply indexes make the data retrieval fast |
6 | PRIMARY KEY | Column Level | Unique identifier for the column. Combination of NOT NULL and UNIQUE values |
7 | FOREIGN KEY | Column Level | Identifies the Column with Unique values in another table referenced in another table. |
SQL Server Constraints can be applied :
- Create Table
- Alter Table
Syntax: SQL Server ADD Constraints in Table
CREATE TABLE table_name (
table_col1 datatype constraint,
table_col2 datatype constraint,
table_col3 datatype constraint,n
….
table_col3 datatype constraint
);
CREATE TABLE oracleappshelpStudents
( StudentId INT IDENTITY PRIMARY KEY,
UserId INT REFERENCES oracleappshelpUsers (UserId),
Email VARCHAR(256) ,
CreatedBy VARCHAR(128) NOT NULL,
UpdatedBy VARCHAR(128) NOT NULL,
CreatedDate DATETIME NOT NULL,
UpdatedDate DATETIME NOT NULL )
We will be discussing in separate blog for each Constraint . Please refer the below given links
SQL Server Not Null Constraint in the table
SQL Server Primary Key / Foreign Key Constraint in the table
Comments are closed.