Skip to content

Instantly share code, notes, and snippets.

@remoharsono
Created October 21, 2014 06:14
Show Gist options
  • Save remoharsono/61d2c0efdc98392a9f17 to your computer and use it in GitHub Desktop.
Save remoharsono/61d2c0efdc98392a9f17 to your computer and use it in GitHub Desktop.
PHP - MySQL Primary Key Dropper
<?php
$database_list = array('db_test_1', 'db_test_2');
echo 'Primary Key Dropper for MySQL' . "\n\n";
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
echo 'Could not connect to database server: ' . mysql_error() . "\n";
exit;
}
foreach($database_list as $database_name) {
echo 'Selecting database: ' . $database_name . "\n";
$db_selected = mysql_select_db($database_name, $link);
if (!$db_selected) {
echo 'Could not use ' . $database_name . ': ' . mysql_error() . "\n";
continue;
}
$sql = "SHOW TABLES FROM $database_name";
$result = mysql_query($sql);
if (!$result) {
echo 'DB Error, could not list tables' . "\n";
echo 'MySQL Error: ' . mysql_error() . "\n";
exit;
}
while ($row = mysql_fetch_row($result)) {
$table_name = $row[0];
$sql_drop_pri = "ALTER TABLE $table_name DROP PRIMARY KEY";
$result_drop_pri = mysql_query($sql_drop_pri);
echo 'Dropping primary key of table: ' . $table_name . "\n";
}
mysql_free_result($result);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment