Search Marketing

   
   

Renaming Table Fields Using Alter & Change Command

Fri May 23, 2008 8:44 pm
<<     >>
Comments: 0 Views: 3171

You can easily change any table field name in MySQL using the “ALTER” command by specifying the “CHANGE” option.

Lets assume we have the following database table.

mysql> select * from Name;
+----+--------+---------------------+
| ID | Name | Date |
+----+--------+---------------------+
| 1 | Joe | 2001-02-12 00:00:00 |
| 2 | Jack | 2003-03-15 00:00:00 |
| 3 | John | 1980-05-15 00:00:00 |
| 4 | James | 1994-09-21 00:00:00 |
| 5 | George | 1996-12-11 00:00:00 |
+----+--------+---------------------+

If we wanted to change the name of the Date field to StartDate, we would use the following command. Remember to backup your database, or copy the /var/lib/mysql source files to a safe location before attempting to utilize commands that may change the structure of your database.

mysql> alter table Name change Date StartDate timestamp;
Query OK, 5 rows affected (0.42 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> select * from Name;
+----+--------+---------------------+
| ID | Name | StartDate |
+----+--------+---------------------+
| 1 | Joe | 2001-02-12 00:00:00 |
| 2 | Jack | 2003-03-15 00:00:00 |
| 3 | John | 1980-05-15 00:00:00 |
| 4 | James | 1994-09-21 00:00:00 |
| 5 | George | 1996-12-11 00:00:00 |
+----+--------+---------------------+

Using these simple commands will allow you to fix problems and clean up any naming mistakes made early on in the programming process.



Comments
Name:
*
URL: http: (ex. cnn.com)
Comment:
*
Number:
Math (16 + 3)
* required
SubCategories
Books

© 2017 Christonium LLC

Christonium.com
|
Terms of Use
|
Privacy
ccc