数据库学习

数据库的SQL语句不分大小写,字符串用单引号,对大小写敏感。

在字段中,如果出现中文,需要在前面加N,如 Name=N'小花'

添加

插入语句:insert into person2(ID,Name,Age) values(4,'小强',16)

insert into person1(ID,Name,Age) values(newid(),'lily',23)

创建表:create table person2(ID int not null,Name nvarchar(50),

Age int null)

删除表:dorp table person1

修改表:alter table person1

产生新的ID值:select newid()

更新

更新一个列: update person1 set Age=30 (整列值都变成30)

update person1 set Age=Age+1

更新多个列: update person2 set Age=30,Name='tom'

更新部分数据:update person1 set chinesescore=99 where Age>30 (where

携带过滤条件)

注,SQL语句中,等于判断就是一个等号,如 where Age=30.

不等于判断可以写为!=,也可写为<>.

两条件并列要写成:Age=20 or Age=30 (或)

其次还有 and (是),and not (是前者,不是后者)

例子:where (Age>20 and Age<30)or(Age=80)

删除

清除表中全部数据: delete from person1

删除表:drop table person1

清楚表中部分数据: delete from person2 where Age>20

检索

简单的数据检索: select * from person4 (*表示全部)

只检索需要的列: select FNumber from person4

selecr FName,FAge from person4

根据条件检索: select FName from person4 where FSalary<5000

select * from person4 where FSalary<5000

给字段取别名: select FName as 姓名,FAge as 年龄,FSalary as 薪水

from person4 where FSalary<5000

当查询的东西与任何表不相关时,只用写select+查询内容,例如select getdate()

select getdate() as 时间, newid() as ID

查询表中有多少条数据 select count(*)from person4

查询最高工资 select max(FSalary) from person4

最低: min 平均: avg 总和: sum 数量:count

数据排序

select * from person4 order by FAge (order by 根据什么进行排序)

ASC:升序 DESC:降序 例如:order by FSalary DESC

先根据年龄排,后根据工资排:order by FAge DESC,FSalary ASC,...

注:先删选,后排序。

SQL语句中的通配符

单字符通配符:半角下划线_

多字符通配符:半角百分号%

例如: select * from person4 where FName LIKE '_erry'

select * from person4 where FName like '%n%'

空值处理

null 表示不知道,而不是空

select * from person4 where FName is null.(查询名字为null的数据,这里

不可用=及<>跟null连接 ) 它的反义是 FName is not null.

多值匹配

select * from person4 where FAge=23 or FAge=25 or FAge=28

或者写成 select * from person4 where FAge in (23,25,28)

间于两者之间 select * from person4 where FAge>20 and FAge<30

亦写成 select * from person4 where FAge between 20 and 30

数据分组

select Fage,max(Fsalary),count(*) from person4 group by Fage; 按照年龄进行分组,取出每组有多少人。

group by 必须放到where语句之后,没有出现在group by子句中的列不能放到select 语句后的列名列表中(聚合函数中除外)

having 是对分组后信息的过滤,能用的列和select中能用的列一样

where是对原始信息的过滤

在where中不能使用聚合函数,必须使用having,having要位于group by之后

select Fage,count(*) as 人数 from person4 group by Fage having count(*)>1

限制结果行数

select top 3 * from person4 order by Fsalary DESC 找出工资前三名。

检索按照工资从高到低排序检索从第六名开始一共三个人的信息:

select top 3 * form person4

where Funmber not in (select top 5 Funmber from person4 order by FSalary DESC)

order by Fsalary DESC

//把前五个进行排除,再取剩下的前三个

分页会经常用到.

SQLServer2005后增加了Row_Number函数简化实现。

限制重复

Select distinct Fage from person4 不显示重复的数据

Distinct 不针对字段,若后面跟多个字段,只有完全相同的数据被过滤掉。

联合结果集

Select Fname,Fage,0 from person4

Union

Select Fname Fage,Fsalary from person5

可以同时显示来自两个表格的信息,把两个结果合为一个结果

注:查询结果的上下两列的数目相同和数据类型必须相容

Union会默认将完全重复的数据合并掉,若想显示全部则必须用union all

Select Fname from person4

Union all

Select Fname from person5

通常情况都用union all。

例1:

Select ‘正式员工最高年龄’,MAX(Fage) from person4

Union all

Select ‘正式员工最低年龄’,MIN(Fage) from person4

Union all

Select ‘临时员工最高年龄’,MAX(Fage) from person5

Union all

