SQL Server
一.添加数据
1. 插入数据
(1)使用Insert插入数据行:
语法:Insert Into 表名(列名,列名,…)Values(值列表)
例:Insert Into Student(Sname,Saddress,SSex)
Values(‘张三’, ‘上海松江’, ‘男’)
(2)一次插入多行:
语法:Select Identity(数据类型,标识种子,标识增长量)As列名
Into 新表
From 原始表
例:Select Students.Sname,Students.Sadderss,Students.Semail,
Identity(int,1,1)As StudentId
Into TongXunLu From Students
2.使用T-SQL更新数据
语法:Update 表名 Set 列名=更新值 Where 更新条件
例:Update Scores Set Scores=Scores + 5 Where Scores <= 95
3.使用T-SQL删除数据
(1)使用Delect删除数据:
语法:Delect From 表名 where 删除条件
例:Delete From Students Where Sname=’张三’
(2)使用Truncate Table 删除数据
语法:Truncate Table 表名
例:Truncate Table Students
二.查询数据
语法:Select 列名 From 表名
where 查询条件表达式
Order by 排序的列名 ASC 或 DESC
1. 查询所有的数据行的列: Select * from Students
2. 查询部分行列—条件查询:Select Scode,Sname,Saddress
From Students
Where Saddress=’河南新乡’
3. 在查询中使用列名:Select Scode As 学员编号,Sname As 学员姓名,
From Students
Where Scode<>’12’
或 Select FirstName+’.’+LastName As ’姓名’ From Employees
4.查询空行:Select Sname From Students Where Semail Is Null
5.在查询中作用常量列:Select 姓名=Sname,地址= Sadderss,’河北新龙’As ‘学校名称’ From Students
6.查询返回限制的行数:Select Top 5 Sname,Saddress From Students
Where Ssex=0
7.查询排序:Select StudentID As 学员编号,(Score*0.9+5) As 综合成绩
From Score
Where(Score*0.9+5)>60
Order By Score
8.查询中使用的函数
(1)字符串函数
|
函数名 |
描述 |
举例 |
|
CharInderx |
用来寻找一个指定的字符串中的起始位置 |
Select CharIndex(‘Accp’, ’My Accp Course’,1) 返回:4 |
|
Len |
返回传递给它的字符串长度 |
Select Len(‘SQL Server课程’) 返回:12 |
|
Upper |
把传递它的字符串转为大写 |
Select Upper(‘sql server课程’) 返回:SQL SERVER课程 |
|
Ltuim |
清除字符左边的空格 |
Select Ltrim(‘周智宇’) 返回:周智宇(后面的空格保留) |
|
Rtrim |
清除字符右边的空格 |
Select Rtrim(‘周智宇’) 返回:周智宇(前面的空格保留) |
|
Right |
从字符串右边返回指定数目的字符 |
Select Replace(‘买卖提.吐尔松’,3) 返回:吐尔松 |
|
Replace |
替换一个字符串中的字符 |
Select Replace(‘莫乐可切.杨可’,’可’,’兰’) 返回:莫乐兰切.杨兰 |
|
Stuff |
在一个字符串中,删除指定长度的字符,并在该位置插入一个新的字符串 |
Select Replace(‘ABCDEFG’,2,3,’我的音乐我的世界’) 返回:A我的音乐的我的世界EFG |
9.日期函数
|
函数名 |
描述 |
举例 |
|
GetDate |
取得当前的系统日期 |
Select GetDate 返回:今天的日期 |
|
DateAdd |
将指定的数值添加到指定的日期部分后的日期 |
Select DateAdd(mm,4,’01/01/99’) 返回:以前的日期格式返回05/01/99 |
|
DateDiff |
两个日期之间的指定日期部分的区别 |
Select DateDiff(mm,’01/01/99’,’05/01/99’) 返回:4 |
|
DateName |
日期中指定日期部分的字符串的形式 |
Select DateName(dw,’01/01/2000’) 返回:Saturday |
|
DatePart |
日期中指定日期部分的整数形式 |
Select DatePart(day,’01/05/2000’) 返回:15 |
10.数学函数:
|
函数名 |
描述 |
举例 |
|
Abs |
取数值表达式的绝对值 |
Select Abs(-43) 返回:43 |
|
Ceiling |
取大于或等于指定数值、表达式的小整数 |
Select Ceiling(43.5) 返回:43 |
|
Floor |
取小于或等于指定表达式的最大整数 |
Select Floor(43.5) 返回:43 |
|
Power |
取数值表达式的幂值 |
Select Power(5,2) 返回:25 |
|
Round |
将数值表达式四舍五入为指定精度 |
Select Round(43.543,1) 返回:43.5 |
|
Sign |
对于正数返回+1,对于负数返回-1,对于0返回0 |
Select Sign(-43) 返回:-1 |
|
Sqrt |
取浮点表达式的平方根 |
Select Sqrt(9) 返回:3 |
11.系统函数
|
函数名 |
描述 |
举例 |
|
Convert |
用来转变数据类型 |
Select Convert(varchar(5),12345) 返回:字符串12345 |
|
Current_User |
返回当前用户的名字 |
Select Current_User 返回:你登录的用户名 |
|
DataLength |
返回用于指定表达式的字节数 |
Select DataLength(‘中国A联盟’) 返回:5 |
|
Host_Name |
返回当前用户所登录的计算机名字 |
Select Host_Name() 返回:你所登录的计算机的名字 |
|
System_User |
返回当前所登录的用户名称 |
Select System_User 返回:你当前所登录的用户名 |
|
User_Name |
从给定的用户ID返回用户名 |
Select User_Name(1) 返回:从任意数据库中返回“dbo” |
12.模糊查询
(1)使用Like进行模糊查询:
Select * From Students Where Sname Like ’张%’
(2)使用Between在某个范围内进行查询
Select * From Score Where Score Between 60 and 80
(3)使用In在列举值内进行查询:
Select Sname From Students Where Saddress In(‘北京’,’成都’)
Order By Saddress
13.SQL Server 中的聚合函数
(1)Sum
例:Select Sum(ytd_Sales) From titles Where type=’Business’
(2)Avg
例:Select Avg(Score) As 平均成绩 From Score Where Score>=60
(3)Max和Min
例:Select Avg(Score) As平均成绩,Min(score) As 最低分 From Score Where Scoure>=60
(4)Count
例:Select Count(*) As 及格人数 From Score where Scoure>=60
14.在From 子句中使用Join…On:
Select S.Sname,C.CouresId,C.Score Form Students As S Inner Join Score As C On (S.Scode=C.StudentId)
本文介绍了 SQLServer 中数据的基本操作方法,包括数据的增删改查等常见操作,以及如何利用 SQL 语言进行高效的查询。文章还详细讲解了各种查询技巧,如条件查询、模糊查询、使用聚合函数等。
3395

被折叠的 条评论
为什么被折叠?



