Change the column name from a MySQL table with Student record?

MySQLMySQLi Database

To change the column name, use the AS keyword after the column name. Let us first create a table −

mysql> create table DemoTable
(
   Id int,
   StudentFirstNameInCollege varchar(100)
);
Query OK, 0 rows affected (0.68 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(100,'Chris');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values(101,'Bob');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values(102,'David');
Query OK, 1 row affected (0.21 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------+---------------------------+
| Id   | StudentFirstNameInCollege |
+------+---------------------------+
| 100  | Chris                     |
| 101  | Bob                       |
| 102  | David                     |
+------+---------------------------+
3 rows in set (0.00 sec)

Following is the query to change the name of one of the columns in MySQL using AS keyword −

mysql> select Id,StudentFirstNameInCollege AS Name from DemoTable;

This will produce the following output −

+------+-------+
| Id   | Name  |
+------+-------+
| 100  | Chris |
| 101  | Bob   |
| 102  | David |
+------+-------+
3 rows in set (0.00 sec)
raja
Published on 01-Oct-2019 08:04:36
Advertisements