二.表
1.创建表:(先建主键表,再建外键表)
               create table xsxxb
            (        
             xh char(10) primary key,
             xm char(8),
             xb char(2),
              csrq datetime,
             dh char(20)
            ) 
        go
        create table kmxxb
        (
         kmbh char(10),
         kmmc char(20),
          primary key(kmbh)
        )
        go
        create table xscjb
        (
        xh char(10),
          kmbh char(10),
          fs int,
          foreign key(xh)references xsxxb,
        foreign key(kmbh)references kmxxb
        ) 
2.修改表:
   1>增加字段
         alter table xsxxb
         add bz char(50) null
   2>删除字段
         alter table xsxxb
         drop column bz
3.删除表:(先删外键表,再删主键表)
      drop table xscjb
      drop table xsxxb
      drop table kmxxb
4.复制一个表:
   select * into xsxxb2 from xsxxb
5.创建临时表#,##)
       create table #xsxxb
            (        
             xh char(10) primary key,
             xm char(8),
             xb char(2),
              csrq datetime,
             dh char(20)
            ) 
  
       select * from #xsxxb
6.创建用户定义数据类型:
        use students
        go
        sp_addtype sts,'varchar(20)','not null','dbo'
    
        sp_addtype sts,datatime,'null','dbo'
7.删除用户定义数据类型:
        sp_droptype sts
三.操作表中的数据
   1>使用 INSERT 语句向表中插入数据:
            insert into xsxxb values('008','','','')
   2>使用 UPDATE 语句修改表中的数据:
            update xsxxb set xm='不' where xh='001'
   3>使用 DELETE 语句删除表中的数据:
            delete from xsxxb where xh='001'
            delete from xsxxb where xh in('002','004')
            delete from xsxxb 
四.系统内置函数的使用
1.聚合函数:
   1>AVG(表达式)     返回表达式中所有值的平均值。仅用于数字列并自动忽略 null 值。
   2>COUNT(表达式)   返回表达式中非 null 值的数量。可用于数字和字符列。
   3>COUNT(*)          返回表中的行数(包括有 null 值的行)。
   4>MAX(表达式)     返回表达式中的最大值,忽略 null 值。可用于数字、字符和日期时间列。
   5>MIN(表达式)     返回表达式中最小值,忽略 null 值。可用于数字、字符和日期时间列。
   6>SUM(表达式)     返回表达式中所有值的总和,忽略 null 值。仅用于数字列。
2.转型函数:
   CONVERT(datatype[(length)], expression [,style])
       select convert(char(20),getdate(),101)
3.日期函数:
   1>GETDATE()         当前的系统日期。
   2>DATEADD(datepart, number, date)   返回带有指定数字 (number) 的日期 (date),
                                         该数字添加到指定的日期部分 (date part)  。
       select dateadd(yy,2,getdate())  (yy,mm,dd,ww,hh,mi,ss)
   3>DATEDIFF(datepart, date1, date2)   返回两个日期中指定的日期部分之间的不同.
       select datediff(yy,getdate(),'2008/09/09')
   4>DATENAME(datepart, date)          返回日期中日期部分的字符串形式。
   5>DATEPART(datepart, date)           返回日期中指定的日期部分的整数形式。
4.数学函数:
5.字符串函数:
     rtrim()
     ltrim()
     ltrim(rtrim())
     space(2)
     substring(列名,开始位置,长度)
     right(列名,长度)
     left(列名,长度)
     stuff(列名,开始位置,长度,字符串)
五.表的连接
1.内部连接:
        select xsxxb.xh,xm,xscjb.fs from xsxxb inner join xscjb 
         on xsxxb.xh=xscjb.xh
2.多表连接:
        select xsxxb.xh,xm,kmmc,fs from xsxxb inner join xscjb
        on xsxxb.xh=xscjb.xh
        join  kmxxb
        on xscjb.kmbh=kmxxb.kmbh
3.左外部连接:
       select xsxxb.xh,xm,fs from xsxxb left outer join xscjb
       on xsxxb.xh=xscjb.xh
4.右外部连接:
       (与左外部连接相反)
5.完全外部连接:
       select xsxxb.xh,xm,fs from xsxxb full join xscjb
       on xsxxb.xh=xscjb.xh
6.交叉连接:
      select xm,fs from xsxxb cross join xscjb
7.自连接:
       select A.xh,A.fs,B.xh from xscjb A join xscjb B
       on A.kmbh=B.kmbh
       where A.xh>B.xh
8.联合运算符(union):
       select xh,xm from xsxxb
       union   
       select xh,xm from xsxxb2
六.数据汇总
1.排序:   (Asc升,desc降)
       select * from xscjb order by fs Asc
2.分组group by all 包含不符合指定的where条件的组,但将它们设为null)
        
        select xh,sum(fs) as tol from xscjb
        where xh='004'
        group by all xh
3.指定分组后,组的条件(having):
          select xh,sum(fs) as tol from xscjb
          group by xh
          having sum(fs)>80
4.compute汇总:
          select xh,fs from xscjb
          order by xh
          compute sum(fs)
5.compute by汇总:   
          select xh,fs from xscjb
          order by xh
          compute sum(fs) by xh
          compute sum(fs)
6.rollup和cube函数:
   rollup()  对group by子句中的列按不同的等级进行分组.
         
           select xh,sum(fs) as tol from xscjb
            group by xh with rollup
   cube()    是rollup的扩展.
七.数据的查询
1.SELECT 语句的数据检索形式
    1>显示所有列和行:
         SELECT * FROM authors 
    2>显示所有行和特定的列:
         SELECT pub_name, city FROM publishers 
    3>显示限定范围的行:
         SELECT stor_id, qty, title_id FROM sales 
             WHERE qty BETWEEN 20 AND 50 
    4>显示与值列表相匹配的行:
         SELECT * FROM publishers 
             WHERE state IN ('CA', 'TX', 'NY') 
    5>根据未知值显示行:
         SELECT price, title FROM titles 
             WHERE price IS NULL 
    6>显示/隐藏重复的值:
         SELECT DISTINCT city FROM authors
    7>显示根据多个搜索条件查询的结果:
         SELECT title FROM titles 
             WHERE pub_id = 0736 AND type = 'business' 
2.SELECT 语句中使用的关键字:
    BETWEEN 测试值是否在两值之间 
    EXISTS 测试值是否存在 
    IN 测试在一定范围内的值 
    LIKE 测试类似这个值的值 
    IS NULL 测试是否为 null 值 
3.查询通配符的使用:
     (%,_,[],^)
   1> select * from xsxxb where xm like '张%'
   2> select  * from xsxxb where xm like '_花%'
   3> select * from xsxxb where xm like '_[花娇]%'
   4> select * from xsxxb where xm like '_[^花娇]%'
4.简单子查询:
   1>使用返回单个值的子查询:
         select xm,xb,csrq,dh from xsxxb
          where xh=(select xh from xscjb where fs=70)
5.相关子查询:
   1>使用返回多行单列的子查询:
         select xm,xb,csrq,dh from xsxxb
          where xh in(select xh from xscjb where fs>70)
   2>使用exists关键字验证存在性的子查询:
           select xm,xb,csrq,dh from xsxxb
             where exists (select xh from xscjb where kmbh='3' and fs>70 
                                          and xh=xsxxb.xh )
   3>使用not exists关键字验证存在性的子查询:
            select xm,xb,csrq,dh from xsxxb
             where not exists (select xh from xscjb where kmbh='3' and fs>70 
                                          and xh=xsxxb.xh )