SQL server数据库课程笔记

来源:互联网 发布:手机mac支付是什么意思 编辑:程序博客网 时间:2024/06/06 11:47

本博客仅仅为了上课的记忆,完全基础!!!

  1. 数据是数据库中存储的基本对象;
  2. 数据库数据具有永久存储、有组织、可共享三个基本特点;
  3. 数据库是长期存储在计算机内的、有组织的、可共享的数据集合;
  4. 数据库系统是指在计算机系统中引入数据库后的系统构成,一般由数据库、数据库管理系统、应用系统、数据库管理员构成;
  5. 数据库管理系统是位于用户与操作系统之间的一层数据管理软件;
  6. 数据库系统的特点:
    a) 数据机构化;
    b) 数据的共享性高、冗余度低、易扩充;
    c) 数据独立性高;
    d) 数据由 DBMS 统一管理和控制;
  7. 数据库管理系统的主要功能:
    a) 数据库定义功能;
    b) 数据组织、存储和管理;
    c) 数据操纵功能;
    d) 数据库的事物管理和运行管理;
    e) 数据库的建立和维护功能;
  8. 范式:
    1NF:每个数据项不可再分;
    1NF->2NF:消除非主属性对码的部分函数依赖;
    2NF->3NF:消除非主属性对码的传递函数依赖;
    3NF->BCNF:消除主属性对码的部分和传递函数依赖;
    BCNF->4NF:消除非平凡且非函数依赖的多值依赖;
  9. 函数
    getdate()   --获取当前时间
    year(getdate()) --获取当前年份
    left( , );
    right( , );
    substring( , , );
  10. create  table  s(
        sno  char(11)  not  null  primary  key
        sname  varchar(8)
        ssex  char(2)
        sage  tinyint  check(sage > 10  and  sage < 100)
        sdept  varchar(500)
    )   --创建表

    Create unique index SPJ-NO on SPJ(sno ASC, pno DESC, jno ASC); --在供销情况SPJ中Sno按升序,pno按降序,Jno升序建立索引
  11. 常用语句
    select * from  s  --选择
    select * from sc order by cno ASC, grade Desc
    foreign key(sno) references s(sno) --建外键

    Select Sno From Sc Scx, Sc Scy
            where  Scx.sno = Scy.sno  AND  Scx.cno = 'c1'   AND  Scy.cno = 'c3';  --检索至少选修了课程号为“c1”和“c3”的学生号;

    Select Jno, AVG(Qty) From SPJ
           Group  by  Jno
           Having count(distinct(sno)) > 2
           order by Jno  DESC;  --查询某工程至少用了三家供应商供应的零件的平均数量,并按工号的降序排列。

    select sno, sname, 2012-sage csnf  from  student;
    select  distinct  sno  from  sc where  cj < 60
    select  distinct  sno  from  sc  where  sdept  not  in (‘IS’, ‘MA’);

    select max(sage) from s
    --where sno like '20%'
    --where cname like 'DB\_Design' Escape '\'    --'d_'  --意思为倒数第一个字符为d
    --where grade is null;
    select count(distinct sno) from sc;

    select sname, count(*) from s group by sname having count(*) > 1;
    select * from student where sage = (select max(sage) from student);
    select top|* from student order by sage desc;

    drop  table  s  --删除表

    insert  into  s(sno, sname, sage)
         values('2010114', 'hello', '20') ; --插入语句

    delete from spj where sno = 's2';  --删除

    Update teachers Set salary = salary * 1.5;  -- 更新

    Grant All Privileges  On Table S To User1, User2;  --将对表S的所有操作权限赋给用户User1、User2

    Grant Insert On Table S To User1  with Grant Option; --将对表S的插入权限赋给用户User1,并允许其将此权限赋给其他用户

    insert into sc(sno, cno) select sno, '001' from student;

    update sc set grade = grade + 30 where 'cs' =
          (select sdept from student from s.sno = sc.sno);
  12. 修改表语句
    alter table S modify status INT;   --将表S中status字段改为整型

    alter table s alter column sno char(10) not null;

    alter table s add constraint PK_S_SNO;

    alter table s add qq char(12) not null; --增加列

    alter table s drop column qq  default  '40099'; --删除列

    alter table s add constraint df_s_ssex default  '男'
    primary key(sno, cno) --多个主键
  13. 数据模型3要素:数据结构、数据操作、完整性约束。
  14. 常用关键字:
    创建 create、 drop、 primary key、 unique /*取唯一值*/、
    Foreign key Cpno references Couse(cno);  --创外键
    alter table()  /*修改*/
    Group by   /*分组*/
    Order by  ASC || DESC  ;/*排序*/
    Having 短语作用于组,从中选择满足条件的组;
    create view /*创建视图*/
    Grant  --授权限
    Revoke --收回权限
  15. 创建视图
    视图是一个虚拟表,其中的数据来源于其所引用的表。
    create view sanjian_spj_view  as  select  sno, pno, qty  from spj 
    where jno in(select  jno  from  j  where  jname = '三建');

    Create VIEW cs-student
                As  select Sno, Sname, Sage, Sex From Student
                     where  SD = 'CS'  With check option;   --创建“计算机系”学生的视图
  16. 嵌套
    select  sno  from  student 
    where sno not  in(select  sno  from  sc);
  17. 自然连接
    自然连接是特殊的等值连接,它要求两个关系中进行比较的分量必须是相同的属性组,且在结果集中将重复属性列去掉。
    select  sc.sno, sname, cname, grade  from  student, course, sc
    where student.sno = sc.sno  and  course.cno = sc.cno;
  18. 左外连接
    select  * from  student  left  join  sc  on  student.sno = sc.sno;
  19. 自身(别名)
    select  a.sno, a.cpno, a.cname, b.cpno  from
    course  a, course  b  where  a.cpno = b.cno;
  20. 更名运算:as子句
    Select Sname as 姓名, Age as 年龄 From  S;
  21. 字符串操作:like操作符
    '%'匹配任意字符串,'-'匹配任意一个字符,对大小写敏感。
    Select Sname From S Where Sname like '_小军';   --选出名字是“*小军”的所有姓名

    SQL 中允许使用escape关键词来定义转移字符,
    如:Like 'ab\%cd%'  escape  '\'  --匹配所有以"ab%cd"开头的字符串
0 0
原创粉丝点击