How Can We Add Columns With Default Values To An Existing MySQL ...

  • Home
  • Whiteboard
  • Online Compilers
  • Practice
  • Articles
  • AI Assistant
  • Jobs
  • Tools
  • Corporate Training
  • Courses
  • Certifications
Menu Categories Login
  • Switch theme
  • SQL
  • HTML
  • CSS
  • Javascript
  • Python
  • Java
  • C
  • C++
  • PHP
  • Scala
  • C#
  • Tailwind CSS
  • Node.js
  • MySQL
  • MongoDB
  • PL/SQL
  • Swift
  • Bootstrap
  • R
  • Machine Learning
  • Blockchain
  • Angular
  • React Native
  • Computer Fundamentals
  • Compiler Design
  • Operating System
  • Data Structure and Algorithms
  • Computer Network
  • DBMS
  • Excel
Technical Questions and Answers
  • Data Structure Data Structure
  • Networking Networking
  • RDBMS RDBMS
  • Operating System Operating System
  • Java Java
  • MS Excel MS Excel
  • iOS iOS
  • HTML HTML
  • CSS CSS
  • Android Android
  • Python Python
  • C Programming C Programming
  • C++ C++
  • C# C#
  • MongoDB MongoDB
  • MySQL MySQL
  • Javascript Javascript
  • PHP PHP
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who
How can we add columns with default values to an existing MySQL table? MySQLMySQLi Database

While adding columns to an existing table with the help of ALTER command we can specify the default value also.

Syntax

Alter table table-name ADD (column-name datatype default data);

Example

In the example below, with the help of ALTER Command, column ‘City’ is added with default value ‘DELHI’ to the table ‘Student’.

mysql> Alter table Student ADD(City Varchar(10) Default 'DELHI'); Query OK, 5 rows affected (0.33 sec) Records: 5 Duplicates: 0 Warnings: 0

Now from DESCRIBE command, we can check the default value of ‘City’ column.

mysql> describe Student\g +---------+--------------+------+-----+---------+-------+ | Field   | Type         | Null | Key | Default | Extra | +---------+--------------+------+-----+---------+-------+ | RollNO  | int(11)      | YES  |     | NULL    |       | | Name    | varchar(20)  | YES  |     | NULL    |       | | Class   | varchar(15)  | YES  |     | NULL    |       | | Grade   | varchar(10)  | YES  |     | NULL    |       | | Address | varchar(25)  | YES  |     | NULL    |       | | Phone   | int(11)      | YES  |     | NULL    |       | | Email   | varchar(20)  | YES  |     | NULL    |       | | City    | varchar(10)  | YES  |     | DELHI   |       | +---------+-------------+------+-----+---------+--------+ 8 rows in set (0.04 sec) Vikyath Ram Vikyath Ram Updated on: 2020-01-29T05:42:28+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started Print Page Previous Next Advertisements

Tag » Add Column Mysql Default Value