在 MySQL 中重新创建 UNI 和 MULTI 索引
Recreating UNI & MUL indexes in MySQL
我正在做一个 MySQL 不同,以帮助将我的 table 从开发更新到生产,但我在索引方面遇到了一些麻烦。
我正在使用 show full columns from table
获取 table 的 indexes/keys 并比较它们,然后构建必要的查询以更新第一个 table。
我得到了主键,这很有效。但是我一直在阅读,但我仍然没有完全理解其他两种可能性,MUL
和 UNI
。
我阅读了手册页..
If Key is PRI, the column is a PRIMARY KEY or is one of the columns
in a multiple-column PRIMARY KEY.
If Key is UNI, the column is the first column of a unique-valued index
that cannot contain NULL values.
If Key is MUL, multiple occurrences of a given value are permitted
within the column. The column is the first column of a nonunique index
or a unique-valued index that can contain NULL values.
这是我目前得到的,如果它有某种相关性的话。
if($key1 != $key2){
if($key1['PRI'] != $key2['PRI']){
// remove any existing primary keys
$results[$table_name][] = "ALTER TABLE $table_name DROP PRIMARY KEY;";
// add the new primary keys
$results[$table_name][] = "ALTER TABLE $table_name ADD CONSTRAINT pk_$table_name PRIMARY KEY (".implode(",", $key2['PRI']).")";
}
if($key1['MUL'] != $key2['MUL']){
// what to do here?
}
if($key1['UNI'] != $key2['UNI']){
// what to do here?
}
}
我想我可以为 UNI
做这样的事情:ALTER TABLE table
ADD CONSTRAINT uc_myKey UNIQUE (indexColumn)
..对吗?
MUL
tiple 键怎么样?它的语法是什么?
创建 MUL
和 UNI
类型键的 MySQL 语法是什么?
谢谢。
呃...
switch($type){
case "PRI":
$ret[] = $add ?
"ALTER TABLE `$tbl` ADD PRIMARY KEY(`$col`);" :
"ALTER TABLE `$tbl` DROP PRIMARY KEY;" ;
break;
case "MUL":
$ret[] = $add ?
"ALTER TABLE `$tbl` ADD UNIQUE (`$col`);" :
"ALTER TABLE `$tbl` DROP INDEX `$col`;" ;
break;
case "UNI":
$ret[] = $add ?
"ALTER TABLE `$tbl` ADD INDEX (`$col`);" :
"ALTER TABLE `$tbl` DROP INDEX `$col`;" ;
break;
}
我正在做一个 MySQL 不同,以帮助将我的 table 从开发更新到生产,但我在索引方面遇到了一些麻烦。
我正在使用 show full columns from table
获取 table 的 indexes/keys 并比较它们,然后构建必要的查询以更新第一个 table。
我得到了主键,这很有效。但是我一直在阅读,但我仍然没有完全理解其他两种可能性,MUL
和 UNI
。
我阅读了手册页..
If Key is PRI, the column is a PRIMARY KEY or is one of the columns in a multiple-column PRIMARY KEY.
If Key is UNI, the column is the first column of a unique-valued index that cannot contain NULL values.
If Key is MUL, multiple occurrences of a given value are permitted within the column. The column is the first column of a nonunique index or a unique-valued index that can contain NULL values.
这是我目前得到的,如果它有某种相关性的话。
if($key1 != $key2){
if($key1['PRI'] != $key2['PRI']){
// remove any existing primary keys
$results[$table_name][] = "ALTER TABLE $table_name DROP PRIMARY KEY;";
// add the new primary keys
$results[$table_name][] = "ALTER TABLE $table_name ADD CONSTRAINT pk_$table_name PRIMARY KEY (".implode(",", $key2['PRI']).")";
}
if($key1['MUL'] != $key2['MUL']){
// what to do here?
}
if($key1['UNI'] != $key2['UNI']){
// what to do here?
}
}
我想我可以为 UNI
做这样的事情:ALTER TABLE table
ADD CONSTRAINT uc_myKey UNIQUE (indexColumn)
..对吗?
MUL
tiple 键怎么样?它的语法是什么?
创建 MUL
和 UNI
类型键的 MySQL 语法是什么?
谢谢。
呃...
switch($type){
case "PRI":
$ret[] = $add ?
"ALTER TABLE `$tbl` ADD PRIMARY KEY(`$col`);" :
"ALTER TABLE `$tbl` DROP PRIMARY KEY;" ;
break;
case "MUL":
$ret[] = $add ?
"ALTER TABLE `$tbl` ADD UNIQUE (`$col`);" :
"ALTER TABLE `$tbl` DROP INDEX `$col`;" ;
break;
case "UNI":
$ret[] = $add ?
"ALTER TABLE `$tbl` ADD INDEX (`$col`);" :
"ALTER TABLE `$tbl` DROP INDEX `$col`;" ;
break;
}