
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to add column using alter in MySQL?
Following is the syntax to add column using alter in MySQL:
alter table yourTableName add column yourColumnName yourDataType default yourValue;
Let us first create a table:
mysql> create table alterTableDemo -> ( -> Id int, -> Name varchar(10) -> ); Query OK, 0 rows affected (0.69 sec)
Let us check the description of the table using DESC command. This displays Field, Type, Key, etc. of the table:
mysql> desc alterTableDemo;
This will produce the following output
+-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | Id | int(11) | YES | | NULL | | | Name | varchar(10) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.01 sec)
Now, add column Age with default value 18. If user won’t supply value for column Age then MySQL will use the default value for Age column. Following is the query to add column using alter command.
mysql> alter table alterTableDemo add column Age int default 18; Query OK, 0 rows affected (0.67 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us check the table description once again:
mysql> desc alterTableDemo;
This will produce the following output
+-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | Id | int(11) | YES | | NULL | | | Name | varchar(10) | YES | | NULL | | | Age | int(11) | YES | | 18 | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
Let us insert record in the table using insert command.
Following is the query
mysql> insert into alterTableDemo(Id,Name,Age) values(100,'Chris',24); Query OK, 1 row affected (0.16 sec) mysql> insert into alterTableDemo(Id,Name) values(101,'Robert'); Query OK, 1 row affected (0.25 sec)
Following is the query to display all records from the table using select statement:
mysql> select *from alterTableDemo;
Following is the output. Since we haven’t set age for ‘Robert’, therefore the default 18 would be set for Age:
+------+--------+------+ | Id | Name | Age | +------+--------+------+ | 100 | Chris | 24 | | 101 | Robert | 18 | +------+--------+------+ 2 rows in set (0.00 sec)
- Related Articles
- How to add comment to column in MySQL using Python?
- Alter a MySQL column to be AUTO_INCREMENT?
- How to add column values in MySQL without using aggregate function?
- How do I alter a MySQL table column defaults?
- How can we alter table to add MySQL virtual GENERATED COLUMNS?
- How can we alter table to add MySQL stored GENERATED COLUMNS?
- How to add auto-increment to column in MySQL database using PhpMyAdmin?
- How to alter the data type of a MySQL table’s column?
- Add a new column and index to an existing table with ALTER in a single MySQL query?
- ALTER TABLE to add a composite primary key in MySQL?
- Alter a table column from VARCHAR to NULL in MySQL
- How to alter a MySQL Column from varchar(30) to varchar(100)?
- How to alter column type of multiple columns in a single MySQL query?
- MySQL ALTER column to remove primary key and auto_increment?
- Add to existing value in MySQL column using CONCAT function?
