刪除MySQL中冗餘欄位

來源:互聯網
上載者:User

標籤:des   strong   ar   資料   div   amp   sp   on   ad   

去除表deny_mail中email,type重複的欄位設計deny_mail表時未考慮到唯一性,導致插入了許多重複資料,現編寫資料庫語句修正之。  步驟1:建立暫存資料表tmp儲存 冗餘 鍵資訊
1)create table tmp as select min(id),type,mail from deny_mail having count(type,email)>1  group by type,email; 步驟2:根據暫存資料表刪除冗餘資訊2)delete from deny_mail where id not in (select id from tmp) having count(type,email)>1 group by type,email;
   步驟3:刪除暫存資料表3)drop table tmp; 步驟4:給表添加唯一鍵4)alter table deny_mail add constraint job_unique unique (type,email); mysql> desc deny_mail;+-------------+------------------+------+-----+-------------------+----------------+| Field       | Type             | Null | Key | Default           | Extra          |+-------------+------------------+------+-----+-------------------+----------------+| email       | varchar(255)     | NO   | MUL | NULL              |                | | type        | int(10) unsigned | NO   |     | 0                 |                | | create_time | timestamp        | NO   |     | CURRENT_TIMESTAMP |                | | reason      | varchar(255)     | YES  |     | NULL              |                | | otherReason | varchar(255)     | YES  |     | NULL              |                | | id          | int(10) unsigned | NO   | PRI | NULL              | auto_increment | +-------------+------------------+------+-----+-------------------+----------------+mysql> show create table deny_mail;| Table     | Create Table                                                                       CREATE TABLE `deny_mail` (  `email` varchar(255) NOT NULL,  `type` int(10) unsigned NOT NULL default ‘0‘,  `create_time` timestamp NOT NULL default CURRENT_TIMESTAMP,  `reason` varchar(255) default NULL,  `otherReason` varchar(255) default NULL,  `id` int(10) unsigned NOT NULL auto_increment,  PRIMARY KEY  (`id`),  UNIQUE KEY `email` (`email`,`type`)) ENGINE=InnoDB AUTO_INCREMENT=1698 DEFAULT CHARSET=latin1 資料庫基礎太TM重要了。

刪除MySQL中冗餘欄位

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.