--修改字段类型:
--alter table 表名 alter column 待修改字段名 待修改字段类型
alter table users alter column userid varchar(10)

--多个字段建立唯一索引
create unique index [索引名] on 表名(字段名,字段名)
alter table 表名 add constraint 约束名 unique (字段名)
alter table 表名 add constraint 约束名 unique (字段名,字段名)

--创建表的时候创建自增主键
create table tb(id int identity(1,1) primary key ) 

--修改字段主键  自增进入ssms界面设置
alter table tb alter column id int not null 
alter table tb add constraint pkid primary key (id)

--修改字段为非空
alter table tb alter column id int not null 

--建立索引
create index mycolumn_index on mytable (myclumn) 
--AmazonOrderId和seller_id两个字段重复
SELECT count(*) 
FROM (
         SELECT rn = ROW_NUMBER()OVER(PARTITION BY AmazonOrderId, seller_id ORDER BY GETDATE()), * 
         FROM Orders
     ) t
WHERE rn = 1
--学校名称重复的数据
SELECT *  
FROM (  
         SELECT rn = ROW_NUMBER()OVER(PARTITION BY SchoolName ORDER BY GETDATE()), *   
         FROM XS_School 
     ) t  
WHERE rn > 1  

--学校名称重复的数据 且ID最小的记录
SELECT * from XS_School a 
where (a.SchoolName) in (SELECT SchoolName
                           from XS_School group by SchoolName having count(*) > 1) 
and a.ID not in (select min(ID) from XS_School group by SchoolName having count(*)>1) 



本文转载:CSDN博客