码迷,mamicode.com
首页 > 数据库 > 详细

SQL server 基本增删改减(表内)

时间:2015-07-16 23:55:39      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

use mydb
--增
insert into Fruit(Ids,Name,Price) values(‘k008‘,‘榴莲‘,9.9)--增加时 主键不能为空,其他项系统默认为 NULL
insert into Fruit values(‘k009‘,‘甜瓜‘,4.8,‘淄博‘,80,‘image/4.gif‘)--所有列都添加,一个也不能落下
--删
begin tran
delete from Fruit --和begin tran 一块执行 数据能回复(roll back)
rollback
delete from Fruit where Ids=‘k008‘ --删除指定行

--修改
update Fruit set Source=null where Ids=‘k005‘ --修改指定行的某项数据
update Fruit set Numbers=65 where Ids=‘k002‘  --修改指定行的某项数据
update Fruit set Ids=‘k008‘where Name=‘甜瓜‘ and Price=‘4.80‘ --修改指定名称和价格的 Ids



--查询
select * from Fruit --查询所有
select Name,Price,Numbers  from Fruit --查询指定列
select Name as‘名称‘,Price as‘价格‘,Source as ‘产地‘ from Fruit --查询指定列,并改变 虚拟表 的列名称(数据库中表没变)
select * from Fruit where Ids=‘k001‘ --查询指定行
select * from Fruit where Price=2.4 and Numbers=65 --查行内所有信息(列指定条件)
select Name from Fruit where Numbers between 80 and 100--查行内名称(列指定条件)
select name from Fruit where Source in(‘烟台‘,‘广东‘)--只查指定地区的行内的名称
select distinct numbers from Fruit --去重查询数量(相同数量只显示一次)

select * from News
select * from News where title like ‘%大龄%‘--模糊查询,查带大龄的信息(%代表很多字符)
select * from News where title like ‘要爱情%‘--模糊查询,查以 要爱情开头的
select * from News where title like ‘%志在必得‘--模糊查询,查以志在必得结尾的
select * from News where title like ‘%农业户_‘--模糊查询,查农业户后只有一个字符的
select * from Fruit order by Numbers asc --按照数量升序排列(desc降序)
select * from Fruit order by Numbers desc,Price desc --按照数量降序排列,相同的在按价格降序排列

select * from Fruit --查询所有
select COUNT(*) from Fruit --返回fruit表里有多少条数据,有的企业为了节省资源 搜索 count(1)也可以,返回的是一个数字
select AVG(Price) as ‘平均价格‘ from Fruit --查询某一列的平均值,输出的是一个数字,列名用 平均价格 显示 SUM-数据和,MAX-最大值,MIN-最小值
select SUM(Numbers) from Fruit
select *,(price*0.8)as ‘折后价格‘ from Fruit --加一列数据库中没有的列,这里是加了一个8折后的价格列,显示为"折后价格"
select numbers,COUNT(1)from Fruit group by Numbers --根据某一列分组,求出该组内成员数量(根据number分组,相同number的被分为一组,并显示组内数量)
select Numbers,COUNT(*) from Fruit group by Numbers having COUNT(*)>1--根据numbers(列)分组,求出每组内成员数量,返回成员数大于1的组

 

SQL server 基本增删改减(表内)

标签:

原文地址:http://www.cnblogs.com/xtxtx/p/4652553.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!