现有excel中的值:
希望把词汇表的数据,保存到mysql中。
而mysql中,已经定义了,级别=type,是int类型,希望保存数字1,2,3之类的
现在希望去搞清楚:
mysql中如何保存字符串的枚举的值为1,2,3这种数字
好像需要外部再去定义一个字典的表?
好像还需要外键?
mysql 字段 枚举类型
mysql中的set和enum类型的用法和区别 – CSDN博客
(*´∇`*) 被你发现啦~ mysql数据类型-枚举类型 | 戒修-沉迷技术的小沙弥
mysql how save string enum
mysql how save enum of string
说是:
用字符串,而不是数字,是很多(虽然很久的但是很有名的CMS系统的常见做法
虽然缺点是字符串相比数字,增加存储空间,但是可以忽略不计
而好处是方便查看,不用额外的enum的表了。
How Does MySQL Store Enums? – Stack Overflow
8 Reasons Why MySQL’s ENUM Data Type Is Evil – Chris Komlenic
mysql 如何保存字符串的枚举值
《高性能MySQL》笔记-枚举(ENUM)类型 – CSDN博客
REST 设计困惑?Mysql 是直接存字符串好还是数字好? – SegmentFault 思否
“为了便于自己认识使用 varchar
为了高效(或者说专业设计)使用 int
如果确定用数字存储的话可以考虑用 tinyint 这个相对来说效能会高于 int 这个数值范围是小于 int 的, 不过是 postgresql 的话就不行了这数据库不支持 tinyint 类型”
感觉说的有点道理
“ENUM类型不是SQL标准,属于MySQL,而其他DBMS不一定有原生的支持。 PostgreSQL, MariaDB,与Drizzle (后面那两个就MySQL的分支), 我只知道这三个是支持的ENUM的。”
不推荐用mysql内置的ENUM枚举
mysql中的枚举类型ENUM的用法:-映梦_xiaohan-51CTO博客
mysql string enum to int
mysql save string enum to int
mysql save string enum to int
mysql string enum choose string vs int vs enum
php – MySQL enum. Is it better to use strings or numbers – Stack Overflow
现在暂时用同事选择的:用int去保存
然后对于枚举的值,采用另外的表去保存,
然后其中有个考虑,希望category+value是unique的
搜:
mysql 组合字段 unique
mysql 给表添加唯一约束、联合唯一约束,指定唯一约束的名字 – CSDN博客
mysql创建多列组合唯一索引,unique index – CSDN博客
mysql two field unique
How do I specify unique constraint for multiple columns in MySQL? – Stack Overflow
ter Smitten’s » MySQL; How to make a PRIMARY- or UNIQUE key of multiple fields
MySQL: unique combination of two columns – Database Administrators Stack Exchange
【总结】
mysql中,保存枚举类型的值,采用了:保存int数值
即:
CREATE TABLE `thesaurus` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(100) NOT NULL,
`type` int(11) DEFAULT NULL,
`source` varchar(45) DEFAULT NULL,
`fitAgeLower` int(11) DEFAULT ‘-1’ COMMENT ‘count by month’,
`fitAgeUpper` int(11) DEFAULT ‘-1’,
`difficulty` int(11) DEFAULT ‘0’,
`createTime` datetime DEFAULT NULL,
`modifyTime` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
UNIQUE KEY `name_type_UNIQUE` (`name`,`type`)
) ENGINE=InnoDB AUTO_INCREMENT=1609 DEFAULT CHARSET=utf8;
中的type和difficulty
然后另外再用一个字典的表enum_value_dict,用来查询,int值对应的字符串:
CREATE TABLE `enum_value_dict` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category` enum(‘word_type’,’word_difficulty’) NOT NULL,
`name` char(20) NOT NULL,
`value` int(11) NOT NULL,
`comments` varchar(200) DEFAULT ”,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
UNIQUE KEY `category_name_value_UNIQUE` (`category`,`name`,`value`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
通过category是word_type去获取name是n时,value是1:
然后后续用代码:
# (1) word name
wordNameCellValue, noUseCellRange = crifanOpenpyxl.getRealCellValue(ws, eachRow[ColNumName – 1])
if wordNameCellValue is None:
wordNameCellValue = ""
else:
wordNameCellValue = wordNameCellValue.strip()
wordName = wordNameCellValue
# (2) word difficulty
difficultyCellValue, noUseCellRange = crifanOpenpyxl.getRealCellValue(ws, eachRow[ColNumDifficulty – 1])
if difficultyCellValue is None:
difficultyCellValue = ""
else:
difficultyCellValue = difficultyCellValue.strip()
# Note: Specially process: movers -> mover, starters->starter
if difficultyCellValue[-1] == "s":
difficultyCellValue = difficultyCellValue[0:-1]
getWordDifficultySql = "SELECT * FROM `%s` WHERE (`category`=’word_difficulty’ and `name`=’%s’)" %\
(EnumValueDictTableName, difficultyCellValue)
logging.debug("getWordDifficultySql=%s", getWordDifficultySql)
searchDifficultyOk, resultDict = connection.executeSql(getWordDifficultySql)
logging.debug("%s -> %s, %s", getWordDifficultySql, searchDifficultyOk, resultDict)
if not searchDifficultyOk:
logging.error("Invalid word difficulty %s for row [%d] -> %s", difficultyCellValue, wsCurRowNum, resultDict)
continue
wordDifficulty = resultDict["data"][0]["value"]
# (3) word type
typeCellValue, noUseCellRange = crifanOpenpyxl.getRealCellValue(ws, eachRow[ColNumType – 1])
logging.debug("wordNameCellValue=%s, difficultyCellValue=%s, typeCellValue=%s",
wordNameCellValue, difficultyCellValue, typeCellValue)
if typeCellValue is None:
typeCellValue = ""
else:
typeCellValue = typeCellValue.strip()
getWordTypeSql = "SELECT * FROM `%s` WHERE (`category`=’word_type’ and `name`=’%s’)" %\
(EnumValueDictTableName, typeCellValue)
logging.debug("getWordTypeSql=%s", getWordTypeSql)
searchTypeOk, resultDict = connection.executeSql(getWordTypeSql)
logging.debug("%s -> %s, %s", getWordTypeSql, searchTypeOk, resultDict)
if not searchTypeOk:
logging.error("Invalid word type %s for row [%d] -> %s", typeCellValue, wsCurRowNum, resultDict)
continue
wordType = resultDict["data"][0]["value"]
curDatetime = datetime.now()
source = ""
fitAgeLower = -1
fitAgeUpper = -1
createTime = curDatetime
modifyTime = curDatetime
insertSingleWordSql = """
INSERT INTO %s(`id`,`name`,`type`,`source`,`fitAgeLower`,`fitAgeUpper`,`difficulty`,`createTime`,`modifyTime`)
VALUES(%d, ‘%s’, %d, ‘%s’, %d, %d, %d, ‘%s’, ‘%s’)""" %\
(VocabularyTableName, curId, wordName, wordType, source, fitAgeLower, fitAgeUpper, wordDifficulty, createTime, modifyTime)
logging.debug("[%d] insertSingleWordSql=%s", wsCurRowNum, insertSingleWordSql)
insertOk, resultDict = connection.executeSql(insertSingleWordSql)
logging.debug("%s -> %s, %s", insertSingleWordSql, insertOk, resultDict)
可以正常保存数据进去。
转载请注明:在路上 » 【已解决】mysql中如何保存字符串枚举类型的值