SqlServer 中的增删改查语句的语法

一、增删改查SQL语法:
1.查询语句
第一种法方:
select 列名 from table(数据库表名) where(条件)
第二种法方:
select *(表示所有的列) from table(数据库表名) where(条件)
注意:列名与列名之间用逗号分开。
eg:
1.select ProductID,ProductName,Price
from Product
where Price>5.0
2.select * from Product where Price>5.0

3.如何给列加汉子名称:
格式:“‘列标题’=列名” 或 “'列名'AS 列标题”
eg:
select ProductID=‘产品编号’,ProductName,Price
from Product
where Price>5.0

select '产品编号'as ProductID,ProductName,Price
from Product
where Price>5.0

where 语句中可以使用逻辑运算符
AND OR NOT
eg:
select ProductID,ProductName,Price
from Product
where Price>=5.0 And Price<=10.0

2.使用字符串模糊匹配
格式:
expression[not] like 'string'(escape"换码字符")

3.使用查询列表
如果列的取值范围不是一个连续的区间,而是一些离散的值,此时就应使用 SQL Server 提供的另一个关键字 IN 。

语法格式:column_name [not] IN (value1,value2....)
eg:
select SaleID,SaleName,Sex,Birthday,HireDate,Address
form Seller
where SaleID IN('S01','S02',S07)

4.空值的判定
在SQL Server中,通过null。

5.top 和 distinct
语法:select top integer || top interger percent columnName
from tableName

eg:
分别从Customer表中检索出前5个及表中前20%的顾客信息。
select top 5 *
from Customer
select top 20 percent *
from Customer

查询Product 表中价格最高的6种商品。
eg:
select top 6 *
from Product
order by price desc
asc(低—>高) desc(高->低)
2.向表中插入数据
语法:insert into tableName(columnName...(要插入的数据的列名)) values(expression(与columnName相对应的值))

注意:再插入数据时,对于允许为空的列可以使用NUll插入空值;对于具有默认值的列,可使用Defaulf插入默认值。

eg:
向Seller 表中插入一行数据,其中Sex字段使用默认值为‘男’,HireDate等字段均去空值。
insert into seller(saleid,saleName,sex,birthday,hireDate,address,telephone,telephone,notes)
values('s11','赵宇飞',default,'1974-07-25',null,null,null,null)
or
insert into seller(saleid,saleName,brithday)
values('s11','赵宇飞','1974-07-25')

3.修改表中的数据
语法:update tableName
set columnName=expression(...)
where search_conditions

eg:
1.将Product表中"啤酒"的价格改为4元
update product
set price=4
where productName='啤酒'(注意:一定要加条件 +“where”)

4.删除数据
语法:delete [from] tableName
where search_conditions
eg:
delete from Seller
 where SaleID='s11'(注意:一定要加条件 +“where”,不然就把该表中所有的数据删除了)

转载于:https://www.cnblogs.com/88myself/archive/2011/06/05/2073048.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值