{"id":559,"date":"2016-04-15T04:28:42","date_gmt":"2016-04-15T04:28:42","guid":{"rendered":"https:\/\/sqltutorial.org\/?page_id=559"},"modified":"2025-02-08T01:07:58","modified_gmt":"2025-02-08T08:07:58","slug":"sql-unique-constraint","status":"publish","type":"page","link":"https:\/\/www.sqltutorial.org\/sql-unique-constraint\/","title":{"rendered":"SQL UNIQUE Constraint"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the SQL <code>UNIQUE<\/code> constraint to ensure all values in a column or set of columns are distinct.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-sql-unique-constraint'>Introduction to SQL UNIQUE constraint <a href=\"#introduction-to-sql-unique-constraint\" class=\"anchor\" id=\"introduction-to-sql-unique-constraint\" title=\"Anchor for Introduction to SQL UNIQUE constraint\">#<\/a><\/h2>\n\n\n\n<p>In SQL, a <code>UNIQUE<\/code> constraint ensures that all values in a column or set of columns are unique within the same table. A <code>UNIQUE<\/code> constraint helps you maintain the data integrity by preventing duplicate values in the specified columns.<\/p>\n\n\n\n<p>In practice, you&#8217;ll find <code>UNIQUE<\/code> constraints helpful for defining columns that require unique values like <code>username<\/code> and <code>email<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='creating-a-unique-constraint'>Creating a UNIQUE constraint <a href=\"#creating-a-unique-constraint\" class=\"anchor\" id=\"creating-a-unique-constraint\" title=\"Anchor for Creating a UNIQUE constraint\">#<\/a><\/h2>\n\n\n\n<p>To create a unique constraint for a column, you use the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name (\n    column1 datatype <span class=\"hljs-keyword\">UNIQUE<\/span>,\n    ...\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, you use the <code>UNIQUE<\/code> keyword to create a unique constraint for the <code>column1<\/code> when <a href=\"https:\/\/www.sqltutorial.org\/sql-create-table\/\">creating the table<\/a>. This <code>UNIQUE<\/code> constraint is a column constraint because we define it in the column definition of the table.<\/p>\n\n\n\n<p>If a <code>UNIQUE<\/code> constraint includes more than one column, you can define a <code>UNIQUE<\/code> constraint as a table constraint:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name (\n    column1 datatype,\n    column2 datatype,\n    <span class=\"hljs-keyword\">UNIQUE<\/span> (column1, column2)\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, you provide a comma-separated list of columns in within the parentheses followed by the <code>UNIQUE<\/code> keyword that appears at the end of the columns list.<\/p>\n\n\n\n<p>First, <a href=\"https:\/\/www.sqltutorial.org\/sql-create-table\/\">create a table<\/a> called <code>headhunters<\/code> that store the information about headhunters:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> headhunters (\n  <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n  first_name <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n  last_name <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n  email <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span> <span class=\"hljs-keyword\">UNIQUE<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=Q1JFQVRFIFRBQkxFIGhlYWRodW50ZXJzICggaWQgSU5UIEdFTkVSQVRFRCBBTFdBWVMgQVMgSURFTlRJVFkgUFJJTUFSWSBLRVksIGZpcnN0X25hbWUgVkFSQ0hBUigyNTUpIE5PVCBOVUxMLCBsYXN0X25hbWUgVkFSQ0hBUigyNTUpIE5PVCBOVUxMLCBlbWFpbCBWQVJDSEFSKDI1NSkgTk9UIE5VTEwgVU5JUVVFICk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In the <code>headhunters<\/code> table, we use a <code>UNIQUE<\/code> constraint for the <code>email<\/code> column to ensure that the email column will not have any duplicate values.<\/p>\n\n\n\n<p>Second, <a href=\"https:\/\/www.sqltutorial.org\/sql-insert\/\">insert a new row<\/a> into the <code>headhunters<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span>\n  headhunters (first_name, last_name, email)\n<span class=\"hljs-keyword\">VALUES<\/span>\n  (<span class=\"hljs-string\">'John'<\/span>, <span class=\"hljs-string\">'Doe'<\/span>, <span class=\"hljs-string\">'j.doe@example.com'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=SU5TRVJUIElOVE8gaGVhZGh1bnRlcnMgKGZpcnN0X25hbWUsIGxhc3RfbmFtZSwgZW1haWwpIFZBTFVFUyAoJ0pvaG4nLCAnRG9lJywgJ2ouZG9lQGV4YW1wbGUuY29tJyk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Third, attempt to insert a new row with an email that already exists in the <code>email<\/code> column:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span>\n  headhunters (first_name, last_name, email)\n<span class=\"hljs-keyword\">VALUES<\/span>\n  (<span class=\"hljs-string\">'Jane'<\/span>, <span class=\"hljs-string\">'Doe'<\/span>, <span class=\"hljs-string\">'j.doe@example.com'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The database system issued an error indicating that the email is duplicated and rejected the insert:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">ERROR:  duplicate key value violates unique constraint \"headhunters_email_key\"<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='unique-constraint-name'>UNIQUE constraint name <a href=\"#unique-constraint-name\" class=\"anchor\" id=\"unique-constraint-name\" title=\"Anchor for UNIQUE constraint name\">#<\/a><\/h2>\n\n\n\n<p>When defining a unique constraint, you can optionally assign it a name using the <code>CONSTRAINT<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">CONSTRAINT constraint_name\nUNIQUE (column1, column2, ...);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following statement creates a table called <code>users<\/code> that stores the user information:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-keyword\">users<\/span> (\n  <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n  username <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">25<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n  <span class=\"hljs-keyword\">password<\/span> <span class=\"hljs-built_in\">TEXT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n  phone <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">20<\/span>),\n  <span class=\"hljs-keyword\">CONSTRAINT<\/span> unique_username <span class=\"hljs-keyword\">UNIQUE<\/span> (username)\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=Q1JFQVRFIFRBQkxFIHVzZXJzICggaWQgSU5UIEdFTkVSQVRFRCBBTFdBWVMgQVMgSURFTlRJVFkgUFJJTUFSWSBLRVksIHVzZXJuYW1lIFZBUkNIQVIoMjUpIE5PVCBOVUxMLCBwYXNzd29yZCBURVhUIE5PVCBOVUxMLCBDT05TVFJBSU5UIHVuaXF1ZV91c2VybmFtZSBVTklRVUUgKHVzZXJuYW1lKSApOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this example, we create a unique constraint with the name <code>unique_username<\/code> to ensure that the username column has no duplicate values.<\/p>\n\n\n\n<p>If you don\u2019t provide a name for the&nbsp;<code>UNIQUE<\/code> constraint, the database system will generate a default name for it. The generated unique constraint name may vary depending on the database systems.<\/p>\n\n\n\n<p>The following table illustrates the default names for the unique constraints in different database systems:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Database System<\/th><th>Default Unique Constraint Name<\/th><th>Explanation<\/th><\/tr><\/thead><tbody><tr><td>SQL Server<\/td><td>UQ_<code>TableName<\/code>_<code>ColumnName<\/code>_<code>Hex<\/code><\/td><td>Prefix <code>UQ_<\/code>, followed by the table name, column name, and a hexadecimal value to ensure uniqueness.<\/td><\/tr><tr><td>Oracle<\/td><td>SYS_C<code>n<\/code><\/td><td>Prefix <code>SYS_C<\/code> and followed by a unique number (n).<\/td><\/tr><tr><td>MySQL<\/td><td>unique_constraint_<code>name<\/code><\/td><td>Uses the name provided by the user or defaults to a system-generated name if not specified.<\/td><\/tr><tr><td>PostgreSQL<\/td><td><code>table<\/code>_<code>column<\/code>_key<\/td><td>Concatenate table name, column name, and literal string key.<\/td><\/tr><tr><td>SQLite<\/td><td>sqlite_autoindex_<code>TableName<\/code>_<code>N<\/code><\/td><td>Prefix <code>sqlite_autoindex_<\/code>followed by the table name and a unique number (<code>N<\/code>)<\/td><\/tr><tr><td>DB2<\/td><td>SQL<code>nnnnn<\/code><\/td><td>Prefix SQL, followed by a unique number <code>nnnnn<\/code> .<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='adding-unique-constraints-to-existing-tables'>Adding UNIQUE constraints to existing tables <a href=\"#adding-unique-constraints-to-existing-tables\" class=\"anchor\" id=\"adding-unique-constraints-to-existing-tables\" title=\"Anchor for Adding UNIQUE constraints to existing tables\">#<\/a><\/h2>\n\n\n\n<p>To add a unique constraint to an existing table, you can use the <code>ALTER TABLE ... ADD CONSTRAINT<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">CONSTRAINT<\/span> constraint_name \n<span class=\"hljs-keyword\">UNIQUE<\/span> (colum1, column2, ...);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If the table has data, the values in columns (<code>column1<\/code>, <code>column2<\/code>, &#8230;) of the unique constraint must be distinct or you&#8217;ll encounter an error.<\/p>\n\n\n\n<p>For example, the following statement uses the <code>ALTER TABLE ... ADD CONSTRAINT<\/code> statement to add a unique constraint to the <code>users<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-keyword\">users<\/span>\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">CONSTRAINT<\/span> unique_phone <span class=\"hljs-keyword\">UNIQUE<\/span> (phone);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=QUxURVIgVEFCTEUgdXNlcnMgQUREIENPTlNUUkFJTlQgdW5pcXVlX3Bob25lIFVOSVFVRSAocGhvbmUpOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>If you want to add a new column with a <code>UNIQUE<\/code> constraint, you use the <code>ALTER TABLE ... ADD column<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name\n<span class=\"hljs-keyword\">ADD<\/span> new_column datatype <span class=\"hljs-keyword\">UNIQUE<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following statement adds the <code>employee_id<\/code> column with the <code>UNIQUE<\/code> constraint to the <code>users<\/code> table.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-keyword\">users<\/span>\n<span class=\"hljs-keyword\">ADD<\/span> employee_id <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">UNIQUE<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=QUxURVIgVEFCTEUgdXNlcnMgQUREIGVtcGxveWVlX2lkIElOVCBVTklRVUU7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>This unique constraint ensures that no two employees share the same user account.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='removing-unique-constraints'>Removing UNIQUE constraints <a href=\"#removing-unique-constraints\" class=\"anchor\" id=\"removing-unique-constraints\" title=\"Anchor for Removing UNIQUE constraints\">#<\/a><\/h2>\n\n\n\n<p>To remove a <code>UNIQUE<\/code> constraint from a table, you use the <code>ALTER TABLE .. DROP CONSTRAINT<\/code> statement with the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name\n<span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">CONSTRAINT<\/span> unique_constraint_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, specify the table name you want to remove the unique constraint.<\/li>\n\n\n\n<li>Second, provide the unique constraint name you want to drop in the <code>DROP CONSTRAINT<\/code> clause.<\/li>\n<\/ul>\n\n\n\n<p>For example, the following statement drops the <code>unique_phone<\/code> constraint from the <code>users<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-keyword\">users<\/span>\n<span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">CONSTRAINT<\/span> unique_phone;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='unique-vs-primary-key-constraints'>UNIQUE vs. PRIMARY KEY constraints <a href=\"#unique-vs-primary-key-constraints\" class=\"anchor\" id=\"unique-vs-primary-key-constraints\" title=\"Anchor for UNIQUE vs. PRIMARY KEY constraints\">#<\/a><\/h2>\n\n\n\n<p>A table can have one and only one <a href=\"https:\/\/www.sqltutorial.org\/sql-primary-key\/\">PRIMARY KEY<\/a> constraint but can have multiple <code>UNIQUE<\/code> constraints. Additionally, a <code>PRIMARY KEY<\/code> constraint does not allow <code>NULL<\/code> whereas a <code>UNIQUE<\/code> constraint allows <code>NULL<\/code>.<\/p>\n\n\n\n<p>The following table compares the <code>PRIMARY KEY<\/code> and <code>UNIQUE<\/code> constraints:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>PRIMARY KEY constraint<\/th><th>UNIQUE constraint<\/th><\/tr><\/thead><tbody><tr><td>The number of constraints<\/td><td>One primary key per table<\/td><td>Multiple unique constraints per table<\/td><\/tr><tr><td>Allow Nulls<\/td><td>No<\/td><td>Yes<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>UNIQUE<\/code> constraint to ensure a column of a set of columns has distinct values.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=unique-constraint\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n\n\n<h2 class=\"wp-block-heading\" id='databases'>Databases <a href=\"#databases\" class=\"anchor\" id=\"databases\" title=\"Anchor for Databases\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-unique-constraint\/\" target=\"_blank\" rel=\"noreferrer noopener\">PostgreSQL UNIQUE constraint<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-unique-constraint\/\" target=\"_blank\" rel=\"noreferrer noopener\">MySQL UNIQUE constraint<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.mariadbtutorial.com\/mariadb-basics\/mariadb-unique-constraint\/\" target=\"_blank\" rel=\"noreferrer noopener\">MariaDB UNIQUE constraint<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.oracletutorial.com\/oracle-basics\/oracle-unique-constraint\/\" target=\"_blank\" rel=\"noreferrer noopener\">Oracle UNIQUE constraint<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.db2tutorial.com\/db2-basics\/db2-unique-constraint\/\" target=\"_blank\" rel=\"noreferrer noopener\">Db2 UNIQUE constraint<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-unique-constraint\/\" target=\"_blank\" rel=\"noreferrer noopener\">SQLite UNIQUE constraint<\/a><\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"559\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqltutorial.org\/sql-unique-constraint\/\"\n\t\t\t\tdata-post-title=\"SQL UNIQUE Constraint\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"559\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqltutorial.org\/sql-unique-constraint\/\"\n\t\t\t\tdata-post-title=\"SQL UNIQUE Constraint\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn how to use the SQL UNIQUE constraint to ensure all values in a column or set of columns are distinct. Introduction to SQL UNIQUE constraint # In SQL, a UNIQUE constraint ensures that all values in a column or set of columns are unique within the same table. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":44,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-559","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL UNIQUE Constraint<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn how to use the SQL UNIQUE constraint to ensure all values in a column or set of columns are distinct.\" \/>\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.sqltutorial.org\/sql-unique-constraint\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL UNIQUE Constraint\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to use the SQL UNIQUE constraint to ensure all values in a column or set of columns are distinct.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqltutorial.org\/sql-unique-constraint\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-08T08:07:58+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-unique-constraint\/\",\"url\":\"https:\/\/www.sqltutorial.org\/sql-unique-constraint\/\",\"name\":\"SQL UNIQUE Constraint\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqltutorial.org\/#website\"},\"datePublished\":\"2016-04-15T04:28:42+00:00\",\"dateModified\":\"2025-02-08T08:07:58+00:00\",\"description\":\"In this tutorial, you will learn how to use the SQL UNIQUE constraint to ensure all values in a column or set of columns are distinct.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-unique-constraint\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqltutorial.org\/sql-unique-constraint\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-unique-constraint\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqltutorial.org\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL UNIQUE Constraint\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqltutorial.org\/#website\",\"url\":\"https:\/\/www.sqltutorial.org\/\",\"name\":\"SQL Tutorial\",\"description\":\"An Interactive SQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqltutorial.org\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL UNIQUE Constraint","description":"In this tutorial, you will learn how to use the SQL UNIQUE constraint to ensure all values in a column or set of columns are distinct.","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.sqltutorial.org\/sql-unique-constraint\/","og_locale":"en_US","og_type":"article","og_title":"SQL UNIQUE Constraint","og_description":"In this tutorial, you will learn how to use the SQL UNIQUE constraint to ensure all values in a column or set of columns are distinct.","og_url":"https:\/\/www.sqltutorial.org\/sql-unique-constraint\/","og_site_name":"SQL Tutorial","article_modified_time":"2025-02-08T08:07:58+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqltutorial.org\/sql-unique-constraint\/","url":"https:\/\/www.sqltutorial.org\/sql-unique-constraint\/","name":"SQL UNIQUE Constraint","isPartOf":{"@id":"https:\/\/www.sqltutorial.org\/#website"},"datePublished":"2016-04-15T04:28:42+00:00","dateModified":"2025-02-08T08:07:58+00:00","description":"In this tutorial, you will learn how to use the SQL UNIQUE constraint to ensure all values in a column or set of columns are distinct.","breadcrumb":{"@id":"https:\/\/www.sqltutorial.org\/sql-unique-constraint\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqltutorial.org\/sql-unique-constraint\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqltutorial.org\/sql-unique-constraint\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"SQL UNIQUE Constraint"}]},{"@type":"WebSite","@id":"https:\/\/www.sqltutorial.org\/#website","url":"https:\/\/www.sqltutorial.org\/","name":"SQL Tutorial","description":"An Interactive SQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/comments?post=559"}],"version-history":[{"count":0,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/559\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/media?parent=559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}