Oracle 9i基本SQL操作

Oracle 9i基本SQL操作


---------重命名表------
Rename HR_BASE_CATEGORY To HR_TEMP_TABLE;

----------把一个表的数据拷到另一个相同结构的表
Insert Into HR_BASE_CATEGORY Select
 CATEGORY_ID,
 CATEGORY_NAME,
 CATEGORY_ALIAS,
 CATEGORY_DESC,
 MODULE_ID,
 MODULE_NAME,
 SORT_ORDER
From HR_TEMP_TABLE;

---------创建表----------

-- 创建班级表

Create table class
(
 class_id  varchar2(10) not null primary key, --主键
 class_name varchar2(10) not null,
 class_create date default sysdate,
 class_number number(2,0),
 class_remark  varchar2(50)
)

-- 删除表class

drop table class cascade constraint;


-- 创建学生表
create table student
(
 stu_id varchar2(10) not null primary key,
 stu_name varchar2(8),
 stu_gender integer default 0,
 stu_birthday date,
 stu_from varchar2(20),
 stu_tel varchar2(14),
 stu_dorm varchar2(8),
 stu_class_id varchar2(10)
)


--创建课程表
create table course
(
course_id varchar2(6) not null primary key,
course_name varchar2(16),
course_book varchar2(30)
)

drop table course cascade constraint;


--创建成绩表
create table score
(
score_stu_id varchar2(10) not null ,
score_course_id varchar2(6) not null,
score_pingshi  number(5,2),
score_final number(5,2),
score_Total number(5,2) --总成绩
)


--------修改表---------

alter table student add  email varchar2(30);         ------在学生表中增加字段:email地址;-------

alter table course modify course_book varchar2(36);  ------增加课程表字段"教科书"的长度;-------

alter table score modify score_Total default 0;    ------设置成绩表的总评成绩字段缺省值为0;-----


--------创建视图----------
如果你在创建视图时使用了DISTINCT语句,那么你就不能插入或更新这个视图中的记录
create view student_view(stu_name,course_name,score_info)
as
select student.STU_NAME , course.COURSE_NAME , score.score_Total
from student,score,course
where student.STU_ID = score.SCORE_STU_ID
    and course.COURSE_ID = score.SCORE_COURSE_ID;


--------创建索引----------

create index score_index on score(score_stu_id,score_course_id);

--------约束条件的创建-------


--------创建主键---------

--如果在表创建时已创建主键,可不再单独创建主键

alter table class add constraint pk_class_id primary key;

alter table student add constraint pk_stu_03 primary key;

alter table score add constraint pk_score_03 primary key;


----------创建外键---------

alter table student add constraint fk_class foreign key (stu_class_id) references class(class_id);

alter table score add constraint fk_stu_id foreign key(score_stu_id) references student(stu_id);

alter table score add constraint fk_course_id foreign key(score_course_id) references course(course_id);


----------创建check约束---------

alter table student add constraint ck_stu_gender check( stu_gender in (0,1) );


----------插入数据 和 事务控制语句------------

insert into class values(031,jsj,to_date(2003-09-01,yyyy-mm-dd,30,good);
insert into class values(032,xg1,to_date(2003-09-01,yyyymm-dd,26,good);

insert into student
values(001,xinhe,0,to_date(1977-09-22,yyyy-mm-dd),hunan,135,e208,031wxinhe2004@126.com);

insert into student values(002,huarong,0,to_date(1977-09-22,yyyy-mm-dd),hunan,135,e209,032,wxin004@126.com);

insert into course values(001,c++,jixietushu);
insert into course values(002,shujuku,qinghuatushu);

insert into score values(001,001,90.00,90.00,90.00);
insert into score values(002,001,80.00,80.00,80.00);

commit;


----------修改数据 和 事务控制语句------------

update course set coursename=cyuyan where coursename=c++;
update student set name=wangxinhe where name=xinhe;
update class   set appendxi=average where classid=032;
update score set totalscore=95.0 where totalscore=90.00;

commit;

----------修改数据 和 事务控制语句------------
delete course where coursename=c++;
rollback;

delete student;

delete score;

delete class;

commit;


------------多表连接查询语句-----------

1)查询学生名称、编号、性别、生日(按yyyy-mm-dd显示)、所在班级的名称,其中,0显示为“男”,1显示为“女”(decode函数);

  select a.stu_name ,a.stu_id , decode(a.stu_gender,0,男,1,女) 性别,
       to_char(a.stu_birthday,yyyy-mm-dd) 出生日期, b.class_name
  from student a,class b
  where a.stu_class_id=b.class_id;


2)查询学生的各门功课的平时成绩、期末成绩和总评成绩;

   select a.stu_id,a.stu_name, b.course_name, c.score_pingshi 平时,c.score_final 期末,c.score_Total 总成绩
   from student a,course b, score c
   where  a.stu_id = c.score_stu_id
       and b.course_id = c.score_course_id


