java 从零开始,学习笔记之基础入门<SQL_Server>(二十一)(二)

2014-11-24 08:29:15 · 作者: · 浏览: 1
id asc;
--模糊查询 like %(匹配所有) _(占有符号)
--查询班级名为java开头的所有班级
select *from tb_student where sclasslike 'java%';
--查询以命名的班级
select *from tb_student where sclasslike '%1002';
select *from tb_student where sclasslike '____1002';
select *from tb_student where sclasslike 'net____';
--更新 update表名set 字段名= '修改之后的值',字段名='修改之后的值' where 条件
--将id为的那条记录的sclass改为net1205
update tb_student set sclass = 'net1205' where sid = 5;
--一次将一条记录中的所有的字段都改主键的值可不可以改?
update tb_student set sname = 'IBM',sbirth= getdate(),sclass='aaaa'where sid =1;
--删除delete from表名where 条件表示删除表中符合条件的记录
--删除sid=8的那条记录
delete from tb_studentwhere sid=8;
--删除多条删除id>5的记录
delete from tb_studentwhere sid>=3;
--清空表中的数据表还存在知识数据没了
truncate table tb_student;
--删除一张表,表中的数据和表在数据库中都删除
drop table tb_student;
select *from tb_student;
SQL Server的常用函数
索引
--创建一张表tb_student(sid name,score,sclass,startschool);
create table tb_student(
sid int identity(1,1),
sname varchar(32)not null,
score int default 0,
sclass varchar(32),
startschool datetime,
primary key(sid),
);
--往其中插入条记录
insert into tb_student(sname,score,sclass,startschool)
values('es',80,'天灾',getdate());
insert into tb_student(sname,score,sclass,startschool)
values('bs',90,'天灾',getdate());
insert into tb_student(sname,score,sclass,startschool)
values('cs',30,'近卫',getdate());
insert into tb_student(sname,score,sclass,startschool)
values('as',40,'近卫',getdate());
--聚合函数
--count 用来取出表中有多少条记录
select count(*)from tb_student;
select count(*) as'数据条数'from tb_student;
select count(1) from;
--sum求和函数对某一列的值进行求和
select sum(score)as '总成绩'from tb_student;
--avg求平均数的函数,对某一列的值进行求平均数
select avg(score) as '平均成绩'from tb_student;
--max求最大值对某一列的值进行求最大值运算
select max(score)as '最高分'from tb_student;
--min 求最小值对某一列的值进行求最小值运算
select min(score)as '最低分'from tb_student;
--日期和时间函数
--取到系统时间
select getdate() as'时间';
--取到系统时间的部分事件
select DATEPART(yyyy,getdate())as '年份';
select DATEPART(MM,getdate())as '月份';
select DATEPART(hh,getdate())as '小时';
select DATEPART(minute,getdate())as '分钟';
--用来改变取到的系统的时间
select DATEADD(yyyy,-20,getdate())as '20年前';
select DATEADD(dd,-1,getdate())as '昨天';
--DATEDIFF
select DATEDIFF(dd,'2012-1-28','2012-2-28');
--求你出生到现在有多少天
select DATEDIFF(dd,'2000-2-1','2012-2-28')as '你出生的天数';
--字符函数
--CHAR 返回对应的字符的ASSIC码值
select char(68);
--将ASCII装换成数字
select ASCII('A');
--LEET('',N);
select LEFT('IBM',3)as '333';
select RIGHT('IBM',3)
--返回指定字符串的长度(字符串尾随空格不计算)
select len('IBM');
select len(' i b m ');
--字符串的截取(表示从第三个位置开始截取两个长度的字符串,包括开始位置)
select substring('IBM',3,2);
--索引的用处:
--索引的创建一般是用来提高数据的查询效率,
--设置数据的合法性
--唯一索引:读表中的某一个或者多个字段设置成唯一索引,那么设置的字段不能有重复的数据
--创建唯一索引的语法格式:
--create unique index 唯一索引名on
-- 表名(字段,字段);
--在表的sname列上建立唯一索引
create uniqueindex uname on tb_student(sname);
--如果想一次建立多个列的唯一索引则只需要在sname后面添加即可
create uniqueindex uname on tb_student(sname,sclass);
--建立唯一索引之后无法插入有相同值的列索引名不能重复
insert into tb_student(sname,score,sclass,starts