SQL学习


  1. 几个操作时间的函数

     --datapart 获取时间中的年月日时分秒等部分
    select DATEPART(year,current_timestamp);
    select DATEPART(DAY,current_timestamp);
    select DATEPART(MONTH,current_timestamp); --dateadd 在相应时间上加上年月日时分秒等
    select CURRENT_TIMESTAMP,DATEADD(DAY,10,CURRENT_TIMESTAMP);
    select DATEADD(month,11,'2001-2-28 12:00:00') as 上帝时刻; --datediff 获取两时间段的差值并换算为时分秒年月日等
    select DATEDIFF(month,'2014-8-3','2015-9-10'); --转换函数 转换类型
    --cast convert select ''+456;
    select ''+cast(456 as varchar);
    select ''+CONVERT(varchar,456); --convert时间类型转换 后面的数字即不同地区的时间表示方式
    select CURRENT_TIMESTAMP,
    CONVERT(VARCHAR,CURRENT_TIMESTAMP,111);--中国时间表示 select CURRENT_TIMESTAMP,
    CONVERT(VARCHAR,CURRENT_TIMESTAMP,110);--美国时间表示

  2. 简单练习
     --练习题
    use TextDB
    create table TB_CallRecord
    (
    Id int not null identity(1,1),
    CallNumber nvarchar(50),
    TelNum varchar(50),
    StartDateTime datetime null,
    EndDateTime datetime null
    )
    --主键约束
    alter table TB_CallRecord
    add constraint PK_CallRecord primary key (Id);
    --检查约束
    alter table TB_CallRecord
    add constraint CK_CallRecords check(CallNumber like '[0-9][0-9][0-9]') alter table TB_CallRecord
    add constraint CK_CallRecords_EndDateTime check(EndDateTime > StartDateTime) --默认约束
    alter table TB_CallRecord
    add constraint DF_CallRecords default(getdate()) for EndDateTime INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009DAF00A4CB80 AS DateTime), CAST(0x00009DAF00A62E94 AS DateTime));
    INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009DB000D63BC0 AS DateTime), CAST(0x00009DB000D68DC8 AS DateTime));
    INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009DB000E85C60 AS DateTime), CAST(0x00009DB000E92F50 AS DateTime));
    INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009DB2015BB7A0 AS DateTime), CAST(0x00009DB2015C4DA0 AS DateTime));
    INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009DA4014C9C70 AS DateTime), CAST(0x00009DA4014E0308 AS DateTime));
    INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009DB400DAA0C0 AS DateTime), CAST(0x00009DB400DD5FE0 AS DateTime));
    INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009DB200B9AB40 AS DateTime), CAST(0x00009DB200B9FC1C AS DateTime));
    INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009DB8014042B8 AS DateTime), CAST(0x00009DB80141804C AS DateTime));
    INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009D9A00FB9898 AS DateTime), CAST(0x00009D9A00FE6118 AS DateTime));
    INSERT TB_CallRecord ([CallNumber], [TelNum], [StartDateTime], [EndDateTime]) VALUES ('', '', CAST(0x00009D9A00FB9898 AS DateTime), CAST(0x00009D9A00FE6118 AS DateTime)); --查看全表
    select * from TB_CallRecord; --输出通话时间最长的五条记录
    select top 5 *,DATEDIFF(SECOND,StartDateTime,EndDateTime) as 通话时长 from TB_CallRecord order by DATEDIFF(SECOND,StartDateTime,EndDateTime) DESC; --输出所有数据中拨打长途号码(对方号码以0开头)的总时长。like、sum
    SELECT SUM(DATEDIFF(SECOND,StartDateTime,EndDateTime)) from TB_CallRecord WHERE TelNum like '0%'; --输出通话总时长最多的前三个呼叫员的编号。
    select top 3 Id,DATEDIFF(SECOND,StartDateTime,EndDateTime) as 通话时长 from TB_CallRecord order by DATEDIFF(SECOND,StartDateTime,EndDateTime) DESC; --输出本月拨打电话次数最多的前三个呼叫员的编号.group by,count(*)
    select top 3 CallNumber,Id,StartDateTime From TB_CallRecord where DATEPART(MONTH,StartDateTime) = 7 order by CallNumber DESC; --表序列化row_number()over(order by 字段) 就是将不连续的表依据某一列值排序
    select ROW_NUMBER()over(order by Id) as 序列化 ,*from TB_CallRecord;

  3. 事务
     --事务:SQL中每一条语句都是一个事务,任何错误都会导致整个事务失败
      --语法:
    /*
      begin transaction
       代码
    end
    */
    begin transaction
    declare @myError int;
    update TextDB..TB_CallRecord set CallNumber = 0127897789 where CallNumber = '';
    set @myError = (select @@ERROR);
    update TextDB..TB_CallRecord set CallNumber = '' where Id = 10;
    set @myError += (select @@ERROR);
    if(@myError!=0)
    begin
    rollback transaction --回滚当前的操作
    end
    else
    begin
    commit transaction --执行当前的操作
    end
    --事务的特征:如果一个事务满足原子性,持久性,隔离性,一致性,那么这个操作则称为事务。 --begin transaction select *from TextDB..TB_CallRecord;

  4. 存储过程
     --存储过程
    --语法:
    /*
    create proc[edure] 存储过程名字
    参数 as 类型 [默认值|out]
    as
    begin
    代码
    end
    */
    --例如:
    go
    create proc usp_text
    as
    begin
    begin transaction
    declare @myError int;
    update TextDB..TB_CallRecord set CallNumber = '' where CallNumber = '';
    set @myError = (select @@ERROR);
    update TextDB..TB_CallRecord set CallNumber = '' where Id = 10;
    set @myError += (select @@ERROR);
    if(@myError!=0)
    begin
    rollback transaction
    end
    else
    begin
    commit transaction
    end end
    --执行存储过程
    exec usp_text; select * from TextDB..TB_CallRecord; --带参数的存储过程
    go
    create proc usp_text2
    @oldnum as nvarchar(50)
    ,@newnum as nvarchar(50)
    as
    begin
    update TextDB..TB_CallRecord set CallNumber = @newnum where CallNumber = @oldnum;
    end --调用带参数的存储过程
    exec usp_text2 '','';
    select * from TextDB..TB_CallRecord --带参数和返回值的存储过程
    go
    create proc usp_text3
    @oldnum as nvarchar(50)
    ,@newnum as nvarchar(50)
    ,@isSuccess int output --使用output将函数内参数抛出给外部
    as
    begin
    declare @myError int
    update TextDB..TB_CallRecord set CallNumber = @newnum where CallNumber = @oldnum;
    set @myError = (select @@ERROR);
    if(@myError=0)
    begin
    commit
    set @isSuccess = 1
    end
    else
    begin
    rollback
    set @isSuccess = 0;
    end
    end --调用带参数和返回值的存储过
    declare @result int
    exec usp_text3 '','',@result output;
    select @result; --使用try catch
    go
    create proc usp_text4
    @oldnum as nvarchar(50)
    ,@newnum as nvarchar(50)
    ,@IsSucess int output
    as
    begin
    begin transaction
    update TextDB..TB_CallRecord
    set CallNumber = @newnum
    where CallNumber = @oldnum
    begin try
    commit
    set @IsSucess = 1;
    end try
    begin catch
    rollback
    set @IsSucess = 0;
    end catch
    end --调用带有try,catch的存储过程
    declare @result int
    exec usp_text4 '','',@result output;
    select @result;

  5. 触发器
     --触发器
    --语法
    /*
    create trigger tr_类型触发器名字 on 表名
    触发类型:after|instea of
    操作类型:inser|delete|update
    as
    begin
    代码
    end
    */ --案例
    --插入数据的同时获得自动增长的Id
    select * from TextDB..TB_USER; insert INTO TextDB..TB_USER (userID,[password],code,lastTime)
    OUTPUT inserted.userID
    VALUES ('aaa','DDD','','2007-03-12'); -----------
    go
    create trigger tr_delete_不会删除的表 on TextDB..TB_USER
    after
    delete
    as
    insert into TextDB..TB_USER(userID,[password],code,lastTime)
    select userID,[password],code,lastTime from deleted;
    go select * from TextDB..TB_USER;
    delete TB_USER
    select SUSER_NAME();

    学习于蒋坤老师视频教程

