温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java37: 数据库(Oracle)

发布时间:2020-07-30 09:08:21 来源:网络 阅读:316 作者:浪漫的偷笑 栏目:数据库

DB2

Oracle

Infomix

MySQL

Sybase

SQLServer



1 什么是数据库

    数据需要持久保存

    (磁盘文件)

 数据库(数据库管理系统)是个软件

    安全,可靠,高效

 

 数据库服务器,数据中心


 关系型数据库

    用表来存储数据的数据库

 SQL 通用的关系型数据库的操作语言

 不同数据库之间SQL有一定的差异


 NoSQL Not only SQL

    芒果DB......

   

T-SQL

PLSQL


  SQL 中的分类

    DDL(Data Definition Language) 数据定义语言

        create;drop...

    DML(Data Maniplation Language)数据操作语言

        insert;delete;update...

    TCL(Transaction Control Language)事物控制语言

        begin transaction;commit;rollback....

    DQL(Data Query Language) 数据查询语言

        select...

    DCL(Data Control Language)数据控制语言

        grant;revoke...

 


 安装Oracle(DBA/OP)

    创建库(DBA/OP) 

    创建用户(DBA/OP)

     登陆数据库(RD)

      访问数据库(RD)

文档

表名,

字段名 ,类型 ,说明



create 语句创建表, 指定表明 表有几列,列名,每个列的类型

类型:

数字 number(m) 不能超过m

    number(m,n) 一共m位,小数占n位

字符串 varchar2(m) 不能超过m

    (可变长的字符串2版本)

日期 date

    有默认格式 可以修改

    在插入日期时必须要符合这个格式

    对于日期可以使用to_date函数(但是保存在表中的数据还是系统设置的格式)

    第一个参数自己定义的格式

    to_date('2016-10-10','yyyy-mm-dd')

    第二个参数指明自定义格式

    

CREATE TABLE table_name 
    (column_name column_type);

//创建表
create table emp(id number(10),
          name varchar2(50),
          hire_date date,
          salary number(8,2));
//删除表
DROP TABLE tablename;                
drop table emp;
//查看表的结构(只能看表的结构,不能看数据)
DESC tablename;
desc emp

//给表添加行
插入式可以指定为null
没有插入的列就为null 除非有默认值
INSERT INTO tablename(
    column_name,column_name,column_name)
    values
    (xxx,xxx,xxx,xxx);

insert into emp(id,name,hire_date,salary)values(101,'lmdtx',to_date('2016-10-10','yyyy-mm-dd'),123123.12);
OR
insert into emp values (101,'lmdtx',to_date('2016-10-10','yyyy-mm-dd'),123123.12);

//修改表中的某些行 where 是根据具体的条件来
UPDATE table_name SET field1=new-value1;
UPDATE table_name SET field1=new-value1, field2=new-value2[WHERE Clause]
update emp set salary=200 where id=101;

删除表中行
DELETE FROM tablename;
DELETE FROM table_name [WHERE Clause];
delete from emp where id=101;


SELECT * FROM tablename;
SELECT * FROM tablename [WHERE Clause];
select * from emp;
select * from emp where salary>100;
select name,salary from emp where salary>100;


oracle 字符操作:

    varchar和varchar2 低版本还有区别高版本一样的

    char 和 varchar2的区别:

        char 是定长  char(20)  无论用多少都是20空间

                =》指的是存储空间

        varchar2是可变长 varchar2(20) 按照实际的给空间

                默认是存储20个英文字符的空间

    length()


    varchar2 中文:

        一个中文占用几个字符要看你使用的编码 

        oracle 不是用的Unicode

        在设计表的时候要注意  一般是中文长度 *3        

查看oracle 的字符编码 userenv('language')

        

select userenv('language') from dual;

dual表 无实际意义,就是伪表给我们用一些函数使用的



    nvarchar2 类型  是几个字符就是几个字符 和编码无关


char 和varchar2的最大长度

char 2000个

varchar2 4000个

 


字符串的连接 ||  或者用 conncat(m,n)函数

'aa'||'bb'

create table class(
    xing nvarchar2(50),
    ming nvarchar2(50)
);

