代码改变世界

sql回顾而知新

2016-04-23 15:38  TNT_xxx  阅读(295)  评论(0编辑  收藏  举报

++++++++++++++++++++++++++++++++++++++

1.数据库的 数据类型: int ,decimal ,money,varchar(20)

                       差不多 就可以包含所有的 事务类型

                 对于 日期 就是 datatime,2001-01-02

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

2. 实体约束的问题,就是  primary key ,identity,not null,

                   默认值: addr  varchar(10) not null default '中国', 

                        stuNo int identity(1001,1) primary key,--主键

                     外键约束: classId int foreign key references Tb_class(stuNo)--其他的表的主键作为 外键

        取值约束,    age int check (age>=10 and age<20)

++++++++++++++++++++++++++++++++++++++++++++++++++++

3.操作语言:

           部分插入    insert  T_stu(f_name,f_sex) values('马云','男')

            全部插入  insert T_stu values(25,'电气','马云','男',2001-11-14)

           删除: delete from T_stu where name='马云'

           删除记录: truncate table student

           更改: update T_stu set name='马玉华' where name='马云'

++++++++++++++++++++++++++++++++++++++++++++++++++++

4.基本查询

       select  list_xx  into new_table

    from T_xx, T_XXX  可以是一个表或者是多个表

   where 条件  group by 列名 必须是 前面会出现的 

                      having 已经分组的 表 又加 条件必须 组列中 的一个

 order by XX asc/desc 就是根据 其 排序   

++++++++++++++++++++++++++++++++

   *** select * from teacher age>40

     where DName in ('计算机','电气')

   **** CName like '%气'

                 _ 表示一个 字符

                  __表示 两个   like‘ [计电]’ 就是 只要 有 任意的一个 字出现就行 

+++++++++++++++++++++++++++++++

5.聚合函数

    通常要和 分组命令 一起使用

      1.选中的 列的 总数        sum(sal) as 工资总数 from  teacher where age>40

       2. 列中值得 个数     count(*) from teacher where sex='女'--所有女教师的人数

       3.某一列中 的 最大 最小  max(age)from TT where sex='男'

          年龄最大 男 教师的  信息

               select * from teacher where age=(select max(age) from TT where sex='男')

        4. 平均年龄的 avg(age)   

++++++++++++++++++++++++++++++++++++++++++++++