--创建表students
create table students(
studentsid number(10) primary key,
sname varchar2(10) not null,
sex varchar2(4),
grade number(3),
classid number(10),foreign key (classid) references class(classid)
);
--重命名表students中字段studentsid
alter table students rename column studentsid to studentid;
--在表students中添加字段
alter table students add
studentid number(8) primary key;
--在表students中删除字段studentsid
alter table students drop (studentsid);
--删除表students
drop table students;
--创建表class
create table class (
classid number(10),
cname varchar2(20)
)
--修改表class中字段classid为主键
alter table class modify (classid number(10) primary key);
alter table students modify (studentid number(10));
--创建视图ST
create view ST as
select sname,sex from students;
--删除视图ST
drop view ST;
--创建视图SDetail,students详细信息
create view SDetail as
select s.studentid,s.sname,s.sex,s.grade,c.cname from students s,class c;
--在表students中插入数据
insert into students(studentid,sname,sex,grade,classid) values(10001,'Jim','男',65,1001)
insert into class (classid,cname) values(1001,'信息与技术科学')
--删除表中数据
delete from students t where t.studentid=10001;
--更新表中id
update students set studentid=rownum+10000;
--插入新的id的数据,但只能一条条插入
insert into students (studentid,sname)
select distinct a.m,students.sname from students,(select max(studentid)+1 m from students) a
--插入数据,多条,推荐,可多次使用
declare
i number(2);
begin
for i in 1..8 loop
insert into students (studentid,sname)
select distinct a.m,students.sname from students,(select max(studentid)+1 m from students) a;
end loop;
end;
--插入studentid为random类型的。控制id五位数,只能插入一条,且插入的id格式不一致
insert into students (studentid,sname)
select substr(cast(dbms_random.value as varchar2(1000)),5,5),students.sname from dual,students;
select count(*) from students
练习:
--首字母最大,结果为Hello World
select initcap('hello world') from dual
---创建比赛记录表,mdate日期,rq胜负
create table Match(
mdate date,
rq varchar2(4)
);
--插入数据
insert into match values('2005-5-10','胜')
---------------------------------------------------------------------
--用于归纳不同分类时用,此例为归纳每个月胜或负的总数
--方法1:用case...when...then...else...语句
select mdate,sum(case when rq='胜' then 1 else 0 end) as 胜,sum(case when rq='负' then 1 else 0 end) as 负 from match
group by mdate
--方法2:用嵌套子查询+where
select a.mdate,a.胜,b.负 from
(select mdate,count(rq) 胜 from match where rq='胜' group by mdate) a,
(select mdate,count(rq) 负 from match where rq='负'group by mdate) b
where a.mdate=b.mdate
--方法3:用嵌套子查询+join on
select a.mdate,a.胜,b.负 from
(select mdate,count(rq) 胜 from match where rq='胜' group by mdate) a join
(select mdate,count(rq) 负 from match where rq='负'group by mdate) b
on a.mdate=b.mdate
-----------------------------------------------------------------------------------
--日期格式做对比,必须使用to_char进行转换,否则对比没有返回结果
select * from se_innerapplies t where to_char(t.operatedate,'yyyy-mm-dd')=to_char(sysdate,'yyyy-mm-dd')
-----查询
--结果显示为
create table Score(
scoreid varchar2(10) primary key,
studentid varchar2(10),
mark number(3),
subject varchar(20)
)
----查询每个学生每门课程是否及格。使用case...when...then...when...then...else...end
select d.studentid,(case when d.mark>=80 then '优秀' when d.mark>60 then '及格' else '不及格' end) , d.subject
from score d
---- 右连接
select d.studentid,d.subject from score d right join (select studentid,mark from score) f
on d.studentid=f.studentid
----查询每个学生有多少成绩记录
select d.studentid,count(*) as 总数 from score d group by d.studentid having count(*)>1