• sql笔记/分页存储过程


    wuhu0723@126.com
    c#中进行++操作可以是整数或小数,sql中只能对整数进行++操作。
    char类型 适合存储长度波动较小不回收效率高
    varchar 类型 适合存储长度波动较大可以回收
    nchar代表unicode 存储内容包括汉字时候考虑加n


    SQL语句特点
    1不区分大小写
    2没有双引号所有字符串都包含在单引号
    3没有逻辑相等,逻辑相等和赋值一样都是用=
    4没有bool值得概念,但是在视图中可以输入true/false
    5也有关系运算符。
    6也有逻辑运算符 &&-- and || --or ! --not
    7解释性语言,不是编译型

    --保证数据完整性:保证数据是真实的,有效的
    --四种完整性:
    --1.实体完整性:实体就是指表的一行记录。它就是保证表的每一行记录不是重复的,是唯一的。
    --    主键:唯一,非空。一个表只能一个主键。组合键是多个字段组成一个主键。
    --    标识列:系统自动生成,永远不重复,往往标识列它就是用来主键的。
    --    唯一键:标记这个字段的值不能重复。但是可以为null,只能null一次
        
    --2.域完整性:域就是指字段,它是保证某一列的值是准确和真实有效的。
    --    实现方式:类型,非空,check约束,默认值,关系(主外键)
        
    --3。自定义完整性:
    --    check约束   【存储过程  触发器】
    
    --4.引用完整性:一个表的某个字段的值引用自另外一张表的某个字段值
    --    主外键约束--关系
    --    1.建立关系的字段类型需要一致
    --    2.建立关系的字段的意义一致
    --    3.被引用的表就是主键表,引用的表是外键表
    --    4.创建主外键关系 需要 在外键表中创建。
    --    5.创建主外键关系的字段在中必须是主键或者唯一键
    --    6.在添加数据的时候,先添加主键表再添加外键表
    --    7.在删除的时候先删除外键表的数据再删除主键表的数据
    
    --使用代码创建数据完整性:
    --主键约束(primary key   PK)  唯一键约束(unique  UQ)   默认值约束(default  DF)    check约束(check  CK)  主外键约束(foreign key  FK)
    --语法:
    --alter table 表名
    --add constraint 约束名称(前缀+自定义名称)  约束类型 约束说明(字段名称  表达式  值)
    --为Id设置主键约束
    alter table teacher
    add constraint PK_Teacher_Id  primary key(Id)
    --为email添加唯一键约束
    if exists(select * from sysobjects where name='UQ_Teacher_Email')
     alter table teacher drop constraint UQ_Teacher_Email
    go 
    alter table teacher
    add constraint UQ_Teacher_Email unique(email)
    --为工资添加默认值,为年龄添加check约束
    alter table teacher
    add constraint DF_Teacher_Salary default(5000) for salary,
    constraint CK_Teacher_Age check(age>0 and age<=100)
    
    ---添加主外键约束
    alter table teacher
    with nocheck  --不检查现有数据
    add constraint FK_Teacher_Classes_Classid foreign key(classid) references classes(cid)
    on delete set null
    --on delete|update
    --no action|cascade|set null|set default
    --对于关系而言,有级联的操作:
    --1.不执行任何:如果可以删除就删除如果不能删除就报错
    --2.级联:删除主表记录,会将对应的外键表记录也删除
    --3.设置null:删除主表记录会将外键表对应的记录的值设置为null.前提是这个字段值可以为null
    --4.设置default:删除主表记录会将外键表对应的记录的值设置为默认值.前提是这个字段值之前设置了默认值
    use master
    if exists(select * from sysdatabases where name ='School')
    drop database School
    exec sp_configure 'show advanced options',1
    reconfigure
    exec sp_configure 'xp_cmdshell',1
    reconfigure
    exec xp_cmdshell 'mkdir d:database自动创建'
    create database School
    on primary
    (
        name='School_data.mdf',
        size=3mb,
        filegrowth=10%,
        maxsize=100mb,
        filename='d:database自动创建School_data.mdf'
    )
    log on
    (
        name='School_log.ldf',
        size=3mb,
        filegrowth=10%,
        maxsize=100mb,
        filename='d:database自动创建School_log.ldf'
    )
    代码创建数据库
    分页存储过程***************************************
    if
    exists(select * from sysobjects where name='usp_Person') drop procedure usp_Person go create procedure usp_Person @pagtotlecount int output, @pageindex int=1, @pagecount int =10 as select * from (select ROW_NUMBER() over(order by pid) as id,* from [person]) as temp where id between @pagecount*@pageindex-@pagecount+1 and @pageindex*@pagecount select @pagtotlecount=CEILING( COUNT(*)*1.0/@pagecount) from [person] print @pagtotlecount go declare @pagetotlenumber int execute usp_Person @pagetotlenumber output, 1,10 go

    插入重复值触发器*************************************************
    --person表插入重复值触发器 if exists(select * from sysobjects where name='tr_person_insert') drop trigger tr_person_insert go create trigger tr_person_insert on person after insert as declare @pcid int,@pid int select @pcid=inserted.PCID,@pid=inserted.PID from inserted if(not exists(select * from Classes where CID=@pcid)) begin delete from [Person] where PID=@pcid print '插入失败不存在的班级' end go

    插入聚焦非聚焦索引

    --创建索引
    --索引:就是为了查询提高效率的。相当于一个引用,可以快速找到数据的位置
    --聚集索引:索引的顺序就对应着内容的顺序,所以聚集索引意味着排序
    --非聚集索引:索引的顺序和内容的顺序没有本质的关联
    --语法:
    --create clustered|nonclustered index IX_名称
    --on 表(列)

    
    

    if exists(select * from sysindexes where name='ix_personname')
    drop index person.ix_personname
    create nonclustered index ix_personname
    on [person](pcname)



  • 相关阅读:
    [矩阵快速幂专题]
    [hdoj6415 Rikka with Nash Equilibrium][dp]
    [codeforces][dp]
    [2019年湘潭大学程序设计竞赛(重现赛)H chat][背包dp]
    [一道区间dp][String painter]
    [hdoj4578][多延迟标记的线段树]
    [cf1138BCircus][枚举,列等式]
    [Assign the task][dfs序+线段树]
    Codeforces round 419 div2 补题 CF 816 A-E
    Educational Codeforces Round 23 A-F 补题
  • 原文地址:https://www.cnblogs.com/zhanying/p/4129482.html
Copyright © 2020-2023  润新知