1. 程式人生 > >關於oracle數據庫的小知識

關於oracle數據庫的小知識

數字函數 左右 -o 滿足 set ltr one tar 行記錄

--查詢語句: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數據庫的小知識