数据库SQL语句(期末冲刺)

数据库Sql语句(期末冲刺)

大家好,我是寸铁✨

期末没复习,没关系,看我给大家总结的必考SQL语句,把分稳稳拿到手,希望对你有帮助,持续更新中,建议收藏✨ 💪 💪 💪

第一期

常考操作

创建表

1.建立一个学生表(学号,姓名,性别,年龄),性别默认为男,可以选择男或女,主键为学号,非空唯一,年龄在18-25之间

create table 学生表(

学号 varchar(5) not null unique,
姓名 varchar(8) not null 
性别 char(2) default '男'
年龄 varchar(15)
primary key (学号)
check ((性别='男')or(性别='女'))
check (年龄>=18 and 年龄<=20)
);

注意:对于年龄和性别的约束用check()约束
default :设置默认值
check (年龄between 18 and 25)这样写也成立
级联删除,引用该属性列的都要删除: on delete cascade
不删除:on delete no action

Select

Select查询语句

步骤如下:

Step1:将要查的内容,属性放在Select的后面即可。
Step2:清楚知道用到哪些表
Step3:建立表与表的连接
语法格式
select 属性名(*)
from table XX
where 连接的条件

1.查询全体学生的情况

select *
from student

像这种问情况、信息基本上是Select *

2.查询选修了数据库的学生的学号

select sc.Sno
from sc,course
where sc.Cno = Course.Cno and CName='数据库'

数据库这个课程名在课程表中,需要与课程表建立连接,用选课表的课程号与课程表的课程号进行连接,再说明课程名为数据库。

3.查询数据库 成绩不及格的学生名单(输出学号、姓名、成绩);

select sc.Sno,sc.Score,Student.SName
from sc,Student,course
where sc.SNo = Student.SNo and sc.Cno = Course.CNo and CName = '数据库' and Score < 60

数据库课程名在课程表(course)中,成绩在选课表(sc)中,姓名在学生表中(Student)。
需要进行连接,课程表的课程号和选课表的课程号进行连接
学生表的学号和选课表的学号进行连接

4.使用内连接查询每个学生及其选修课程情况

select *
from Student
inner join sc on Student.SNo = sc.SNo

inner(内)、left(左外)、right(右外) 哪张表 on 建立连接的条件

AS:取别名

1.查询选修了数据库的学生的学号,取名为选修数据库的学号

select sc.Sno as 选修数据库的学号
from sc,course
where sc.Cno = Course.Cno and CName='数据库'

在select后的属性加上 as + 命名 即可

Distinct

1.查询所有选修课程学生的学号,要求学号不能重复出现

select distinct sno
from sc

注意:这里是查询所有选修课程学生的学号,需要加上distinct。
因为一门课程会被多个学生选择

order by

1.查询所有职工的姓名和年龄,按年龄从小到大的顺序排列。

select TName,age
from teacher
order by age 

2.查询所有职工的姓名和年龄,按年龄从大到小的顺序排列。

select TName,age
from teacher
order by age DESC

3.查询课程号为001的成绩前五名的学生学号和成绩(提示:使用top 5)。

select top(5) Sno,Score
from SC
where Cno=001
oeder by Score DESC

注意:默认是升序(ASC)
降序为(DESC)

between and not between and

1.查询教职工中年龄在30-40岁之间的教职工姓名。

select TName
from teacher
where age between 30 and 40

2.查询教职工中年龄不在18-25岁之间的教职工姓名。

select TName
from teacher
where age not between 18 and 25

注意:where子句对年龄限制用between and
建表时对年龄的限制需要加上check
如:check(age between 18 and 25 )
或者check (age >=18 and age <=25)

in null / in not null

1.查询没有给出成绩的选课情况。

select *
from SC
where Score is null

注意:Sql的空表示为is null 不能写成 = null 或者 = 0 ! ! !

like

1.查询姓马的同学信息。

select *
from Student
where SName like '马%%'

2.查询含马的同学信息。

select *
from Student
where SName like '%马%'

3.查询姓马的同学信息(3个字)。

select *
from Student
where SName like '马_ _'

注意:字符串匹配用like
%表示不定长字符
_ 表示占一个字符

in 和 not in

使用IN查询修读课程名为数据库的所有学生的学号和姓名
select Student.Sno,Student.SName
from Student
where Student.Sno in
    (select sc.Sno
     from sc
     where sc.Cno in
        (select course.Cno
         from course
         where CName = '数据库'))

一层一层嵌套,in+需要查找属性,in表示在,not in 表示不在

exists

基本语法

select 
from 
where exits()
1.使用EXISTS查询所有选修了001号课程的学生的姓名;
select Student.SName
from Student
where exists
    (select sc.Cno
     from sc
     where sc.Sno=Student.Sno and Cno='001')

注意,单词拼写是exists 而不是 exist
exists 返回的是true or false
exists 返回true表示非空,返回false表示空
not exists 返回true表示空,返回false表示非空

2. 使用NOT EXISTS查询未选修了001号课程的学生的姓名
select Student.SName
from Student
where not exists
    (select *
     from sc
     where sc.Sno=Student.Sno and sc.Cno='001')
3.使用NOT EXISTS查询选修了课程学分为4的全部课程的学生的姓名

注意:在Sql中无ALL函数,不能直接查询全部课程
需要转换一下,选修了全部课程,等价于没有一门课没有选
即两个not exists嵌套使用,双重否定表肯定

select SName
from Student
where not exists
    (select *
     from course
     where Credits = '4'
     and not exists
     (select *
      from sc
      where course.Cno=sc.Cno and sc.Sno=Student.Sno))
4.查询选修了全部课程的学生姓名
select SName
from Student
where not exists
    (select *
     from course
     where not exists
     (select *
      from sc
      where course.Cno=sc.Cno and sc.Sno=Student.Sno))

像这道题是用两个not exists嵌套查询,双重否定表肯定
可以这么理解,选修了全部课程这件事。
课程为选修的宾语,选修为谓语
没有课程是存在没有选的情况,即:
课程的not exists放在第一个not exists
选修的not exists放在第二个not exists
这样实现了没有课程是没有被选,也就是所有课程都选了

聚集函数

常用如下:
Count():统计符合条件的元组个数
Max():求最大值
Min():求最小值
Avg():求平均值
Sum():对某一属性列的求和
Count和Sum用法注意区分

注意:

聚集函数用于select语句和group by语句后面

1.统计教职工的总人数最高工资、最少工资、平均工资

用于select语句后面

select count(*) as 总人数,Max(sal) as 最大工资,Min(sal) as 最少工资,avg(sal) as 平均工资
from Teacher
2.查询选修两门课程以上的学生学号

用于group by语句后面

错误写法 ! ! ! ! !
select Sno
from sc
where count(Cno)>=2
正确写法
select Sno
from sc
group by Sno
having count(Cno)>=2

update

1.更新表SC中的成绩,把所有的成绩加 1
update sc
set Score = Score + 1
2.更新表SC中的成绩,把选修了数据库的成绩加 1
update sc
set Score = Score + 1
where Cno = 
    (select Cno
     from course
     where CName = '数据库')
3. 替换字段中的部分字符,替换指定字符的方法

例如:将列Student中的1118改成1110, 202121314118 改成202121314110

update tab set Student= replace(Student,1118,1110) where 条件
  • 7
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寸 铁

感谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值