MYSQL - 使用字符串作为主键的问题
可能的重复:
Mysql 中的字母数字排序
我的表对记录的排序不正确。主键是一个字符串,它是一个字符,并与从 1 开始的数字组合,例如:F1。当它达到两位数时就会出现问题,例如:F10。数据库仅检查前两个字符并丢弃其余字符。例如:
它应该看起来像这样 F1、F2、F3、...F9、F10、F11 等。
看起来像 F1、F10、F11、F12、......、F2、F3、F4 等。
因此,如果您有任何想法,请分享。
注意:使用MySql 5服务器和MySql Workbench 5.2
以下是数据库中一张表的脚本代码:
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';
DROP SCHEMA IF EXISTS `RimpexDB` ;
CREATE SCHEMA IF NOT EXISTS `RimpexDB` DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci ;
USE `RimpexDB` ;
-- -----------------------------------------------------
-- Table `RimpexDB`.`RpxFornecedor`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `RimpexDB`.`RpxFornecedor` ;
CREATE TABLE IF NOT EXISTS `RimpexDB`.`RpxFornecedor` (
`FID` VARCHAR(80) NOT NULL ,
`FNome` VARCHAR(80) NOT NULL ,
`FDescricao` VARCHAR(1000) NULL ,
`FNTel` VARCHAR(45) NULL ,
`FNCel` VARCHAR(45) NULL ,
`FEndereco` VARCHAR(200) NOT NULL ,
`FEmail` VARCHAR(100) NULL ,
`FFax` VARCHAR(45) NULL ,
`FActivo` CHAR NOT NULL ,
`FDataAct` VARCHAR(200) NOT NULL ,
`FDataNAct` VARCHAR(200) NULL ,
PRIMARY KEY (`FID`) ,
INDEX `FID` (`FID` ASC, `FNome` ASC, `FDescricao` ASC, `FNTel` ASC, `FNCel` ASC, `FEndereco` ASC, `FEmail` ASC, `FFax` ASC, `FActivo` ASC, `FDataAct` ASC, `FDataNAct` ASC) )
ENGINE = InnoDB
COMMENT = 'Fornecedor Table' ;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
Possible Duplicate:
Alphanumeric Order By in Mysql
My tables are sorting my records incorrectly. The primary key is a string that as one char and its combined with a number starting from 1 ex: F1. It becomes problematic when it got to two digit numbers ex: F10. The database only check the first two characters and discard the rest. Ex:
It should look like this F1,F2,F3,...F9,F10,F11,etc.
It looks like this F1, F10, F11, F12,....,F2,F3,F4,etc.
So if you have any ideas please share it.
Note: using MySql 5 server and MySql Workbench 5.2
Here is the script code for one table in the database:
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';
DROP SCHEMA IF EXISTS `RimpexDB` ;
CREATE SCHEMA IF NOT EXISTS `RimpexDB` DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci ;
USE `RimpexDB` ;
-- -----------------------------------------------------
-- Table `RimpexDB`.`RpxFornecedor`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `RimpexDB`.`RpxFornecedor` ;
CREATE TABLE IF NOT EXISTS `RimpexDB`.`RpxFornecedor` (
`FID` VARCHAR(80) NOT NULL ,
`FNome` VARCHAR(80) NOT NULL ,
`FDescricao` VARCHAR(1000) NULL ,
`FNTel` VARCHAR(45) NULL ,
`FNCel` VARCHAR(45) NULL ,
`FEndereco` VARCHAR(200) NOT NULL ,
`FEmail` VARCHAR(100) NULL ,
`FFax` VARCHAR(45) NULL ,
`FActivo` CHAR NOT NULL ,
`FDataAct` VARCHAR(200) NOT NULL ,
`FDataNAct` VARCHAR(200) NULL ,
PRIMARY KEY (`FID`) ,
INDEX `FID` (`FID` ASC, `FNome` ASC, `FDescricao` ASC, `FNTel` ASC, `FNCel` ASC, `FEndereco` ASC, `FEmail` ASC, `FFax` ASC, `FActivo` ASC, `FDataAct` ASC, `FDataNAct` ASC) )
ENGINE = InnoDB
COMMENT = 'Fornecedor Table' ;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MySQL 不只是查看前两个字符并丢弃其余字符,而是按词法顺序(即字母顺序)排序。您需要的排序类型有时称为自然排序。
如果您可以选择将 PK 从字符串更改为数字列,并为每条记录删除“F”字符,那么您的情况会好得多。
MySQL is not just looking at the first two characters and discarding the rest but rather sorting in lexical ordering (ie alphabetic) ordering. The type of ordering you need is sometimes referred to as natural ordering.
If you have the option of changing the PK from the string to just a number column dropping off the 'F' character for each record, you'll be much better off.
假设第一个字符始终是字母(并且只有第一个字符是),这就是您需要对当前数据结构执行的操作:
但是您确实应该考虑将 PK 更改为数字。
Assuming the first character is always a letter (and only the first character is), this is what you need to do with your current data structure:
But you should really consider changing your PK to a numeric one.
一种解决方法是使用 F00001、F00002、...F12345 字符串作为主键。
One workaround could be to use F00001, F00002, ... F12345 strings as your primary key.