- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the use of DEFAULT constraint? How can it be applied to a column while creating a table?
DEFAULT constraint is used set a default value for a column in MySQL table. If it is applied on a column then it will take the default value of not giving any value for that column. Its syntax would be as follows −
Syntax
DEFAULT default_value
Here, default_value is the default value set for the column.
Example
The query below will create a table named workers where we assign the column id a DEFAULT value 1000.
mysql> Create table workers(Name Varchar(25), Id INT NOT NULL DEFAULT 1000); Query OK, 0 rows affected (0.47 sec) mysql> Insert into workers(Name, Id) values('Ram', 101); Query OK, 1 row affected (0.04 sec) mysql> Insert into workers(Name) values('Mohan'); Query OK, 1 row affected (0.10 sec) mysql> Select * from Workers; +-------+------+ | Name | Id | +-------+------+ | Ram | 101 | | Mohan | 1000 | +-------+------+ 2 rows in set (0.00 sec)
The above result set stores the default value 1000 for id when we do not give any value for the same.
- Related Articles
- What is MySQL GENERATED COLUMN and how to use it while creating a table?
- Can we use {} while creating a MySQL table?
- What is MySQL NOT NULL constraint and how can we declare a field NOT NULL while creating a table?
- What is MySQL UNIQUE constraint and how can we apply it to the field of a table?
- Set DEFAULT values for columns while creating a table in MySQL
- How MySQL CONCAT() function, applied to the column/s of a table, can be combined with the column/s of other tables?
- While creating a MySQL table use the reserved keyword ‘Key’
- How can we apply a NOT NULL constraint to a column of an existing MySQL table?
- While creating a MySQL table, how can I specify the storage engine of my choice rather than using the default storage engine InnoDB?
- Set AUTO_INCREMENT in a table while creating it in MySQL?
- How can we use a combination of logical operators while creating MySQL views?
- How can we remove NOT NULL constraint from a column of an existing MySQL table?
- How can we remove PRIMARY KEY constraint from a column of an existing MySQL table?
- How can we remove FOREIGN KEY constraint from a column of an existing MySQL table?
- Fix Error in MySQL syntax while creating a table column with name “index”?

Advertisements