How to make a mysql field unique?

*爱你&永不变心* 提交于 2019-12-11 06:54:00

问题


Is there a way to make an existing txt field unique (do not accept duplicated values)?

Field: post_title
Type: text
Collation: utf8_unicode_ci
Null: Sim
Default: NULL

What gonna happens if someone tries to insert a post with an existing title?

Could that affect some functionality of my site?

Structure

CREATE TABLE IF NOT EXISTS `hotaru_posts` (
  `post_id` int(20) NOT NULL AUTO_INCREMENT,
  `post_archived` enum('Y','N') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'N',
  `post_updatedts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `post_author` int(20) NOT NULL DEFAULT '0',
  `post_date` timestamp NULL DEFAULT NULL,
  `post_pub_date` timestamp NULL DEFAULT NULL,
  `post_status` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'processing',
  `post_type` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
  `post_category` int(20) NOT NULL DEFAULT '1',
  `post_tags` text COLLATE utf8_unicode_ci,
  `post_title` text COLLATE utf8_unicode_ci,
  `post_orig_url` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `post_domain` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `post_url` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `post_content` text COLLATE utf8_unicode_ci,
  `post_votes_up` smallint(11) NOT NULL DEFAULT '0',
  `post_votes_down` smallint(11) NOT NULL DEFAULT '0',
  `post_comments` enum('open','closed') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'open',
  `post_media` varchar(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'text',
  `post_img` text COLLATE utf8_unicode_ci NOT NULL,
  `post_subscribe` tinyint(1) NOT NULL DEFAULT '0',
  `post_updateby` int(20) NOT NULL DEFAULT '0',
  `post_views` int(20) NOT NULL DEFAULT '0',
  `post_last_viewer_ip` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '111.111.111.111',
  PRIMARY KEY (`post_id`),
  KEY `post_archived` (`post_archived`),
  KEY `post_status` (`post_status`),
  KEY `post_type` (`post_type`),
  FULLTEXT KEY `post_title` (`post_title`,`post_domain`,`post_url`,`post_content`,`post_tags`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Story Posts' AUTO_INCREMENT=38275 ;

回答1:


here error happens because MySQL can index only the first N chars of a BLOB or TEXT column. So The error mainly happen when there is a field/column type of TEXT or BLOB or those belongs to TEXT or BLOB types such as TINYBLOB, MEDIUMBLOB, LONGBLOB, TINYTEXT, MEDIUMTEXT, and LONGTEXT that you try to make as primary key or index. With full BLOB or TEXT without the length value, MySQL is unable to guarantee the uniqueness of the column as it’s of variable and dynamic size. So, when using BLOB or TEXT types as index, the value of N must be supplied so that MySQL can determine the key length. However, MySQL doesn’t support limit on TEXT or BLOB. TEXT(88) simply won’t work.

So the solution is remove TEXT and set to VARCHAR datatype with lenght of 255.(BY default lenght).

`post_title` varchar(255) COLLATE utf8_unicode_ci UNIQUE KEY



回答2:


Try This:

ALTER TABLE tableName   
  ADD  UNIQUE INDEX `post_title_Index` (`post_title`);



回答3:


I think you need to add a length to the post_title field. Give it some number and that should resolve the error.

ALTER TABLE `hotaru_posts` 
  ADD  UNIQUE INDEX `post_title_Index` (`post_title`,`value`(255));

Give that a shot.



来源:https://stackoverflow.com/questions/9527583/how-to-make-a-mysql-field-unique

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!