- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Inserting data into a new column of an already existing table in MySQL?
Let us first create a table −
mysql> create table DemoTable( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(100) ); Query OK, 0 rows affected (0.47 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Name) values('Bob'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Name) values('Adam'); Query OK, 1 row affected (0.28 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+------+ | Id | Name | +----+------+ | 1 | John | | 2 | Bob | | 3 | Adam | +----+------+ 3 rows in set (0.00 sec)
Here is the query to add column −
mysql> alter table DemoTable add column Gender ENUM('MALE','FEMALE'); Query OK, 0 rows affected (0.55 sec) Records :0 Duplicates : 0 Warnings : 0
Following is the query to insert data into a new column of an already existing table −
mysql> update DemoTable set Gender='FEMALE' where Id=2; Query OK, 1 row affected (0.13 sec) Rows matched − 1 Changed − 1 Warnings − 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+----+------+--------+ | Id | Name | Gender | +----+------+--------+ | 1 | John | NULL | | 2 | Bob | FEMALE | | 3 | Adam | NULL | +----+------+--------+ 3 rows in set (0.00 sec)
Advertisements