您的位置:首页 > 数据库 > Oracle

oracle的基本知识

2010-01-13 19:04 309 查看
练习:
drop table Employee;

create table Employee(
id number primary key,
uName varchar2(32) ,
dept_id int ,
salary number
);

insert into Employee (id ,uName, dept_id, salary ) values (1,'ZhangShan',null, 10000);
insert into Employee (id ,uName, dept_id, salary ) values (2,'LiShi',1, 8000);
insert into Employee (id ,uName, dept_id, salary ) values (3,'WangWu',2, 6000);
insert into Employee (id ,uName, dept_id, salary ) values (4,'ZhaoLiu',3, 4000);
insert into Employee (id ,uName, dept_id, salary ) values (5,'QianQi',4, 2000);

要求:
<1>使用自连接查出表中员工的姓名及其领导的姓名?
select a.ename,b.ename
from emp a inner join emp b
on a.mgr = b.empno ;

<2>使用外连接查出表中员工的姓名及其领导的姓名?
select a.ename,b.ename
from emp a inner join emp b
on a.mgr =b.empno(+) ;

<3> 用一条SQL语句实现
给 2 部门员工将工资降至原来 80%
给 4 部门员工将工资减至原来 70%
给 1 3 5 部门员工将工资涨至原来 150%

update emp set sal=decode(deptno,10,0.8*sal,20,0.7*sal,30,1.5*sal) ;

<4>利用SQL语句查出 Employee 表中最大的领导?
select ename from emp where mgr is null ;

行列转换问题
--建表
create table Student(
id number primary key,
name varchar2(32) not null,
course varchar2(32) not null,
score number not null
);
--模拟数据
insert into Student(id,name ,course, score ) values (1,'aaa','java',89);
insert into Student(id,name ,course, score ) values (2,'aaa','cpp',88);
insert into Student(id,name ,course, score ) values (3,'bbb','java',83);
insert into Student(id,name ,course, score ) values (4,'bbb','cpp',95);

转换前:
id name course score
1 aaa java 88
2 aaa cpp 99
3 bbb java 97
4 bbb cpp 79

转换后:
name java cpp
aaa 99 88
bbb 97 79

答案:

select name,sum(decode(course,'java', score)) "java",
sum(decode(course,'cpp', score)) "cpp"
from Student
group by name ;

一对一
Person
Card 身份证

create table person (
p_id number primary key,
name varchar(20),
age varchar(20));

--desc person;
create table card(
d_id number,
name varchar(20),
c_id references person(p_id) unique);

insert into person values(123,'bbb','35');

insert into person values(456,'bbb','35');
insert into card values(123,'bbb',123);
drop table card;
drop table person;

一对多
create table teacher(
t_id number primary key,
course varchar(20),
grades varchar(2));

create table student(
d_id number primary key,
name varchar(20),
score number,
s_id references teacher(t_id));

insert into teacher values(112,'eeee','2');
insert into teacher values(118,'dddd','3');
insert into student values(323,'fddfdf',112,112);

drop table student;
drop table teacher;

多对多

create table person(
p_id number primary key,
name varchar(20));

create table address(
a_id number primary key,
c_name varchar(20));

create table house(
h_id references address(a_id),
m_id references person(p_id),
primary key(h_id,m_id)
);

insert into person values(12,'aa');
insert into person values(13,'bb');
insert into address values(56,'cc');
insert into address values(78,'dd');
insert into house values(78,12);

drop table house;
drop table address;
drop table person;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: