第3章 SQL语言

第3章 SQL语言

考试范围: 3.1-3.10

考试题型:计算题

考试内容: (可按标准sql、mysql的语法格式来写SQL语句,考试时要求有无明显语法错误)

Select

Insert

Delete

Update

create table

alter table

Select
  • SQL 允许在关系和查询结果中重复,因为消除重复非常耗时。

    select distinct dept_name from instructor ;
    select all dept_name from instructor ;--不删除重复项
    
  • select 子句可以包含涉及运算 +、–、*和 / 以及对元组的常量或属性进行操作的算术表达式。

    select name, salary/12 from instructor;
    
  • where语句:比较结果可以使用逻辑连接词和、或和非组合。

    select name from instructor where dept_name = 'Comp.Sci.'  and salary > 80000;
    
  • 对多个关系的查询

    • 对于所有教过一些学生的教师,找到他们的姓名和学生的学生证
    select name, s_id 
    from instructor, advisor 
    where instructor.ID = advisor.i_id ;
    
    • 列出教师及其学生的姓名
    select instructor.name, student.name 
    from instructor, advisor, student 
    where instructor.ID = advisor.i_id and student.ID = advisor.s_id;
    
  • 联表查询(同一张表)

    • 找到每个员工的姓名以及他/她的经理姓名。

    • IDnamesalarymanagerID
      1001Wu70001003
      1002He75001003
      1003Li9000NULL
    select E1.name as employee_name, E2.name as manager_name
    from employee as E1, employee as E2
    where E1.managerID = E2.ID
    
  • 模糊查询

    • 查找名称中包含子字符串“stein ”的所有教师的姓名
    select name
    from instructor
    where name like '%stein%' ; 
    
  • 排序

    select * from  instructor order by salary desc, name asc ;
    
  • 限定范围

    select name
    from instructor
    where salary between 90000 and 100000 ;
    
  • 集合操作

    • 并集:查找 2017 年或 2018 年开设的课程
    (select course_id from section where year = 2017)
    union
    (select course_id from section where year = 2018) ;
    
    • 交集:查找 2017 年和 2018 年开设的课程
    (select course_id from section where year = 2017)
    intersect
    (select course_id from section where year = 2018) ;
    
    • 差集:查找 2017 年运行但 2018 年未运行的课程
    (select course_id from section where year = 2017)
    except
    (select course_id from section where year = 2018) ;
    
  • 聚合函数

    --where后面别出现聚合函数,select、having后面可以
    avg: average value
    min:  minimum value
    max:  maximum value
    sum:  sum of values
    count:  number of values
    Group By: 分组 满足“SELECT子句中的列名必须为分组列或列函数”
    Having:筛选分组 having是在分好组后找出特定的分组,通常是以筛选聚合函数的结果
    
    --查找老师中的最大工资
    select name, salary
    from instructor
    where salary = (select max(salary) from instructor);
    
    --查找各部门讲师的平均工资
    select dept_name, avg (salary) as avg_salary
    from instructor
    group by dept_name;
    
  • Having 语句:让子句删除一些组

    --查找平均工资大于70000的所有部门的名称和平均工资
    select dept_name, avg (salary)
    from instructor
    group by dept_name
    having avg (salary) > 70000 ;
    
    --对于 2017 年提供的每个课程部分,如果该部分至少有 2 名学生,请查找在该部分注册的所有学生的平均总学分 (tot_cred)
    select course_id, sec_id, semester, year, avg(tot_cred)
    from student, takes
    where student.ID = takes.ID and year = 2017
    group by course_id, sec_id, semester, year
    having count(student.ID) >= 2;
    
  • 嵌套子查询

    set membership: in, not in
    set comparisons: some, all
    empty set: exists, not exists
    set containment: not exists
    duplicate tuples: unique, not unique
    
    --查找 2009 年和 2010 年提供的课程
    select distinct course_id
    from section
    where year= 2009 and
          course_id in (select course_id
                        from section
                        where year= 2010) ;
    
    --查找 2009 年提供但 2010 年未提供的课程
    select distinct course_id
    from section
    where year= 2009 and
          course_id  not in (select course_id
                             from section
                             where year= 2010) ;
                             
    --查找薪水高于某些(至少一个)部门预算的讲师姓名。
    select name
    from instructor
    where salary > some (select budget
                         from department ) ;
                         
    --查找工资高于所有部门预算的讲师姓名。
    select name
    from instructor
    where salary > all (select budget
                        from department ) ;
    
    
    --exists如果参数子查询为非空,则存在构造返回值 true。
    --所有教师至少教过一名学生(相关子查询)
    select ID
    from instructor
    where exists (select * from advisor where i_id = ID) ;
    
    --查找所有参加过生物系提供的所有课程的学生
    --X是Y(s)的子集 等价于 X – Y(s) = Ø  等价于 not exists ( X except Y(s) )
    --(X代表生物系提供的所有课程,Y(s)代表学生选的所有课程)
    select ID, name 
    from student 
    where not exists (
        (select course_id 
         from course
         where dept_name = 'Biology')
         except
       (select course_id 
                 from takes
                 where student.ID = takes.ID)) ;
    
    
    --unique如果参数子查询不包含重复元组,则唯一构造测试返回 true。
    --查找 2017 年最多提供一次的所有课程。
    select course_id
    from course
    where unique (select section.course_id
                  from section
                  where course.course_id = section.course_id 
                  and year = 2017) ;	
    
    
Insert
--向课程添加新元组
insert into course
values ('CS-437', 'Database Systems', 'Comp. Sci'., 4) ;

insert into course (course_id, title, dept_name, credits)
values ('CS-437', 'Database Systems', 'Comp. Sci.', 4) ;


--使音乐系的每个学分超过 144 小时的学生成为音乐系的讲师,薪水为 18,000 美元
insert into instructor
       select ID, name, dept_name, 18000
       from student
       where dept_name = 'Music' and total_cred > 144;


Delete
--从财务部门删除所有教师
delete from instructor where dept_name= 'Finance ;
Update
--对收入低于70000的讲师加薪5%
update instructor
set salary = salary * 1.05
where salary < 70000;

--case when
update instructor
set salary = case
             when salary <= 100000 then salary * 1.05
             else salary * 1.03
             end ;

create table
  • common

在这里插入图片描述

  • primary key

在这里插入图片描述

  • primary key && foreign key

    在这里插入图片描述

  • other constrains

    在这里插入图片描述

alter table

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值