Christmas Special : Upto 40% OFF! + 2 free courses - SCHEDULE CALL
Discover the power of SQL the language that lets you store, manipulate, and retrieve data effortlessly. It's the gold standard for managing databases. Get ready to harness the potential of constraints in SQL to handle and describe your data seamlessly. Learn to create table constraints in SQL and check constraint in SQL. Additionally, also learn about Boyce Codd standard form and various constraints.
We will learn about how to create constraint SQL server and different types of SQL Constraints.
NOT NULL - This table constraint in SQL ensures that a column cannot have a NULL value
The syntax for implementing null constraint in a table using T-SQL is as follows
The syntax for implementing null constraint in a table using T-SQL is as follows
CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Age int );
The output looks like below
UNIQUE - This table constraint in SQL ensures that all values in a column are different
The syntax for unique constraints is as follows
CREATE TABLE Persons1 ( ID int NOT NULL UNIQUE, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int );
The output looks like below
PRIMARY KEY - This constraint in SQL Server is a mix of both NOT NULL and UNIQUE. Uniquely identifies each row in a table
The syntax for declaring the primary key is as below
CREATE TABLE Persons2 ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, CONSTRAINT PK_Person PRIMARY KEY (ID, LastName) );
The output looks like below
FOREIGN KEY - This SQL Constraint in dbms prevents actions that would destroy links between tables
The syntax for creating the foreign key is as below
CREATE TABLE Orders ( OrderID int NOT NULL PRIMARY KEY, OrderNumber int NOT NULL, PersonID int FOREIGN KEY REFERENCES Persons(PersonID) );
CHECK - Check constraints in dbms makes sure that the values in a column satisfy a specific condition
The syntax for check constraint is as below
CREATE TABLE Persons5 ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int CHECK (Age>=18) );
The output is as below
DEFAULT - This SQL Constraint sets a default value for a column if no value is specified
The syntax for the default constraint is as below
CREATE TABLE Persons6 ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, City varchar(255) DEFAULT 'Sandnes' );
The output is as below
CREATE INDEX - This SQL creates table constraint SQL which is utilized to develop and retrieve data from the database very quickly
The syntax for creating an index is as below
CREATE INDEX index_name ON table_name (column1, column2, ...);
In the above section, we learned about different create constraint in SQL Server. Next, we will learn about the advantages and disadvantages of database constraints in SQL.
The following are some of the benefits of SQL Constraints in DBMS:
The process of organizing data in a database is known as normalization. It includes forming tables and establishing relationships between them by rules that safeguard the data and make the database more adaptable by removing redundant and inconsistent dependencies. A vast database defined as a single relation may turn into data duplication. This repetition of data may result in:
Therefore, to deal with these issues, we ought to analyze and break down the redundant data into smaller, more straightforward, and well-structured relations with desirable properties. Normalization is the process of breaking down relationships into more manageable subsets.
In short, normalization means:
Eliminating these anomalies is the primary goal of normalizing the relationships. As the database grows, data redundancy, issues with data integrity, and other issues can arise if anomalies are not removed. A set of guidelines called "normalization" will help you put together a good database structure. One such normalization is the Boyce Codd Normal Form or BCNF. We'll talk about BCNF next.
In database normalization, the Boyce–Codd normal form (also known as BCNF or 3.5NF) is utilized. The third normal form is slightly stronger in this form. Raymond F. Boyce and Edgar F. Codd developed the BCNF in 1974 to deal with certain kinds of anomalies that the 3NF didn't cover in its original definition.
All redundancy based on functional dependency has been eliminated if a relational schema is in BCNF, although other types of redundancy may still exist. A relational schema R is in Boyce–Codd normal form if and only if at least one of the following is valid for each of its dependencies X Y.
Boyce Codd Normal Form with Example
The more advanced variant of 3NF is called BCNF (Boyce Codd Normal Form). If every functional dependency in a table is X->Y, and X is the table's super key, then the table is in BCNF. The table should be in 3NF for BCNF and each FD. LHS is crucial.
A table is said to be in BCNF form when
Example
Consider a relation R with attributes (student, subject, teacher).
F: { (student, Teacher) -> subject (student, subject) -> Teacher Teacher -> subject}
Candidate keys are (student, teacher) and (student, subject).
Since there is no transitive dependency, the above relation is in 3NF. If X must be a key for each non-trivial FD X->Y, then a relation R is in BCNF.
Because the teacher is not a key in the FD (teacher->subject), the aforementioned relationship does not exist in BCNF. This relationship has anomalies. For instance, if we attempt to delete the student Subbu, we will lose the knowledge that R. Prasad teaches C. These issues are brought on by the fact that the teacher is a determinant but is not a candidate key.
SQL Training For Administrators & Developers
Over the last few paragraphs, we learned about different types of constraints and how to create a table in SQL with constraints. We also learned about the advantages of BCNF (Boyce Codd Normal Form). Hope this can be a building block for the students who want to study more about different types of Constraints in a database and BCNF.
What is Schema in SQL With Example: All You Need to Know
Data Definition Language (DDL) Commands in SQL
What does a Database Administrator do? A Detailed Study
How To Create Database Table-All You Need To know
Cyber Security
QA
Salesforce
Business Analyst
MS SQL Server
Data Science
DevOps
Hadoop
Python
Artificial Intelligence
Machine Learning
Tableau
Download Syllabus
Get Complete Course Syllabus
Enroll For Demo Class
It will take less than a minute
Tutorials
Interviews
You must be logged in to post a comment