SQL PHP HTML ASP JavaScript articles and free scripts to download
SQL

SQL RENAME TABLE Command

We can rename any table by using RENAME sql command. The data will not be lost. Only the table name will be changed to new name. Here is the command to change the name of a table. Here we will change the name of our student table name to student_new table.

We can also copy data from one table to a new table. Same way we can copy data from one table with WHERE clause to an existing table.

RENAME TABLE student TO student_new


This sql command will rename the student table to student_new

Sample PHP Script to change name of the table.

We will use PDO functions to manage MySQL database from PHP. We will establish connection to MySQL database first by including the config.php file. Read more on database connection using PDO here.
Below code will print success or failure message based on the status of the query execution.

<?Php
require "config.php"; // Database Connection

$count=$dbo->prepare("rename table student2 to student");

if($count->execute()){
echo " Success <br>";
echo " Your table name is changed<br> ";
$row = $count->fetch(PDO::FETCH_OBJ);
}else{
print_r($dbo->errorInfo()); // Printing database error messages
echo " <br>Failed to change the name of the table <br>";
}
?>

After changing the name of the table we can check by displaying all the name of the tables present in the database. Here is the code for that. You can add below code at the end of the table name change code given above.

echo "<br>-----<br><b>Table List</b> <br>-----<br>";
$result = $dbo->query("SHOW TABLES");
while ($row = $result->fetch(PDO::FETCH_NUM)) {
echo $row[0]."<br>";
}

Be the first to post comment on this article :


Google+

Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked




HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer