网站导航网学 原创论文 原创专题 网站设计 最新系统 原创论文 论文降重 发表论文 论文发表 UI设计定制 论文答辩PPT格式排版 期刊发表 论文专题
返回网学首页
网学原创论文
最新论文 推荐专题 热门论文 论文专题
当前位置: 网学 > 交易代码 > SQL语法 > 正文

SQL_server数据库学习记录

论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务

【网学网提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了SQL_server数据库学习记录的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!


    createtablestudent(
    --创建student表
    stu_idintprimarykeyidentity(100,5),,--创建一个字段,且被赋予主键--identity主键的值自动增长,括号内可省略,表示从100开始,以5为单位增长stu_salintcheck(stu_sal>=1000andstu_sal<=8000),--check约束stu_sal值在K与K之间stu_sexnchar(1)default('男')--()可以省,在数据库中单引号表示字符串,双引号表示一个事物的名字stu_namenvarchar(200)unique--unique指明stu_name的值是唯一的,不能重复,--唯一键与主键的区别:可以为空)insertintostudent(stu_id,stu_sal)values(1,1000);--插入值,1000分别插入在了stu_id,stu_salinsertintostudentvalues(2,1050,'女','黎明');insertintostudentvalues(3,1050,'女','黎');createtablebanzhu(banzhu_idintconstraintheiheiprimarykey,--constraint指出约束的名字是“heihei”banzhu_namenvarchar(20)notnull,--定义banzhu_namenvarchar型,非空。banzhu_sexnchar(1),zhanwuzu_id)用一个编号当主键,即用代理主键当主键,不要用业务逻辑做主键,业务逻辑用唯一键。intconstrainthahahaforeignkeyreferencesstudent(stu_id)--外键关联到student表的stu_id
     表的多对多关系事例:表的多对多关系事例--班级表createtablebanji(banji_idintprimarykey,banji_numintnotnull,banji_namenvarchar(100))--教师表createtableteacher(teacher_idintprimarykey,teacher_namenvarchar(200))--关系表用来模拟班级和教师的关系createtablebanji_teacher_mapping(banji_idintconstraintfk_banji_idforeignkeyreferencesbanji(banji_id),teacher_idintforeignkeyreferencesteacher(teacher_id),kechengnvarchar(20),constraintpk_banji_id_jiaoshi_idprimarykey(banji_id,teacher_id,kecheng))insertintobanjivalues(1,20,'JAVA');insertintobanjivalues(2,40,'C++');insertintobanjivalues(3,30,'C');insertintobanjivalues(4,50,'PHP');insertintoteachervalues(1001,'AA');insertintoteachervalues(1002,'BB');insertintoteachervalues(1003,'CC');insertintoteachervalues(1004,'DD');insertintobanji_teacher_mappingvalues(2,1002,'JAVA1');insertintobanji_teacher_mappingvalues(1,1001,'JAVA222');insertintobanji_teacher_mappingvalues(1,1003,'JAVA333');
     数据查询--计算列select*fromemp;--*表示所有的selectempno,enamefromemp;selectename,sal*12as"年薪"fromemp;--as可以省略,注意年薪上是双引号selectename,sal*12as"年薪",sal"月薪",jobfromemp;--查询薪水*12作为年薪输出查询sal作为月薪输出select23222fromemp;--此行无实际意义,输出结果行数是emp的行数,值全为23222--distinct(实际意义“清楚地”,数据库中可理解为"不允许重复的")selectdistinctdeptnofromemp;--distinct过滤掉重复的数据selectdistinctcommfromemp;--可以过滤重复的NULLselectdistinctcomm,deptnofromemp;--过滤comm,depno整体重复
    的数据--between--查询sal为的结果select*fromempwheresal=5000--查询工资大于并且小于的值,以下两个命令等价select*fromempwheresal>='1500'andsal<='3000'select*fromempwheresalbetween1500and3000--查询工资小于或者大于的值,以下两个命令等价select*fromempwheresal<=1500orsal>=3000select*fromempwheresalnotbetween1500and3000--in(属于若干个孤立的值)select*fromempwheresalin(1500,3000,5000)select*fromempwheresal=1500orsal=3000orsal=5000select*fromempwheresal<>1500andsal!=3000andsal!=5000--数据库中不等于有两种:!=;<>;--top(最前面的,不是最大的)selecttop2*fromemp;selecttop2salfromemp;
     --把工资在与之间的最高四位的工资输出selecttop1*fromempwheresal>=1500andsal<=3000orderbysaldesc--orderby排序;desc降序,不写则默认升序--NULL的运算--输出将近非空的员工的信息select*fromempwherecomm<>null;--error,因为NULL不能参与逻辑运算,能参与is,isnot运算select*fromempwherecommisnotnull;selectename,sal*12+commas'年薪'fromemp;--凡comm为NULL的,年薪都将显示为NULL,证明NULL不能参与任何数学运算selectename,sal*12+isnull(comm,0)fromemp;--1(comm,0)如果comm为NULL,返回,否则返回NULL的值--任何数据类型都允许为NULL,与NULL是不一样的
    --模糊查询select*fromempwhereenamelike'%a%'--%表示任意或者多个字符--上句表示为“ename中只要有a就输出”select*fromempwhereenamelike'a%'--此语句则输出字母开头的值select*fromempwhereenamelike'_a%'--_下划线表示任意单个字符--上句表示为第二个字母为a的值select*fromempwhereenamelike'_[a-f]%'--[a-f]表示在a-f之间的字符,含a-f--上句表示为第二个字母为A-F之间的值select*fromempwhereenamelike'_[a,c]%'--[a,c]表示值为a或c的字符--上举表示为输出第二个字符值为a或者c的值select*fromempwhereenamelike'_[^a,c]%'--[^]表示取反,此句为不是a或c的任意单个字符--上句表示为第二个字符不是a或c的值--关于字符串中含有通配符(_%)的查询select*fromempwhereenamelike'%\%%'escape'\'--把ename中含有%的值输出usescott;--从一个数据库切换到另外一个数据库
     函数--聚合函数max();min();avg();count();--单行函数:每一行返回一个值--多行函数:多行返回一个值;聚合函数是多行函数--单行函数与多行函数不能混用selectlower(ename)fromemp;--此为单行函数;lower转化为小写selectmax(ename)fromemp;--此为多行函数;max求最大值selectcount(*)fromemp;--统计emp表记录个数selectcount(distinctdeptno)fromemp;--统计不重复的drpno个数selectcount(comm)fromemp;--统计非空的comm个数--count返回表中某一字段的非空的个数,每一个重复的记录都会统计为有
    效值分组--输出每个部门的编号和该部门的平均工资selectdeptnoas"部门名称",avg(sal)as"部门平均工资"fromempgroupbydeptno--按照部门编号分组
    selectdeptno,job,avg(sal)"平均工资",count(*)fromempgroupbydeptno,joborderbydeptnoselectcomm,avg(sal)fromempgroupbycomm--空值会被当做单独一组--按照部门编号、职业分组
     having--输出平均工资大于的部门的"部门编号、部门平均工资"selectdeptnoas"部门名称",avg(sal)as"平均工资"fromempgroupbydeptnohavingavg(sal)>2000--输出部门编号大于的"部门编号、部门平均工资"selectdeptnoas"部门名称",avg(sal)as"平均工资"fromempgroupbydeptnohavingdeptno>10结合使用having与where--把中不包含A的所有员工按照部门编号分组--输出平均工资大于的部门的"部门编号、部门平均工资"selectdeptnoas"部门名称",avg(sal)as"平均工资"fromempwhereenamenotlike'%[a]%'groupbydeptnohavingavg(sal)>2000--把工资大于,部门平均工资大于的”部门编号,平均工资“输出selectdeptnoas"部门编号",avg(sal)as"平均工资"intoemp_2fromempwheresal>2000groupbydeptnohavingavg(sal)>3000总结:where与having的异同where和having都是对数据进行过滤where是对原始数据进行过滤,having是对分组后的数据进行过滤where要用在having之前,顺序不能颠倒,否则出错--新建一个表并将查询的结果放入这个表中
     内连接查询顺序selecttop...fromAjoinBon...joinCon...where...groupby...having...orderby...select*fromemp,dept--产生的结果是把两个表行数相乘,列数相加。即笛卡尔积--即把A表的每一条记录和B表的每一个记录组合输出行列select*fromemp,deptwhereempno=7369--产生的笛卡尔积用where过滤,输出行列selectD.ename"员工",E.deptno"部门名称"fromemp"D"joindept"E"onD.deptno=E.deptno--join表示连接--on表示连接条件,有join必须有on
    --emp"D"是给emp一个别名,如果赋予了别名,别的emp一定要用别名替代select*fromemp,deptwhereemp.deptno=dept.deptno--此例与上例输出结果相同,但推荐使用上例方法
     内连接习题--把工资大于的员工的、部门名称、工资等级输出select"E".ename,"F".grade,"D".dname,"E".salfromemp"E"joindept"D"on"E".deptno="D".deptnojoinsalgrade"F"on"E".SAL>="F".losalAND"E".SAL<="F".hisalwhere"E".sal>2000--输出工资最高的前三名的每个人的、工资、部门名称、工资等级selecttop3"E".ename,"S".grade,"D".dname,"E".salfromemp"E"joindept"D"one.deptno="D".deptnojoinsalgrade"S"on"E".SALbetween"S".losalAND"S".hisalorderby"E".saldesc--输出工资最高的,中不包含‘I’的前三名的每个人的、工资、部门名称、工资等级selecttop3
    "E".ename,"S".grade,"D".dname,"E".salfromemp"E"joindept"D"one.deptno="D".deptnojoinsalgrade"S"on"E".SALbetween"S".losalAND"S".hisalwhere"E".enamenotlike'%i%'orderby"E".saldesc--求出每个员工的、部门名称、薪水、薪水等级select"E".ename,"D".dname,"e".sal,"S".gradefromemp"E"joindept"D"on"E".deptno="D".deptnojoinsalgrade"S"on"E".salbetween"S".losaland"S".hisal
     --查找每个部门的编号、部门名称、该部门所有员工的平均工资、平均工资的等级select"D".dname,"T".deptno,"T".avgsal,"S".gradefrom(selectdeptno,avg(sal)"avgsal"fromempgroupbydeptno)"T"joindept"D"on"T".deptno="D".deptnojoinsalgrade"S"on"T".avgsalbetween"S".losaland"S".hisal--求出emp表中所有领导的信息select*fromempwhereempnoin(selectmgrfromemp)--求出薪水最高的部门的编号和部门的平均工资selecttop1deptno,avg(sal)"部门平均工资"fromempgroupbydeptnoorderbyavg(sal)desc--把工资大于的所有员工按照部门分组--把部门平均工资大于的最高的前两个部门的信息输出select*fromdept"D"join(selecttop2"E".deptno,avg(sal)"avg(sal)"fromemp"E"where"E".sal>1500groupby"E".deptnohavingavg("E".sal)>2000orderbyavg("E".sal)desc)"T"on"D".deptno="T".deptnojoinsalgrade"S"on"T"."avg(sal)"between"S".losaland"S".hisal
     外连接、完全连接、交叉连接、自连接、外连接、完全连接、交叉连接、自连接、联合--外连接,leftjoin或者rightjoin--左表的每一项与右表一一匹配,无匹配右表则返回空值select*fromdeptleftjoinemponemp.deptno=dept.deptno--完全连接select*fromdeptfulljoinemponemp.deptno=dept.deptno--结果包含三部分的内容:1、两个表中匹配的所有行记录--2、左表中那些在右表中找不到匹配的行的记录,这些记录右边全为NULL--3、右表中那些在左表中找不到匹配的行的记录,这些记录左边全为NULL--交叉连接select*fromdeptcrossjoinemp--产生笛卡尔积--自连接:自己和自己连接起来查询数据--不准使用聚合函数,查询薪水最高的员工的信息select*fromempwhereempnonotin(selectdistinct"E1".empnofromemp"E1"joinemp"E2"on"E1".sal>"E2".sal)--联合:表和表之间的数据以纵向的方式连接在一起--之前讲过的连接都是以横向的方式连接在一起--输出每个员工的、工资、上司的select"E1".ename,"E1".sal,"E2".enamefromemp"E1"joinemp"E2"on"E1".mgr="E2".empnounionselectename,sal,'主席'fromempwheremgrisnull
     分页查询--输出工资最高的低的~6名员工的信息selecttop3*fromempwheresalnotin(selecttop3salfromemporderbysalasc)orderbysalasc--总结:假设显示N条记录,当前要显示的是M页,表名为A,主键为A_IDselecttopn*fromAwhereA_IDnoti
    n(selecttop(m-1)*nA_IDfromemp)
    --视图视图createviewv SQL_server数据库学习记录_网学
当前位置: 网学 > 交易代码 > SQL语法 > 正文

SQL_server数据库学习记录

论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务
content
  • 上一篇资讯: SQL_公司语法
  • 版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师
    emp1asselectdeptno,avg(sal)"avg_sal"fromempgroupbydeptnoselect*fromv SQL_server数据库学习记录_网学
    当前位置: 网学 > 交易代码 > SQL语法 > 正文

    SQL_server数据库学习记录

    论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务
    content
  • 上一篇资讯: SQL_公司语法
  • 版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师
    emp1whereavg_sal=(selectmax(avg_sal)fromv SQL_server数据库学习记录_网学
    当前位置: 网学 > 交易代码 > SQL语法 > 正文

    SQL_server数据库学习记录

    论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务
    content
  • 上一篇资讯: SQL_公司语法
  • 版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师
    emp1)--简化查询,避免书写大量重复的语句
        
        
  • 上一篇资讯: SQL_公司语法
  • 下一篇资讯: SQL_Server口令的脆弱性资料
  • 相关资讯

    网学推荐

    原创论文

    文章排行榜

    设为首页 | 加入收藏 | 网学首页 | 原创论文 | 计算机原创
    版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师