基础T-Sql语句

1.增加数据

  ① insert [into] <表名>(col1,col2,col3......) value(value1,value2,value3......)   

               --列名和值要一一对应;[into]可以省略;如果省略列名,将依次插入多有列

   将现有的表B中的数据添加到已存在的表A中

     insert into <表名A>(Acol1,Acol2,Acol3......) select Bcol1,Bcol2,Bcol3...... from <表名B> 

            --查询得到的数据个数,顺序,数据类型和插入的一致;TableNameA必须是已存在的,并具有Acol1,Acol2,Acol3......

   将现有表B中的数据添加到新表中

       select Bcol1,Bcol2,Bcol3...... into <表名A> from <表名B>

  将现有表B中的数据添加到新表中,并插入标识列

      select identity(int,1,1) as Bcol1,Bcol2,Bcol3...... into <表名A>from <表名B>

   通过union关键字合并数据插入

    insert <表名>(col1,col2,col3)

   select Avalue1,Avalue2,Avalue3 union

     select Svalue1,Svalue2,Svalue3 union

        select Dvalue1,Dvalue2,Dvalue3       --最后一行数据添加时不加union关键字

2.删除数据

  delete from <表名> where 条件

    --如果不写where条件将会删除表中的所有数据,并且标识列不会重新编号

  ②truncate table <表名>

         --执行速度跟快,删除后标识列会重新开始编号,但不能用于有外键约束的表,

3.修改数据

  updata <表名> set colName=newValue where 条件   

          --如果不写where条件,那么将更新所有数据

4.查询数据

  select <列名> from <表名> where <查询条件> roder by <排序的列名>  asc [or desc]

    --asc,desc 按照某一列尽心升序或降序排列

  ②查询所有行

     select <列名> from <表名>

  ③查询部分数据

     select <列名> from <表名> where <查询条件>

  ④在查询中使用别名

    select col1+'.'+col2 as 姓名 from <表名>

    or  select  姓名=col1+'.'+col2 from <表名>

  ⑤在查询中使用常用量

      select A=col1,B=col2,'ccccc' as C from <表名>

    --C这一列就全部显示为ccccc

  ⑥查询返回限制的行数

     select top 10 col1,col2,col3 from <表名> where <查询条件>

    --返回符合条件的前十条数据,默认asc排序

posted @ 2014-04-01 16:55  1175925083  阅读(136)  评论(0)    收藏  举报