oracle 簡單備註

1. 創建數據庫mysql

備註:sql

1) oracle 不一樣於mysql 能夠直接create database數據庫

2) oracle 建立schema時對應一個用戶,即該schema的訪問用戶,與用戶一一對應;但能夠存在多個訪問用戶(帶權限控制)express


1.1 建立數據庫文件session

CREATE TABLESPACE XX LOGGING DATAFILE 'D:\app\XX\oradata\orcl\XX.dbf' SIZE 1000M;oracle

create temporary tablespace XX tempfile 'D:\app\XX\oradata\orcl\XX.dbf' size 1000m;app

1.2 建立用戶spa

CREATE USER XX IDENTIFIED BY XX DEFAULT TABLESPACE XX TEMPORARY TABLESPACE XX;索引

1.3 受權權限控制

grant connect, resource to XX;
grant create session to XX;

 

2. 數據庫操做(默認sccot用戶):

2.1 create

create table persons(
person_id NUMBER PRIMARY KEY,
first_name VARCHAR2(50) NOT NULL,
last_name VARCHAR2(50) NOT NULL,
score NUMBER,
type VARCHAR2(20)
);

備註:

1) ORA-02000: missing ALWAYS keyword : 在11g版本里不用用GENERATED BY DEFAULT AS IDENTITY ,要用PRIMARY KEY
2) oracle 原本只有number類型,用做number(19,2),即有小數位;後爲了兼容其餘數據庫,新增int,只能是整形

2.2 insert

insert into persons values(1, 'fred', 'xu', 0, 'a');

insert into persons values(SEQ_PERSON_ID.NEXTVAL, 'fred3', 'xu3', 0, 'b')

備註:

1)oracle下設置自增沒有mysql那麼簡單,步驟以下:

1.1) CREATE SEQUENCE SEQ_PERSON_ID start with 100; #建立一個序列
1.2) INSERT INTO persons VALUES(SEQ_PERSON_ID.NEXTVAL, 'fred1', 'xu1', 0, 'a');

#此時插入persons表記錄的person_id被設置成了100

1.3)也能夠採用觸發器的形式
CREATE TRIGGER persons_trigger
BEFORE INSERT ON persons
FOR EACH ROW
WHEN (new.person_id is null)
begin
select SEQ_PERSON_ID.nextval into :new.person_id from sys.dual;
end;

備註:sys.dual 是個虛擬表,oracle保證裏面只有一條記錄
:new— 觸發器執行過程當中觸發表做操做的當前行的新紀錄
:old— 觸發器執行過程當中觸發表做操做的當前行的舊紀錄

在有觸發器以後,插入數據時不須要填寫主鍵 INSERT INTO persons(first_name, last_name, score, person_type) VALUES('fred2', 'xu2', 0, 'a');

2.3 select

select * from persons where first_name = 'fred' or last_name = 'xu1'
select * from persons where first_name like '%fred%'
select * from persons where CONCAT(first_name, last_name) like '%1%'

select * from users where rownum BETWEEN 0 AND 5 #利用rownum關鍵字分頁

2.4 group by

select AVG(score), person_type from persons group by person_type
select AVG(score), person_type from persons group by person_type having person_type = 'b'

備註:

1)ORA-00979: not a GROUP BY expression group by 後的列要能被處理

2.5 in

select * from persons where person_type in ('a','b');

2.6 insert all

insert all
into persons(first_name, last_name, score, person_type) values('test', 'test', 1, 'c')
into persons(first_name, last_name, score, person_type) values('test1', 'test1', 1, 'c1')
into persons(first_name, last_name, score, person_type) values('test2', 'test2', 1, 'c2')

SELECT 1 FROM dual;

備註:

SELECT 1 FROM dual; 最後一次select必須有


2.7 視圖

create view person_view as select * from persons where first_name like '%f%'

select * from person_view;

備註:

視圖是需表,只是邏輯定義;除非是一種物化視圖,那個纔是有物理佔用

2.8 索引

create index person_index on persons (first_name);

相關文章
相關標籤/搜索