insert into class values('张','三');
insert into class vlaues('steve','steve')
select xing||ming from class;
select xing||'.'||ming from class;
select concat(xing,ming) from class;
select concat(concat(xing,'.'), ming) from class;



trim 去掉前后空格trim(String);

ltrim 左边空格

rtrim 右边空格

lpad 在左边填充字符到指定长度 lpad(String,50) 默认是空格

rpad 在右边填充字符到指定长度rpad(String,50,'*')

lower 小写lower(String)

upper 大写

initcap 首字母大写

length 求长度

substr 截取字符串 substr(String,起始位置)  取到结尾

            substr(String,起始位置,取多少个)

            substr(String,-20) 从后往前20个

    从1 开始

instr 在一个字符串中查找字符串  

返回第一次找到的位置    instr(String1,String2) 在String1 中查找String2

从1开始         instr(String1,String2,5) 在String1 中查找String2 从第5个开始找 

           instr(String1,String,5,2)在String1 中查找String2从第5个开始找到的第2个


create table emp(id number(10),
          name varchar2(50),
          hire_date date,
          salary number(8,2));
desc emp;

insert into emp(id,name,hire_date,salary)values(101,'lmdtx',to_date('2016-10-10','yyyy-mm-dd'),123123.12);

select *from emp;

select * from emp where id=102;

select name,salary from emp where salary>1000;

update emp set salary=100 where id=102;

update emp set salary=3000 where id=1011;

update emp set salary=99999 where id>2000;

update emp set salary=99999999999 where id=101;

delete  from emp where id = 010;

delete  from emp where id = 101;

drop table emp;

create table INFO_COST(
  ID number(11) primary key not null,
  COST_DESC VARCHAR2(200),
  BASE_DURATION number(11),
  BASE_COSR number(11,2),
  UNIT_COST number(11,2));
  
insert into INFO_COST(ID,COST_DESC,base_duration,base_cosr,unit_cost
)values(1,'包20小时',20,2.45,0.30);
  
insert into INFO_COST(ID,COST_DESC,base_duration,base_cosr,unit_cost
)values(2,'包40小时',40,3.45,0.30);
  
insert into INFO_COST(ID,COST_DESC,base_duration,base_cosr,unit_cost
)values(3,'包100小时',100,4.45,0.30);

insert into INFO_COST(ID,COST_DESC,base_duration,base_cosr,unit_cost
)values(4,'包200小时',200,5.45,0.30);
  
insert into INFO_COST(ID,COST_DESC,base_duration,base_cosr,unit_cost
)values(5,'普通资费',null,null,0.20);
  
insert into INFO_COST(ID,COST_DESC,base_duration,base_cosr,unit_cost
)values(6,'包月',null,10,null);
  
update INFO_COST set unit_cost= unit_cost+(unit_cost*10) where id<5;
  
delet from INFO_COST WHERE id=5;
  
select COST_DESC,unit_cost from INFO_COST;


create table foo_1(c1 char(5),c2 varchar2(5));
insert into foo_1 values('abc','abc');
select length(c1),length(c2) from foo_1;

create table foo_2(c1 varchar(6));
drop table foo_2;
insert into foo_2 values('你好');
select * from foo_2;
select userenv('language') from dual;
desc dual;


create table foo_3(c1 nvarchar2(2));
insert into foo_3 values('你好');

create table class(
    xing nvarchar2(50),
    ming nvarchar2(50)
);

insert into class values('张','三');
select xing||ming from class;
insert into class values('steve','steve')
select xing||ming from class;
select xing||'.'||ming from class;
select concat(xing,ming) from class;
select concat(concat(xing,'.'), ming) from class;

