插入数据
insert into t_person(name,age,gental) values('你好',12,0)
使用newid()产生一个不重复的guid数
或者还可以在创建数据库时
在id的属性 标识规范 标记 是
insert into t_animal(name,id) values('你好',newid())
newid() 也可以当默认值设置
创建表
create table T_Animal1(ID int not null,name char(10) null)
删除表
drop table t_animal1
当有关于 中文字 的操作时,最好在中文字前加 N
update t_animal set name = N'青年' where age<20
where是判定的条件,可以用 or 和 and 及 not 来连接
也可以用between来取区间
select * from t_animal
where age between 20 and 31
检索
select age,id from t_animal where age > 20
select 可以操作很多和表不相关的操作
select 1+1 as '列1',getdate() as '时间',newid() as 'guid'
查询最高年龄,min()最小,avg()平均,sum()求和
select max(age) from T_animal
查询数据条数
select count(*) from T_animal
按某种顺序排序 DESC降序 ASC升序
select * from T_animal
order by age DESC
当age相同时按年龄排序
select * from T_animal
order by age DESC,name ASC
通用符过滤
_代表一个字符
select * from T_animal
where name like '_好'
查找含有f的数据
select * from T_animal
where name like '%f%'
当查询的数据中含有 _ ,%的时候,用 escape 来标记出 _
select *from T_employee where name like '%/_%' escape '/'
数据的分组 (group by)
取一组的名字排序中最大的,及该组成员个数
select max(name) , count(*) from t_animal
group by age
group后不能用where!!!!!!!!!!!!!!!!!!!!!
可以用having
group前还是可以用where的
也就是 where 是对group前的约定 having 是对group后的行的约定
只选前几个数据
select top 3* from t_animal
order by age ASC
选出3个人按年龄排序,这三个人丛第4个人开始从年龄高到低
select top 3* from t_animal
where age not in (select top 3 age from t_animal order by age DESC)
order by age DESC
添加表中元素
alter table T_employee add Company varchar(20),appartment varchar(20)
删除表中元素
alter table T_employee drop COLUMN Company1
删除数据
delete from T_employee where age>20
插入数据
insert into T_employee(id,name,age,company,appartment)
values(newid(),N'予以',30,'天地通','技术部')
insert into T_employee(id,name,age,company,appartment)
values(newid(),N'朱莉',22,'360','外交部')
insert into T_employee(id,name,age,company,appartment)
values(newid(),N'土豆',28,'qq','人事部')
用distinct来合并重复的项目
select distinct company,appartment from T_employee
用union来联合查询数据
和并重复行如下
select age, name from T_employee
union
select age, name from T_animal
当只用union时查出的事没有重复数据的数据
当加个all后就不会比较,直接输出全部结果
select age, name from T_employee
union all
select age, name from T_animal
注意:在使用union时,select 的项目的属性要相容
即第一个select的事int 的二个select的也要是int型相近数数字数据
select appartment,age from T_employee
union
select '动物无部门',age from T_animal
在没有特殊情况下不要丢掉all,丢掉all会让服务器压力更大
select '人最大年龄',max(age) from T_employee
union all
select '人最小年龄',min(age) from T_employee
union all
select '动物最大年龄',max(age) from T_animal
union all
select '动物最小年龄',min(age) from T_animal
alter table T_employee add salary int
update T_employee set salary = 2500
where age <20
select name,salary from T_employee
union all
select '工资合计',sum(salary) from T_employee
substring和c#中的一样用
select substring('你是一个傻瓜!',2,3)
在一个时间上加减多少时间
select dateadd(day,3,getdate())
计算两个时间的差值
select datediff(hh,getdate(),dateadd(day,3,getdate()))
update T_employee set intime = dateadd(yy,-3,getdate())
where not age = 20
取出一个时间的某一个部分
select datepart(yy,intime) as '入职年份',count(*) as '数量'
from T_employee
group by datepart(yy,intime)
数据类型转换
有cast和convert两种用法
select cast('2008-08-08'as datetime),cast('123'as int)+2,
convert(datetime,'2008-8-8'),convert(varchar(max),123456)
isnull函数
填充没有赋值的数据
select IsNull(name,'无名')as '姓名' from T_employee
case语句
select name,
(
case gental
when 1 then '男'
when 0 then '女'
else '未知'
end
)as '性别'
from t_person
case还能实现离散值的判断
此时case后面不能有参数!!!!
select name,
(
case
when salary < 2000 then '低收入'
when salary between 2000 and 2500 then '中等收入'
else '高收入'
end
)as '收入水平'
from t_employee
测试:计算出胜败数
create table T_basket(tname nvarchar(20) not null,score nvarchar(5) not null)
insert into T_basket(tname,score) values( N'火箭',N'胜')
select tname,
sum(
case score
when N'胜' then 1
else 0
end
)as N'胜',
sum(
case score
when N'败' then 1
else 0
end
)as N'败'
from T_basket
group by tname