sql 总结一下,以备后面用
---create database jqtest create database jctest --创建数据库 drop database jctest --删除数据库 create table users (id int not null primary key,sex int)--创建创建表 create table users_news (id int not null primary key,sex int)--创建创建表 alter table users add username varchar(50) alter table users add passwords varchar(100) alter table users add primary key(id)--添加主键 alter table users add updatetime datetime --经常查询的字段需要设置聚集索引, create unique index username_index on users(username) --创建视图 create view NewView as select n.id as nid,u.id as [uid] from users u,users_news n select * from NewView--查询 insert into users(id,username,passwords,sex) values(001,'姜强','1234567',1)--增 update users set updatetime=getdate() where id=001--改 delete users where id=001--删 select sum(id)from users--和 select min(id)from users--最小值 select max(id)from users--最大值 select avg(id)from users--平均值 ----------------------------高级查询----------------------------- --union all 全部数据,union 消除重复行 --union 可以将两个完全相同的表数据完全查出来 select * from users union all select * from users_news --except 可以查出table1存在table2不存在的数据 select * from users except select * from users_news --intersect(相交);intersect 可以查出table1与table2都有的数据 select * from users intersect select * from users_news ----------------------------连接查询------------------------------ --左连接 select * from users u left join users_news n on u.id=n.id --右连接 select *from users u right join users_news n on u.id=n.id --全连接 select * from users u full join users_news n on u.id=n.id ---------------------------特殊函数用法-------------------------- --between select * from users u where id between 1 and 2 --datediff(参数,开始时间,结束时间) 返回的是结束时间-开始时间(参数为单位) select *,datediff(second,updatetime,getdate())as [second] from users where datediff(second,updatetime,getdate())>200 --newid()获取随即ID为87605956-8004-483A-B902-1A22813FE735 select top 2 *,newid() as [newid] from users order by newid() --删除重复数据 --得到不重复的列的ID,得到重复列的最大ID delete from table id not in (不重复ID)and id not in (重复列最大ID) delete from users where id not in ----得到重复列最大ID (select max(id) from users group by passwords having count(passwords)>1) and id not in -----得到不重复ID (select max(id) from users group by passwords having count(passwords)=1) -----------------------------------------存储过程------------------------------------------------ ---分页存储 create proc Pager @pagesize int, @pageindex int as begin declare @start int,@end int set @start=((@pageindex-1) * @pagesize)+1; set @end=@pageindex * @pagesize; --row_number() over(order by id asc) 这是sql2005之后加的函数,将数据按照id 排序并生成新的排序号 select * from (select *,row_number() over(order by id asc) as number from users) t where t.number between @start and @end end exec Pager 2,3 select * from users select distinct(passwords) from users select * from users where username in('姜强北京','姜强拉萨') -------------------------事物------------------------------------------- alter proc shiwu @pagesize int, @pageindex int as begin tran insert into users(id,username,passwords,sex)values(006,'姜强北京','姜强北京',1); insert into users(id,username,passwords,sex) values(008,'321564','2313',1)--增 declare @start int,@end int set @start=((@pageindex-1)*@pagesize)+1; set @end=@pageindex*@pagesize; select * from (select *,row_number()over(order by id asc) as number from users) t where t.number between @start and @end print @@error if @@error<>0 begin rollback tran end else begin commit tran end exec shiwu 1,1