{"id":3981,"date":"2025-06-07T08:57:36","date_gmt":"2025-06-07T08:57:36","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3981"},"modified":"2025-06-07T08:57:37","modified_gmt":"2025-06-07T08:57:37","slug":"sql-not-null-default","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-not-null-default\/","title":{"rendered":"SQL NOT NULL \/ DEFAULT"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>NOT NULL<\/strong> ensures a column must have a value \u2014 it cannot be left empty (NULL).<br><strong>DEFAULT<\/strong> sets a value automatically if no explicit value is provided during insert.<\/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>-- NOT NULL constraint\ncolumn_name data_type NOT NULL;\n\n-- DEFAULT value assignment\ncolumn_name data_type DEFAULT default_value;<\/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  id INT PRIMARY KEY,\n  first_name VARCHAR(50) NOT NULL,          -- must have a value\n  last_name VARCHAR(50) NOT NULL,\n  department VARCHAR(100) DEFAULT 'Sales'  -- default is 'Sales' if not specified\n);\n\n-- Insert with explicit department\nINSERT INTO employees (id, first_name, last_name, department)\nVALUES (1, 'John', 'Doe', 'HR');\n\n-- Insert without department uses default\nINSERT INTO employees (id, first_name, last_name)\nVALUES (2, 'Jane', 'Smith');\n\n-- Query to verify\nSELECT * FROM employees;<\/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>NOT NULL columns reject any attempts to insert or update with NULL.<\/li>\n\n\n\n<li>DEFAULT values help maintain consistent data and avoid NULLs.<\/li>\n\n\n\n<li>You can combine both on a column to make it mandatory but still provide a fallback value.<\/li>\n\n\n\n<li>Not all data types support DEFAULT in every DBMS (check your system docs).<\/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>NOT NULL<\/td><td>Column must have a value, no NULL allowed<\/td><\/tr><tr><td>DEFAULT<\/td><td>Assigns default value if none provided<\/td><\/tr><tr><td>Use case<\/td><td>Mandatory fields, ensure data consistency<\/td><\/tr><tr><td>Syntax example<\/td><td><code>column datatype NOT NULL DEFAULT value<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udca1 Use NOT NULL and DEFAULT wisely to enforce data integrity and reduce errors during inserts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NOT NULL ensures a column must have a value \u2014 it cannot be left empty (NULL).DEFAULT sets a value automatically if no explicit value is provided during insert. \ud83d\udd39 Basic Syntax \ud83d\udd39 Example \ud83d\udd39 Important Notes \ud83e\udde0 Quick Recap Key Point Explanation NOT NULL Column must have a value, no NULL allowed DEFAULT Assigns default [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3981","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3981","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=3981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}