Oracle(三)伪表与DML

 

复制代码
--sql  分类
DDL 数据定义语言
  create  alter drop  truncate
DML 数据操作语言
  insert delete update  select
TCL  事务控制语言
  commit rollback 
DCL 数据控制语言
grant revoke

--创建序列
create sequence sq_teacher_tno  --序列的名称
start with 10   --开始的位置
increment by 1   --递增的序列值
maxvalue 9999999999   --设置最大值
--是否循环  cycle  |  nocycle  
--是否保存到内存中   cache| nocache

--查询创建的序列信息
select * from user_sequences;
--查询序列的当前值
select SQ_TEACHER_TNO.Currval from dual;
--查询序列的下个值
select SQ_TEACHER_TNO.nextval from dual;


--使用序列新增数据
insert into teacher(tno,tname)  
values(SQ_TEACHER_TNO.Nextval,'小黑2')


--dual确实是一个表,只有一个字段
select * from dual;

select 99*99 from dual;
select sysdate from dual;
select to_char(sysdate,'yyyy-MM-dd hh:mi:ss') from dual;

--sql标准 规定!  select 语句中必须要有from ! 就是用dual来
--当作一个伪表!也就是查询的信息不存在任何一个表中的时候!
复制代码

 

复制代码
--导入sql  语句
  01.cmd 
  02.sqlplus 用户名/密码
  03.@ sql语句的地址
  
--01.查询老师的姓名和对应导师的姓名   自连接
select t1.tname as 老师姓名,t2.tname as 导师的姓名
from teacher t1,teacher t2
where t1.mgrno=t2.tno
--02. 查询老师姓名,部门名称和部门编号
select tname,dname,dept.deptno
from teacher,dept 
where teacher.deptno=dept.deptno
--03.查询 姓 王 的老师信息  _代表一个字符  %
select * from teacher
where tname like '王%'

select * from teacher
where tname like '王_'


--04.查询陈老师和王老师的薪水和姓名
select tname,sal from teacher
where tname like '陈%' or tname like '王%'

--05.给所有姓王的老师 增加薪水
update teacher set sal=sal+20000
where tname like '王%'

--06.删除所有的teacher表中的数据  ! 表结构还在
delete  from teacher

--07.删除表! 包含数据和表结构
drop table dept

--08.回忆外键约束  建立在从表中
alter table teacher add constraint fk_teacher_deptno
foreign key(deptno)
references dept(deptno)

--09.查询女姓老师的编号和姓名
select tno,tname from teacher
where gendar=''

--10.查询薪水在10K-20k之间的老师编号,姓名  薪水
select tno,tname,sal from teacher
where sal between 10000 and 20000

--11.查询职位是 讲师或者研发的老师姓名  按照薪水的降序排列
select tname,sal,job from teacher
where job in('讲师','研发')
order by sal desc

--12.查询部门所有数据的insert语句
select 'insert into dept values('||deptno||','''||dname||''','''||loc||''');'
from dept
复制代码
 
 
 
 
 
-- 创建stuinfo表
create  table  stuInfo(
stuNo  number(4)  not null,
stuName nvarchar2(20) not null,
stuAge number(3) not null
)

-- 新增测试数据
insert into stuInfo   values (1,'小黑1',10);
insert into stuInfo   values (2,'小黑2',20);
insert into stuInfo   values (3,'小黑3',30);
insert into stuInfo   values (4,'小黑4',40);
insert into stuInfo   values (5,'小黑1',50);
insert into stuInfo   values (6,'小黑1',60);



-- 选择无重复的行 名字重复 显示一条
select * from  stuInfo  for  update --  查询所有
select distinct stuName from  stuInfo
--按照姓名进行升序,如果姓名相同按照年龄的降序排列
select distinct stuName,stuAge from  stuInfo 
order by  stuName,stuAge desc

--使用列别名  如果有特殊字符 必须使用双引号
select stuName "姓 名",stuAge as "年龄"
from stuInfo

--利用现有的表创建新表

create  table student as  select stuName from stuInfo

-- 查看表中行数
select  count(*) from stuInfo
select  count(1) from stuInfo  -- 推荐使用


-- 取出不重复数据的记录
select  stuName,stuAge from  stuInfo
group by stuName,stuAge
having(count(stuName||stuAge)=1)


-- 删除stuName,stuAge列重复的行,保留一行

--  01. 查询到重复的记录 保留一条!
select MAX(rowid) from  stuInfo
group by stuName,stuAge
having(count(stuName||stuAge)>1)

-- 02.查询不重复的数据
select MAX(rowid) from  stuInfo
group by stuName,stuAge
having(count(stuName||stuAge)=1)


--  03.拼接 01  02

delete  from stuInfo
where  rowid not in
(
select MAX(rowid) from  stuInfo
group by stuName,stuAge
having(count(stuName||stuAge)>1)
union
select MAX(rowid) from  stuInfo
group by stuName,stuAge
having(count(stuName||stuAge)=1)
)


-- 事务控制    事务的特性 ACID  事务隔离级别 
insert into stuInfo values(100,'小白1',100);
insert into stuInfo values(200,'小白2',200);
savepoint haha;  --  设置回滚点
insert into stuInfo values(300,'小白3',300);
rollback to savepoint haha;  --回到 指定的回滚点
select * from  stuInfo;
rollback;
select * from  stuInfo;
posted @ 2018-03-19 08:08  ★半支烟`☆  阅读(169)  评论(0编辑  收藏  举报
··