Constraints
Constraints are rules applied to database columns or tables that enforce standards on the type of data that can be stored. Whether you’re designing a simple database for a school project or managing enterprise-grade systems, constraints help prevent errors, maintain data integrity, reliability and simplify validation within a relational database.
Types of Constraints
- NOT NULL
- NULL
- UNIQUE
- PRIMARY KEY
- FOREIGN KEY
- CHECK CONSTRAINT
- DEFAULT CONSTRAINT
- IDENTITY PROPERTY
1) NOT NULL CONSTRAINT:
The NOT NULL constraint is one of the most basic and important constraints in SQL. It ensures that a column cannot have NULL (empty) values. It guarantees that every record in a table has a valid value for the specified column. It helps maintain data completeness and consistency.
2) NULL CONSTRAINT:
The NULL constraint allows a column in a table to accept NULL values, meaning the data in that column is optional or unknown. A column with no constraint (by default) allows NULLs unless specified otherwise using NOT NULL.
NULL means no value. It is not the same as zero or an empty string value.
3) UNIQUE CONSTRAINT:
The UNIQUE constraint ensures that all values in a column are different. It helps maintain data integrity by preventing duplicate entries. It ensures that no two rows in a table have the same value in the specified column, except for NULL (which is allowed unless restricted otherwise).
4) PRIMARY KEY:
A PRIMARY KEY is a column or a combination of columns that uniquely identifies each row in a table. It is one of the most important constraints in relational database design. It does not allow any NULL values. UNIQUE constraint allows only one NULL value but PRIMARY KEY does not allow one NULL value also. That is the main difference between PRIMARY KEY and UNIQUE constraint.
5) FOREIGN KEY:
A FOREIGN KEY is a constraint used to link two tables together by enforcing a relationship between a column in one table (child) and the PRIMARY KEY in another table (parent). It helps to maintain referential integrity between related tables. It ensures that the value in the child table must match a value in the parent table.
6) CHECK CONSTRAINT:
The CHECK constraint is used to limit the values that can be stored in a column by specifying a condition that each row must satisfy. It ensure that the data in a column meets a specific condition or business rule before it is inserted or updated.
7) DEFAULT CONSTRAINT:
The DEFAULT constraint is used to assign a predefined value to a column when no value is provided during an INSERT operation. It automatically fills in a default value when the user does not explicitly specify one. This helps maintain consistency and reduces null entries for optional fields.
8) IDENTITY PROPERTY:
The IDENTITY property is used in SQL to auto-generate sequential numeric values for a column, typically for primary keys. It is a convenient way to automatically assign unique IDs to new rows without manual input. It helps to create a self-incrementing column — most often used for IDs, order numbers, invoice numbers, etc.
Want to Master SQL and Unlock High-Paying Data Roles?
Join SQL School — India’s leading real-time training institute for SQL Server and T-SQL development.
✅ Learn SQL Server 2019 & 2022 step-by-step
✅ Master complex queries, joins, subqueries, indexing, and performance tuning
✅ Work on real-time projects with live enterprise servers
✅ Hands-on experience with SSMS, stored procedures, triggers, and optimization
📞 Call Now: +91 96666 40801
🌐 Visit: www.sqlschool.com for a FREE Demo Session
SQL School – Your Real-Time Guide to SQL Server Mastery & Career Success.