--黑窗口输入 net start OracleServiceORCL 打开oracle数据库服务
--net stop OracleServiceORCL 关闭服务
--创建一个表
create table tab_test
(
t_id number(5) primary key,
t_name varchar2(100) not null,
t_sex varchar2(5),
constraint t_sex check(t_sex='男' or t_sex='女'),
t_cj number(20) not null,
t_date Date
)
--创建第二个表
create table tab_test1(
t1_id number(5) primary key,
t_id number(5),
constraint pk_test_id foreign key (t_id) references tab_test(t_id)
)
--添加第一个表的序列
create sequence tex_id
start with 1
increment by 1
--查询tab_test表的信息
select *from tab_test
--单行添加数据
insert into tab_test values(tex_id.nextval,'王青山','女',22,'14-4月-1997')
--多行添加数据
insert into tab_test
select 11,'顾坤伦','男','18-3月-1998' from dual union
select 12,'白银龙','男','18-3月-1998' from dual union
select 13,'冯荒芜','男','18-3月-1998' from dual
--新创一张表并复制已有表的所有信息
create table tab_test1 as select * from tab_test
--已有tab_test1把tab_test表的信息复制到tab_test表里面
insert into tab_test1 select from tab_test
select * from tab_test1
--新创一张表并复制已有表的所有信息(只能用于黑窗口)
select table tab_test1 from tab_test
--修改
update tab_test set t_name='徐兵兵',t_sex='女' where t_id=6
--删除 姓名:徐兵兵,并且 性别:女
delete from tab_test where t_name='徐兵兵' and t_sex='女'
--删除 姓名:徐兵兵 或者 性别:女
delete from tab_test where t_name='徐兵兵' or t_sex='女'
--truncate删除表的所有信息 truncate后面不能跟where条件
truncate table tab_test1
--单行子查询:
-- 查询成绩最高学生所有的信息
select * from tab_test where t_cj = (select max(t_cj)from tab_test)
-- 查询成绩最高学生指定的信息
select t_name,t_id from tab_test where t_cj = (select max(t_cj)from tab_test)
--多行子查询(在where语句中使用多行子查询时,必须使用多行运算符 in,not in,exists,not exists,all,anyb)
--查询成绩比白银龙成绩高的所有学生信息
select * from tab_test where t_cj>=all (select t_cj from tab_test where t_name='白银龙')
--查询成绩比白银龙成绩高的指定学生信息
select t_cj from tab_test where t_cj>=all (select t_cj from tab_test where t_name='白银龙')
--相关子查询
--查询所有成绩大于60学生的成绩
select * from tab_test a where 60 <(select t_cj from tab_test b where a.t_id=b.t_id)
--分页查询
--根据伪列rownum进行分页
select e.* from (select rownum r,t_id,t_name,t_sex,t_cj,t_date from tab_test) e where r>=1 and r<=6yy
--数据库自带表的查询
select * from emp
--1.要求查询工资最高员工的信息
select *from emp where sal =(select max(sal) from emp)
--2.要求查询工资高于部门:20所有员工的信息
select * from emp where sal>(select max(sal) from emp where deptno=30)
select * from emp where sal>all(select sal from emp where deptno=30)
--3.查询各部门工资最低的员工信息
select b.* from emp b join
(select deptno,min(sal) as minsal from emp group by deptno) a on a.deptno=b.deptno and b.sal=a.minsal
--匹配多行多列
select * from emp where (deptno,sal) in (select deptno,min(sal) as minsal from emp group by deptno)
--4.查询负责管理其他员工的管理员信息
select * from emp a where exists (select * from emp b where a.empno=b.mgr)
--
select 字段1,字段2 from 表1,表2 where 表1.字段 = 表2.字段 and 条件
select 字段1,字段2 from 表1 join 表2 on 表1.字段 = 表2.字段 where 条件
--Oracle 查询出来的数据取第一条
select * from (select * from <table> order by <key>) where rownum=1;
select * from (select * from <table> order by <key> desc) where rownum=1;
Oracle控制语句
最新推荐文章于 2022-05-14 09:36:42 发布
387

被折叠的 条评论
为什么被折叠?