Select ‘临时员工最低年龄’,MIN(Fage) from person5

例2:

Select Fnumber,Fsalary from person4

Union all

Select ‘工资合计’,sum(FSalsry) from person4

数字函数

Abs() 求绝对值

Ceiling() 舍入到最大整数。 3.33被舍入到4, 2.89被舍入到3, -3.61被舍入到-3

Ceiling:天花板

Floor() 舍入到最小整数。 3.33被舍入到3, 2.89被舍入到2, -3.61被舍入到-4

Floor:地板

Round() 四舍五入。 舍入到“离我半径最近的数”

Round:半径 round(a,b) 把a四舍五入后精确到小数点后b位。

例: select round (-3.61,0) select round(3.1415926,3)

字符串函数

Len() 计算字符串长度。

Lower() 转小写。

Upper() 转大写。

Ltrim() 字符串左侧的空格去掉。

Rtrim() 字符串右侧的空格去掉。

若想要去掉两边的空格,ltrim(rtrim(‘ aa ’))

Substring(string,start-position,length) 取子字符串。 string:主字符串,start-position:子字符串在主字符串中的起始位置,length 子字符串的最大长度

例:select substring(‘abcdefg12345’,2,4)

日期函数

Getdate() 取得当前日期时间

Dateadd(datepart,number,date) 计算增加以后的日期

例:dateadd(day,3,getdate()) 计算3天后的日期。

Dateadd(mouth,-8,getdate()) 计算8个月之前的日期。

注:小时:hh ,getdate():表示当前日期。

Datediff(datepart,startdate,enddate) 计算两个日期之间的差额。

例:select datediff(hh,getdate(),dateadd(day,-3,getdate())) 三天前的日期和现在日期相差多少小时数。

Select Fname,Findate,datediff(year,Findate,getdate()) from person5 计算入职年份

Select datediff((year,Findate,getdate()),count(*) from person5 group by datediff ((year, Findate,getdate()) 统计不同工龄的员工数。

Datepart(datepart,date) 返回一个日期的特定部分

例:select datepart(year,getpart())

Select datepart(year,Findate),count(*) from person5 group by datepart(year,Findate) 统计员工入职年份的个数。

类型转换函数

Cast(expression as date-type)

Convert(date-type,expression)

例如:select cast(‘123’ as int),cast(‘2008-08-08’ as datetime)

Convert(datetime, ‘2008-08-08’),convert(verchar(50),123)

空值处理函数

Isnull(expression,value) 如果expression不为空,则返回expression,否则返回value.

例:Select isnull(Fname,’佚名’) as 姓名 from person4 若Fname为空,则返回“佚名”,若不为空,则返回原来的值

Case函数用法

Case i

When 1 then ‘aaa’

When 2 then ‘bbb’

When 3 then ‘ccc’

Else ‘XXX’

End

当case 的值是1,返回aaa;case的值是2,返回bbb;case的值是3,返回ccc,否则返回XXX。

例:

Select Fname

(

Case when Fslary<2000 then ‘低收入

Case when Fsalary>=2000 and Fsalary<5000 then ‘中等收入’

Else ‘高收入’

) as 收入水平

From person4

索引Index

对经常要进行查询的数据建索引,可提高查询效率,对写代码不影响,缺点是比较占空间。

在where中经常出现的字段建索引。

即使创建了索引,仍然有可能全表扫描,比如like ,函数,类型转换

表连接Join

语法如:Select o.BillNo,c.Name,c.Age

from T_orders as o join T_Customers as c on o.CustonmeId=c.Id

where…

子查询

Select * from (select * from T2 where Fage<30)

单值子查询:

Select 1 as f1,2,(select min(FYeatPublished)from :T_Book),

(select max(FYearPublished)from T_Book) as f4

(只能一行一列)

多行单列子查询:

Select * from T_Reader

Where FYearofJoin in

(

Select FYearPublished from T_Book

)

在书出版那一年出生的读者信息。

Select row_number() over(order by FSalary DESC) as rowunm,

FNumber,FName,FSslary,FAge from T_Employee

Row number() 显示行号,属于开窗函数,不能出现在where中,只能出现在select或order by中。

当要显示第3-5条数据时,可写成

Select * from

(

Select row_number() over(order by FSalary DESC) as rowunm,

FNumber,FName,FSslary,FAge from T_Employee

) as e1

Where e1.rownum>=3 and e1.rownum<=5

经常用于分页。

原文地址:https://www.cnblogs.com/HuXiaoxia/p/3391905.html