又到了总结知识的时候了,今天主要把SQL数据库给简单的学完了,明天开始就要开始学ADO.NET的知识了。好了,话不多说,还是看一下今天都学了哪些内容。
1 字符串类型的知识点
--类型的使用 --截取字符串 select 姓名,substring(姓名,1,1) as 姓氏,substring(姓名,2,2)as 名字 from 职工 --substring(express,start,length) --express:字符串 start:整数 制定字符串分割的开始位置 length: 分割的长度 --大小写的转换 select 仓库号,UPPER(仓库号) as 仓库号大写,面积 from 仓库 select 仓库号, lower(仓库号) as 仓库号小写,面积 from 仓库 --转换 select 仓库号,CONVERT (char(10),创建时间,111) as 创建时间 from 仓库 --将datatime转换为字符串。 --111的显示格式:mm/dd/yy
结果是:
补充:
2.聚合函数以及分组
3 查询语句
(1)简单查询
--查询 select 城市,面积 from dbo.仓库 --查询仓库表中的城市,面积 select 仓库Id,仓库号,城市,面积,创建时间 from dbo.仓库 --查询仓库表中的所有列 select * from dbo.仓库 --查询仓库表中的所有列(这里不建议用*,因为会影响计算机的性能) select distinct(城市) from 仓库 --去除重复的列值 select 姓名,性别,工资,涨后工资=工资*1.1 from 职工 --计算添加的列 select 姓名 as name from 职工 --将姓名列名改为name(这里只是显示出来name,在表结构中是未改的) select 姓名,性别,工资 from 职工 where 姓名='吴平安' -- 查找姓名为吴平安的信息 select 姓名,性别,工资 from 职工 where 姓名!='吴平安' --查找姓名不为吴平安的信息(这里不等于“!=” or “<>”) select * from dbo.职工 where 工资>1500 and 性别='男' --查找工资大于1500并且性别为男的信息 select * from 仓库 where not(城市='上海' or 城市='济南') --查找城市不是上海或济南的仓库信息 select * from 职工 where 工资 not between 1200 and 1800 --查找不在范围内的信息 select * from dbo.仓库 where 城市 is null --查找城市为空的仓库信息 --模糊查询 --“%:可以是多个字符” --“_:只能是一个字符” select * from 职工 where 姓名 like '%平%' and 工资 between 1000 and 2000 select * from 职工 where 姓名 like '_平_' --排序 select * from 职工 order by 工资 ASC,仓库号 DESC select * from 职工 order by 工资 desc select * from 职工 order by NEWID() --随即排序 select top 10 percent * from 职工 --查找记录的前10%
(1)表查询和插入、修改、删除记录
--连接查询 select 姓名,职工号,工资,城市 from 职工,仓库 --笛卡尔积 这种查询很少用 --多表连接 select 姓名,工资,城市 from 职工,仓库 where 职工.仓库号=仓库.仓库号 --内连接(inner join) select 姓名,城市 from 职工 inner join 仓库 on 职工.仓库号=仓库.仓库号 --左连接(left join) select 姓名,城市 from 职工 left join 仓库 on 职工.仓库号=仓库.仓库号 --只显示左表(职工)的数据和仓库表中仓库号相对应的数据 --右连接(right join) select 姓名,城市 from 职工 right join 仓库 on 职工.仓库号=仓库.仓库号 --只显示右表(仓库)的数据和职工表中仓库号相对应的数据 --全连接(full join) select 姓名,城市 from 职工 right join 仓库 on 职工.仓库号=仓库.仓库号 --两个表中的数据都会显示 --插入记录 insert into 职工(职工号,姓名,性别,工资) values('zg20','cindy','女','2000') --插入一条记录 insert into 职工(职工号,姓名,性别,工资) values('zg21','莱恩','男','2050'), --插入多条记录 ('zg22','阿萨德','男','2150'), ('zg23','张三','男','1909') --修改记录 update 仓库 set 面积=888,创建时间='2014-12-09' where 仓库号='wh2' --删除记录 -->删除信息 delete 仓库 where 仓库号 in('wh1','wh2') --删除多条记录 drop table 仓库 --删除仓库表 truncate table 仓库 --清空仓库表中的数据
补充:delete 表名 where 条件
drop table 表名
truncate table 表名
区别: