{"id":3985,"date":"2025-06-07T09:01:07","date_gmt":"2025-06-07T09:01:07","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3985"},"modified":"2025-06-07T09:01:08","modified_gmt":"2025-06-07T09:01:08","slug":"sql-primary-key","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-primary-key\/","title":{"rendered":"SQL PRIMARY KEY"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <strong>PRIMARY KEY<\/strong> uniquely identifies each row in a table. It ensures no duplicate or NULL values exist in the key column(s).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd39 <strong>Basic Syntax<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- Single column primary key\ncolumn_name data_type PRIMARY KEY;\n\n-- Composite primary key (multiple columns)\nPRIMARY KEY (column1, column2);<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd39 <strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>CREATE TABLE employees (\n  employee_id INT PRIMARY KEY,   -- unique ID, no NULLs\n  first_name VARCHAR(50),\n  last_name VARCHAR(50),\n  email VARCHAR(100) UNIQUE\n);\n\n-- Composite key example\nCREATE TABLE orders (\n  order_id INT,\n  product_id INT,\n  quantity INT,\n  PRIMARY KEY (order_id, product_id)  -- combination uniquely identifies each row\n);<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd39 <strong>Important Notes<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PRIMARY KEY columns must be UNIQUE and NOT NULL.<\/li>\n\n\n\n<li>Each table can have only <strong>one<\/strong> PRIMARY KEY.<\/li>\n\n\n\n<li>Primary key automatically creates a unique index for faster lookups.<\/li>\n\n\n\n<li>Often used as a reference in FOREIGN KEY constraints.<\/li>\n\n\n\n<li>If no primary key exists, it\u2019s harder to uniquely identify rows and enforce data integrity.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83e\udde0 <strong>Quick Recap<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Key Point<\/th><th>Explanation<\/th><\/tr><\/thead><tbody><tr><td>PRIMARY KEY<\/td><td>Uniquely identifies each row, no duplicates or NULLs<\/td><\/tr><tr><td>Single or Composite<\/td><td>Supports one or multiple columns as key<\/td><\/tr><tr><td>Unique &amp; NOT NULL<\/td><td>Enforced automatically<\/td><\/tr><tr><td>One per table<\/td><td>Only one primary key allowed per table<\/td><\/tr><tr><td>Index created<\/td><td>Unique index created for fast data retrieval<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udca1 Always define a PRIMARY KEY to ensure each record is uniquely identifiable and maintain data integrity.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The PRIMARY KEY uniquely identifies each row in a table. It ensures no duplicate or NULL values exist in the key column(s). \ud83d\udd39 Basic Syntax \ud83d\udd39 Example \ud83d\udd39 Important Notes \ud83e\udde0 Quick Recap Key Point Explanation PRIMARY KEY Uniquely identifies each row, no duplicates or NULLs Single or Composite Supports one or multiple columns as [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3985","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3985","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/cposts"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=3985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}