字符串作为主键? [英] String as primary KEY?
问题描述
我有这个资料库结构
CREATE TABLE `productinfo` (
`ProductID` int(11) NOT NULL AUTO_INCREMENT,
`ProductName` varchar(255) NOT NULL,
`ProductImage` varchar(255) NOT NULL,
`CategoryID` int(11) NOT NULL,
`SubCategoryID` int(11) NOT NULL,
`ProductBrief` varchar(255) NOT NULL,
`Features` text NOT NULL,
`Specifications` text NOT NULL,
`Reviews` text NOT NULL,
`Price` varchar(255) NOT NULL,
`Status` tinyint(4) NOT NULL,
PRIMARY KEY (`ProductID`)
) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;
现在我需要将ProductID,CategoryID和SubCategoryID转换为类似Ps-5678的字符串零件号。 ProductID是主键,因此如何更改数据库的结构。 CategoryID和SubCategoryID是其他表中的主键,所以我如何处理这个。它很容易转为
I now I need to turn ProductID, CategoryID, and SubCategoryID into a string like Ps-5678 for the part number. ProductID is the primary key so how do i change the structure of the database. CategoryID, and SubCategoryID are primary keys in other tables so how do i handle this..is it as easy as turning
`ProductID` int(11) NOT NULL AUTO_INCREMENT
into a string..and getting rid of
PRIMARY KEY (`ProductID`)
建议,任何人
推荐答案
主键用于数据库。
显示名称适用于最终用户。
Display names are for end users.
不要与其他人混淆!不要从有意义的东西中创建主键。
Do not confuse one with another! Don't make a primary key out of something that has a meaning. You will regret it sooner or later.
将代理键/身份/自动编号作为主键是一个非常好的主意,并在数据库设计中广泛使用。
Having a surrogate key / identity / autonumber as a primary key is a very good idea and is used widely in database design.
您可以添加列,甚至添加一个DERIVED COLUMN,并在其上添加唯一约束。
You can add a column or even a DERIVED COLUMN and add a unique constraint on it.
这篇关于字符串作为主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!