java SSM第三章学习内容(oracle基本查询语句,系统函数,创建用户)

1.子查询(八大)


一.虚表查询
1.结构
2.数据挖掘
3.区别(别名可以不取,mysql要取)
4.特性:虚表
select * from #student    虚表,不是真实存在的

二.select 子查询
1.结构
sql版:
select((select count(1) from students),(select count(1) from teachers))
oracle版本:
select (select count(1) from students) ts,(select count(1) from teachers) xs from dual
2.用法:数据统计
3.区别:
4.特性:count(*) count(id) count(1) count('x')  后面两个效率高

三.where
一样


四.exiats
    (if exiats 如果存在)
    例:如果学生成绩低于60,编号=1的人减20
    if exiats(select * from students where score<60)
    update teachers set wage=wage-20 where id=1
    
    (not exiats优化)
    
五.in    正常参数是一列,多参数是六

    select * from teachers
    where wage in(select wage form teachers where id=1)

六.多行子查询
    多参数属于多行子查询
    select * from teachers
    where (wage,bonus) in(select wage,bonus form teachers where id=1)

七.ALL极限子查询
    ALL作用比后面的值要大
    select * from Teachers
    where wage>
    ALL(select avg(wage)
    from Teachers group by id)

八.ALL反极限子查询
    ALL作用比后面的值要大
    select * from Teachers
    where wage<
    ANY(select avg(wage)
    from Teachers group by id)

九.having子查询(工作中不用)

2.集合操作

一.并集操作
去重(效率低)
select name from students
union
select name form teachers;

不去重
select name from students
union all
select name form teachers;

二.交叉操作
select name from students
intersoct
select name form teachers;

三.差集操作
select name from students
minus
select name form teachers;



 

表连接

一.相等连接
    内连接
    sql
    select A.*,b.* from A inner join B on A.id=B.id;
    orcel
    select A.*,b.* from A,B where A.id=B.id;

    外连接
    sql
    select A.*,b.* from A left join B on A.id=B.id;
    
    orcel
    1.select A.*,b.* from A left  outer join B on A.id=B.id;
    2.select A.*,b.* from A , B where A.id(+)=B.id;

    全连接
    sqlserver(叫交叉连接)
    select A.*,b.* from A cross join B on A,id=B.id


    select A.*,b.* from A , B on A,id=B.id(+) union all select A.*,b.* from A , B on A,id(+)=B.id
    select A.*,b.* from A full outer join B WHERE A,id=B.id

二.不等连接
    第一份表连接第二份表的两列
    SELECT student_id, score, grade
  FROM Students_grade sg, Grades g
    WHERE sg.score BETWEEN g.low_score AND g.high_score;
     
三.自连接
    把一份表拆分两份并根据要求连接
    select s1.name,s2.name from students s1,students s2 where s2.mid=s1.id
    


 

    

    a number:=1;
begin
  case a
    when 0 then DBMS_OUTPUT.PUT_LINE('A=0');
    WHEN 1 then DBMS_OUTPUT.PuT_LINE('A=1');
    else DBMS_OUTPUT.PUT_LINE('都不是');
    END CASE;
END;




            exit when avgSal>3000;
            end loop;
        commit;
        DBMS_OUTPUT.PUT_LINE('cg');
end;

 

第八章 系统函数
8.1
    select abs(-9,2) from dual;        绝对值
        round            四舍五入
        ceil            取大于等于数值的最小整数;
        floor            取小于等于数值的最大整数
    
    select mod(5,2) from dual        求余

select (name||wage)from teachers;        两列进行拼接
select concat(name,wage) from xx;        两列进行拼接2

SELECT * from teachers t where t.name like  concat(concat('%','文'),'%');

select length(name) from teachers;        查看name列的长度
select substr(name,1,3) from xx;        截取字符1-3    
select trim(name) from xx;            去空格
select upper(wage) from teachers;        查看列总行数        
select replace(name,'杰','jay')from teachers;     把属性里面的杰字符替换成jay

日期
select sysdate from dual;            显示当前日期
select current_date from dual;            显示当前日期-小时
select current_timestamp from dual;        显示具体日期(年月日分秒==)

增加月
select add_months(sysdate,5) from dual;    在现在月份基础上增加五个月

求两年之间的值
SELECT MONTHS_BETWEEN('31-8月-2008', '31-1月-2008'),
  MONTHS_BETWEEN('31-1月-2008', '31-8月-2008') FROM dual;        

select next_day(sysdate,'星期三') from dual;    在现在日期基础上查找下一个星期

转换函数
to_char to_date to_number                转换字符或转换数字
select to_char(sysdate,'YYYY-MM-DD-HH24:MI:SS')FROM dual;    转换时间格式


第九章 创建用户
create user aa identified by sc;

创建表空间
craate tablespace tp22
    datefile    'd:\data\aa.dbf'            //如果data文件夹没有则有手动创建
    size 60m

enit;
create user aa2 indentified by sc2            //创建一个用户在sc2表空间中
default tablespace tp22                //删除表空间

alter  user aa2 indentified by sc2;            //修改密码
drop user aa2 cascade;                //删除用户

授权
第一种
grant dba to 用户            dba用户
grant resouce to 用户;        普通
grant connect to 用户        临时

第二种
grant create table to 用户;
grant create procedure to xx;
grant select any table to xx;
grant delete any table to xx;

第三
grant select on teachers to xx;

第四
grant select(name,wage) on teachars to xx;

revoke回收权限
revoke select on teachers from xx;
revoke delete any table from xx;
revoke dba from xx;

自增长
1.创建一张表
2.自增长 create sequence s1;
3.插入语句         insert into xx value(s1.nextval,属性,属性)

第二种方式  循环使用       从1开始,1-1000之间,是否循环 
create sequence s2
start with 1
minvalue 1
maxvalue 1000
cycle;        


自增长要自定义的名字如:accp1
插入语句         insert into xx value('accp'||s1.nextval,属性,属性)

序列---------思考

复制表
create table lxsl3
    as(select name from students where score<60
    union
    select name from teachers where wage<3000)
crate table a2
as select * from students

修改表
alter table tachers add(age number(6));
alter table teachers drop(name);
alter table teachers modify name varchar(10));

增加主键约束
alter table teachers add constraint s_pk primary key(id);

1.修改表名
rename teachers to teachers;
2.修改表里面的列名
alter table teachers rename name to name8;

修改同义词(分为公有和私有)
    把表名转换成te

    create or replace synonym te for teachers;

删除同义词 drop


第九章----查询
把重复去掉
select distinct xx from xx;

如果工资为空则显示0
select nv1(comm,0) from xx;

判断奖金不为空显示第二(奖金+工资),如果没奖金显示工资第三
select nv12(donus,donus+wage,wage) from xx;

知识点:默认有两张表  scott.emp         scott..dept

分页查询格式

2B数索引
树状结构
create index in_aa on teachers(wage);
列状结构
create bitmap index in_aa2 on teachers(name);
反向建索引(前面的名字一致,从后面还是方向)
create index in_aa on students(address) reverse;

查权限
select * from user_sys_privs;
select * from user_tab_columns where table_name='teachers';
select * from user_indexes where table_name='teachers';
select * from user_views where table_name='teachers';

猜你喜欢

转载自blog.csdn.net/JayVergil/article/details/81545720