SQL学习(时间,存储过程,触发器)的更多相关文章

  1. sql学习笔记--存储过程

    存储过程(stored procedure)有时也称sproc,它是真正的脚本,更准确地说,它是批处理(batch),但都不是很确切,它存储与数据库而不是单独的文件中. 存储过程中有输入参数,输出参数 ...

  2. PL/SQL学习笔记之触发器

    一:触发器响应的事件 数据库操作(DML)语句(DELETE,INSERT,UPDATE) 数据库定义(DDL)语句(CREATE,ALTER或DROP) 数据库操作(SERVERERROR,登录,注 ...

  3. 1.8(SQL学习笔记)触发器

    一.触发器简介 当需要某些操作在某些语句执行之前或之后执行就需要使用触发器. 例如每次插入数据时进行数据校对,每次删除数据后将删除内容备份到新表. 这些操作我们希望它(某些语句)在满足某些条件时自动执 ...

  4. SQL学习笔记七之MySQL视图、触发器、事务、存储过程、函数

    阅读目录 一 视图 二 触发器 三 事务 四 存储过程 五 函数 六 流程控制 一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名 ...

  5. Oracle学习2 视图 索引 sql编程 游标 存储过程 存储函数 触发器

    ---视图 ---视图的概念:视图就是提供一个查询的窗口,来操作数据库中的数据,不存储数据,数据在表中. ---一个由查询语句定义的虚拟表. ---查询语句创建表 create table emp a ...

  6. 查看SQL SERVER 加密存储过程,函数,触发器,视图

    原文:查看SQL SERVER 加密存储过程,函数,触发器,视图 create  PROCEDURE sp_decrypt(@objectname varchar(50))ASbeginset noc ...

  7. Sql Server 查看存储过程最后修改时间

    Sql Server 查看存储过程最后修改时间 select * from sys.procedures order by modify_date desc

  8. 2020重新出发,MySql基础,MySql视图&索引&存储过程&触发器

    @ 目录 视图是什么 视图的优点 1) 定制用户数据,聚焦特定的数据 2) 简化数据操作 3) 提高数据的安全性 4) 共享所需数据 5) 更改数据格式 6) 重用 SQL 语句 MySQL创建视图 ...

  9. MySQL笔记---视图,存储过程, 触发器的使用入门

    大二学数据库的时候,只是隐约听到老师提起过视图啊,存储过程啊,触发器啊什么的,但只是淡淡的记住了名字,后来自己做些小项目,小程序,也没有用上过,都只是简单的建表,关联表之类的,导致我对这些东西的理解只 ...

