SQL从入门到精通视频笔记(sql2005)

create table T_Person(Id int NOT NULL,Name nvarchar(50),Age int NULL)


Drop table T_Person1    删除表。

insert into Person1(Number,Name,Age,NickName) values(3,‘名字’,30,‘aaa’);//

Drop table,create table属于数据定义语言,DDL,

insert,select属于DML,数据操作语言。

有些数据类型可以设置主键。

只有两种可以来做(合理的)int(bint),uniquedentifier(GUID,UUID)

标志增长(自动增长的长度)
一般一个表只有一个标志段,自动增长字段。


guid生成器。

sql中生成 GUID 函数:  select newid(),.NET中是  Guid.Newid() ,返回式Guid类型。

int自增字段。 GUID函数(占空间大,不易读)。业界主流的项目是使用GUID作为主键。。。。

使用GUID的是,数据类型是uniquedetifier, guid 的类型

update Person1 set Age=30;   所有的age设置为30.


update Person1 set Age=50,Name='lucy'

update Person1 set Age=50,NickName=N'中文前面加上N', where Age>=20

deltet form T_Person   只是删除数据


delete from T_Person WHERE FAge>20   

用的最多的是数据的查询

select * from T_Employee

where FSalary<5000


select * from T_Employee

where FSalary<5000


数据与表无关的查询:  select  getdate()   当前时间

select @@Version

sql版本。

select 1+1,getdate(),newid()




select 1+1  as  列1,getdate() as  日期,newid()  as  编号

取出数据  进行运算,取别名,进行统计

select count(*)  from T_Employee   结果集的所有的条数。

select max(FSalary)  from T_Employee
select min(FSalary)  from T_Employee
select avg(FSalary)  from T_Employee
select sum(FSalary)  from T_Employee


对数据进行统计的函数叫  聚合函数,

select * from T_Employee
order by FSalary  排序。


select * from T_Employee
order by FSalary  DESC


select * from T_Employee
order by FSalary  ASC  升序排序



select * from T_Employee
order by FAge ASC  ,FSalary  ASC  升序排序

LIKE  通配符过滤使用。

_,单个字符。

%,任意长度字符。k%  以k开头的任意个字符

select * from T_Employee
where FNmame  LIKE '%N%'   检索姓名中有字符n的员工信息。


select * from T_Employee
where FNmame  LIKE '_erry' 以erry结尾的。

数据库中 字段值为NULL,表示不知道。


c#  指针为null表示不指向对象。

sql中用  is null  ,is not null来进行空值判断。


select * from T_Employee
where  FAge  bttwen 20 and 30

 分组:
select FAge ,count (*) from T_Employee
order by FAge



聚合函数不能出现在where子句中
select * from T_Employee
where count(*)>10
order by FAge

select * from T_Employee
where FSalary>1000
Group by FAge       取出工资大于1000的,再按照年龄进行分组。


select * from T_Employee
Group by FAge
having FSalary >2000     having 是对分组后的数据进行过滤的。不能代替 where

ABS() 求绝对值
CEILING() 舍入到最大整数

FLOOR()  舍入到最小整数

ROUND()  四舍五入。


ROUND(3.141592,3)  保留三位数。
select len('abc')

select FName len(FName) from T_Employee

select SUBSTRING('abc123e',2,3)从第2个位置开始的3个字符。

select LTRIM('   bb  '),RTRIM('   B B  ') ,len

RTRIM:字符串右侧的空格去掉
LTRIM:字符串左侧的空格去掉
LOWER(),UPPER(),大小写转换
LEN(),计算字符串长度
LTRIM(RTRIM('   b   '))
select  

GETDATE(),获取当前日期
DATEADD(datepart,number,date),计算增加以后的日期,

(datepart 计量单位,number, 增量date 待计算日期),


(datepart :

year
month
。。。

select DateAdd(month,-8,getdate())
select DateAdd(day,-3,getdate())

select DateDiff(hh,getdate(),DateAdd(day,-3,getdate())   差的小时数。

select FName,FInDate,DateDiff(year,FInDate,getdate())

FInDate入职时间,


取出每一年入职的员工的个数:  


select  DateDiff(year,FInDate,getdate(),count(*) from T_Employee
 
Group by DateDiff(year,FInDate,getdate())  


DATEPART(datepart,date)  

select DatePart(year,FInDate),count(*)
from T_Employee
group by DatePart(year,FInDate),count(*)


类型转换函数。

cast(expression AS data_type)
convert(data_type,expression)

select case('1234'as int) ,cast('2008-3-2' as datetime),
convert(datetime,'2009-2-2'),convert(varchar(50),123)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值