feat(数据库): 修改VIP客户表索引为手机号和车牌号组合唯一
移除phone字段的唯一约束,添加phone和car_number的复合唯一索引 更新相关SQL脚本和PHP代码以支持新的索引规则
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
// 更新VIP客户表结构的PHP脚本
|
||||
require 'db_connect.php';
|
||||
|
||||
try {
|
||||
echo "开始更新VIP客户表结构...<br>";
|
||||
|
||||
// 1. 移除phone字段的UNIQUE约束
|
||||
echo "正在移除phone字段的UNIQUE约束...<br>";
|
||||
$stmt = $pdo->prepare("ALTER TABLE vip_customers DROP INDEX phone");
|
||||
$stmt->execute();
|
||||
echo "✓ phone字段UNIQUE约束已移除<br>";
|
||||
|
||||
// 2. 添加phone和car_number的复合唯一索引
|
||||
echo "正在添加phone+car_number的复合唯一索引...<br>";
|
||||
$stmt = $pdo->prepare("ALTER TABLE vip_customers ADD UNIQUE INDEX idx_phone_car_number (phone, car_number)");
|
||||
$stmt->execute();
|
||||
echo "✓ 复合唯一索引已添加<br>";
|
||||
|
||||
// 3. 验证索引
|
||||
echo "正在验证索引...<br>";
|
||||
$stmt = $pdo->prepare("SHOW INDEX FROM vip_customers");
|
||||
$stmt->execute();
|
||||
$indexes = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
echo "<br>✓ 索引列表:<br>";
|
||||
echo "<table border='1' cellpadding='5' cellspacing='0'>";
|
||||
echo "<tr><th>Table</th><th>Non_unique</th><th>Key_name</th><th>Column_name</th></tr>";
|
||||
foreach ($indexes as $index) {
|
||||
echo "<tr>";
|
||||
echo "<td>{$index['Table']}</td>";
|
||||
echo "<td>{$index['Non_unique']}</td>";
|
||||
echo "<td>{$index['Key_name']}</td>";
|
||||
echo "<td>{$index['Column_name']}</td>";
|
||||
echo "</tr>";
|
||||
}
|
||||
echo "</table>";
|
||||
|
||||
echo "<br>✅ 数据库结构更新成功!";
|
||||
|
||||
} catch (PDOException $e) {
|
||||
echo "<br><span style='color:red'>❌ 错误: " . $e->getMessage() . "</span><br>";
|
||||
|
||||
// 检查是否是因为索引不存在导致的错误
|
||||
if (strpos($e->getMessage(), "doesn't exist") !== false) {
|
||||
echo "可能是因为索引已经不存在,尝试直接添加复合索引...<br>";
|
||||
try {
|
||||
$stmt = $pdo->prepare("ALTER TABLE vip_customers ADD UNIQUE INDEX idx_phone_car_number (phone, car_number)");
|
||||
$stmt->execute();
|
||||
echo "✓ 复合唯一索引已添加<br>";
|
||||
} catch (PDOException $e2) {
|
||||
echo "<br><span style='color:red'>❌ 添加索引失败: " . $e2->getMessage() . "</span><br>";
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
// 关闭连接
|
||||
$pdo = null;
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user