频道栏目
首页 > 资讯 > SQL Server > 正文

sql在没有主键的情况下删除表中的重复字段的实例教程

18-06-30        来源:[db:作者]  
收藏   我要投稿

1.查询不重复的数据存到临时表里,删除掉原表,然后将临时表的数据存到原表里,上代码:

[sql] 

Select distinct * into Tmp from a  

Drop table a  

Select * into a from Tmp  

Drop table Tmp  

Select distinct * into Tmp from a

Drop table a

Select * into a from Tmp

Drop table Tmp

2.当原表和其他表有关联时,删除整个表可能造成数据乱掉,因此可以在表中新增一列自增的临时列,删除数据后再将这一列删除,上代码: 

[sql] 

alter table a add newfield int identity(1,1);  

delete a 

    where newfield not in 

       (select min(newfield) from a group by Prodid,Proddes)  

alter table a drop column newfield  

alter table a add newfield int identity(1,1);

delete a

where newfield not in

(select min(newfield) from a group by Prodid,Proddes)

alter table a drop column newfield

相关TAG标签
上一篇:linux根据进程查端口,端口查进程的方法
下一篇:centos安装PHP的zip扩展步骤教程
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站