学习的点点滴滴

   C#的学习告一段落了,这星期我们开始学习了SQL。因为在学校学过SQL所以学起来稍微轻松一些但是有些知识还是忘了。所以还是需要多多练习巩固的。好了下面来回顾这两天学的SQl语句吧。

   一SQL的三个语句

 (1),if ()
   begin 

   end
   else
   begin  

   end 

  (2),while()
    begin
    --循环变量的自加
    end

  (3),case语句
   declare @i int
   set @i = 7;
   declare @result varchar(20)
   set @result = case @i
   when 8 then 'A'
   when 7 then 'B'
   else 'C'
   end
   print @result

   二,数据库,表

   ---创建数据库
         CREATE DATABASE database_name
         demo: create database blog
    ---使用数据库
         use blog
    ---创建表
         CREATE TABLE table_name
         ( 
             computed_column_definition
         )

    ---demo
     create table employee
     (
    eid int,
    ename varchar(20),
    esex bit ,
    esalary float,
    etel varchar(11)
     )

    三,几个简单的基本的sql语句
     ---为了防止录入员输出错误,可以实现检查性约束。
         check (能够插入的字段条件)
     ---给某一列设一个默认值 default 值
     注意:如果设置了Default值,那么在插入数据的时候,如果不是默认值,自行输入,如果是默认值可以写default,但是不能不写
      insert into employee values(1,'孙悟空',default,5000,13838383838)
     ---当设置主键后,如何保证主键不重复,需要设置主键为标示字段identity
      insert into employee values('王璐璐',default,3000,13838383838)
     ---允不允许为空
       not null,or null
     ---对表的操作
        1,删除表
         drop table_name ---物理删除。
         truncate table_name---数据删除,只是把表中的数据清空。
         select * from employee
        2,更改表
         如果要增加一列,那么这个列是没有数据的。
         alter table employee add eaddress varchar(20)
         alter table employee drop  column eaddress
         --如果要更改列的类型,首先要确保这一列没有数据。
         alter table employee alter column eaddress int         --
         alter table employee alter column eid int  not null
         alter table employee add constraint pk_eid primary key (eid)
  选择:select * from table1 where 范围
  插入:insert into table1(field1,field2) values(value1,value2)
  删除:delete from table1 where 范围
  更新
:update table1 set field1=value1 where 范围

  查找:select * from table1 where field1 like ’%value1%’ ---,模糊查询 like  _:一个字符, %:零个或多个  

    排序:select * from table1 order by field1,field2 [desc]
  总数:select count as totalcount from table1
  求和:select sum(field1) as sumvalue from table1
  平均:select avg(field1) as avgvalue from table1
  最大:select max(field1) as maxvalue from table1
  最小:select min(field1) as minvalue from table1

   四,order by 和 group by

   order by [desc|asc]
    ----select * from employee order by esalary asc
    group by分组就是把相同的放在一起,如果使用group by 语句来分组,那么group by后面的分组条件可以出现在select语句后面,如果在select语句后面出现的查询字段没有出现在group by后面则无法分组查询,但是在select中可以出现聚合函数。一般情况下,分组语句和聚合函数一起使用。
    -----select eaddress,'总工资'=sum(esalary) from employee group by eaddress

 

posted on 2012-08-06 22:08  兰@net  阅读(248)  评论(0编辑  收藏  举报

学无止尽