数据库学校版笔记 表 数据

xx 表名 name 数据名 xxx数据类型

表头

create table xx(name xxx );

drop table xx;

信息

添加

alter table xx add name xx;

修改

数据类型

####### alter table xx modify name xx;

####### alter table xx rename column name to other name;

表名

####### alter table xx rename to oo;

####### alter table xx drop column  name ;

查看

所有select * from

信息完整性

添加

位置

create table xx(name xxx constraint t1 www);

格式

主码

####### primary key

非空

####### not null

范围

####### check(title in ('教授','副教授','讲师','助教')

####### check (age between 18 and 60)

默认

引用

create table xx(name xxx constraint references other name xxx on delete cascade);

修改

添加

alter table xx modify name constraint s1 www;

删除

alter table xx drop constraint t2;

数据

更改

update xx set credit=4 where name='数据库技术';

删除

insert into xx(name) values('田中华');

update xx set name=null where name='d01';

插入新数据

insert into xx  values(2018122101,'数据类型相同的数据',null);

insert into xx(name1,name2,name3,name4) values(3000,500,'副教授','d02');

1.查询出所有学生的学号、姓名,所在系的系号
select sno,sname,dno
from student;

2.查询出所有男学生的学号、姓名,所在系的系号
select sno,sname,dno
from student
where sex='男';

3.查询教授和副教授的年收入,修改输出的列标题为工号、姓名、职称和年收入
select tno 工号,tname 姓名,title 职称,sal*12+nvl(bonus,0) 年收入
from teacher
where title='教授' or title='副教授';

4.查询年收入超过10万的教师的工号、姓名、职称和年收入
select tno,tname,title,sal*12+nvl(bonus,0)
from teacher
where sal*12+nvl(bonus,0)>100000;

5.查询课序号为c01的课程成绩为良好(80分到89分,包含80分和89分)的同学的学号
select sno
from sc
where cno='c01' and grade between 80 and 89;

6.查询课序号为c01这门课不及格的学生的学号
select sno
from sc
where cno='c01' and grade<60;

查找

单查询

select name1,,name2,name3,name4 from student;

select name1,name2,name3,name4 from student where sex='boy' and grade>60;

修改列名输出

select name 姓名,sal*12+nvl(bonus,0) 年收入 from teacher where title='教授'

所有内容

select * from xx;

具体内容

select distinct name from xx where a=  ;

姓名中

#######  '%田%';

名字中

####### '_%田%';

1.查询有科目不及格的学生的学号
select distinct sno
from sc
where grade<60;

2.查询正在学习课序号为c01、c02、c03和c04这几门课的学生的学号
select distinct sno
from sc
where grade is null and cno in('c01','c02','c03','c04');

3.查询课序号为c01、c02、c03和c04这几门课之外的课程成绩及格的学生的学号
select distinct sno
from sc
where cno not in('c01','c02','c03','c04') and grade>=60;

4.查询姓名中有个田字的同学的信息
select *
from student
where sname like '%田%';

5.查询名字中有个田字的同学的信息
select *
from student
where sname like '_%田%';

排列

升序

select name, other name...

####### other

######## extract(year from sysdate)-extract(year from hiredate)

######### extract提取

######## to_char(sysdate,'yyyy')-to_char(hiredate,'yyyy')

######### hiredate参加工作时间

######### sysdate现在日期

######## floor(months_between(sysdate,hiredate)/12)

######### floor 给…铺地板

####### 尾

######## order by 3 desc,1;

######### 第几个

select*from

####### order by ...

######## to_char(sysdate,'yyyy')-to_char(birth,'yyyy'),sno desc;

######## sno desc

######### 从小到大

######## desc,sno

######### 从大到小

1.从学生表student中查询学生的学号和姓名,按年龄从高到低排序输出(这里的年龄不止要比较出生年份,而且要比较到出生的月份和日子)
select sno,sname
from student
order by birth;

2.从学生表student中查询男学生信息,按年龄从低到高排序输出(这里的年龄只比较出生年份,不比较到出生的月份和日子),年龄相同的按学号从高到低排序
select *
from student
where sex='男'
order by to_char(sysdate,'yyyy')-to_char(birth,'yyyy'),sno desc;

3.从教师表teacher中查询所有教授的工龄(工作了多少年),输出工号、姓名和工龄,按工龄从高到低排序输出,工龄相同按工号从低到高排序(要求计算工龄用两种写法)
select tno,tname,to_char(sysdate,'yyyy')-to_char(hiredate,'yyyy')
from teacher
where title='教授'
order by 3 desc,tno;

select tno,tname,extract(year from sysdate)-extract(year from hiredate)
from teacher
where title='教授'
order by 3 desc,1;

select tno,tname,floor(months_between(sysdate,hiredate)/12)
from teacher
where title='教授'
order by 3 desc,1;

多表查询

select name1,name2 from xx1,xx2 where xx1 name1=xx2 name1 and grade<0,

1.查询物理系和化学系教师的信息,输出工号、姓名、职称和系名。
select tno,tname,title,dname
from teacher,dep
where teacher.dno=dep.dno and dname in('物理系','化学系');
2.查询陈洁有哪些科目不及格,输出课程名和成绩。
select cname,grade 
from course,sc,student 
where course.cno =sc.cno and sc.sno=student.sno and sname ='陈洁' and grade<90;
3.查询正在上大学英语的学生的信息,输出学号、姓名和系名。
select sc.sno,sname,dname 
from student,sc,course,dep
where sc.sno=student.sno and dep.dno =student.dno and sc.cno = course.cno and cname ='大学英语' and grade is null;
4.查询物理系大学英语不及格学生的信息,输出学号、姓名和成绩。
select sc.sno,sname,grade 
from student,sc,course,dep
where sc.sno=student.sno and dep.dno =student.dno and sc.cno = course.cno and dname ='物理系'and grade<60 and cname ='大学英语' ;
5.查询这学期有上课的物理系老师的工号和姓名。
select distinct teacher.tno,tname
from teacher,sc,course,dep
where sc.cno=course.cno and dep.dno =teacher.dno and sc.cno = course.cno and dname in('物理系') and grade is null;
6.查询正在给物理系学生上课的教师信息,输出教师的工号和姓名。
select distinct teacher.tno,tname
from student,sc,course,dep,teacher
where sc.sno=student.sno and dep.dno =student.dno and sc.cno = course.cno and dname in('物理系') and grade is null;

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值