
- 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 create and fill a new column in an already created MySQL table?
Let us first create a table −
mysql> create table DemoTable -> ( -> Value1 int, -> Value2 int -> ); Query OK, 0 rows affected (0.77 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10,10); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(20,30); Query OK, 1 row affected (0.24 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+--------+--------+ | Value1 | Value2 | +--------+--------+ | 10 | 10 | | 20 | 30 | +--------+--------+ 2 rows in set (0.00 sec)
Following is the query to create and fill a new third column in a MySQL table. Here, boolean is considered as an integer in an integer context, i.e. the values 0 for false and 1 for true.
We have used generated column here −
mysql> alter table DemoTable add Value3 int generated always as (Value1 = Value2); Query OK, 0 rows affected (0.51 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us display all records from the table once again −
mysql> select *from DemoTable;
Output
This will produce the following output −
+--------+--------+--------+ | Value1 | Value2 | Value3 | +--------+--------+--------+ | 10 | 10 | 1 | | 20 | 30 | 0 | +--------+--------+--------+ 2 rows in set (0.00 sec)
- Related Articles
- How to add a column using MySQL SELECT in an already created table?
- Create a MySQL table from already created table selecting specific rows?
- How to insert auto_increment in an already created table in MySQL?
- Inserting data into a new column of an already existing table in MySQL?
- How to add NOT NULL constraint to an already created MySQL column?
- How can a new column be created to a dataframe using the already present columns in Python?
- I want to create a new field in an already created document. How can this be done using MongoDB query?
- How to check an empty table already in a MySQL database?
- Adding new enum column to an existing MySQL table?
- How to randomize an already created vector in R?
- Create a Pipeline and remove a row from an already created DataFrame - Python Pandas
- Add a new column to table and fill it with the data of two other columns of the same table in MySQL?
- How to concatenate column values and create a new column in an R data frame?
- How can we create a new MySQL table by selecting specific column/s from another existing table?
- How to add a “created at” column in a table to set the timestamp in MySQL?

Advertisements