sql sever 2005教程.doc_第1页
sql sever 2005教程.doc_第2页
sql sever 2005教程.doc_第3页
sql sever 2005教程.doc_第4页
sql sever 2005教程.doc_第5页
已阅读5页,还剩36页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1.数据库的连接?数据库包括两部分:一是库本身(后台数据库),二十软件(客户端软件)。可以访问其他机器的库。访问的方式是通过命令,发送到本地或其他机器的数据库。2编程语言为啥还需要数据库?对内存的数据操作时编程语言的强项,而对硬盘数据啊操作确实编程语言的强项。是数据库研究的核心问题。如果对数据的操作很复杂,就把数据调入内存,用高级语言实现,然后再写入数据库。3.初学者学习数据库一:数据库是如何存储数据的?防止冗余的办法(身份证号)编号字段 ,记录,表 ,约束(主键,外键,唯一键,非空,check default)主键:辨别不能事物;相当于与编号(ID)个人所在的部门信息也属于个人信息。1.表的相关数据字段: 一个事物某一个静态的特征记录:字段的组合,表示的是一个具体的事物表 :整体表(记录的组合)表示饿时同一类型事物的集合表和字段,记录的关系字段是事物的是属性记录是食物本身表是食物的集合列字段的另一种称谓属性字段的另一种称谓元组 :一行记录的另一种称谓2.creat table 命令通过图形化界面建表通过命令create tableCreate table emp(-)用sql命令创建表和约束create table dept /创建表 dept(dept_id int primary key, /设置主键dept_name nvarchar(100) not null,dept_address nvarchar(100)create table emp /创建表emp( -不能写成emp_id int constraint pk_emp_id_hahaha primary key,/创建约束emp_name nvarchar(20) not null,emp_sex nchar(1) ,dept_id int constraint fk_dept_id_heihei foreign key references dept(dept_id),/创建主键,副键的连接 constraint:约束 references 照会对象) create table ttt(i int,j int)create table student2(stu_id int primary key,stu_sal int check (stu_sal=1000 and stu_sal=1000 and stu_sal=1000 and stu_sal=1500 and sal=3000等价于select * from emp where sal between 1500 and 3000-查找工资在小于1500或大于3000之间的所有的员工的信息select * from emp where sal3000等价于select * from emp where sal not between 1500 and 30005. in【属于某个孤立的值】分页查询后面select * from emp where sal in (1500, 3000, 5000)等价于select * from emp where sal=1500 or sal=3000 or sal=5000select * from emp where sal not in (1500, 3000, 5000) -把sal既不是1500 也不是3000也不是5000的记录输出等价于select * from emp where sal1500 and sal3000 and sal5000-数据库中不等于有两种表示: != 推荐使用第二种-对或取反是并且 对并且取反是或6. top【最前面的若干记录】select * from emp;select top 5 * from emp;select top 15 percent * from emp; -输出的是3个,不是2个15 percent=14*15%=2.1=3-把工资在1500到3000之间(包括1500和3000)的员工中工资最高的前4个人的信息输出select top 4 * from empwhere sal between 1500 and 3000order by sal desc -desc降序 不写则默认是升序(=descend)7. null【没有值,空值】零和null是不一样的,null表示空值,没有值,零表示一个确定的值select * from emp;-输出奖金非空的员工的信息select * from emp where comm null; -输出为空 errorselect * from emp where comm != null; -输出为空 errorselect * from emp where comm = null;-输出为空 error-总结:null不能参与如下运算: != = -null可以参与如下运算:is not isselect * from emp where comm is null; -输出奖金为空的员工的信息select * from emp where comm is not null; -输出奖金不为空的员工的信息-任何类型的数据都允许为null任何数字与null参与数学运算的结果永远是null。create table t1 (name nvarchar(20), cnt int, riqi datetime); insert into t1 values (null, null, null)select * from t1;select * from emp;-输出每个员工的姓名 年薪(包含了奖金) comm假设是一年的奖金select empno, ename, sal*12+comm 年薪 from emp;-本程序证明了:null不能参与任何数据运算 否则结果永远为空select top 5 *from emp;-rightselect top 5 from emp;-error例子:select top 5 from emp;select * from emp where comm is not nullselect ename, sal*12+comm from emp;-输出图一select ename, sal*12+isnull(comm, 0) 年薪 from emp;-输出图二,排除那些没有comm为null的ename,防止结果不全。-isnull(comm, 0) 如果comm是null 就返回零 否则返回comm的值 -isnull(comm, 0)是函数,如果为null 则返回0。输出结果:图1 图28. order by【以某个字段排序】order by a, b -a和b都是升序order by a, b desc -a,升序b降序 order by a desc, b -a,降序b升序order by a desc ,b desc -a和b 都是降序。文字描述;如果不指定排序的标准,则默认是声讯,升序用asc表示,默认可以不写为每一个字段制定排序标准并不会对另一个最短产生影响强烈建议为每一个字段都是制定排序的标准。-asc是升序的意思默认可以不写 desc是降序select * from emp order by sal; -默认是按照升序排序select * from emp order by deptno, sal; -先按照deptno升序排序,如果deptno相同,再按照sal升序排序select * from emp order by deptno desc, sal;-先按deptno降序排序如果deptno相同再按照sal升序排序-记住sal是升序不是降序-order by a desc, b, c, d desc只对a产生影响,不会对后面的b c d 产生影响select * from emp order by deptno, sal desc-问题:desc是否会对deptno产生影响?-答案:不会-先按deptno升序,如果deptno相同,再按sal降序9. 模糊查询【搜索时经常用到】格式:select 字段的集合from 表名where 某个字段的名字like匹配的条件匹配的条件通常含有通配符通配符表示任意0个或多个字符%_这是下划线不是减号表示任意单个字符单引号表示字符串。双引号表示对象的名。【a-f】s到f中的任意单个字符 ,只能是a b c d e f 中的任意一个字符【a,f 】 a或者f【a-c】不是a 也不是b 也不是c的人以单个字符注意:匹配的条件必须的用单引号括起来不能省略也不能改用双引号通配符作为select * from emp where ename like %A% -ename只要含有字母A就输出select * from emp where ename like A% -ename只要首字母是A的就输出select * from emp where ename like %A -ename只要尾字母是A的就输出select * from emp where ename like _A% -ename只要第二个字母是A的就输出select * from emp where ename like _A-F% -把ename中第二个字符是A或B或C或D或E或F的记录输出select * from emp where ename like _A-F% -把ename中第二个字符不是A也不是B也不是C也不是D也不是E也不是F的记录输出select * from emp where ename like _A-F% -把ename中第二个字符不是A也不是B也不是C也不是D也不是E也不是F的记录输出create table student(name varchar(20) null,age int);insert into student values (张三, 88);insert into student values (Tom, 66);insert into student values (a_b, 22);insert into student values (c%d, 44);insert into student values (abc_fe, 56);insert into student values (haobin, 25);insert into student values (HaoBin, 88);insert into student values (c%, 66)insert into student values (longs, 100)select * from student;select * from student where name like % escape -把name中包含有%的输出select * from student where name like %_% escape -把name中包含有_的输出10.聚合函数【多行记录返回至一个值通常用于统计分组的信息】函数的分类max() ,min(), avg() ,count() 求个数 Count(*) 返回表中所有的记录的个数Count(字段名) 返回字段值非空的记录的个数,重复的记录也会被当做有效的记录Count(distinct 字段名) 返回字段不重复并且非空的记录个数注意的问题select max(sal),min (sal),,count(*)from empselect max(sal)“最高工资“,min(sal)”最低工资”,count(*)员工人数 fromemp;单行函数:每一行返回一个值多行函数:多行返回一个值聚合函数是多行函数例子:select lower(ename) from emp; - 最终返回的是行lower()是单行函数select max(sal) from emp; -返回行max()是多行函数select * from emp;select count(*) from emp; -返回emp表所有记录的个数select count(deptno) from emp; -返回值是这说明deptno重复的记录也被当做有效的记录select count(distinct deptno) from emp; -返回值是 统计deptno不重复的记录的个数select count(comm) from emp; -返回值是这说明comm为null的记录不会被当做有效的记录select max(sal) 最高工资, min(sal) 最低工资, count(*) 员工人数 from emp; -ok select max(sal), lower(ename) from emp;注意单行函数和多行函数不用混用10. group by【分组】格式:group by 字段的集合功能:把表中的记录按照字段分成不同的组例子查询 不同部门的平均工资select deptno, avg(sal) as 部门平均工资 from emp 注意:理解group by a,b c,的用法先按a分组,如果相同,再按分组,如果相同,在按分组。最终统计的是最小分组的信息use scott;/使用Scott select * from emp;-输出每个部门的编号和该部门的平均工资select deptno, avg(sal) as 部门平均工资 from emp group by deptno - 判断下面语句是否正确(错)select deptno, avg(sal) as 部门平均工资, enamefrom empgroup by deptno - 判断下面语句是否正确(错)select deptno, enamefrom empgroup by deptno 总结:使用了group by 之后select 中只能出现分组后的整体信息,不能出现组内的详细信息group by a, b 的用法-errorselect deptno , job, salfrom empgroup by deptno, job-errorselect *from empgroup by deptno, job-okselect deptno , job, avg(sal) from empgroup by deptno, job-okselect deptno , job, avg(sal) 平均工资, count(*) 部门人数, sum(sal) 部门的总工资, min(sal) 部门最低工资 from empgroup by deptno, joborder by deptnoselect comm, count(*) from empgroup by commselect max(sal) from emp; -默认把所有的信息当做一组11. having【对分组之后的信息进行过滤】1.having子句是用来对分组之后的数据进行过滤因此使用having时通常都会先使用group by2.如果没使用group by 但使用了having,则意味着having把所有的记录当做一组来过滤select count(*) from emp having avg(sal)10003. having子句出现的字段必须是分组之后的整体信息having 子句不允许出现组内的详细信息4.尽管select 字段中可以出现别名但是having子句中不能出现字段的别名,只能使用字段最原始的名字原因不得而知5having和where的异同相同点:都是对数据过滤,只保存有效的数据where和having一样,都不允许出现字段的别名,只允许出现最原始的字段名select ename sal “工资”from empwhere sal 2000 -where “工资”2000。(错误)不同点:where是对原始的记录过滤having是对分组之后的记录过滤where必须出现having的前面,顺序不可颠倒,否则运行出错-输出部门平均工资大于的部门的部门编号部门的平均工资select deptno, avg(sal)from empgroup by deptnohaving avg(sal) 2000-判断下列sql语句是否正确(正确)select deptno, avg(sal) as 平均工资from empgroup by deptnohaving avg(sal) 2000-判断下列sql语句是否正确(错误)select deptno, avg(sal) as 平均工资from empgroup by deptnohaving 平均工资 2000-判断下列sql语句是否正确select deptno 部门编号, avg(sal) as 平均工资from empgroup by deptnohaving deptno 1-判断下列sql语句是否正确select deptno 部门编号, avg(sal) as 平均工资from empgroup by deptnohaving 部门编号 1-判断下列sql语句是否正确select deptno, avg(sal) as 平均工资from empgroup by deptnohaving deptno 10-判断下列sql语句是否正确select deptno, avg(sal) as 平均工资from empgroup by deptnohaving count(*) 3-判断下列sql语句是否正确select deptno, avg(sal) as 平均工资from empgroup by deptnohaving ename like %A%-把姓名不包含A的所有的员工按部门编号分组,-统计输出部门平均工资大于的部门的部门编号部门的平均工资select deptno, avg(sal) 平均工资from empwhere ename not like %A%group by deptnohaving avg(sal) 2000-把工资大于,-统计输出部门平均工资大于的部门的部门编号部门的平均工资select deptno, avg(sal) 平均工资, count(*) 部门人数, max(sal) 部门的最高工资from empwhere sal 2000 -where是对原始的记录过滤group by deptnohaving avg(sal) 3000 -对分组之后的记录过滤-判断入选语句是否正确select deptno, avg(sal) 平均工资, count(*) 部门人数, max(sal) 部门的最高工资from empgroup by deptnohaving avg(sal) 3000 -对分组之后的记录过滤where sal 2000 -where是对原始的记录过滤总计:所有select的参数的顺序是不允许变化的,否则编译时出错select * from emp;select count(*) from emp having avg(sal) 1000select ename, sal 工资 from empwhere sal 2000select ename, sal 工资 from empwhere 工资 2000select deptno, avg(sal) 平均工资, count(*) 部门人数, max(sal) 部门的最高工资into emp_2from empwhere sal 2000 -where是对原始的记录过滤group by deptnohaving avg(sal) 3000 -对分组之后的记录过滤select * from emp_2-输出部门编号部门的平均工资select deptno, avg(sal) 平均工资from empgroup by deptno-errorselect *from empgroup by deptno-error 因为job是组内详细信息select deptno, avg(sal) 平均工资, jobfrom empgroup by deptno-okselect deptno, avg(sal) 平均工资, count(*), sum(sal)from empgroup by deptno-okselect deptno, avg(sal) 平均工资, count(*) 部门人数, sum(sal) 部门工资总和from empgroup by deptnohaving sum(sal) 9000-error where子句不应该出现聚合函数select deptno, avg(sal) 平均工资, count(*) 部门人数, sum(sal) 部门工资总和from empwhere avg(sal) 2000 -error 因为where是对原始的数据过滤不能使用聚合函数因为还没有分组group by deptnohaving sum(sal) 5000select deptno, job, count(*)from empwhere sal 1000group by deptno, jobhaving count(*) 1order by deptno desc group byhaving12. 连接查询定义:将两个表或者两个以上的表以一定的连接条件连接起来从中检索出满足条件的数据分类【重点的重点】 内连接(重点)1,select .from A,B 的用法emp是14行,8列, dept 是5行,3列输出结果是:行数=14*5.列数=8+32,select 。from A,B where .的用法对select。.from A ,B 产生的笛卡尔积用where中的条件进行过滤例子- 输出5行,11列select * from emp, dept where empno = 73693,select from A join B on 的用法select E.ename 员工姓名, D.dname 部门名称from emp Ejoin dept D -join是连接on E.deptno = D.deptno4,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论