create table foo_4(c1 varchar(500));
drop table foo_4;
insert into foo_4 values('     the fortification in your pole. It is like a peek your wallet as the thief, 
when you are thinking how to spend several hard-won lepta, when you are wondering whether new money, it has laid background.
    Because of you, then at the heart of the most lax, alert, and most low     of    ');
    
select trim(c1) from foo_4;
select ltrim(c1) from foo_4;
select rtrim(c1) from foo_4;
select lower(c1) from foo_4;
select upper(c1) from foo_4;
select initcap(c1) from foo_4;
select lpad(c1,600) from foo_4;
select lpad(c1,600,'@') from foo_4;
select substr(c1,100) from foo_4;
select substr(c1,100,120) from foo_4;
select substr(c1,-20) from foo_4;
select instr(c1,'of') from foo_4;
select instr(c1,'of',224) from foo_4;
select instr(c1,'of',224,2) from foo_4;



Oracle 日期操作

1日期类型

date 年月日时分秒(基本都是人输入)

timestamp 年月日时分秒(秒带小数点)(系统来 时间戳)

sysdate 系统时间

systimestamp 系统时间戳

select sysdate from dual;
select systimestamp from dual;


to_date 把一个字符串转换为日期

select todate('201610-10','yyyy-mm-dd') from dual;

to_char 把时间转换为指定的格式

select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual;
create table foo_5(d1 date);
insert into foo_5 values(to_date('2016/10/10 09:00:00','yyyy/mm/dd hh:mi:ss'));
insert into foo_5 values(sysdate);
select to_char(d1,'yyyy~mm~dd hh34:mi:ss') from foo_5;
select to_char(d1,'yyyy~mm~dd hh34:mi:ss') from foo_5 where d1>sysdate;

last_day 计算所在月的最后一天

months_between 一个日期和另一个日期差几个月

least 一个日期和给定日期那一个时间更早

返回小的

greatest一个日期和给定日期那一个时间更近

返回大的

create table foo_6(birthday date);
insert into foo_6 values(to_date('1990/09/09','yyyy/mm/hh'));
insert into foo_6 values(to_date('1890/09/09','yyyy/mm/hh'));
insert into foo_6 values(to_date('2016/10/10','yyyy/mm/hh'));
select to_char(last_day(to_date('2016/10/10','yyyy/mm/dd')),'dd') from dual;
select months_between(sysdate, birthday)/12 from foo_6;
select to_char(least(birthday,to_date('2016/08/08','yyyy-mm-dd')),'yyyy-mm-dd') from foo_6;
select least (1, 3 ) from dual 
select to_char(greatest(birthday,to_date('2016/08/08','yyyy-mm-dd')),'yyyy-mm-dd') from foo_6;
select greatest (1, 3 ) from dual


round  对时分秒进行舍入 24小时制 12点后 算明天

trunc    对时分秒直接去掉

select to_char(round(sysdate),'yyyy/mm/dd') from dual;
select to_char(trunc(sysdate),'yyyy/mm/dd') from dual;


用于从一个date或者interval类型中截取到特定的部分 

extract(year from 日期)

extract(MONTH from 日期) 

extract(DAY from 日期)

extract(HOUR from 日期)

extract(MINUTE from 日期)

extract(SECOND from 日期)

select extract(year from sysdate) from dual;
select extract(month from sysdate) from dual;
select extract(day from sysdate) from dual;
select extract(hour from systimestamp) from dual;
select extract(minute from systimestamp) from dual;
select extract(second from systimestamp) from dual;



create 

drop (很少用,必须少用)

alter (很少用,必须少用,在 create 的时候要考虑好)

create table foo_11(
    n1 number(20);
    c1 varchar(50);
    
);
insert into foo_11 values(1,'abc');
insert into foo_11 values(null,'bcd');
insert into foo_11 values(3,null);
select * from foo_11 where c1 is null;


null 相关:


nvl

nvl(arg1,arg2)

如果arg1 为null 返回arg2

如果arg1不为null 返回arg1


nvl2(arg1,arg2,arg3)

如果arg1为null 返回arg3;

如果arg1不为null 返回arg2;

select nvl(c1,'空')from foo_11;
select nvl2(c1,'非空','空') from foo_11;


not null 约束

 指定某些列不能为null

create table foo_12(
    n1 number(20) not null;
    c1 varchar(20) not null;
);


number的舍入(先舍入,在看长度  )

create  table  foo_13(
    n1 number(5),
    n2 number(5,2), //对多的小数位直接舍弃,整数位多报错
    n3 number(5,-1),//  倒数 变 0  抹掉零头..先舍入,在看长度  最后一个0不算
);


主键 

数据的唯一性


1创建主键

主键也是列(多列,联合主键)

一般没有业务含义

唯一标识数据表中的某一行

必须有主键

类型组好是number

主键不为null

主键不能重复

constraint 主键约束名 primary key(主键列)


create table stu(
  stu_id number(11),
  stu_no number(8),
  stu_name varchar2(50),
  constraint stu_pk primary key(stu_id)
);

insert into stu values(123,100001,'张三');



修改表


删除:

drop 删除表

drop table stu;

delete删除数据 可以恢复,速度慢

delete table stu;
delete table str where stu_id=123;

truncate删除表内容不能恢复,速度快

truncate table stu;



alter


添加列

如何增加not null列

    增加一个可以为空的列

    update设置值

    将该列改为not null

设置为主键

     更麻烦

修改列的属性

删除列


create table foo_22
  ( name varchar2(50)
  );
insert into foo_22 values('张三');
insert into foo_22 values('李四');
insert into foo_22 values('王五');
insert into foo_22 values('赵六');
insert into foo_22 values('齐七');

alter table foo_22 add
(
  salary number(8,2)
);

desc foo_22;

select * from foo_22;

alter table foo_22 modify(
  name varchar2(55) not null
);

desc foo_22;

alter table foo_22 add(
  s_id number(8)
);

desc foo_22;

update foo_22 set s_id=3 where name='张三';
update foo_22 set s_id=4 where name='李四';
update foo_22 set s_id=5 where name='王五';
update foo_22 set s_id=6 where name='赵六';
update foo_22 set s_id=7 where name='齐七';

select * from foo_22;

alter table foo_22 drop column s_id;

desc foo_22;
select * from foo_22;


拷贝数据

但是不能复制约束

create table emp2 
as select empno, ename, job,sal
from emp;

create table emp2 
as select empno, ename, job,sal
from emp where ename='lmdtx';

拷贝表结构(在where 子句中 加上一个不成立的条件)但是不能复制约束

create table emp3
as select * from emp
where 1=2;




>= 

<= 

<> 不等于

AND 

OR

select * from emp2 where ename=''

a' or 'b'='b
注入***
select * from emp2 where ename='a' OR 'b'='b';

like 模糊查询

 % 任意个字符

    M% 开头是M的

    %M% 有M的

 _ 一个字符

select * from emp2 where ename like 'M%';
select * from emp2 where ename like '%A%';
select * from emp2 where ename like '_A%';



where 子句中的between ** and **


select salary from emp2 where salary between 1500 and 3000;


where 子句 中的 in 和 not in

select salary from emp2 where salary in (100,200,300);
 
select * from emp2 where ename='WARD' or ename='JAMES';

select * from emp2 where ename in ('WARD','JAMES');

select * from emp2 where ename not in ('WARD','JAMES');


where 子句判断空值 is null 和 is not null

select * from emp2 where ename is null;
select * from emp2 where ename is not null;


where 中 子查询

select * from emp2 where SAL=(select salary from emp2 where ename='WARD');
select *  from emp2 where ename in (select ename from emp2 where sal<1000 );


where 中 可以使用函数

select * from emp2 where length(ename)>4;


select 列的别名

    select 查询的结果可以看成是一个逻辑上的一张表

select ename name from emp2;
        列名
             别名

select ename 姓名 from emp2;


去重 distinct

select distinct salary from emp2;


select 中可以使用函数

select ename||job from emp2;
select  ename||'='||job namejob from emp2;//在加上一个别名




聚合函数(聚集函数)


count// null的不计数  统计 主键可以查有多行

select count(ename) from emp2;

min

select max(salary) from emp2;

max

select max(salary) from emp2;

sum

select sum(salary) from emp2;

avg

select avg(salary) from emp2;



排序

order by 对查询的结果 可以根据某个或者几个列进行排序

    默认 asc 升序  

       desc 降序

  order by 后面也可以使用函数

    多个列

        先按照一个排序 再按照另一个拍 用

select ename,salary from emp2 where salary>1200 order by salary;

select ename,salary from emp2 where salary>1500 order by salary desc;

select ename,salary from emp2 where salary>1500 order by length(ename);

select ename,salary from emp2 orderby salary,length(ename);


















































向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI