SQL_Server_Day04_T_SQL高级1

来源:互联网 发布:网络不给力是什么原因 编辑:程序博客网 时间:2024/06/03 14:28
1.    工厂方法设计模式
    使用工厂方法设计模式连接多个数据库Sql Server和Access
    
2.    多条件查询案例

        sql = "select * from blueTooth_Earings";
        List<string> list = new List<string>();

        if (cbBrand.Checked)
        {
            list.Add("brand = @brand");
            listParams.Add(new SqlParameter("@brand", tbBrand.Text));
        }
        ...
        
        if (list.Count>0)
        {
            sql += " where " + string.Join(" and ", list);
        }
        
3.    SQL Server高级内容
    
    case表达式
    子查询
    表表达式

    基于T-SQL的编程
    事务
    存储过程
    触发器
    优化与索引
    
4.    case表达式

    软删除 isDel属性 = 1,在查询的时候就不再列出了,经过一段时间再真正删除

    case语句相当于一个三元表达式或C#中的选择结构
    
    语法1
    case(if...else if... ... else...)
        when 字段表达式 then 结果
        when 字段表达式 then 结果
        ...
        else 结果
    end
    
    case when stuSex = 'f' then '女'
         else '男'
    end as stuSex
    
    语法2(switch--case)
        case 字段
        when 值 then 结果
        when 值 then 结果
        ...
        else 结果
    end
    
    case stuSex
         when 'f' then '女'
         else '男'
    end as stuSex
    
    透视变换
    学号    课程    成绩     ---->      学号    语文    数学    英语
    001        语文    87                    001        87        79        95    
    001        数学    79                    002        69        84        0
    001        英语    95
    002        语文    69
    002        数学    84
    
    将某一列聚合,将某一列变为列名,将某一列结果变为字段的值
    select
        学号
        ,sum(case when 课程 = ‘语文’ then 成绩 else 0 end) as 语文
        ,sum(case when 课程 = ‘数学’ then 成绩 else 0 end) as 数学
        ,sum(case when 课程 = ‘英语’ then 成绩 else 0 end) as 英语
    from
        MyTestDB..Score
    Group by
        学号
        
    pivot语法 -- sql SERVER独有的
    
5.    子查询(一个查询中含有另一个查询,把子查询的结果作为主查询的条件)
    子查询返回的结果集可能是三种情况:
    单值(一个值)    --> where子句使用 =
    多值(一列)    --> where字句使用 in    
    表值(一个表)    --> ??
    
    以上都是 独立子查询(子查询可以独立执行)
    
    相关子查询  在子查询中使用了主查询的条件  
    select *
        , (select AVG((testBase + testBeyond + testPro)*1. / 3) from TestDatabase..Score where stuId = t.stuId)
    from TestDatabase..Student as t
    where
        stuName = '纪明杰';

6.    表连接(将多张表合并成一张表) <--> 与union联合结果集的区别
    交叉连接   cross join  --> 没实际用途
    内连接       在交叉连接基础上筛选得到有效数据  inner join  on条件
    外连接       在内连接中将删除掉的数据再找回来  left join   right join

7.    对象关系映射ORM object relation mapping
    对象与关系型数据库的关系一一映射,表是什么样子,对象就是什么样子,
    由此可以用list<T>装表的内容

0 0
原创粉丝点击