3)查询姓名XX所在班级所有学生信息(嵌套查询);

   select * from student where stu_class_id =
       (select stu_class_id from student where stu_name=xinhe);


4)查询课程编号为“xxx”和“xxx”的学生成绩信息(集合查询);

   select stu_id,score_Total  from score  where score_course_id = 001 
   union
   select stu_id,score_Total  from score  where score_course_id = 002;


5) 查询各班每门功课的最好成绩;
   
   select A.stu_class_id, B.score_course_id, max(score_Total)
   from student A,score B
   where A.stu_id=B.score_stu_id
   group by A.stu_class_id, B.score_course_id

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
oracle 9i所有版本最新下载链接 直接迅雷下载 Oracle9i Database Release 2 Enterprise/Standard/Personal Edition for Windows NT/2000/XP http://download.oracle.com/otn/nt/oracle9i/9201/92010NT_Disk1.zip http://download.oracle.com/otn/nt/oracle9i/9201/92010NT_Disk2.zip http://download.oracle.com/otn/nt/oracle9i/9201/92010NT_Disk3.zip Oracle9i Database Release 2 Enterprise/Standard/Personal/Client Edition for Windows XP 2003/Windows Server 2003 (64-bit) http://download.oracle.com/otn/nt/oracle9i/9202/92021Win64_Disk1.zip http://download.oracle.com/otn/nt/oracle9i/9202/92021Win64_Disk2.zip Oracle9i Database Release 2 Enterprise/Standard Edition for Intel Linux http://download.oracle.com/otn/linux/oracle9i/9204/ship_9204_linux_disk1.cpio.gz http://download.oracle.com/otn/linux/oracle9i/9204/ship_9204_linux_disk2.cpio.gz http://download.oracle.com/otn/linux/oracle9i/9204/ship_9204_linux_disk3.cpio.gz Oracle9i Database Release 2 (9.2.0.4) Enterprise/Standard Edition for Linux x86-64 http://download.oracle.com/otn/linux/oracle9i/9204/amd64_db_9204_Disk1.cpio.gz http://download.oracle.com/otn/linux/oracle9i/9204/amd64_db_9204_Disk2.cpio.gz http://download.oracle.com/otn/linux/oracle9i/9204/amd64_db_9204_Disk3.cpio.gz Oracle9i Database Release 2 Enterprise/Standard Edition for AIX – Based 4.3.3 Systems (64-bit) http://download.oracle.com/otn/aix/oracle9i/9201/server_9201_AIX64_Disk1.cpio.gz http://download.oracle.com/otn/aix/oracle9i/9201/server_9201_AIX64_Disk2.cpio.gz http://download.oracle.com/otn/aix/oracle9i/9201/server_9201_AIX64_Disk3.cpio.gz http://download.oracle.com/otn/aix/oracle9i/9201/server_9201_AIX64_Disk4.cpio.gz Oracle9i Database Release 2 Enterprise/Standard Edition for AIX- Based 5L Systems http://download.oracle.com/otn/aix/oracle9i/9201/A99331-01.zip http://download.oracle.com/otn/aix/oracle9i/9201/A99331-02.zip http://download.oracle.com/otn/aix/oracle9i/9201/A99331-03.zip http://download.oracle.com/otn/aix/oracle9i/9201/A99331-04.zip

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值