关于oracle数据库的小知识
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于oracle数据库的小知识相关的知识,希望对你有一定的参考价值。
--查询语句:select 列名/通配符/列别名/表达式 from 表名 (修饰/限制语句)
select * from tab;
select tname from tab;--指定的列
select tabtype from tab;
select tname "T-M" from tab;--新建列别名
select 3+4 from dual;--虚表(只有一行一列)
select 3+4 as "he" from dual;
--employees:雇员表
desc employees;--desc:显示一个表的结构(列名,烈的约束,列的数据类型)
select * from employees;
--null:1.参与运算,整体为null
--nvl函数:nvl (列名,值)
select (1+commission_pct)*salary*12 as "年薪" from employees;
--有null值则用0来代替null运算
select (1+nvl(commission_pct,0))*salary*12 as "年薪" from employees;
select commission_pct,(1+nvl(commission_pct,0))*salary*12 as "年薪" from EMPLOYEES;
--nvl2函数:nvl2 (列名,值1,值2)为null时,显示值2,不为null时,显示值1
select commission_pct,nvl2(commission_pct,‘有提成‘,‘没提成‘) from EMPLOYEES;
--集合内去除重复的行:DISTINCT
SELECT DISTINCT first_name FROM EMPLOYEES;
--双行查询(列1列2不能重复)
--order by(排序,默认从小到大ASC(DESC相反))(排序的列名可以不再语句中出现)
select employee_id,salary from employees;
select employee_id,last_name,salary from employees order by salary;
select employee_id,last_name,salary from employees order by salary desc;
select employee_id,last_name,salary as "sal" from employees order by "sal" desc;
--各列在select语句中的顺序号,从1开始
select employee_id,last_name,salary from employees order by 3 desc;
--条件子句:查找特定id员工的信息
--where 条件
select * from employees where employee_id = 100;
-- 不等于<>
select * from employees where employee_id <> 100;
--between 值1 and 值2:获取两个值之间的数据.
select * from employees where employee_id between 100 and 107;
--in(值1,值2,值3...):获取一组特定的数据/满足任意一个即可
select * from employees where employee_id in( 100,105,200);
--is NULL:判断是否为空(is Null/is not Null)
select * from employees where COMMISSION_PCT is Null;
--not in():特定的值不会选取
select * from employees where employee_id not in (100,200,107);
--逻辑运算符 and(需全部满足) or(满足一个就行) not(选中的不能选取)运算时加括号
select * from employees where employee_id = 100 and employee_id = 200;
select * from employees where employee_id = 100 or employee_id = 200;
select * from employees where employee_id not in (200,100);
--模糊查询:%代替一个或者多个字符(区分大小写)
--是否包含
select last_name from employees where last_name like ‘%m%‘;
--排在首位的
select last_name from employees where last_name like ‘M%‘;
--查找排在第二位的
select last_name from employees where last_name like ‘_m%‘;
--查找排在倒数第二位的
select last_name from employees where last_name like ‘m_%‘;
--替代变量:&(筛选)代码的重用性
select last_name,salary from employees where salary >=‘&薪水‘;
--字符串的连接||
select ‘a‘||‘b‘ from dual;
select first_name||‘ ‘||last_name from EMPLOYEES;
select first_name as "姓",last_name as "名",first_name||‘ ‘||last_name as "姓名" from EMPLOYEES;
--脚本运行(.sql):@+文件所在的路径
@e:\\a.sql;
******************************************************
--单行函数:作用于一行记录
--length:求字符长度
select length(‘就让往事皆随风c‘)as "长度" from dual;
--统计员工表中,员工的姓名长度,改为名长
--lower upper initcap
--大写转小写
select lower(‘ADCDEFG二连‘) from dual;
--小写转大写
select upper(‘abcdefg二连‘) from dual;
--initcap:首字母大写,其余小写(汉字忽略不计)
select initcap(‘喊cajppjPPdsIHOasd‘) from dual;
--concat(‘‘,‘‘):字符连接
select ‘a‘ || ‘b‘ from dual;
select concat(‘aa‘,‘汉字‘) from dual;
--substr(‘‘,start,end):字符串截取(‘字符串‘,‘截取长度‘,‘截取长度‘)(截取长度:正数从左往右,负数则相反)
select SUBSTR(‘ABCDEFG‘,2,3) from dual;
--instr(‘hellowword‘,‘‘w)(字符串内查找出现的位置)
select instr(‘就让往事皆随风c‘,‘往‘)from dual;
--R/LPAD(salary,10,‘*‘)(左右填充)
select lpad(salary,10,‘*‘)from employee;
--replace(‘jack and jue‘,‘j‘,‘bl‘)(替代)
select replace (‘就让往事皆随风c‘,‘往‘,‘??‘)from dual;
--trim(‘H‘from‘Helloword‘)查找给定字符,并截掉(只能截取开头和结尾的字符)
select TRIM(‘风‘ from ‘就让往事皆随风‘) from dual;
--左截
select LTRIM(‘xyxyxyxyxyLVxyxyxyxyxy‘,‘xy‘) from dual;
--右截
select RTRIM(‘xyxyxyxyxyLVxyxyxyxyxy‘,‘xy‘) from dual;
***************************************************************
--数字函数
--舍入和截断,取余
--round:四舍五入(正数,取小数位.负数,取整数位)
--15.2
select round(15.193,1)from dual;
--15
select round(15.193,0)from dual;
--15
select round(15.193)from dual;
--20
--trunc:截取
--15.1
select trunc(15.193,1)from dual;
--15
select trunc(15.193,0)from dual;
--15
select trunc(15.193)from dual;
--10
select trunc(15.193,-1)from dual;
--mod:取余数
--1
select mod(16,3)from dual;
******************************************************************
--日期函数 sysdate
--获取当前时间
select sysdate from dual;
--add_months:在给定的日月数上加上给定数字
select add_months(sysdate,6)from dual;
--next_day:计算下个星期四是几月几号
select next_day(sysdate,‘星期四‘)from dual;
--last_day:当前月的最后一天
select last_day(sysdate)from dual;
--计算日期格式的数据(数字代表天数)
select sysdate+1 from dual;
--字符串表示日期
select add_months(‘18-6月-12‘,6)+20 from dual;
--months_between(date1,date2):两个日期之间的月数差(前减后)
select months_between(sysdate,‘26-4月-16‘) from dual;
--round:日期的舍入
--month:看天数 大于15,月数加一
select round(TO_DATE(‘26-6月-17‘),‘month‘)from dual;
--year:看月数 大于6,年数加一
select round(TO_DATE,‘year‘)from dual;
--day:按星期/dd:按小时/hh:看分钟
select round(sysdate,(‘16-6月-17 12:00:59‘,‘dd-Month-yy hh24:mi:ss‘),‘dd‘)from dual;
--trunc:日期截取(不考虑天数/月数)
select trunc(sysdate,‘month‘)from dual;
select trunc(sysdate,‘year‘)from dual;
*************************************************************************
--转换函数
--数据类型的隐式转换
--字符串转换为数值类型
select salary from employees where salary >‘12000‘;
--字符串转换为日期类型
select hire_date from employees where hire_date>‘01-5月-05‘;
--to_char:数字类型转换为指定格式的字符串
select salary from employees ;
--0:补位
select to_char(salary,‘L00,000‘) from employees ;
select to_number(‘2‘) from dual ;
--to_char:日期类型转换为指定格式的字符串
select last_name,hire_date from employees ;
select last_name,to_char(hire_date,‘yyyy-MM-dd hh:mi:ss‘) from employees ;
--to_date:日期类型转换为指定格式的字符串(hh24:24小时制)(month:6月/MM:06)
select to_date (‘2017-6月-29 19:28:28‘,‘yyyy-month-dd hh24:mi:ss‘) from dual;
***************************************************************************************
--多表查询
desc employees;
desc departments;
desc locations;
desc ;
select last_name from employees;
select department_name from departments;
--笛卡尔积
select last_name,department_name from employees cross join departments;
--等价连接(可以用新名去.列名/一旦修改新名后,必须使用新名)
select last_name,department_name from employees,departments where employees.EMPLOYEE_ID = departments.DEPARTMENT_ID;
select e.last_name,d.department_name from employees e,departments d where e.EMPLOYEE_ID = d.DEPARTMENT_ID;
--natural join:自然连接(所有相似的部分)
select e.last_name,d.department_name from employees e natural join departments d;
--创建表
CREATE TABLE "GRADES"
( "LEV" VARCHAR2(10),
"LOW" NUMBER(6,0),
"HIGHT" NUMBER(6,0)
);
INSERT INTO GRADES VALUES(‘A‘,1000,2999);
INSERT INTO GRADES VALUES(‘B‘,3000,5999);
INSERT INTO GRADES VALUES(‘C‘,6000,9999);
INSERT INTO GRADES VALUES(‘D‘,10000,14999);
INSERT INTO GRADES VALUES(‘E‘,15000,24999);
INSERT INTO GRADES VALUES(‘F‘,25000,40000);
commit;
desc grades;
select * from grades;
--不等价连接(数值分区)
select e.last_name,e.salary,f.lev from EMPLOYEES e,GRADES f where e.salary BETWEEN f.low and f.hight;
--表的自连接
--(普通情况)
select manager_id,last_name from EMPLOYEES where LAST_NAME = ‘Jones‘;
select last_name from EMPLOYEES where EMPLOYEE_ID = 123;
--(自连接)
select e1.last_name,e2.last_name from EMPLOYEES e1,EMPLOYEES e2 where e1.MANAGER_ID = e2.EMPLOYEE_ID and e1.LAST_NAME = ‘Jones‘;
--上午↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
--左外链接(+)号的位置在等号右边
select e.last_name,d.DEPARTMENT_ID, d.DEPARTMENT_NAME,e.SALARY from employees e,departments d where e.DEPARTMENT_ID = d.DEPARTMENT_ID(+);
select e.last_name,d.DEPARTMENT_ID, d.DEPARTMENT_NAME,e.SALARY from employees e LEFT OUTER JOIN DEPARTMENTS d ON (d.department_id = e.department_id);
--右外链接
select e.last_name,d.DEPARTMENT_ID, d.DEPARTMENT_NAME,e.SALARY from employees e,departments d where e.DEPARTMENT_ID(+) = d.DEPARTMENT_ID;
select e.last_name,d.DEPARTMENT_ID, d.DEPARTMENT_NAME,e.SALARY from employees e RIGHT OUTER JOIN DEPARTMENTS d ON (d.department_id = e.department_id);
--全连接
select e.department_id,d.department_id,e.last_name,d.department_name from EMPLOYEES e full outer join DEPARTMENTS d on (e.department_id = d.department_id);
--nuion/union all
select e.LAST_NAME,d.department_name from employees e,departments d where e.DEPARTMENT_ID(+) = d.department_id
union all
select e.LAST_NAME,d.department_name from employees e,departments d where e.DEPARTMENT_ID = d.department_id(+);
--minus(第一个查询结果减去第二个查询结果 剩下 第二部分不重复的部分)
select e.LAST_NAME,d.department_name from employees e,departments d where e.DEPARTMENT_ID = d.department_id(+)
minus
select e.LAST_NAME,d.department_name from employees e,departments d where e.DEPARTMENT_ID(+)= d.department_id;
--intersect(联合唯一性)(两个集的交集)
select e.LAST_NAME,d.department_name from employees e,departments d where e.DEPARTMENT_ID = d.department_id(+)
intersect
select e.LAST_NAME,d.department_name from employees e,departments d where e.DEPARTMENT_ID(+)= d.department_id;
--组函数
--count
select count (DEPARTMENT_ID) from EMPLOYEES;
select count (*) from EMPLOYEES where DEPARTMENT_ID = &部门;
select count (commission_pct) from EMPLOYEES where DEPARTMENT_ID = &部门;
--最大(自然顺序)/最小/求和/平均值(限制汉字)
select max(salary),min(salary),sum(salary),round(avg(salary))from EMPLOYEES;
select max(salary),min(salary),sum(salary),round(avg(salary)) from EMPLOYEES where DEPARTMENT_ID = 90;
--group by(分组函数)(having字句对分组函数进行条件筛选)(后面只能跟列名而不是列别名)
--where>分组>组函数>having>order by
select department_id,round(avg(salary)) from EMPLOYEES GROUP BY DEPARTMENT_ID order by 2 desc;
select department_id,round(avg(salary)) from EMPLOYEES GROUP BY DEPARTMENT_ID having round(avg(salary))>9000;
*********************************************************************************************************
--一般查询与子查询
select last_name,salary from employees where salary = 11000;
--单行子查询(多行结果)(可以使用条件连接符)
--1.作为where子句
select last_name,salary,job_id from EMPLOYEES where job_id=(select job_id from EMPLOYEES where last_name=‘Abel‘);
select last_name,salary,job_id from EMPLOYEES where
job_id=(select job_id from EMPLOYEES where last_name=‘Abel‘)
and
salary>(select salary from EMPLOYEES where last_name=‘Abel‘);
--2.作为from子句
select last_name,salary from EMPLOYEES where job_id = ‘SA_REP‘ and salary = 11000;
--后面作为一个表,外部再从内部查询(子查询可以多层嵌套(三层不能再多了))
select last_name,salary from
(select last_name,salary from EMPLOYEES where job_id = (select job_id from EMPLOYEES where last_name=‘Abel‘))
where salary = 11000;
--3.子查询中使用分组函数
select last_name||first_name,salary,job_id from EMPLOYEES where salary=(select max(salary) from employees);
--4.having中使用子查询
select department_id,min(salary) from EMPLOYEES
GROUP by DEPARTMENT_ID having min(salary)>
(select min(salary) from employees where department_id=50);
--最小工资大于100号部门的所有部门的id和最小工资
select department_id,min(salary) from EMPLOYEES
group by DEPARTMENT_ID having min(salary)>
(select min(salary)from EMPLOYEES where DEPARTMENT_ID=100);
--查询与姓氏为**的员工职位相同的员工的姓名,薪资和部门.
--(in/= any)用法相同(<>any是无效操作,等同于没有)
select last_name,job_id,salary from EMPLOYEES where job_id = any
(select job_id from EMPLOYEES where last_name = ‘King‘);
--(>any/<any 大于最小的,小于最大的)
select employee_id,last_name,job_id,salary from EMPLOYEES where salary <> any
(select salary from EMPLOYEES where job_id=‘IT_PROG‘);
select last_name,job_id from EMPLOYEES where job_id =
(select job_id from employees where last_name =‘Abel‘);
select last_name,job_id from EMPLOYEES where DEPARTMENT_ID =
(select department_id from EMPLOYEES where last_name = ‘Abel‘);
select last_name||‘ ‘||first_name,salary,department_id from employees where salary<
(select avg(salary) from EMPLOYEES where department_id=100)and department_id<>100;
select * from EMPLOYEES;
********************************************************************************************************
--创建表
--create table 表名
--(
--列1 数据类型 约束(可选),
--列2 数据类型 约束
--);
--创建一个表(Test_Oaec) 3列:id name age
create table oaec_test
(
--列级别
--id number(10) CONSTRAINT oaec_test_id_pk PRIMARY key,
--表级别
id number(10) ,
CONSTRAINT oaec_test_id_pk primary key(id),
name varchar2(20) CONSTRAINT oaec_test_name_nn not null,
age number(5)check (age>0 and age<150),
birthday date
);
--birthday date default sysdate;
--描述表
desc oaec_test;
--插入数据
insert into oaec_test (ID,name,age)values(1,‘花色‘,DEFAULT);
--查看
select * from OAEC_TEST;
--删除表
drop table oaec_test;
--查看约束
select * from user_constraints;
--查看oaec_table表的约束(约束名称和约束类型)
select constraint_name,constraint_type from user_constraints where TABLE_NAME=‘OAEC_TEST‘;
--约束
--not null(不能为空)(只能写在列级别)
--unique(唯一)
--primary key(非空且唯一)
--foreign key(外键列)(依赖关系.依赖主键.主键有外键不一定有,主键没有外键一定没有)
--check(自定义约束)例如:(age>0 and age<150)
create table friend
(
id NUMBER(10),
phone number(11),
last_name VARCHAR2(25)
CONSTRAINT friend_last_name_nn not null,
gender VARCHAR2(10),
age number(3),
CONSTRAINT friend_id_pk PRIMARY KEY(id),
CONSTRAINT friend_phone_uk UNIQUE(phone),
CONSTRAINT friend_gender_ck check(gender in(‘男‘,‘女‘)),
CONSTRAINT friend_age_ck check(age>0 and age<150),
--外键列,引用oaec_test的id主键列(列级别)
--test_id number(10) CONSTRAINT friend_test_id_fk REFERENCES oaec_test(id)
--表级别
test_id number(10),
CONSTRAINT friend_id_fk FOREIGN key(test_id) REFERENCES oaec_test(id)
);
drop table oaec_test;
drop table friend;
drop table copy_friend;
select * from OAEC_TEST;
select * from FRIEND;
select * from copy_friend;
INSERT INTO oaec_test (id,name)VALUES(1,‘花花‘);
INSERT INTO oaec_test (id,name)VALUES(2,‘花花1‘);
INSERT INTO oaec_test (id,name)VALUES(3,‘花花1‘);
INSERT INTO oaec_test (id,name)VALUES(4,‘花花1‘);
--INSERT INTO oaec_test VALUES(3,‘花花1‘,21,sysdate);
INSERT INTO FRIEND VALUES(1,16345678910,‘花‘,‘男‘,22,null);
INSERT INTO FRIEND VALUES(2,12345678910,‘花‘,‘男‘,22,1);
INSERT INTO FRIEND VALUES(3,16346678910,‘花‘,‘男‘,22,2);
INSERT INTO FRIEND VALUES(4,16345978910,‘花‘,‘男‘,22,2);
--使用子查询创建表
--创建一个只有ID,age的表
create table copy_friend as
select id,age from FRIEND where AGE=22;
--复制整个源表
create table copy_friend as
select * from friend;
--创建表后再添加一列
alter table oaec_test add(salarys number(10) CONSTRAINT oaec_test_salarys_uk unique);
desc oaec_test;
--修改表创建后的列
alter table oaec_test MODIFY(name number(10));
alter table oaec_test MODIFY(birthday date default sysdate);
--创建表后添加约束
alter table oaec_test add(CONSTRAINT oaec_test_birthday_uk unique(birthday));
--创建表后删除约束(根据约束名删除)
Alter table oaec_test DROP CONSTRAINT oaec_test_birthday_uk;
--约束失效(DISABLE)
--删除表中的列(列中即使有数据也能删除)(一次只能删除一个并且必须保留一个列)
alter table oaec_test drop column salarys;
--表的删除(DROP table)(在被引用的时候,可以使用级联删除)
DROP table oaec_test;
--(先删约束,再删表)
DROP table oaec_test CASCADE CONSTRAINTS;
*********************************************************************************************
--1.往表中插入数据
--insert into 表名(表名若不写,表示表的列全部要插入数据)
--values(插入的数据和列一一对应)
select * from tab;
--创建test表
create table tests
(
id number(10) ,
CONSTRAINT oaec_tests_id_pk primary key(id),
name varchar2(20) CONSTRAINT oaec_tests_name_nn not null,
age number(5)check (age>0 and age<150),
birthday date DEFAULT sysdate
);
select * from tests;
insert into tests(id,name,age,birthday)
values(1,‘花色‘,21,sysdate);
insert into tests(id,name,age,birthday)
values(2,‘花色‘,21,TO_DATE(‘2000-05-01‘,‘yyyy-MM-dd‘));
insert into tests(id,name,age,birthday)
values(4,‘花色‘,21,TO_DATE(‘2000-5月-01‘,‘yyyy-Mon-dd‘));
create table oaec_tests_copy as select * from oaec_test;
select * from OAEC_TESTS_COPY;
--删除表中的数据
delete OAEC_TESTS_COPY;
--2.插入数据//用子查询插入数据(查询出的数据全部插入到指定的表/列要一一对)(普通插入只能插入一列)
insert into OAEC_TESTS_COPY select * from OAEC_TEST;
insert into OAEC_TESTS_COPY(id,name) select id,name from OAEC_TEST;
insert into OAEC_TESTS_COPY(id,name) select id,name from OAEC_TEST where id in(1,4);
--更新表中的数据(UPDATE 表名
--select 列名 = 新值
--where 条件 (确定数据的位置/不写则更行所有的)
select * from OAEC_TEST;
update OAEC_TEST
set id = 5 where id = 3;
update OAEC_TEST set name = ‘faker‘ where NAME = ‘比克大魔王faker‘;
--3.删除数据
--delete from 表名
--where 条件(确定要删除数据的位置);
delete from OAEC_TEST;
rollback;
delete from OAEC_TEST
where id = 0;
delete from OAEC_TEST
where NAME like ‘%花花1%‘;
--注意:插入/删除/更新 不要违反约束!尤其是外键约束!
commit;--提交
ROLLBACK;--回滚
SAVEPOINT update_p;--设置回滚点
ROLLBACK to update_p;--回滚到指定的回滚点
以上是关于关于oracle数据库的小知识的主要内容,如果未能解决你的问题,请参考以下文章
oracle在安装dbca时,报错:ORA:04031 关于shared Memory,我应该怎么调呢
Oracle startup in restricted mode