{"id":33744,"date":"2021-06-08T11:26:00","date_gmt":"2021-06-08T05:56:00","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/"},"modified":"2024-09-03T14:27:18","modified_gmt":"2024-09-03T08:57:18","slug":"sql-constraints","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/","title":{"rendered":"SQL Constraints - Everything You Need to Know"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"what-are-sql-constraints\"><strong>What are SQL constraints? <\/strong><\/h2>\n\n\n\n<p>SQL constraints are a set of rules implemented on tables in relational databases to dictate what data can be inserted, updated or deleted in its tables. This is done to ensure the accuracy and the reliability of information stored in the table.<\/p>\n\n\n\n<p>Constraints enforce limits to the data or type of data that can be inserted\/updated\/deleted from a table. The purpose of constraints is to maintain the data integrity during an update\/delete\/insert into a table. Once the constraint is placed, if any operation in the database does not follow the rules specified by the constraint, the particular operation is aborted.<\/p>\n\n\n\n<p>In this article, we will go through what SQL constraints are, what the different kinds of SQL constraints commonly used are, and how to implement and remove them using <a href=\"https:\/\/www.mygreatlearning.com\/blog\/sql-commands\/\">SQL commands<\/a>. First, however, we will take a brief look into why they are needed.<\/p>\n\n\n\n\n<div class=\"course-slider courses slider-block\" style=\"background:#f6f7f8\">\n\t<div class=\"site-container\">\n\t\t<h2 class=\"section-title\" class=\"section-title\" id=\"learn-more-about-sql\"> Learn More About SQL <\/h2>\n\t\t<div class=\"gl-slider\">\n\t\t\t<div class=\"gl-frame\">\n\t\t\t\t<div class=\"posts gl-slides\">\n\n\t\t\t\t\t\n\t\t\t\t\t<article class=\"post\">\n\t\t\t\t\t\t<div class=\"inner-content\">\n\t\t\t\t\t\t\t<div class=\"thumb\">\n\t\t\t\t\t\t\t\t<a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/advanced-sql\" target=\"_blank\"><img decoding=\"async\" width=\"1254\" height=\"836\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-942201110.jpg\" class=\"attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"mysql tutorial\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-942201110.jpg 1254w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-942201110-300x200.jpg 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-942201110-1024x683.jpg 1024w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-942201110-768x512.jpg 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-942201110-696x464.jpg 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-942201110-1068x712.jpg 1068w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-942201110-630x420.jpg 630w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/iStock-942201110-150x100.jpg 150w\" sizes=\"(max-width: 1254px) 100vw, 1254px\" \/><\/a>\n\t\t\t\t\t\t\t\t<img decoding=\"async\" class=\"logo\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo-3.jpg\" width=\"100\" height=\"63\" alt=\"\">\n\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<a class=\"content\" href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/advanced-sql\" target=\"_blank\">\n\t\t\t\t\t\t\t\t<span class=\"source\">Great Learning Academy<\/span>\n\t\t\t\t\t\t\t\t<h3 class=\"title\" class=\"title\" id=\"advanced-sql\"> Advanced SQL<\/h3>\n\t\t\t\t\t\t\t\t<div class=\"meta\">\n\t\t\t\t\t\t\t\t\t<span class=\"duration\">1.0 Hourse<\/span>\n\t\t\t\t\t\t\t\t\t<span class=\"location\">Online<\/span>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t<div class=\"label\"> Land your dream job<\/div>\n\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/article>\n\n\t\t\t\t\t\n\t\t\t\t\t<article class=\"post\">\n\t\t\t\t\t\t<div class=\"inner-content\">\n\t\t\t\t\t\t\t<div class=\"thumb\">\n\t\t\t\t\t\t\t\t<a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/joins-in-sql-1\" target=\"_blank\"><img decoding=\"async\" width=\"640\" height=\"853\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/08\/cropped-digitization-1599552_1920.jpg\" class=\"attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"sql developer salary\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/08\/cropped-digitization-1599552_1920.jpg 640w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/08\/cropped-digitization-1599552_1920-225x300.jpg 225w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/08\/cropped-digitization-1599552_1920-315x420.jpg 315w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/08\/cropped-digitization-1599552_1920-150x200.jpg 150w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a>\n\t\t\t\t\t\t\t\t<img decoding=\"async\" class=\"logo\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo-3.jpg\" width=\"100\" height=\"63\" alt=\"\">\n\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<a class=\"content\" href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/joins-in-sql-1\" target=\"_blank\">\n\t\t\t\t\t\t\t\t<span class=\"source\">Great Learning Academy<\/span>\n\t\t\t\t\t\t\t\t<h3 class=\"title\" class=\"title\" id=\"joins-in-sql\"> Joins in SQL<\/h3>\n\t\t\t\t\t\t\t\t<div class=\"meta\">\n\t\t\t\t\t\t\t\t\t<span class=\"duration\">1.5 Hours<\/span>\n\t\t\t\t\t\t\t\t\t<span class=\"location\">Online<\/span>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t<div class=\"label\"> Land your dream job<\/div>\n\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/article>\n\n\t\t\t\t\t\n\t\t\t\t\t<article class=\"post\">\n\t\t\t\t\t\t<div class=\"inner-content\">\n\t\t\t\t\t\t\t<div class=\"thumb\">\n\t\t\t\t\t\t\t\t<a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/sql-projects-for-beginners\" target=\"_blank\"><img decoding=\"async\" width=\"1000\" height=\"518\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1825592495.jpg\" class=\"attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1825592495.jpg 1000w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1825592495-300x155.jpg 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1825592495-768x398.jpg 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1825592495-696x361.jpg 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1825592495-811x420.jpg 811w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1825592495-150x78.jpg 150w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/a>\n\t\t\t\t\t\t\t\t<img decoding=\"async\" class=\"logo\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo-3.jpg\" width=\"100\" height=\"63\" alt=\"\">\n\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<a class=\"content\" href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/sql-projects-for-beginners\" target=\"_blank\">\n\t\t\t\t\t\t\t\t<span class=\"source\">Great Learning Academy<\/span>\n\t\t\t\t\t\t\t\t<h3 class=\"title\" class=\"title\" id=\"sql-projects-for-beginners\"> SQL Projects for Beginners<\/h3>\n\t\t\t\t\t\t\t\t<div class=\"meta\">\n\t\t\t\t\t\t\t\t\t<span class=\"duration\">0.5 Hours<\/span>\n\t\t\t\t\t\t\t\t\t<span class=\"location\">Online<\/span>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t<div class=\"label\">Stand out to recruiters<\/div>\n\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/article>\n\n\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t<\/div>\n<\/div>\n\n\n\n\n<h2 class=\"wp-block-heading\" id=\"need-for-sql-constraints\"><strong>Need for SQL Constraints<\/strong><\/h2>\n\n\n\n<p>Before we get into the details of what SQL constraints are, let's take a look at why they are necessary. In order to arrive at that answer, we need to first understand the way in which information is stored in relational databases and why it is of primal importance to ensure that frameworks are in place governing what information can be entered or altered in a way that that information in the tables are not corrupted.<\/p>\n\n\n\n<p>At a high level, information in <a href=\"https:\/\/www.mygreatlearning.com\/blog\/dbms-tutorial\/\" target=\"_blank\" rel=\"noreferrer noopener\">relational databases<\/a> is segregated into two types, facts and dimensions. Fact tables contain information relating to mathematical information, as a thumb rule, any information on which mathematical operations can be applied, are facts. Sales numbers, costs and revenues are examples of facts. Whereas information like name, date of birth, geographic location etc are examples of dimensional information. In an optimally normalised database, facts and dimensions are stored in separate tables. In order to facilitate rapid extraction of information, relations are built between particular columns in the fact and dimension tables.&nbsp; This is where relational databases get their name from and it is on the basis of these relationships that multiple tables are joined and the data in the tables extracted. Fact and dimension tables are organized in particular structures known as schemas.&nbsp; Star schema and snowflake schema are popular ways of organising this information.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"star-schema\"><strong>Star Schema<\/strong><\/h2>\n\n\n\n<p>A star schema is a database organisational structure optimised for use in a data warehouse or business intelligence that stores transactional or measured data in a single large fact table and attributes about the data in one or more smaller dimensional tables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"snowflake-schema\">Snowflake Schema&nbsp;<\/h2>\n\n\n\n<p>Generally, the association between fact and dimension tables happen via something called a PRIMARY KEY \u2013 FOREIGN KEY relationship. A PRIMARY KEY is a unique identifier of a particular row of information in a fact table, which connects to the FOREIGN KEY in a dimension table to identify dimension information for the same row in the dimension table. Since PRIMARY KEYS are unique identifiers for particular sets of information, for example, a particular sale in the Sales table, it is imperative that the PRIMARY KEY column in the table do not contain duplicates, that is, multiple sales cannot have the same unique ID. Also, values in the PRIMARY KEY column cannot be blank.&nbsp; Otherwise, we wouldn't be able to identify that particular Sale. So generally, there are rules placed on PRIMARY KEY columns in fact tables that ensure the column doesn't contain duplicates and no row in the column is blank. This is a classic example of a SQL constraint!&nbsp; Relational databases are quite fragile in the way that very small changes in the information stored in tables can upset the proper functioning of data storage and data extraction from the database.&nbsp; SQL constraints are placed to ensure that insertions, updates, and deletion of information in a database occur in a way that the smooth functioning of the database is maintained.<\/p>\n\n\n\n<p>Below is a list of common reasons why SQL constraints are applied on databases:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Prevent bad data from being entered into tables of interest.<\/li>\n\n\n\n<li>Enforce business logic at the database level.<\/li>\n\n\n\n<li>Documentation of important database rules.<\/li>\n\n\n\n<li>Enforce relational integrity between any number of tables.<\/li>\n\n\n\n<li>Improve database performance.<\/li>\n\n\n\n<li>Enforce uniqueness.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"types-of-sql-constraints\"><strong>Types of SQL Constraints<\/strong><\/h2>\n\n\n\n<p>SQL constraints can be at a column or a table level. Column level constraints apply to specific columns in a table and do not specify a column name except the check constraints. They refer to the column that they follow. The names are specified by the Table-level constraints of the columns to which they apply.<\/p>\n\n\n\n<p>Following is a list of the most commonly used column and table level SQL constraints:<\/p>\n\n\n\n<p>Column Level Constraints include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>NOT NULL Constraint<\/li>\n\n\n\n<li>UNIQUE Constraint<\/li>\n\n\n\n<li>DEFAULT Constraint<\/li>\n\n\n\n<li>CHECK Constraint<\/li>\n\n\n\n<li>PRIMARY KEY Constraint<\/li>\n\n\n\n<li>FOREIGN KEY Constraint<\/li>\n<\/ul>\n\n\n\n<p>Column Level Constraints include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>UNIQUE Constraint<\/li>\n\n\n\n<li>CHECK Constraint<\/li>\n\n\n\n<li>PRIMARY KEY Constraint<\/li>\n\n\n\n<li>FOREIGN KEY Constraint<\/li>\n\n\n\n<li>INDEX Constraint<\/li>\n<\/ul>\n\n\n\n<p>Let us now dive into the world of SQL constraints! We will browse in detail what each constraint is, why we use them and how to apply and remove them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"the-not-null-constraint\">The NOT NULL Constraint<\/h3>\n\n\n\n<p>A NOT NULL constraint specifies that no cell value for any row in this column can be blank. Generally, this rule is applied to columns that capture information that is absolutely vital to identify and extract data from a table. Continuing the Sales table example, Sale_Id and the Sales_Amount would be potential columns for applying the NOT NULL constraint.<\/p>\n\n\n\n<p>Applying the NOT NULL constraint:<\/p>\n\n\n\n<p>The NOT NULL constraint can be defined either during the creation of the table or can be put in place later via an alter statement.<\/p>\n\n\n\n<p>Declaring a NOT NULL Constraint during the Creation of a Table:<\/p>\n\n\n\n<p>The following SQL creates a NOT NULL constraint in the columns \u2018Sale_Id\u2019, \u2018Sale_Amount\u2019 and \u2018Vendor_Name\u2019 when the table \u2018Sales\u2019 is created:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE TABLE Sales (\n    Sale_Id int NOT NULL,\n    Sale_Amount int NOT NULL,\n    Vendor_Name varchar(255) NOT NULL,\n    Sale_Date date,\n    Profit int\n);\n\n<\/pre><\/div>\n\n\n<p>By specifying the words NOT NULL after the column definition, we create a \u2018Sales\u2019 table where the \u2018Sale_Id\u2019, \u2018Sale_Amount\u2019 and \u2018Vendor_Name\u2019 columns cannot be blank. The column \u2018Profit\u2019 can have null values.<\/p>\n\n\n\n<p>Altering a NOT NULL Constraint after the Creation of a Table:<\/p>\n\n\n\n<p>Consider, that after the creation of the \u2018Sales\u2019 table and storing information in the same, business logic changes and now we're instructed that no sales can be recorded in the \u2018Sales\u2019 table without recording the amount of profit that was earned on the sale. In that case, we will now have to add a constraint that the profit column cannot be null. This is how we would do it:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales\nMODIFY Profit int NOT NULL;\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"the-unique-constraint\"><strong>The UNIQUE Constraint&nbsp;<\/strong><\/h3>\n\n\n\n<p>The UNIQUE&nbsp;constraint specifies that no cell value in a column can be repeated throughout the table.&nbsp; That is, each row for this column in the table has to be unique and non-repetitive. Both the&nbsp;UNIQUE&nbsp;and&nbsp;PRIMARY KEY&nbsp;constraints provide a guarantee for uniqueness for a column or set of columns. A&nbsp;PRIMARY KEY&nbsp;constraint has a&nbsp;UNIQUE&nbsp;constraint automatically. However, you can have many UNIQUE constraints in a table, but only one PRIMARY KEY constraint can be there in one table.<\/p>\n\n\n\n<p>Applying the UNIQUE constraint:<\/p>\n\n\n\n<p>The UNIQUE&nbsp;constraint can be defined either during the creation of the table or can be put in place later via an alter statement.<\/p>\n\n\n\n<p>Declaring a UNIQUE&nbsp;Constraint during the Creation of a Table:<\/p>\n\n\n\n<p>The following SQL creates a UNIQUE&nbsp;constraint in the columns \u2018Sale_Id\u2019 when the table \u2018Sales\u2019 is created in various relational databases:<\/p>\n\n\n\n<p><strong>SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE TABLE Sales (\n    Sale_Id int NOT NULL UNIQUE,\n    Sale_Amount int NOT NULL,\n    Vendor_Name varchar(255) NOT NULL,\n    Sale_Date date,\n    Profit int\n);\n\n<\/pre><\/div>\n\n\n<p><strong>MySQL:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE TABLE Sales (\n    Sale_Id int NOT NULL,\n    Sale_Amount int NOT NULL,\n    Vendor_Name varchar(255) NOT NULL,\n    Sale_Date date,\n    Profit int,\n    UNIQUE(Sale_Id)\n);\n\n<\/pre><\/div>\n\n\n<p>To name a&nbsp;UNIQUE&nbsp;constraint, and to define a&nbsp;UNIQUE&nbsp;constraint on multiple columns, use the following SQL syntax:<\/p>\n\n\n\n<p><strong>SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE TABLE Sales (\n    Sale_Id int NOT NULL,\n    Sale_Amount int NOT NULL,\n    Vendor_Name varchar(255) NOT NULL,\n    Sale_Date date,\n    Profit int,\n    CONSTRAINT UC_Sales UNIQUE (Sale_Id,Sale_Amount));\n\n<\/pre><\/div>\n\n\n<p>Altering a UNIQUE Constraint after the Creation of a Table:<\/p>\n\n\n\n<p>To create a&nbsp;UNIQUE&nbsp;constraint on the \"Sale_Id\" column when the table is already created, use the following SQL:<\/p>\n\n\n\n<p><strong>MySQL \/ SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales\nADD UNIQUE(Sale_Id);\n\n<\/pre><\/div>\n\n\n<p>To name a&nbsp;UNIQUE&nbsp;constraint, and to define a&nbsp;UNIQUE&nbsp;constraint on multiple columns, use the following SQL syntax:<\/p>\n\n\n\n<p><strong>MySQL \/ SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales\nADD CONSTRAINT UC_Sales UNIQUE(Sale_Id,Sale_Amount);\n<\/pre><\/div>\n\n\n<p>Dropping a UNIQUE Constraint:<\/p>\n\n\n\n<p>To drop a&nbsp;UNIQUE&nbsp;constraint, we will need to specify the naming convention that was used during the creation of the constraint:<\/p>\n\n\n\n<p><strong>MySQL:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales\nDROP INDEX UC_Sales;\n\n<\/pre><\/div>\n\n\n<p><strong>SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales\nDROP CONSTRAINT UC_Sales;\n\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"the-check-constraint\"><strong>The CHECK Constraint&nbsp;<\/strong><\/h3>\n\n\n\n<p>The CHECK constraint is used to ensure that all the records in a certain column follow a specific rule. Generally, this constraint is used to enforce business logic on values in a column to make sure that no corrupt information is entered. For example, in the \u2018Sales\u2019 table,&nbsp; let's say that the business has specified a rule that sales made to a certain vendor \u2018ABC\u2019 should not be entered into the \u2018Sales\u2019 table. To ensure that sales to this vendor are not entered into our table, we add a CHECK constraint on the \u2018Vendor_Name\u2019 column which will reject any operation that tries to insert the value \u2018ABC\u2019 in the \u2018Vendor_Name\u2019 column.<\/p>\n\n\n\n<p><strong>Applying the CHECK Constraint:<\/strong><\/p>\n\n\n\n<p>The CHECK constraint can be defined either during the creation of the table or can be put in place later via an alter statement.<\/p>\n\n\n\n<p><strong>Declaring a CHECK Constraint during the Creation of a Table:<\/strong><\/p>\n\n\n\n<p>The following SQL creates a CHECK constraint on the column \u2018Vendor_Name\u2019 when the table \u2018Sales\u2019 is created in various relational databases:<\/p>\n\n\n\n<p><strong>MySQL:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE TABLE Sales (\n    Sale_Id int NOT NULL UNIQUE,\n    Sale_Amount int NOT NULL,\n    Vendor_Name varchar(255),\n    Sale_Date date,\n    Profit int,\nCHECK (Vendor_Name&amp;lt;&gt; \u2019ABC\u2019)\n);\n\n\n<\/pre><\/div>\n\n\n<p><strong>SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE TABLE Sales (\n    Sale_Id int NOT NULL UNIQUE,\n    Sale_Amount int NOT NULL,\n    Vendor_Name varchar(255) CHECK (Vendor_Name&amp;lt;&gt; \u2019ABC\u2019),\n    Sale_Date date,\n    Profit int\n);\n\n<\/pre><\/div>\n\n\n<p>Consider now that along with the rule for the vendor \u2018ABC\u2019, the business only wants sales with a profit greater than 500 to be recorded in the \u2018Sales\u2019 table.&nbsp;<\/p>\n\n\n\n<p>To allow naming of a&nbsp;CHECK&nbsp;constraint, and for defining a&nbsp;CHECK&nbsp;constraint on multiple columns, use the following SQL syntax:<\/p>\n\n\n\n<p><strong>MySQL \/ SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE TABLE Sales (\n    Sale_Id int NOT NULL,\n    Sale_Amount int NOT NULL,\n    Vendor_Name varchar(255),\n    Sale_Date date,\n    Profit int,\nCONSTRAINT Chk_Sales CHECK (Vendor_Name &amp;lt;&gt; \u2019ABC\u2019 and Profit&gt;500)\n);\n\n<\/pre><\/div>\n\n\n<p><strong>Altering a CHECK Constraint after the Creation of a Table:<\/strong><\/p>\n\n\n\n<p>To create a&nbsp;CHECK&nbsp;constraint on the \u2018Vendor_Name\u2019 column when the table is already created, use the following SQL:<\/p>\n\n\n\n<p><strong>MySQL \/ SQL Server \/ Oracle \/ MS Acces<\/strong>s<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales\nADD CHECK (Vendor_Name&amp;lt;&gt; \u2018ABC\u2019);\n\n<\/pre><\/div>\n\n\n<p>To allow naming of a&nbsp;CHECK&nbsp;constraint, and for defining a&nbsp;CHECK&nbsp;constraint on multiple columns, use the following SQL syntax:<\/p>\n\n\n\n<p><strong>MySQL \/ SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales\nADD CONSTRAINT Chk_Sales CHECK (Vendor_Name &amp;lt;&gt; \u2019ABC\u2019 and Profit&gt;500)\n\n<\/pre><\/div>\n\n\n<p>Dropping a CHECK Constraint:<\/p>\n\n\n\n<p>To drop a&nbsp;CHECK&nbsp;constraint, we will need to specify the naming convention that was used during the creation of the constraint:<\/p>\n\n\n\n<p><strong>MySQL:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Persons\nDROP CHECK CHK_Sales;\n\n<\/pre><\/div>\n\n\n<p><strong>SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Persons\nDROP CONSTRAINT CHK_Sales;\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"the-default-constraint\">The DEFAULT Constraint<\/h3>\n\n\n\n<p>The DEFAULT constraint is used to specify a default value that is to be entered in any record in a particular column is left blank. The default value will be added to all new records if no other value is specified.<\/p>\n\n\n\n<p><strong>Applying the DEFAULT Constraint:<\/strong><\/p>\n\n\n\n<p>The DEFAULT constraint can be defined either during the creation of the table or can be put in place later via an alter statement.<\/p>\n\n\n\n<p><strong>Declaring a DEFAULT Constraint during the Creation of a Table:<\/strong><\/p>\n\n\n\n<p>The following SQL sets a&nbsp;DEFAULT&nbsp;value for the \u2018Vendor_Name\u2019 column when the \u2018Sales\u2019 table is created:<\/p>\n\n\n\n<p><strong>My SQL \/ SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE TABLE Sales (\n    Sale_Id int NOT NULL UNIQUE,\n    Sale_Amount int NOT NULL,\n    Vendor_Name varchar(255) DEFAULT \u2018Unknown Vendor\u2019,\n    Sale_Date date,\n    Profit int\n);\n\n<\/pre><\/div>\n\n\n<p>The DEFAULT&nbsp;constraint can also be used to populate columns with system values, for example, GETDATE().<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE TABLE Sales (\n    Sale_Id int NOT NULL UNIQUE,\n    Sale_Amount int NOT NULL,\n    Vendor_Name varchar(255),\n    Sale_Date date DEFAULT GETDATE(),\n    Profit int\n);\n\n<\/pre><\/div>\n\n\n<p><strong>Altering a DEFAULT Constraint after the Creation of a Table:<\/strong><\/p>\n\n\n\n<p>To create a&nbsp;DEFAULT&nbsp;constraint on the \u2018Vendor_Name\u2019 column when the table is already created, use the following SQL:<\/p>\n\n\n\n<p><strong>MySQL:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales\nALTER Vendor_Name SET DEFAULT &#039;Unknown Vendor&#039;;\n\n<\/pre><\/div>\n\n\n<p><strong>SQL Server:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales\nADD CONSTRAINT df_Vendor\nDEFAULT &#039;Unknown Vendor&#039; FOR Vendor_Name;\n\n<\/pre><\/div>\n\n\n<p><strong>MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales\nALTER COLUMN Vendor_Name SET DEFAULT &#039;Unknown Vendor&#039;;\n\n<\/pre><\/div>\n\n\n<p><strong>Oracle:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales\nMODIFY Vendor_Name DEFAULT &#039;Unknown Vendor&#039;;\n\n<\/pre><\/div>\n\n\n<p>Dropping a DEFAULT Constraint:<\/p>\n\n\n\n<p><strong>To drop a&nbsp;DEFAULT&nbsp;constraint, use the following SQL:<\/strong><\/p>\n\n\n\n<p><strong>MySQL:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales\nALTER Vendor_Name DROP DEFAULT;\n\n<\/pre><\/div>\n\n\n<p><strong>SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales\nALTER COLUMN Vendor_Name DROP DEFAULT;\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"the-index-constraint\">The INDEX Constraint<\/h3>\n\n\n\n<p>The INDEX constraint is used to create indexes on a table in a relational database. Tables in a relational database can grow to be extremely long with a great number of rows present in each table, under the circumstances, retrieving information via SQL can sometimes be a very time taking process. By creating an index, the performance of data retrieval queries can be greatly improved. The users cannot see the indexes, they are just used by the SQL engine to speed up searches\/queries.<\/p>\n\n\n\n<p><strong>Applying the INDEX Constraint:<\/strong><\/p>\n\n\n\n<p>We have the option of creating an INDEX that allows duplicates, or we can create a unique INDEX. Indexes can be created or dropped at any point in time and do not have to be a part of the table definition at the time of table creation.<\/p>\n\n\n\n<p><strong>Creating an INDEX Constraint:<\/strong><\/p>\n\n\n\n<p>Creates an INDEX on a table with duplicate values allowed:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE INDEX idx_id\nON Sales (Sale_Id);\n\n<\/pre><\/div>\n\n\n<p><strong><span style=\"text-decoration: underline;\">Creating aUNIQUEINDEX Constraint:<\/span><\/strong><\/p>\n\n\n\n<p>Creates a unique INDEX on a table. Duplicate values are not allowed:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE UNIQUE INDEX idx_id\nON Sales (Sale_Id);\n\n<\/pre><\/div>\n\n\n<p>If you want to create an index on a combination of columns, you can list the column names within the parentheses, separated by commas:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE UNIQUE INDEX idx_sale\nON Sales (Sale_Id, Sale_Amount);\n\n<\/pre><\/div>\n\n\n<p><strong><span style=\"text-decoration: underline;\">Dropping an INDEX Constraint:<\/span><\/strong><\/p>\n\n\n\n<p>The&nbsp;DROP INDEX&nbsp;statement is used to delete an index in a table:<\/p>\n\n\n\n<p><strong>MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nDROP INDEX idx_id ON Sales;\n\n<\/pre><\/div>\n\n\n<p><strong>SQL Server:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nDROP INDEX Sales.idx_id;\n\n<\/pre><\/div>\n\n\n<p><strong>DB2\/Oracle:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nDROP INDEX idx_id;\n\n<\/pre><\/div>\n\n\n<p><strong>MySQL:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales\nDROP INDEX idx_id;\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"the-primary-key-constraint\"><strong>The PRIMARY KEY Constraint<\/strong><\/h3>\n\n\n\n<p>PRIMARY KEYS&nbsp;are unique identifiers for each row present in a table. They can be values present in a single column of a table or a combination of multiple columns in the table. The PRIMARY KEY&nbsp;column cannot be NULL and has to be UNIQUE. The value of the PRIMARY KEY&nbsp;in the table is a unique identifier for a particular row in the parent table which connects the row of the table to further information available in another table (the child table), where the same unique identifier exists as a FOREIGN KEY. Every FOREIGN KEY value in the second table has to exist in the first as a PRIMARY KEY. This is how information is kept consistent in relational databases when they are broken down into multiple Fact and Dimension tables. The PRIMARY KEY - FOREIGN KEY columns are used as the join condition between two tables and the contained information in the tables are extracted.<\/p>\n\n\n\n<p><strong><span style=\"text-decoration: underline;\">Applying the PRIMARY KEY Constraint:<\/span><\/strong><\/p>\n\n\n\n<p>The PRIMARY KEY constraint can be defined either during the creation of the table or can be put in place later via an alter statement.<\/p>\n\n\n\n<p><strong><span style=\"text-decoration: underline;\">Declaring a PRIMARY KEY Constraint during the Creation of a Table:<\/span><\/strong><\/p>\n\n\n\n<p>The following SQL creates a&nbsp;PRIMARY KEY&nbsp;on the \u2018Sale_Id\u2019 column when the \u2018Sales\u2019 table is created in various relational databases:<\/p>\n\n\n\n<p><strong>MySQL:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE TABLE Sales (\n    Sale_Id int NOT NULL,\n    Sale_Amount int NOT NULL,\n    Vendor_Name varchar(255),\n    Sale_Date date,\n    Profit int,\n    PRIMARY KEY (Sale_Id)\n);\n\n<\/pre><\/div>\n\n\n<p><strong>SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE TABLE Sales (\n    Sale_Id int NOT NULL PRIMARY KEY,\n    Sale_Amount int NOT NULL,\n    Vendor_Name varchar(255),\n    Sale_Date date,\n    Profit int,\n);\n\n<\/pre><\/div>\n\n\n<p>To allow naming of a&nbsp;PRIMARY KEY&nbsp;constraint and for defining a&nbsp;PRIMARY KEY&nbsp;constraint on multiple columns, use the following SQL syntax:<\/p>\n\n\n\n<p><strong>MySQL \/ SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE TABLE Sales (\n    Sale_Id int NOT NULL,\n    Sale_Amount int NOT NULL,\n    Vendor_Name varchar(255),\n    Sale_Date date,\n    Profit int,\n    CONSTRAINT PK_Sales PRIMARY KEY (Sale_Id, Sale_Amount) \n);\n<\/pre><\/div>\n\n\n<p><strong>Note:<\/strong>&nbsp;In the example above there is only ONE&nbsp;PRIMARY KEY&nbsp;(PK_Sales). However, the VALUE of the primary key is made up of TWO COLUMNS (Sale_Id + Sale_Amount).<\/p>\n\n\n\n<p><strong><span style=\"text-decoration: underline;\">Altering a PRIMARY KEY Constraint after the Creation of a Table:<\/span><\/strong><\/p>\n\n\n\n<p>To create a&nbsp;PRIMARY KEY&nbsp;constraint on the \u2018Sale_Id\u2019 column when the table is already created, use the following SQL:<\/p>\n\n\n\n<p><strong>MySQL \/ SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales\nADD PRIMARY KEY (Sale_Id);\n\n<\/pre><\/div>\n\n\n<p>To allow naming of a&nbsp;PRIMARY KEY&nbsp;constraint, and for defining a&nbsp;PRIMARY KEY&nbsp;constraint on multiple columns, use the following SQL syntax:<\/p>\n\n\n\n<p><strong>MySQL \/ SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales\nADD CONSTRAINT PK_ Sales PRIMARY KEY (Sale_Id,Sale_Amount);\n\n<\/pre><\/div>\n\n\n<p><strong>Note:<\/strong>&nbsp;If you use&nbsp;ALTER TABLE&nbsp;to add a primary key, the primary key column(s) must have been declared to not contain NULL values (when the table was first created) and have only UNIQUE values, otherwise the SQL statement will not be executed.<\/p>\n\n\n\n<p><strong><span style=\"text-decoration: underline;\">Dropping a PRIMARY KEY Constraint:<\/span><\/strong><\/p>\n\n\n\n<p>To drop a&nbsp;PRIMARY KEY&nbsp;constraint, use the following SQL:<\/p>\n\n\n\n<p><strong>MySQL:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales\nDROP PRIMARY KEY;\n\n<\/pre><\/div>\n\n\n<p><strong>SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales\nDROP CONSTRAINT PK_Sales;\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"the-foreign-key-constraint\">The FOREIGN KEY Constraint<\/h3>\n\n\n\n<p>The foreign key constraint is used to prevent operations in a relational database that would destroy links between tables. The FOREIGN KEY is a column (or a group of columns) in one table, that refers to the PRIMARY KEY of another table. The table with the FOREIGN KEY is called the child table while the referenced table with the PRIMARY KEY is called the parent table.&nbsp;<\/p>\n\n\n\n<p>Consider the two following tables:<\/p>\n\n\n\n<p><strong>Sales table:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Sale_Id<\/td><td>Sale_Amount<\/td><td>Vendor_Name<\/td><td>Sale_Date<\/td><td>Profit<\/td><\/tr><tr><td>123<\/td><td>100<\/td><td>ABC<\/td><td>01-12-2018<\/td><td>20<\/td><\/tr><tr><td>234<\/td><td>200<\/td><td>BCD<\/td><td>14-06-2019<\/td><td>55<\/td><\/tr><tr><td>345<\/td><td>500<\/td><td>CDE<\/td><td>22-03-2020<\/td><td>32<\/td><\/tr><tr><td>456<\/td><td>100<\/td><td>EFG<\/td><td>25-04-2021<\/td><td>40<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Sales_Person table:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Sales_Person_Id<\/td><td>Sales_Person_Name<\/td><td>Sales_Person_Location<\/td><td>Sale_Id<\/td><\/tr><tr><td>1<\/td><td>Rahul<\/td><td>Kolkata<\/td><td>234<\/td><\/tr><tr><td>2<\/td><td>Sweta<\/td><td>Mumbai<\/td><td>456<\/td><\/tr><tr><td>3<\/td><td>Atul<\/td><td>New Delhi<\/td><td>123<\/td><\/tr><tr><td>4<\/td><td>Shruti<\/td><td>Mumbai<\/td><td>345<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The \u2018Sale_Id\u2019 column in the \u2018Sales_person\u2019 table refers to the \u2018Sale_Id\u2019 in the \u2018Sales\u2019 table.&nbsp;<\/p>\n\n\n\n<p>The \u2018Sale_Id\u2019 in the \u2018Sales\u2019 table is the PRIMARY KEY.<\/p>\n\n\n\n<p>The \u2018Sale_Id\u2019 in the \u2018Sales_Person\u2019 table is the FOREIGN KEY.<\/p>\n\n\n\n<p>Notice that every value for \u2018Sale_Id\u2019 present in the \u2018Sales_Person\u2019 table is also available in the \u2018Sale_Id\u2019 column in the \u2018Sales\u2019 table. This is so due to the PRIMARY KEY - FOREIGN KEY relationship defined between the two tables. No value for a \u2018Sale_Id\u2019 can be entered into the \u2018Sales_Person\u2019 table that does not already exist in the \u2018Sales\u2019 table. If we try to insert such a value, because of the PRIMARY KEY - FOREIGN KEY constraint, the insertion will be rejected.<\/p>\n\n\n\n<p><strong><span style=\"text-decoration: underline;\">Applying the FOREIGN KEY Constraint:<\/span><\/strong><\/p>\n\n\n\n<p>The FOREIGN KEY constraint can be defined either during the creation of the table or can be put in place later via an alter statement.<\/p>\n\n\n\n<p><strong><span style=\"text-decoration: underline;\">Declaring a FOREIGN KEY Constraint during the Creation of a Table:<\/span><\/strong><\/p>\n\n\n\n<p>The following SQL creates a&nbsp;FOREIGN KEY&nbsp;on the \u2018Sale_Id\u2019 column when the \u2018Sales_Person\u2019 table is created:<\/p>\n\n\n\n<p><strong>MySQL:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE TABLE Sales_Person (\n    Sales_Person_Id int NOT NULL,\n    Sales_Person_Name varchar(255),\n    Sales_Person_Location varchar(255),\n    Sale_Id int NOT NULL,\n    PRIMARY KEY (Sales_Person_Id),\n    FOREIGN KEY (Sale_Id) REFERENCES Sales(Sale_Id)\n);\n\n<\/pre><\/div>\n\n\n<p><strong>SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE TABLE Sales_Person (\n    Sales_Person_Id int NOT NULL PRIMARY KEY,\n    Sales_Person_Name varchar(255),\n    Sales_Person_Location varchar(255),\n    Sale_Id int FOREIGN KEY REFERENCES Sales(Sale_Id)\n);\n<\/pre><\/div>\n\n\n<p>To allow naming of a&nbsp;FOREIGN KEY&nbsp;constraint, and for defining a&nbsp;FOREIGN KEY&nbsp;constraint on multiple columns, use the following SQL syntax:<\/p>\n\n\n\n<p><strong>MySQL \/ SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCREATE TABLE Sales_Person (\n    Sales_Person_Id int NOT NULL,\n    Sales_Person_Name varchar(255),\n    Sales_Person_Location varchar(255),\n    Sale_Id int NOT NULL,\n    PRIMARY KEY (Sales_Person_Id),\n    CONSTRAINT FK_Sales_Sales_Person FOREIGN KEY (Sale_Id)\n    REFERENCES Persons Sales(Sale_Id)\n);\n\n<\/pre><\/div>\n\n\n<p><strong><span style=\"text-decoration: underline;\">Altering a FOREIGN KEY Constraint after the Creation of a Table:<\/span><\/strong><\/p>\n\n\n\n<p>To create a&nbsp;FOREIGN KEY&nbsp;constraint on the \u2018Sale_Id\u2019 column when the table is already created, use the following SQL:<\/p>\n\n\n\n<p><strong>MySQL \/ SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales_Person\nADD FOREIGN KEY (Sale_Id) REFERENCES Sales(Sale_Id);\n\n<\/pre><\/div>\n\n\n<p>To allow naming of a&nbsp;FOREIGN KEY&nbsp;constraint and for defining a&nbsp;FOREIGN KEY&nbsp;constraint on multiple columns, use the following SQL syntax:<\/p>\n\n\n\n<p><strong>MySQL \/ SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales_Person\nADD CONSTRAINT FK_Sales_Sales_Person \nFOREIGN KEY (Sale_Id) REFERENCES Sales(Sale_Id);\n\n<\/pre><\/div>\n\n\n<p><strong><span style=\"text-decoration: underline;\">Dropping a FOREIGN KEY Constraint:<\/span><\/strong><\/p>\n\n\n\n<p>To drop a&nbsp;FOREIGN KEY&nbsp;constraint, use the following SQL:<\/p>\n\n\n\n<p><strong>MySQL:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales_Person\nDROP FOREIGN KEY FK_Sales_Sales_Person;\n\n<\/pre><\/div>\n\n\n<p><strong>SQL Server \/ Oracle \/ MS Access:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nALTER TABLE Sales_Person\nDROP CONSTRAINT FK_Sales_Sales_Person;\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>This concludes our list of commonly used SQL constraints. Which one do you think is the most useful? Tell us all about your experience with SQL constraints in the comment section below. Happy Learning!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What are SQL constraints? SQL constraints are a set of rules implemented on tables in relational databases to dictate what data can be inserted, updated or deleted in its tables. This is done to ensure the accuracy and the reliability of information stored in the table. Constraints enforce limits to the data or type of [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":33807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36844],"content_type":[],"class_list":["post-33744","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-sql"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>SQL Constraints | What is SQL constraints - Great Learning<\/title>\n<meta name=\"description\" content=\"SQL Constraints: SQL constraints are a set of rules and regulations implemented on tables in relational databases.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Constraints - Everything You Need to Know\" \/>\n<meta property=\"og:description\" content=\"SQL Constraints: SQL constraints are a set of rules and regulations implemented on tables in relational databases.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-08T05:56:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-03T08:57:18+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1851124747-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"665\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-constraints\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-constraints\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"SQL Constraints - Everything You Need to Know\",\"datePublished\":\"2021-06-08T05:56:00+00:00\",\"dateModified\":\"2024-09-03T08:57:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-constraints\\\/\"},\"wordCount\":3071,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-constraints\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/05\\\/shutterstock_1851124747-1.jpg\",\"keywords\":[\"sql\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-constraints\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-constraints\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-constraints\\\/\",\"name\":\"SQL Constraints | What is SQL constraints - Great Learning\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-constraints\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-constraints\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/05\\\/shutterstock_1851124747-1.jpg\",\"datePublished\":\"2021-06-08T05:56:00+00:00\",\"dateModified\":\"2024-09-03T08:57:18+00:00\",\"description\":\"SQL Constraints: SQL constraints are a set of rules and regulations implemented on tables in relational databases.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-constraints\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-constraints\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-constraints\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/05\\\/shutterstock_1851124747-1.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/05\\\/shutterstock_1851124747-1.jpg\",\"width\":1000,\"height\":665,\"caption\":\"Rename Column Name in SQL\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/sql-constraints\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"SQL Constraints &#8211; Everything You Need to Know\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"SQL Constraints | What is SQL constraints - Great Learning","description":"SQL Constraints: SQL constraints are a set of rules and regulations implemented on tables in relational databases.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/","og_locale":"en_US","og_type":"article","og_title":"SQL Constraints - Everything You Need to Know","og_description":"SQL Constraints: SQL constraints are a set of rules and regulations implemented on tables in relational databases.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2021-06-08T05:56:00+00:00","article_modified_time":"2024-09-03T08:57:18+00:00","og_image":[{"width":1000,"height":665,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1851124747-1.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"14 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"SQL Constraints - Everything You Need to Know","datePublished":"2021-06-08T05:56:00+00:00","dateModified":"2024-09-03T08:57:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/"},"wordCount":3071,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1851124747-1.jpg","keywords":["sql"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/","url":"https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/","name":"SQL Constraints | What is SQL constraints - Great Learning","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1851124747-1.jpg","datePublished":"2021-06-08T05:56:00+00:00","dateModified":"2024-09-03T08:57:18+00:00","description":"SQL Constraints: SQL constraints are a set of rules and regulations implemented on tables in relational databases.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1851124747-1.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1851124747-1.jpg","width":1000,"height":665,"caption":"Rename Column Name in SQL"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/sql-constraints\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"SQL Constraints &#8211; Everything You Need to Know"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1851124747-1.jpg",1000,665,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1851124747-1-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1851124747-1-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1851124747-1-768x511.jpg",768,511,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1851124747-1.jpg",1000,665,false],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1851124747-1.jpg",1000,665,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1851124747-1.jpg",1000,665,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1851124747-1-640x665.jpg",640,665,true],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1851124747-1-96x96.jpg",96,96,true],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/05\/shutterstock_1851124747-1-150x100.jpg",150,100,true]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"What are SQL constraints? SQL constraints are a set of rules implemented on tables in relational databases to dictate what data can be inserted, updated or deleted in its tables. This is done to ensure the accuracy and the reliability of information stored in the table. Constraints enforce limits to the data or type of&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/33744","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=33744"}],"version-history":[{"count":21,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/33744\/revisions"}],"predecessor-version":[{"id":109362,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/33744\/revisions\/109362"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/33807"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=33744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=33744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=33744"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=33744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}