我是靠谱客的博主 糊涂指甲油,这篇文章主要介绍mysql 创建唯一字段,MySQL:在已填充的表中创建新的唯一字段,现在分享给大家,希望可以做个参考。

I need to create a field in a database table, already populated with data. Of course just adding the field empty is not possible. I can only think of creating a new table with a new structure and copy the existing data to the new table, but I wonder if there is a n easier way.

Hint: it is a composite key, comprised of 3 other fields in the same table.

Edit: the field holds a varchar value

Edit: since some of you ask, it is not possible to create a new UNIQUE field in a populated table just by using ADD UNIQUE. It duplicates the new (empty or not) value throughout all entries. Example:

ALTER TABLE 'tablename' ADD 'fieldname' VARCHAR( 64 ) NOT NULL ,

ADD UNIQUE (

'fieldname'

)

error: Duplicate entry '' for key 'fieldname'

解决方案Of course just adding the field empty is not possible.

Why?

I'd do the following:

Create field by ALTER TABLE t ADD COLUMN new_column *type_definition*

Update newly created field like UPDATE t SET new_column=*computation_expression*

Add index by ALTER TABLE t ADD INDEX ... (or ALTER TABLE t ADD PRIMARY KEY ... if you need it to be primary).

最后

以上就是糊涂指甲油最近收集整理的关于mysql 创建唯一字段,MySQL:在已填充的表中创建新的唯一字段的全部内容,更多相关mysql内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(122)

评论列表共有 0 条评论

立即
投稿
返回
顶部