{"id":159,"date":"2018-12-06T10:02:28","date_gmt":"2018-12-06T10:02:28","guid":{"rendered":"https:\/\/barkhane.com\/sql\/2018\/12\/06\/sql-constraints\/"},"modified":"2024-03-03T18:01:50","modified_gmt":"2024-03-03T18:01:50","slug":"sql-constraints","status":"publish","type":"post","link":"https:\/\/barkhane.com\/sql\/sql-constraints\/","title":{"rendered":"SQL Constraints"},"content":{"rendered":"<p>SQL Constraints are the rules enforced on the data columns of a table. These are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the database.<\/p>\n<p>Constraints could be either on a column level or a table level. The column level constraints are applied only to one column, whereas the table level constraints are applied to the whole table.<\/p>\n<p>Following are some of the most commonly used constraints available in SQL.<\/p>\n<ul class=\"list\">\n<li>NOT NULL Constraint\u00a0\u2212 Ensures that a column cannot have NULL value.<\/li>\n<li>DEFAULT Constraint\u00a0\u2212 Provides a default value for a column when none is specified.<\/li>\n<li>UNIQUE Constraint\u00a0\u2212 Ensures that all values in a column are different.<\/li>\n<li>PRIMARY Key\u00a0\u2212 Uniquely identifies each row\/record in a database table.<\/li>\n<li>FOREIGN Key\u00a0\u2212 Uniquely identifies a row\/record in any of the given database table.<\/li>\n<li>CHECK Constraint\u00a0\u2212 The CHECK constraint ensures that all the values in a column satisfies certain conditions.<\/li>\n<li>INDEX\u00a0\u2212 Used to create and retrieve data from the database very quickly.<\/li>\n<\/ul>\n<p>Constraints can be specified when a table is created with the CREATE TABLE statement or you can use the ALTER TABLE statement to create constraints even after the table is created.<\/p>\n<h2>Dropping Constraints<\/h2>\n<p>Any constraint that you have defined can be dropped using the ALTER TABLE command with the DROP CONSTRAINT option.<\/p>\n<p>For example, to drop the primary key constraint in the EMPLOYEES table, you can use the following command.<\/p>\n<pre class=\"prettyprint notranslate prettyprinted\"><span class=\"pln\">ALTER TABLE EMPLOYEES DROP CONSTRAINT EMPLOYEES_PK<\/span><span class=\"pun\">;<\/span><\/pre>\n<p>Some implementations may provide shortcuts for dropping certain constraints. For example, to drop the primary key constraint for a table in Oracle, you can use the following command.<\/p>\n<pre class=\"prettyprint notranslate prettyprinted\"><span class=\"pln\">ALTER TABLE EMPLOYEES DROP PRIMARY KEY<\/span><span class=\"pun\">;<\/span><\/pre>\n<p>Some implementations allow you to disable constraints. Instead of permanently dropping a constraint from the database, you may want to temporarily disable the constraint and then enable it later.<\/p>\n<h2>Integrity Constraints<\/h2>\n<p>Integrity constraints are used to ensure accuracy and consistency of the data in a relational database. Data integrity is handled in a relational database through the concept of referential integrity.<\/p>\n<p>There are many types of integrity constraints that play a role in\u00a0<b>Referential Integrity (RI)<\/b>. These constraints include Primary Key, Foreign Key, Unique Constraints and other constraints which are mentioned above.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL Constraints are the rules enforced on the data columns of a table. These are used to limit the type [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"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":"default","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":[2],"tags":[],"class_list":["post-159","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts\/159","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/comments?post=159"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts\/159\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/media?parent=159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/categories?post=159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/tags?post=159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}