数据库—初识

创建三张表,向其中插入数据,并运行:

Create Table Student(
sno char(10) not null,
sname varchar(20),
sage smallint,
ssex char(1),
sdept char(2),
primary key (sno),
check(ssex in ('M','F')))


Create Table Course(
cno char(10) not null,
cname varchar(20),
cpno char(10) null,
credit smallint,
primary key (cno),
foreign key (cpno) references Course)

Create Table SC(
sno char(10) not null,
cno char(10) not null,
grade smallint,
primary key(sno,cno),
foreign key (sno) references Student,
foreign key (cno) references Course,
check(grade>=0 and grade<=100))

Insert into Student(sno,sname,sage,ssex,sdept)
values('95001','李勇',20,'M','CS');
Insert into Student(sno,sname,sage,ssex,sdept)
values('95002','刘晨',19,'F','IS');
Insert into Student(sno,sname,sage,ssex,sdept)
values('95003','王敏',18,'F','MA');
Insert into Student(sno,sname,sage,ssex,sdept)
values('95004','张立',19,'M','IS');

Insert into Course(cno,cname,cpno,credit)
Values('2','数学',null,2);
Insert into Course(cno,cname,cpno,credit)
Values('6','数据处理',null,2);
Insert into Course(cno,cname,cpno,credit)
Values('7','PASCAL语言','6',4);
Insert into Course(cno,cname,cpno,credit)
Values('4','操作系统','6',3);
Insert into Course(cno,cname,cpno,credit)
Values('5','数据结构','7',4);
Insert into Course(cno,cname,cpno,credit)
Values('1','数据库',null,2);
Insert into Course(cno,cname,cpno,credit)
Values('3','信息系统','1',4);


Insert into SC Values('95001','1',92);
Insert into SC Values('95001','2',85);
Insert into SC Values('95001','3',88); 
Insert into SC Values('95002','2',90);
Insert into SC Values('95002','3',80);


 执行数据查询:

例6 查所有选修过课的学生的学号
例7 查计算机系全体学生的名单
例8 查所有年龄在20岁以下的学生姓名及其年龄
例9 查考试成绩有不及格的学生的学号
例10 查询年龄在20至23岁之间的学生的姓名、系别、和年龄
例11 查询年龄不在20至23岁之间的学生姓名、系别和年龄。
例12 查信息系(IS)、数学系(MA)和计算机科学系(CS)的学生的姓名和性别
例13 查既不是信息系、数学系,也不是计算机科学系的学生的姓名和性别
例14 查所有姓刘的学生的姓名、学号和性别
例15 查姓“欧阳”且全名为三个汉字的学生的姓名
例16 查名字中第二字为“阳”字的学生的姓名和学号
例17 查所以不姓刘的学生姓名
例18 查DB_Design课程的课程号和学分
例19 查以”DB_”开头,且倒数第三个字符为i的课程的详细情况
例20 某些学生选修某门课程后没有参加考试,所以有选课记录,但没有考试成绩,下面我们来查一下缺少成绩的学生的学号和相应的课程号
例21 查所有有成绩的记录的学生学号和课程号
例22 查CS系年龄在20岁以下的学生姓名
例12 中的IN谓词实际上是多个OR运算符的缩写,因此例12中的查询也可以用OR运算符写成如下等价形式

Select sno from SC;
Select * from Student;
Select sname,sage from Student where sage<20;
Select sno from SC where grade<60;
Select sname,sdept,sage from Student where sage between 20 and 30;
Select sname,sdept,sage from Student where sage not between 20 and 30;
Select sname,ssex from Student where sdept in ('CS','MA','IS');
Select sname,ssex from Student where sdept not in ('CS','MA','IS');
Select sname,sno,ssex from Student where sname like '刘%';
Select sname from Student where sname like '欧阳_';
Select sname,sno from Student where sname like '_阳%';
Select sname from Student where sname not like '刘%';
Select cno,credit from Course where cname like '操作系统';
Select * from Course where cname like '%息__';
Select sno,cno from SC where Grade is NULL;
Select sno,cno from SC where Grade is Not NULL;
Select sname from Student where sdept='CS' and sage<25;
Select sno,grade from SC where cno='3' order by grade DESC;
Select * from Student order by sdept, sage DESC;


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值