我有一个函数可以将所有带有列的表从旧数据库获取到新数据库。到目前为止一切正常。现在我需要扩展该函数,以便在我的新数据库中的所有表中添加一个代理键(具有自动增量的主键 ID)。
旧数据库:
+-----------------------------------+------------+--------+
| Col1 | Col2 | NumCol |
+-----------------------------------+------------+--------+
| Value 1 | Value 2 | 123 |
| This is a row with only one cell | | |
| This row is testing html entities | Te<br />st | 45 |
+-----------------------------------+------------+--------+
新数据库:
+----+-----------------------------------+------------+--------+
| ID | Col1 | Col2 | NumCol |
+----+-----------------------------------+------------+--------+
| 1 | Value 1 | Value 2 | 123 |
| 2 | This is a row with only one cell | | |
| 3 | This row is testing html entities | Te<br />st | 45 |
+----+-----------------------------------+------------+--------+
如您所见,除了每个表中的新列 ID 之外,所有内容都保持不变。
这是我将旧数据库中的所有内容复制到新数据库的功能:
public function loadOldDBtoNewDB($oldDB,$newDB){
$sqlshowTables = "SHOW TABLES ";
$statement = $this->db->prepare($sqlshowTables);
$statement->execute();
$tables = $statement->fetchAll(PDO::FETCH_NUM);
foreach($tables as $table){
$sql[] = "INSERT INTO ".$newDB.".`".$table[0]."` SELECT * FROM ".$oldDB.".`".$table[0]."`; ";
}
$sqlState = implode(' ', $sql);
$executeStatement = $this->db->exec($sqlState);
}
注意:运行此函数时,旧数据库和新数据库已经存在。
那么我需要如何更改我的插入语句,以便在每次插入期间添加一个 ID(自动增量)列?
紫衣仙女
眼眸繁星