The DEFAULT Constraint is used to fill a column with a default and fixed value. The value will be added to all new records when no other value is provided.
1. Using DEFAULT on CREATE TABLE :
CREATE TABLE tablename ( Columnname DEFAULT 'defaultvalue' );
To set a DEFAULT value for the “Location” column when the “Geeks” table is created –
CREATE TABLE Geeks ( ID int NOT NULL, Name varchar(255), Age int, Location varchar(255) DEFAULT 'Noida');
INSERT INTO Geeks VALUES (4, 'Mira', 23, 'Delhi'); INSERT INTO Geeks VALUES (5, 'Hema', 27); INSERT INTO Geeks VALUES (6, 'Neha', 25, 'Delhi'); INSERT INTO Geeks VALUES (7, 'Khushi', 26);
select * from Geeks;
2. DROP a DEFAULT Constraint :
ALTER TABLE tablename ALTER COLUMN columnname DROP DEFAULT;
ALTER TABLE Geeks ALTER COLUMN Location DROP DEFAULT;
Let us add 2 new rows in Geeks table :
INSERT INTO Geeks VALUES (8, 'Komal', 24, 'Delhi'); INSERT INTO Geeks VALUES (9, 'Payal', 26);
Dropping the default constraint will not affect the current data in the table, it will only apply to new rows.
Select * from Geeks;
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- SQL | CHECK Constraint
- SQL | UNIQUE Constraint
- Unique Constraint in MS SQL Server
- Check Constraint in MS SQL Server
- Foreign Key constraint in SQL
- Primary key constraint in SQL
- Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
- Neo4j Create Constraint
- MySQL | DEFAULT() Function
- SQL | Procedures in PL/SQL
- SQL | Difference between functions and stored procedures in PL/SQL
- Difference between T-SQL and PL-SQL
- Difference between SQL and T-SQL
- Mitigation of SQL Injection Attack using Prepared Statements (Parameterized Queries)
- SQL | SELECT Query
- SQL | Distinct Clause
- SQL | WHERE Clause
- SQL | AND and OR operators
- SQL | INSERT INTO Statement
- SQL | DELETE Statement
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.