SQL SERVER Study

1. SQL SERVER Install
    可以免费试用180天。最新的是2014版本。
 
2. Attach, Detach 可以添加或去掉已有的数据库文件。
    数据库文件的格式是: .mdf 后缀
 
3. 客户端上可以建立 E-R图。主要注意主外键关系,一个表的外键是另一个表的主键
 
4. T-SQL Study
    eg1: order by no.
    例如: select ProductID, Name, ProductNumber,Color, Size, ListPrice
                 from Production.Product
                 order by 2
    说明: 这里的2,就是说按“Name” 进行排序。按Select后的 第几个column进行排序。
 
    eg2: IsNull 函数: 判断模拟一数据是否为空。
    例如: select ProductID, Name, ProductNumber, IsNull(Color, ''), IsNull(Size, ''), ListPrice
                 from Production.Product
                 order by 2
    
    eg3: as 关键字:给表列取别名。
    例如: select ProductID, Name, ProductNumber, IsNull(Color, '') as Color, IsNull(Size, '') as Size, ListPrice
                 from Production.Product
                 order by 2
 
     eg4: wildcard: 通配符
    例如: select *  from Production.Product
                 where name like '%Mountain%'      ----Wildcard % matches any zero or more characters
 
                 select *  from Production.Product
                 where name like '_ountain%'            
 
    eg5: in   ;  not in
    例如: select *  from Production.Product
                 where size in ('20', '50','55')     
 
                select *  from Production.Product
                 where size not in ('20', '50','55')        
 
    eg6: is null   ;  is not null
    例如: select *  from Production.Product
                 where size is null     
 
                select *  from Production.Product
                 where size is not null        
 
     eg6: and   ;  or
    例如: select *  from Production.Product
                 where color = 'red' and color = 'black'
 
                select *  from Production.Product
                 where color = 'red' or color = 'black'  
 
5. 聚合函数
    eg1: count ; distinct
    例如: select count(SalesPersonID)
                from [Sales].[SalesOrderHeader]
                 where SalesPersonID is not null
 
                select distinct(SalesPersonID)
                from [Sales].[SalesOrderHeader]
                 where SalesPersonID is not null
 
                select count(distinct(SalesPersonID))
                from [Sales].[SalesOrderHeader]
                 where SalesPersonID is not null
    
    eg2: Avg, Min, Max,Sum
    例如: select 
                Avg(SalesPersonID) as AverageTotalSales
                , Min(SalesPersonID) as MinimumTotalSales
                , Max(SalesPersonID) as MaximumTotalSales
                , Sum(SalesPersonID) as SummaryTotalSales
                from [Sales].[SalesOrderHeader]
 
    eg3: The classical T-SQL query !!!
    例如: select SalesPersonID, OrderDate, Max(TotalDue) as MaximumTotalSales
                from [Sales].[SalesOrderHeader]
                where SalesPersonID is not null and OrderDate > '2016/1/1'
                group by SalesPersonID, OrderDate
                having Max(TotalDue) > 150000
                order by SalesPersonID desc
 
6. 小技巧
    eg1: 如何显示Line Number?
    解决方案: 在SS Management Studio 最上面一行 ribbon 里面找到 Tool --> Options
 
    eg2: 如何自由转换 queries 大小写?
    解决方案: 在SS Management Studio 最上面一行 ribbon 里面找到 Edit --> Advanced  --> Make UpperCase / LowerCase
  使用快捷键:
  Ctrl + Shift + U  转换为大写
  Ctrl + Shift + L  转换为小写
原文地址:https://www.cnblogs.com/FocusIN/p/5638489.html