zoukankan      html  css  js  c++  java
  • sql server 笔记 义美

    sql语句 不区分大小写!

    在 sql 语句里面 所有的字符串 都用单引号 ''

    create database Library

    新建一个名字为 Library 的表

    建一个表 名字叫 Users 的表

    (UserId int primary key, 是设置主键的意思)

    create table Users

    (

    UserId int primary key,

    UserName nvarchar(20) not null,

    UserPwd nvarchar(20) not null,

    )

    注释 两个横线  --

    select * from UserInfor

    查询 名字 为UserInfor 我表里面的所有内容

    * 表示所有字段

    select UserId,UserName,Pwd from UserInfor

    只查询部分的内容 比如 id name 密码

    where  查询  查询 name 为 admin  pwd 为123456

    select * from UserInfor where UserName='admin' and Pwd='123456'

    select * from UserInfor where Sex='男'

    查询 UserInfor 表中 所有 Sex 为男的

    select * from UserInfor where Sex='男' and Origin='江西'

    查询  UserInfor 表中 所有 Sex 为男的 Origin 为江西的

    select * from UserInfor where Sex='男' and Origin='江西' and Age>18

    查询  UserInfor 表中 所有 Sex 为男的 Origin 为江西的  年龄大于18的

    or 是或者 或 的意思

    select * from UserInfor where Sex='男' and (Origin='江西' or Origin='湖北' ) and Age>18

    湖北 或 江西

    select * from UserInfor where (age<30 or age>25)

    select * from UserInfor where age<=30 and age>=25

    select * from UserInfor where Age between 25 and 30

    查询年龄 小于 25大于30的

    select * from UserInfor where Age between 25 and 30

    是查区间

    查记录数

    select count(*) from UserInfor where Origin='江西'

    查询江西的 个数 个数 返回个数

    select count(*) as UserCount from UserInfor where Origin!='江西'

    不为江西 的个数

    select count(*) as UserCount from UserInfor where Origin='江西'

    返回的那个个数 多了个 列名 UserCount

    select * from UserInfor order by Age

    select * from UserInfor order by Age asc

    排序查询 通过 Age 升序 查询

    默认是升序 后面的 asc 可以省略

    select * from UserInfor order by Age desc

    按照 Age  降序查询 desc 表示降序

    select * from UserInfor where UserId=5 or UserId=8 or UserId=10

    select * from UserInfor where UserId in(5,8,10)

    查询 UserId 为 5 8 10 的

    select * from UserInfor where UserId not in(5,8,10)

    除了 UserId 为 8 5 10 的

    select * from UserInfor where UserName like '谢%'

    查询查 UserName 为 谢 开头的

    select * from UserInfor where UserName like '%帅'

    查询 UserName 为 帅 结尾的

    select * from UserInfor where UserName like '%帅%'

    查询 UserName 包含 帅 的

    select min(Age) as MinAge from UserInfor

    查询 Age 最小的 列名 为 MinAge

    select max(Age) as MaxAge from UserInfor

    查询 Age 最大的 列名 为 MaxAge

    select avg(Age) as AvgAge from UserInfor

    求平均数  列名 为 AvgAge

    select sum(Age) as SumAge from UserInfor

    求和 列名 为 SumAge

    select * from UserInfor where Age>

    (

    select avg(Age) from UserInfor

    )

    查询大于 平均值的

    select top 3 * from UserInfor order by Age desc

    查询 年龄最高的三个

    select count(*) as oruginCount,Origin from UserINfor group by Origin

    分组查询

    select count(*) as oruginCount,Origin from UserINfor group by Origin having count(*)>2

    查询结果大于二的

    select distinct UserName from UserInfor

    查询不重复的  distinct 表示不重复

    left join

    select UI.UserId,UI.UserName,UI.Sex,SC.MathScore,SC.ChinaScore 

    from UserInfor UI left join Score SC on

    UI.UserId=SC.UserId

    联合查询  UI.UserId=SC.UserId 关联

    right join

    select UI.UserId,UI.UserName,UI.Sex,SC.MathScore,SC.ChinaScore 

    from UserInfor UI right join Score SC on

    UI.UserId=SC.UserId

    inner join

    select UI.UserId,UI.UserName,UI.Sex,SC.MathScore,SC.ChinaScore 

    from UserInfor UI inner join Score SC on

    UI.UserId=SC.UserId

    select UISC.* ,PT.Father,PT.Mum from

    (

    select UI.UserId,UI.UserName,UI.Sex,SC.MathScore,SC.ChinaScore 

    from UserInfor UI inner join Score SC on

    UI.UserId=SC.UserId

    ) UISC inner join Parent PT on

    UISC.UserId=PT.UserId

    三个表 联合查询 嵌套式

    联合查询 很重要 一定要过

    select row_number() over(order by UserId)as rownumber,* from UserInfor

    分页查询

    select top 5* from

    (

    select row_number() over(order by UserId)as rownumber,* from UserInfor

    ) A where rownumber>5

    行号大于5

    (以后用的非常多)

    select UserId Age,

    case

    when Age<20 then '大一'

    when Age>20 and Age<=25 then '大二'

    when Age>25 and Age<=30 then '大三' 

    else '大四'

    end as geade from UserInfor

    对某个字段进行判断

    获取时间

    select year(getdate())

    获取当前系统年

    select month(getdate())

    获取当前系统月

    select day(getdate())

    获取当前系统日

    select dateadd(yy,100,getdate())

    当前系统时间再加100年  yy是年

    select dateadd(mm,100,getdate())

    当前系统时间再加 100个月  mm是月

    select dateadd(dd,100,getdate())

    当前系统时间再加 100个天  dd是天

    可以用在VIP到期时间 添加临时VIP

    间隔时间

    select datediff(yy,getdate(),'2108/12/31')

    当前时间 距离 2108/12/31 多少年

    select datediff(mm,getdate(),'2108/12/31')

    select datediff(dd,getdate(),'2108/12/31')

    间隔多少月 间隔多少天

    下面是增加 增

    查询到这了告一段落

    --添加 下面这种方式 表里面的每一个字段都要写

    insert into UserInfor values ('林东群','dongdong520',20,'1997-7-12 00:00','女','广东')

    如果不想 写值 ‘’ 不能不填

    --下面这种方式可以添加表单中的指定项

    insert into UserInfor(UserName,Pwd,Age) values('刘严','liuyan',22)

    下面学 删除 删

    delete UserInfor

    删除这个 叫 UserInfor 的表

    --下面是删除

    delete UserInfor where UserId=1004

    删除 UserId=1004  的

    下面是该 修改

    update UserInfor set UserName='刘翔',Age=22,Pwd='qwe1234' where UserId=11

    修改 UserId 为11的 UserNam Age Pwd

    --约束

    找到对应的表 设计  选择对应的项 CHECK 约束 然后写表达式

    也可以在JS里面约束  比如年龄 不能小于1  不能大于100

    索引 数据库优化 要用的技术

    聚集索引 

    非聚集索引

    一个表里面只有一个聚集索引

    设置了主键 那么就被默认设置了是聚集索引

    提高查询效率

    (聚集索引 访问量不是特别大就不要加 访问量特别大就要就 比如和百度大那样的就要加)

    ------------------------------------

    视图  原理 就是联合查询

    联合查询 是写在程序里面 写在C# 里面

    视图 把它当做单独个体存在数据库里面 可以单一个单独的表使用

    视图(占用数据库空间 用的比较少)

    存储过程

    相对安全 相对高效

    --触发器

    利用触发器 把删除的东西 显示出来

    delete UserInfor where UserId=1005

    create trigger trigDeIuser

       ON  UserInfor

       after delete /* 有三种INSERT,DELETE,UPDATE*/

    AS 

    begin

    select * from deleted /*在删除后同时查出删除后的内容*/

    end

    数据库的还原 备份

    备份方式 三种

    方法1. 选中要备份的数据库----任务-----备份 找到备份文件

    还原 选中数据库---右键----还原数据库---设备--添加----覆盖现有数据库---确实

    如果向下兼容?

    修改兼容级别

    选中数据库---右键----属性----选项----降低至自己需要的级别

    方法2.通过日志文件还原

    C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA

    下的.mdf  文件 和.ldf

    要先保存起来 不然删了库 这个两个文件也会被删除

    然后加进去

    还原 选中数据库---右键---附加---选中---确定

    方法3.导出脚本

    选择数据库----右键---任务----生成脚本----下一步----下一步----高级---倒数第二个 

    要修改的脚本类型:架构和数据。---然后一直按确定

    存入进去

    找到脚本 拉倒软件里面 执行就可以了 也可以复制代码执行

    修改VIP到期

    --update Users set Isvip='false' where Isvip='true' and  datediff(dd,getdate(),Expire)<0

    --到期了 结果是负数  没到期 结果是正数 刚刚好是0

  • 相关阅读:
    SGU 205. Quantization Problem
    BZOJ1009: [HNOI2008]GT考试
    SGU 204. Little Jumper
    Spring Aspect 获取请求参数
    springboot 是用拦截器Interceptor获取请求的控制器和请求的方法名
    spring boot Filter过滤器的简单使用
    Java 使用Query动态拼接SQl
    java 传入list集合 返回树形菜单,for循环遍历
    spring data jpa 的简单使用
    vue项目引入element
  • 原文地址:https://www.cnblogs.com/shaozhu520/p/8475496.html
Copyright © 2011-2022 走看看