zoukankan      html  css  js  c++  java
  • MySQL 复习笔记

    本文内容

    • SQL 语句
    • 创建数据库
    • 创建数据表
    • 数据完整性约束
    • 四种基本字符类型说明
    • SQL 基本语句
    • 类型转换函数
    • 日期函数
    • 数学函数
    • 字符串函数
    • 合并结果集 union
    • CASE 函数用法
    • IF ELSE 语法
    • WHILE 循环语法
    • 子查询
    • 表连接 join
    • 事务
    • 视图
    • 触发器
    • 存储过程
    • 分页存储过程
    • 索引
    • 临时表

    1,SQL 语句


    SQL 语言:结构化的查询语言(Structured Query Language),是关系数据库管理系统的标准语言。是一种解释语言,写一句执行一句,不需要整体编译执行。

    语法特点:

    • 1.没有“ ”,字符串使用‘ ’包含
    • 2.没有逻辑相等,赋值和逻辑相等都是=
    • 3.类型不再是最严格的。任何数据都可以包含在‘ ’以内
    • 4.没有 bool 值的概念,但是在视图中可以输入true/false
    • 5.有关系运算符:> < >= <= = <> != ,它返回一个bool值
    • 6.有逻辑运算符: !(not) &&(and) ||(or)
    • 7.不区别大小写

    2,创建数据库


    语法:

    create database 数据库名称

    on primary --默认在主文件组上

    (

    name='逻辑名称_data' , --当你发现它不是一句完整的sql语句,而仅仅是一个处理结构中的某一句的时候,就需要添加 ,

    size=初始大小, --数值不包含在‘’以内

    filegrowth=文件增长 ,

    maxsize=最大容量,

    filename='物理路径'

    )

    log on

    (

    name='逻辑名称_log' , --当你发现它不是一句完整的sql语句,而仅仅是一个处理结构中的某一句的时候,就需要添加 ,

    size=初始大小, --数值不包含在‘’以内

    filegrowth=文件增长 ,

    maxsize=最大容量, --一般来说日志文件不限制最大容量

    filename='物理路径'

    )

    示例:

     2015-10-06_162457
     2015-10-06_162649

    3,创建数据表


    语法:

    create table 表名

    (

    字段名称 字段类型 字段特征(是否为null,默认值 标识列 主键 唯一键 外键 check约束),

    字段名称 字段类型 字段特征(是否为null,默认值 标识列 主键 唯一键 外键 check约束)

    )

    创建老师表Teacher :Id、Name、Gender、Age、Salary、Birthday

    示例:

    2015-10-06_162726

    4,数据完整性约束


    实体完整性:实体就是指一条记录。这种完整性就是为了保证每一条记录不是重复记录。是有意义的

    -- 主键:非空和唯一.一个表只有一个主键,但是一个主键可以是由多个字段组成的 组合键

    -- 标识列:系统自动生成,永远不重复

    -- 唯一键:唯一,但是可以为null,只能null一次

    域完整性:域就是指字段,它是为了保证字段的值是准和有效,合理值

    -- 类型 是否null,默认值,check约束,关系

    自定义完整性:

    -- check约束,存储过程 触发器

    引用完整性:一个表的某个字段的值是引用自另外一个表的某个字段的值。引用的表就是外键表,被引用的表就是主键表

    -- 1.建立引用的字段类型必须一致

    -- 2.建立引用的字段的意义一样

    -- 3.建立主外键关系的时候选择 外键表 去建立主外键关系
    -- 4.建立主外键关系的字段在主表中必须是主键或者唯一键
    -- 5.对于操作的影响 :
    -- 01.在添加数据时,先添加主键表再添加外键表数据
    -- 02.在删除的时候先外键表数据再删除主键表数据

    -- 级联的操作:不建议使用:会破坏数据完整性

    -- 不执行任何操作:该报错就报错,该删除就删除

    -- 级联:删除主表记录,从表引用该值的记录也被删除

    -- 设置null:删除主表记录,从表对应的字段值设置为null,前提是可以为null
    -- 设置为default:删除主表记录,从表对应的字段值设置为default,前提是可以为default

    主键约束(PK Primary key) 唯一键约束(UQ unique) 外键约束(FK foreign key) 默认值约束(DF default) check约束(CK check)

    语法:

    alter table 表名

    add constraint 前缀_约束名称 约束类型 约束说明(字段 关系表达式 值)

    示例:

    2015-10-06_162833 

    5,四种基本字符类型说明


    2015-10-06_162955
    2015-10-06_163036

    2015-10-06_163127

    6,SQL基本语句


    数据插入

    调用方法 一 一对应原则:类型对应,数量对应,顺序对应
    语法: 形参 实参
    insert into 表名([字段列表]) values(值列表) --数据必须要符合数据完整性
    插入操作是单个表的操作
    插入操作insert一次只能插入一条记录

    2015-10-06_163233
    2015-10-06_163323

    数据删除
    语法:
    delete [from] 表名 where 条件

    示例:

    2015-10-06_163350

    数据更新(修改):一定需要考虑是否有条件

    语法:

    update 表名 set 字段=值,字段=值 … where 条件

    示例:

    2015-10-06_163420

    数据查询

    语法: *代表所有字段

    select */字段名称列表 from 表列表

    示例:

     2015-10-06_163459
    2015-10-06_163557
    2015-10-06_163643

     2015-10-06_163737
     
    2015-10-06_163825
    2015-10-06_163851 

    7,类型转换函数


    2015-10-06_163924

    8,日期函数


    gedate、dateadd、datediff、datename、datepart 函数。

    2015-10-06_164021

    9,数学函数


    rand、abs、ceiling、floor、power、round、sign 函数。

    2015-10-06_164129

    10,字符串函数


    charindex、len、lower、right、left、substring、replace、stuff 函数。

    2015-10-06_164234

    2015-10-06_164307

    11,合并结果集 union


    2015-10-06_164500
     2015-10-06_164538
    2015-10-06_164613

    2015-10-06_164641

    12,CASE 函数用法


    相当于 switch case---c#中的switch...case只能做等值判断

    这可以对字段值或者表达式进行判断,返回一个用户自定义的值,它会生成一个新列

    2.要求then后面数据的类型一致

    1.第一种做等值判断的case..end

    case 字段或者表达式

    when .值..then .自定义值

    when .值..then .自定义值

    .....

    else 如果不满足上面所有的when就满足这个else

    end

    示例:

    --显示具体班级的名称
    select StudentNo,StudentName,
    case ClassId --如果case后面接有表达式或者字段,那么这种结构就只能做等值判断,真的相当于switch..case
    when 1 then '一班'
    when 2 then '2班'
    when 3 then '3班'
    when null then 'aa' --不能判断null值
    else '搞不清白'
    end,
    sex
    from Student
     
    --2.做范围判断,相当于if..else,它可以做null值判断
    --case --如果没有表达式或者字段就可实现范围判断
    -- when 表达式 then 值 --不要求表达式对同一字段进行判断
    -- when 表达式 then 值 
    -- .....
    --else 其它情况 
    --end
    select StudentNo,StudentName,
    case
    when BornDate>'2000-1-1' then '小屁孩'
    when BornDate>'1990-1-1' then '小青年'
    when BornDate>'1980-1-1' then '青年' 
    --when Sex='女' then '是女的'
    when BornDate is null then '出生不详'
    else '中年'
    end
    from Student
     
    --百分制转换为素质教育 90 -A 80--B 70 --C 60 --D <60 E NULL--没有参加考试
    select StudentNo,SubjectId,
    case
    when StudentResult>=90 then 'A'
    when StudentResult>=80 then 'B'
    when StudentResult>=70 then 'C'
    when StudentResult>=60 then 'D'
    when StudentResult is null then '没有参加考试'
    else 'E'
    end 成绩,
    ExamDate
    from Result

    13,IF ELSE 语法


    1.没有{},使用begin..end.如果后面只有一句,可以不使用begin..end包含

    2.没有bool值,只能使用关系运算符表达式

    3.也可以嵌套和多重

    4.if后面的()可以省略

    示例:

    declare @subjectname nvarchar(50)='office' --科目名称
    declare @subjectId int=(select Subjectid from Subject where SubjectName=@subjectname) --科目ID
    declare @avg int --平均分
    set @avg=(select AVG(StudentResult) from Result where SubjectId=@subjectId and StudentResult is not null) --获取平均分
    print @avg
    if @avg>=60
    begin
    print '成绩不错,输出前三名:'
    select top 3 * from Result where SubjectId=@subjectId order by StudentResult desc
    end
    else
    begin
    print '成绩不好,输出后三名:'
    select top 3 * from Result where SubjectId=@subjectId order by StudentResult 
    end

    14,WHILE 循环语法


    没有{},使用begin..end

    没有bool值,需要使用条件表达式

    可以嵌套

    也可以使用break,continue

    示例:

    go
    declare @subjectName nvarchar(50)='office' --科目名称
    declare @subjectId int--科目ID
    declare @classid int =(select classid from Subject where SubjectName=@subjectName) --查询当前科目属于那一个班级
    set @subjectId=(select SubjectId from Subject where SubjectName=@subjectName) --获取科目ID
    declare @totalCount int --总人数 :那一个班级需要考试这一科目
    set @totalCount=(select COUNT(*) from Student where ClassId=@classid)
    print @totalcount --14
    declare @unpassNum int --不及格人数
    set @unpassNum=(select COUNT(distinct Studentno) from Result where SubjectId=@subjectId and StudentNo in(select StudentNo from Student where ClassId=@classid) and StudentResult<60)
    while(@unpassNum>@totalCount/2)
    begin
    --执行循环加分
    update Result set StudentResult+=2 where SubjectId=@subjectId and StudentNo in(select StudentNo from Student where ClassId=@classid) and StudentResult<=98
    --重新计算不及格人数
    set @unpassNum=(select COUNT(distinct Studentno) from Result where SubjectId=@subjectId and StudentNo in(select StudentNo from Student where ClassId=@classid) and StudentResult<60)
    end
    go
    declare @subjectName nvarchar(50)='office' --科目名称
    declare @subjectId int--科目ID
    declare @classid int =(select classid from Subject where SubjectName=@subjectName) --查询当前科目属于那一个班级
    set @subjectId=(select SubjectId from Subject where SubjectName=@subjectName) --获取科目ID
    declare @totalCount int --总人数
    set @totalCount=(select COUNT(*) from Student where ClassId=@classid)
    print @totalcount --14
    declare @unpassNum int --不及格人数
    while(1=1)
    begin
    set @unpassNum=(select COUNT(distinct Studentno) from Result where SubjectId=@subjectId and StudentNo in(select StudentNo from Student where ClassId=@classid) and StudentResult<60)
    if(@unpassNum>@totalCount/2) 
    update Result set StudentResult+=2 where SubjectId=@subjectId and StudentNo in(select StudentNo from Student where ClassId=@classid) and StudentResult<=98
    else
    break
    end

    15,子查询


    子查询,是一个查询中包含另外一个查询。

    1.子查询的使用方式:使用()包含子查询

    2.子查询分类:

    1)独立子查询:子查询可以直接独立运行,例如:

    查询比“王八”年龄大的学员信息

    select * from Student where BornDate<(select BornDate from Student where StudentName='王八')

    2)相关子查询:子查询使用了父查询中的结果

    示例:

    --子查询的三种使用方式
    --1.子查询做为条件,子查询接在关系运算符后面 > < >= <= = <> !=,如果是接这关系运算符后面,必须保证 子查询只返回一个值
    --查询六期班的学员信息
    select * from Student where ClassId=(select ClassId from grade where classname='八期班')
     
    --子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
    select * from Student where ClassId=(select ClassId from grade)
     
    --查询八期班以外的学员信息
    --当子查询返回多个值(多行一列),可以使用in来指定这个范围
    select * from Student where ClassId in(select ClassId from grade where classname<>'八期班')
     
    --当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式。如果是多行多列或者一行多列就需要使用exists
    --使用 EXISTS 关键字引入子查询后,子查询的作用就相当于进行存在测试。外部查询的 WHERE 子句测试子查询返回的行是否存在
    select * from Student where EXISTS(select * from grade)
    select * from Student where ClassId in(select * from grade)
     
    --2.子查询做为结果集--
    select top 5 * from Student --前五条
     
    --使用top分页
    select top 5 * from Student where StudentNo not in(select top 5 studentno from Student)
     
    --使用函数分页 ROW_NUMBER() over(order by studentno),可以生成行号,排序的原因是因为不同的排序方式获取的记录顺序不一样
    select ROW_NUMBER() over(order by studentno),* from Student
     
    --查询拥有新生成行号的结果集 注意:1.子查询必须的别名 2.必须为子查询中所有字段命名,也就意味着需要为新生成的行号列命名
    select * from (select ROW_NUMBER() over(order by studentno) id,* from Student) temp where temp.id>0 and temp.id<=5
    select * from (select ROW_NUMBER() over(order by studentno) id,* from Student) temp where temp.id>5 and temp.id<=10
    select * from (select ROW_NUMBER() over(order by studentno) id,* from Student) temp where temp.id>10 and temp.id<=15
     
    --3.子查询还可以做为列的值
    select (select studentname from student where studentno=result.studentno),(select subjectname from subject where subjectid=result.SubjectId), StudentResult from Result
     
    --使用Row_number over()实现分页
    --1.先写出有行号的结果集
    select ROW_NUMBER() over(order by studentno),* from Student
     
    --2.查询有行号的结果集 子查询做为结果集必须添加别名,子查询的列必须都有名称
    select * from (select ROW_NUMBER() over(order by studentno) id,* from Student) temp where id>0 and id<=5
     
    --查询年龄比“廖杨”大的学员,显示这些学员的信息
    select * from Student where BornDate<(select BornDate from Student where StudentName='廖杨')
     
    --查询二期班开设的课程
    select * from Subject where ClassId=(select ClassId from grade where classname='二期班')
     
    --查询参加最近一次“office”考试成绩最高分和最低分
    --1查询出科目 ID
    select subjectid from Subject where SubjectName='office'
     
    --2.查询出这一科目的考试日期
    select MAX(ExamDate) from Result where SubjectId=(select subjectid from Subject where SubjectName='office')
     
    --3,写出查询的框架
    select MAX(StudentResult),MIN(StudentResult) from Result where SubjectId=() and ExamDate=()
     
    --4.使用子查询做为条件
    select MAX(StudentResult),MIN(StudentResult) from Result where SubjectId=(
    select subjectid from Subject where SubjectName='office'
    ) and ExamDate=(
    select MAX(ExamDate) from Result where SubjectId=(
    select subjectid from Subject where SubjectName='office'
    )
    )

    16,表连接 join


    --1.inner join :能够找到两个表中建立连接字段值相等的记录
    --查询学员信息显示班级名称
    select Student.StudentNo,Student.StudentName,grade.classname
    from Student
    inner join grade on Student.ClassId=grade.ClassId
     
    --左连接: 关键字前面的表是左表,后面的表是右表
    --左连接可以得到左表所有数据,如果建立关联的字段值在右表中不存在,那么右表的数据就以null值替换
    select PhoneNum.*,PhoneType.*
    from PhoneNum 
    left join PhoneType on PhoneNum.pTypeId=PhoneType.ptId
     
    --右连接: 关键字前面的表是左表,后面的表是右表
    --右连接可以得到右表所有数据,如果建立关联的字段值在右左表中不存在,那么左表的数据就以null值替换
    select PhoneNum.*,PhoneType.*
    from PhoneNum 
    right join PhoneType on PhoneNum.pTypeId=PhoneType.ptId
     
    --full join :可以得到左右连接的综合结果--去重复
    select PhoneNum.*,PhoneType.*
    from PhoneNum 
    full join PhoneType on PhoneNum.pTypeId=PhoneType.ptId

    17,事务


    事务处理,要么都能成功执行,要么都不执行。

    事务的四个特点 ACID:

    • A:原子性:事务必须是原子工作单元;对于其数据修改,要么全都执行,要么全都不执行。它是一个整体,不能再拆分
    • C:一致性:事务在完成时,必须使所有的数据都保持一致状态。。某种程度的一致
    • I:隔离性:事务中隔离,每一个事务是单独的请求将单独的处理,与其它事务没有关系,互不影响
    • D:持久性:如果事务一旦提交,就对数据的修改永久保留

    使用事务:

    将你需要操作的sql命令包含在事务中

    1.在事务的开启和事务的提交之间

    2.在事务的开启和事务的回滚之间

    三个关键语句:

    开启事务:begin transaction

    提交事务:commit transaction

    回滚事务:rollback transaction

    declare @num int =0 --记录操作过程中可能出现的错误号

    begin transaction

    update bank set cmoney=cmoney-500 where name='aa'

    set @num=@num+@@ERROR

    --说明这一句的执行有错误 但是不能在语句执行的过程中进行提交或者回滚

    --语句块是一个整体,如果其中一句进行了提交或者回滚,那么后面的语句就不再属于当前事务,

    --事务不能控制后面的语句的执行

    update bank set cmoney=cmoney+500 where name='bb'

    set @num=@num+@@ERROR

    select * from bank

    if(@num<>0 ) --这个@@ERROR只能得到最近一一条sql语句的错误号

    begin

    print '操作过程中有错误,操作将回滚'

    rollback transaction

    end

    else

    begin

    print '操作成功'

    commit transaction

    end

    --事务一旦开启,就必须提交或者回滚

    --事务如果有提交或者回滚,必须保证它已经开启

    18,视图


    视图是一张虚拟表,可以像使用子查询做为结果集一样使用视图

    select * from vw_getinfo

    使用代码创建视图

    语法:

    create view vw_自定义名称

    as

    查询命令

    go

    --查询所有学员信息

    if exists(select * from sysobjects where name='vw_getAllStuInfo')

    drop view vw_getAllStuInfo

    go --上一个批处理结果的标记

    create view vw_getAllStuInfo

    as

    --可以通过聚合函数获取所以记录数

    select top (select COUNT(*) from Student) Student.StudentNo,Student.StudentName,grade.ClassId,grade.classname from Student

    inner join grade on Student.ClassId=grade.ClassId order by StudentName --视图中不能使用order by

    --select * from grade --只能创建一个查询语句

    --delete from grade where ClassId>100 --在视图中不能包含增加删除修改

    go

    --使用视图。。就像使用表一样

    select * from vw_getAllStuInfo

    --对视图进行增加删除和修改操作--可以对视图进行增加删除和修改操作,只是建议不要这么做:所发可以看到:如果操作针对单个表就可以成功,但是如果 多张的数据就会报错:不可更新,因为修改会影响多个基表。

    update vw_getAllStuInfo set classname='asdas' ,studentname='aa' where studentno=1

    19,触发器


    触发器:执行一个可以改变表数据的操作(增加删除和修改),会自动触发另外一系列(类似于存储过程中的模块)的操作。

    语法:

    create trigger tr_表名_操作名称

    on 表名 after|instead of 操作名称

    as

    go

    if exists(select * from sysobjects where name='tr_grade_insert')

    drop trigger tr_grade_insert

    go

    create trigger tr_grade_insert

    on grade for insert ---为grade表创建名称为tr_grade_insert的触发器,在执行insert操作之后触发

    as

    declare @cnt int

    set @cnt = (select count(*) from student)

    select * ,@cnt from student

    select * from grade

    go

    --触发器不是被调用的,而是被某一个操作触 发的,意味着执行某一个操作就会自动触发 触发器

    insert into grade values('fasdfdssa')

    ---替换触 发器:本来需要执行某一个操作,结果不做了,使用触 发器中的代码语句块进行替代

    if exists(select * from sysobjects where name='tr_grade_insert')

    drop trigger tr_grade_insert

    go

    create trigger tr_grade_insert

    on grade instead of insert ---为grade表创建名称为tr_grade_insert的触发器,在执行insert操作之后触发

    as

    declare @cnt int

    set @cnt = (select count(*) from student)

    select * ,@cnt from student

    select * from grade

    go

    insert into grade values('aaaaaaaaaaaa')

    go

    ---触 发器的两个临时表:

    --inserted: 操作之后的新表:所有新表与原始的物理表没有关系,只与当前操作的数据有关

    --deleted:操作之前的旧表:所有新表与原始的物理表没有关系,只与当前操作的数据有关

    if exists(select * from sysobjects where name='tr_grade_insert')

    drop trigger tr_grade_insert

    go

    create trigger tr_grade_insert

    on grade after insert

    as

    print '操作之前的表:操作之前,这一条记录还没有插入,所以没有数据'

    select * from deleted

    print '操作之后的表:已经成功插入一条记录,所有新表中有一条记录'

    select * from inserted

    go

    --测试:

    insert into grade values('aaaaa')

    if exists(select * from sysobjects where name='tr_grade_update')

    drop trigger tr_grade_update

    go

    create trigger tr_grade_update

    on grade after update

    as

    print '操作之前的表:存储与这个修改操作相关的没有被修改之前的记录'

    select * from deleted

    print '操作之后的表:存储这个操作相关的被修改之后 记录'

    select * from inserted

    go

    --测试

    update grade set classname=classname+'aa' where ClassId>15

    if exists(select * from sysobjects where name='tr_grade_delete')

    drop trigger tr_grade_delete

    go

    create trigger tr_grade_delete

    on grade after delete

    as

    print '操作之前的表:存储与这个修改操作相关的没有被删除之前的记录'

    select * from deleted

    print '操作之后的表:存储这个操作相关的被删除之后 记录--没有记录'

    select * from inserted

    go

    --测试

    delete from grade where ClassId>15

    20,存储过程


    参数,返回值,参数默认值,参数:值的方式调用

    在调用的时候有三个对应:类型对应,数量对应,顺序对应

    创建语法:

    create proc usp_用户自定义名称

    对应方法的形参 --(int age, out string name)

    as

    对应方法体:创建变量,逻辑语句,增加删除修改和查询..return返回值

    go

    调用语法:

    Exec 存储过程名称 实参,实参,实参 ...

    示例:

    --获取所有学员信息

    if exists(select * from sysobjects where name='usp_getAllStuInfo')

    drop proc usp_getAllStuInfo

    go

    create procedure usp_getAllStuInfo

    as

    select * from Student

    go

    --调用存储过程,获取的有学员信息

    execute usp_getAllStuInfo

    --exec sp_executesql 'select * from Student'

    --查询指定性别的学员信息

    go

    if exists(select * from sysobjects where name='usp_getAllStuInfoBySex')

    drop proc usp_getAllStuInfoBySex

    go

    create procedure usp_getAllStuInfoBySex

    @sex nchar(1) --性别 参数不需要declare

    as

    select * from Student where Sex=@sex

    go

    --调用存储过程,获取指定性别的学员信息

    Exec usp_getAllStuInfoBySex '女'

    --创建存储过程获取指定班级和性别的学员信息

    go

    if exists(select * from sysobjects where name='usp_getAllStuInfoBySexandClassName')

    drop proc usp_getAllStuInfoBySexandClassName

    go

    create procedure usp_getAllStuInfoBySexandClassName

    @classname nvarchar(50), --班级名称

    @sex nchar(1)='男'--性别 有默认的参数建议写在参数列表的最后

    as

    declare @classid int ---班级ID

    set @classid=(select classid from grade where classname=@classname) --通过参数班级名称获取对应的班级ID

    select * from Student where Sex=@sex and ClassId=@classid

    go

    --执行存储过程获取指定班级和性别的学员信息

    --exec usp_getAllStuInfoBySexandClassName '八期班'

    exec usp_getAllStuInfoBySexandClassName default, '八期班' --有默认值的参数可以传递default

    exec usp_getAllStuInfoBySexandClassName @classname='八期班' --也可以通过参数=值的方式调用

    exec usp_getAllStuInfoBySexandClassName @classname='八期班' ,@sex='女'

    exec usp_getAllStuInfoBySexandClassName @classname='八期班',@sex='女'

    --创建存储过程,获取指定性别的学员人数及总人数

    go

    if exists(select * from sysobjects where name='usp_getCountBySexandClassName')

    drop proc usp_getCountBySexandClassName

    go

    create procedure usp_getCountBySexandClassName

    @cnt int=100 output, --output标记说明它是一个输出参数。output意味着你向服务器请求这个参数的值,那么在执行的时候,服务器发现这个参数标记了output,就会将这个参数的值返回输出

    @totalnum int =200output, --总人数

    @className nvarchar(50), --输入参数没有默认值,在调用的时候必须传入值

    @sex nchar(1)='男'--输入参数有默认值,用户可以选择是否传入值

    as

    declare @classid int ---班级ID

    set @classid=(select classid from grade where classname=@classname) --通过参数班级名称获取对应的班级ID

    select * from Student where Sex=@sex and ClassId=@classid

    set @cnt= (select COUNT(*) from Student where Sex=@sex and ClassId=@classid) --获取指定班级和性别的总人数

    set @totalnum=(select COUNT(*) from Student) ----获取总人数

    go

    --调用存储过程,获取指定性别的学员人数及总人数

    declare @num int,@tnum int

    exec usp_getCountBySexandClassName @cnt=@num output ,@totalnum=@tnum output , @className='八期班'

    print @num

    print @tnum

    print '做完了'

    ---获取指定班级的人数

    if exists(select * from sysobjects where name='usp_getCount')

    drop proc usp_getCount

    go

    create procedure usp_getCount

    @className nvarchar(50)='八期班'

    as

    declare @classid int=(select classid from grade where classname=@className)

    declare @cnt int

    set @cnt =(select COUNT(*) from Student where ClassId=@classid)

    --return 只能返回int整数值

    --return '总人数是'+cast(@cnt as varchar(2))

    return @cnt

    go

    --调用存储过程,接收存储过程的返回值

    declare @count int

    --set @count=(exec usp_getCount)

    exec @count=usp_getCount '八期班'

    print @count

    if exists(select * from sysobjects where name='usp_getClassList')

    drop proc usp_getClassList

    go

    create procedure usp_getClassList

    as

    select classid,classname from grade

    go

    21,分页存储过程


    if exists(select * from sysobjects where name='usp_getPageData')
    drop proc usp_getPageData
    go
    create procedure usp_getPageData
    @totalPage int output,--总页数
    @pageIndex int =1 ,--当前页码,默认是第一页
    @pageCount int =5 --每一页显示的记录数
    as
    select * from (select ROW_NUMBER() over(order by studentno) id,* from Student) temp where temp.id>(@pageindex-1)*@pagecount and temp.id<=(@pageindex*@pagecount)
    set @totalPage=CEILING((select COUNT(*) from Student)*1.0/@pageCount)
    go

    22,索引


    select * from sysindexes

    --create index IX_Student_studentName

    --on 表名(字段名)

    --clustered index:聚集索引 nonclustered index--非聚集索引

    if exists(select * from sysindexes where name='IX_Student_studentName')

    drop index student.IX_Student_studentName

    go

    create clustered index IX_Student_studentName

    on student(studentname)

    --如果是先创建主键再创建聚集索引就不可以,因为主键默认就是聚集索引

    --但是如果先创建聚集索引,那么还可以再创建主键,因为主键不一定需要是聚集的

    23,临时表


    --创建局部临时表

    create table #newGrade

    (

    classid int ,

    classname nvarchar(50)

    )

    ---局部临时表只有在当前创建它的会话中使用,离开这个会话临时表就失效.如果关闭创建它的会话,那么临时表就会消失

    insert into #newGrade select * from grade

    select * from #newGrade

    select * into #newnewnew from grade

    select * into newGrade from #newgrade

    --创建全局临时表:只要不关闭当前会话,全局临时表都可以使用,但是关闭当前会话,全局临时表也会消失

    create table ##newGrade

    (

    classid int ,

    classname nvarchar(50)

    )

    drop table ##newGrade

    select * into ##newGrade from grade

    select * from ##newGrade

    --创建表变量

    declare @tb table(cid int,cname nvarchar(50))

    insert into @tb select * from grade

    select * from @tb

  • 相关阅读:
    3月工作问题总结
    【读书笔记】linux编程艺术
    项目管理工具 Trac入门
    [node.js]开放平台接口调用测试
    mysql 高并发更新计数问题
    memcache 问题 socket or its streams already null in trueClose call
    hadoop学习笔记
    node.js学习与应用
    mc参数备忘&javajson备忘
    WCF技术剖析_学习笔记之三
  • 原文地址:https://www.cnblogs.com/liuning8023/p/4857286.html
Copyright © 2011-2022 走看看