随机推荐

  1. MVC学习十:MVC 特性作用和MVC 验证

    根据代码分析特性用处 [DisplayName("学员名")] [DataType(DataType.Text)] [StringLength(,ErrorMessage=&quo ...

  2. P2213 [USACO14MAR]懒惰的牛The Lazy Cow_Sliver

    P2213 [USACO14MAR]懒惰的牛The Lazy Cow_Sliver 最大化一个子矩阵的和. 我们如何去做,dp和贪心呀! 大体题意:给定一个正方形,然后在正方形中求出一个大小已经给定的 ...

  3. ZooKeeper系列(1)--分布式系统的基石

    分布式架构有以下几点普适性的共性需求: 1. 提供集群的集中化的配置管理功能,可以不重启就让新的配置参数生效,类似与配置中心      2. 简单可靠的集群节点动态发现机制,便于动态发现服务,动态扩展 ...

  4. JAVA数据类型能串门不?

    JAVA这几种数据类型,能否串门?入了人家门,就得按人家规矩来,入乡随俗哦,难免发生有自觉的 还有不情愿被动的. 自动类型转换 自动类型转换:容量小的数据类型可以自动转换为容量大的数据类型.在图中,黑 ...

  5. kendo UI 倒如css 和 js 后 窗口控件上的工具栏图标不显示如何解决

    examples 文档中找到window的例子打开一个 查看其中文件引入 <head>    <title>API</title>    <meta char ...

  6. C++ map练习

    C++ STL之map map介绍 C++里的map数据结构,会存储键值对信息key-value,通过key得到value的信息.map的key与value有一个特点就是:每个唯一的key拥有唯一对应 ...

  7. MYSQL 8.0.11 安装过程及 Navicat 链接时遇到的问题

    参考博客:https://blog.csdn.net/WinstonLau/article/details/78666423 我的系统和软件版本是这样的: 系统环境:win7.64位 MySQL版本: ...

  8. Mysql 几种常见的插入 Insert into,Replace Into,Insert ignore

    简要说下三者的区别:insert into 最普遍的插入,如果表中存在主键相同的数据,执行会报错. replace into 如果表中存在主键相同的数据则根据主键修改当前主键的数据,反之则插入(存在就 ...

  9. java 用接口实现加减乘除计算器

    class Test{ public static void main(String[] args) { fun i=new fun(); jiafa s1=new jiafa(); jianfa s ...

  10. SAS中的宏语言

    一.sas 宏变量 1) 宏变量属于SAS宏语言,与普通变量的区别是可以独立于DATA步 2) 可以再SAS程序中除数据行之外的任何地方定义并使用宏变量 3) %let语句定义宏变量并分配一个值给宏变 ...