資料庫概念
oracle資料庫
一組:資料檔案、控制檔案、日志檔案
oracle執行個體,它與資料庫的關系
oracle執行個體:執行個體就是資料庫啟動後配置設定的記憶體和建立的背景程序. 資料庫關閉後,實體上的檔案還存在,但執行個體(配置設定的記憶體和建立的程序)就沒有了
資料庫:實體作業系統檔案或磁盤(disk)的集合。
關系: 執行個體就是一組作業系統程序(或者是一個多線程的程序)以及一些記憶體。這些程序可以操作資料庫;而資料庫隻是一個檔案集合(包括資料檔案、臨時檔案、重做日志檔案和控制檔案)。
在任何時刻,一個執行個體隻能有一組相關的檔案(與一個資料庫關聯)。大多數情況下,反過來也成立:一個資料庫上隻有一個執行個體對其進行操作。
不過,oracle的真正應用叢集(real application clusters,rac)是一個例外,這是oracle提供的一個選項,允許在叢集環境中的多台計算機上操作,
這樣就可以有多台執行個體同時裝載并打開一個資料庫(位于一組共享實體磁盤上)。
資料庫中的資料類型:
數字:mysql db2-----short integer long float double
oracle--------number(30),number(30,3)
字元/字元串:char(10) varchar2(10)
日期和時間:date(年、月、日、小時、分鐘、秒) timestamp()(時間戳)
oracle資料庫模式日期格式:03-3月-08 03-mar-08
大對象(characterlob/binary lob)
主鍵應該具備的特征:
1.不能為null
2.主鍵必須唯一
where name like '\%' escape '\'
字元:
upper
lower
initcap
substr
replace
instr
length
lpad/rpad
trim
concat--------||
select substr(last_name,1,1)||lpad(last_name,sbustr(last_name,2,length(last_name)-1,'*')
replace(last_name,substr(last_name,2,leghth(last_name)-2),lpad('',length(last_name)-2,'*')
日期:
months_between
add_months()
next_day()
last_day()
數字:
round(2333,2)
trunc
mod
round(sysdate,'month/year')
類型轉換的函數
to_char to_date to_number
to_char(數字/日期,'fm')
to_char(sysdate,'fmyyyy-mm-dd "fdfdfdfd" hh24:mi:ss am')
to_char(234567,'fm99,999.00000')
to_date('20-3月-58')
to_date('[2008-02-08]','[yyyy-mm-dd]')
rr
68
通用函數:
nvl(commission_pct,0)----第一個表達式為null,傳回第二個表達式的值,否則傳回第一個表達式的值
nvl(commission_pct,1,0)--第一個表達式為null,傳回第三個表達式的值,否則傳回第二個表達式的值
nullif(last_name,first_name)--如果第一個表達式和第二個表達式的值相同,傳回null,否則傳回第一個表達式的值
decode()
lpad('',round((salary*12+salary*12*nvl(commission_pct,0))/1000),'*')
多表的查詢兩種方法:
連接配接查詢
内連接配接:等值連接配接、非等值連接配接、自然連接配接。。。
外連接配接:左、右、全外連接配接
笛卡爾集----子集
select * from users u,orders o where u.id=o.user_id and ;
select * from users u join orders o on u.id=o.user_id;
cross join
select a.dd,b.dd,c.dd from a a,b b,c c where a.pk=b.id and b.id=c.fk
employee
1 a 500
2 b 660
3 c 1000
4 d 4000
level
id name minsalary maxsalay
1 a級 5000 10000
2 b級 3000 5000
3 c級 1000 3000
4 d級 400 1000
a d級
b d級
c c級
d b級
select e.name,l.name from employee e,level l where e.salary between l.minsalary and l.maxsalary;
1 a 500 4 d級 400 1000
2 b 660 4 d級 400 1000
3 c 1000 3 c級 1000 3000
4 d 4000 2 b級 3000 5000
select * from users u left/right/full outer join orders o on u.id=o.user_id;
oracle寫外連接配接的簡便的方式:
select * from users u,orders o where u.id(+)=o.user_id;
select * from departments nature join loctions------兩個表的主外鍵必須同名,同類型
同名,類型不一緻
select * from deparments join locations using(location_id);
select * from departments d,locations l where d.location_id=l.locations_id;
select e.last_name,e.job_id,d.department_name,e.salary,jg.grade_level from employees e,departments d,job_grades jg where e.department_id=d.department_id and e.salary between jg.minsalary and jg.maxsalary;
users(id,name,birth)
insert into users(id,birth) values(1,to_date('1998-01-01','fmyyyy-mm-dd'));
insert into users values(1,'abc',to_date('1998-01-01','fmyyyy-mm-dd'));
delete users where last_name='zhangsan';
sql:結構化查詢語言,select
dml:資料操縱語言:insert update delete
dcl:資料控制語言:commit rollback。
事務:一組sql語句的單元(dml語句為主)
ddl:資料定義語言:create table users()
insert into student values(3,'cc',100.99,null);
ddl:資料定義語言
order by
blueuser
blueorder
blue_user
blue_order
oa_user
create table users(
id number(10),
name varchar2(40),
age number(3)
);
alter table users add constaint users_pk primary key (id);
alter table users disable constraint users_pk;
alter table users enable constraint users_pk;
alter table users drop constraint users_pk;
alter table users set unused (name,age);
alter table users et unused column name;
alter table users set unused column age;
alter table users drop unused columns;
drop table users;
rename users to oa_users;
truncate table users;
select e.last_name,e.salary,e.department_id,d.mm from employees e,(select department_id dept_id,max(salary) mm from employees group by department_id) d where e.department_id=d.dept_id;
select dfdfd from a a left outer join (select dfa)
create or relpace view salary_vu
is
select e.last_name,d.department_name,e.salary,j.name from employees e,departments d,job_grades j where e.department_id=d.department_id and e.salary between j.minsalary and j.maxsalary;
select * from ( select a.*, rownum rn from (select * from table_name ) a where rownum <= to ) where rn >= from