Oracle資料庫語句大全(一)
一.入門部分
1. 建立表空間create tablespace schooltbs datafile ‘D:\oracle\datasource\schooltbs.dbf’ size 10M autoextend on;
2. 刪除表空間drop tablespace schooltbs[including contents and datafiles];
3. 查詢表空間基本資訊select *||tablespace_name from DBA_TABLESPACES;
4. 建立使用者create user lihuaidentified by lihuadefault tablespace schooltbs
5. 更改使用者alter user lihua identified by 123default tablespace users;
6. 鎖定使用者alter user lihua account lock|unlock;
7. 刪除使用者drop user lihua cascade;--刪除使用者模式
8. oracle資料庫中的角色connect,dba,select_catalog_role,delete_catalog_role,execute_catalog_role,exp_full_database,imp_full_database,
9. 授予連線伺服器的角色grant connect to lihua;
10. 授予使用表空間的角色grant resource to lihua with grant option;--該使用者也有授權的許可權
11. 授予操作表的許可權grant select,insert on user_tbl to scott;--當前使用者grant delete,update on lihua.user_tbl to scott;--系統管理員
二.SQL查詢和SQL函式
1.SQl支援的命令:資料定義語言(DDL):create,alter,drop資料操縱語言(DML
2.Oracle資料型別字元,數值,日期,RAW,LOB字元型char:1-2000位元組的定長字元varchar2:1-4000位元組的變長字元long:2GB的變長字元 注意:一個表中最多可有一列為long型
Long列不能定義唯一約束或主鍵約束
long列上不能建立索引
過程或儲存過程不能接受long型別的引數。
數值型number:最高精度38位日期時間型date:精確到sstimestamp:秒值精確到小數點後6位
函式sysdate,systimestamp返回系統當前日期,時間和時區。更改時間的顯示alter session set nls_date_language=’american’;alter session set nls_date_format=’yyyy-mm-dd’;
Oracle中的偽列像一個表列,但沒有儲存在表中
偽列可以查詢,但不能插入、更新和修改它們的值常用的偽列:rowid和rownumrowid:表中行的儲存地址,可唯一標示資料庫中的某一行,可以使用該列快速定位表中的行。rownum:查詢返回結果集中的行的序號,可以使用它來限制查詢返回的行數。
3.資料定義語言
用於操作表的命令create tablealter tabletruncate tabledrop table
修改表的命令alter table stu_table rename to stu_tbl;--修改表名alter table stu_tbl rename column stu_sex to sex;--修改列名alter table stu_tbl add (stu_age number);--新增新列alter table stu_tbl drop(sex);--刪除列alter table stu_tbl modify(stu_sex varchar2(2));--更改列的資料型別alter table stu_tbl add constraint pk_stu_tbl primary key(id);--新增約束
4.資料操縱語言
select,update,delete,insert利用現有的表建立表create table stu_tbl_log as select id,stu_name,stu_age from stu_tbl;--選擇無重複的行
select distinct stu_name from stu_tbl;--插入來自其他表中的記錄insert into stu_tbl_log select id,stu_name,stu_age from stu_tbl;
5.資料控制語言grant,revoke6.事務控制語言commit,savepoint,rollback7.SQL操作符算術操作符:L+-*/比較操作符:L=,!=,<>,>,<,>=,<=,between-and,in,like,is null等邏輯操作符:Land,or,not集合操作符:Lunion,union all,intersect,minus連線操作符:L||示例中stu_tbl_log中的資料如下: ID STU_NAME STU_AGE
---------- -------------------- ----------
1000 李華 20
1001 accp 20
1003 nimda 3stu_tbl中的資料如下: ID STU_NAME ST STU_AGE
---------- -------------------- -- ----------
1000 李華 男 20
1001 accp 男 20
1002 admin 男 30示例:select (3+2)/2 from dual;--算術操作符,結果:2.5select * from stu_tbl where stu_age>=20;--比較操作符select * from stu_tbl where stu_name like '%a%';--比較操作符:likeselect * from stu_tbl where stu_name like 'a___';--比較操作符:likeselect * from stu_tbl where stu_age in(20,30);--比較操作符:inselect * from stu_tbl where stu_age between 20 and 30;--比較操作符:betweenselect stu_name from stu_tbl union allselect stu_name from stu_tbl_log;--集合操作符:union all,測試結果具體如下:STU_NAME
-----------
李華
accp
admin
李華
accp
nimda
已選擇6行。select stu_name from stu_tbl union select stu_name from stu_tbl_log;--集合操作符:union,測試結果具體如下:STU_NAME
---------
accp
admin
nimda
李華select stu_name from stu_tbl intersect select stu_name from stu_tbl_log;--集合操作符:intersect,測試結具體如下:STU_NAME
----------
accp
李華select stu_name from stu_tbl minus select stu_name from stu_tbl_log;--集合操作符:minus,測試結果如下:STU_NAME
----------
Admin從中可以看出:minus是獲取第一張表獨有的資料intersect是獲取兩張表中都有的資料union是整合兩張表的資料,都有的只顯示一次union all是純粹的兩張表資料整合select id,stu_name||' '||stu_sex as name_sex,stu_age from stu_tbl;--連線操作符||,測試結果具體如下: ID NAME_SEX STU_AGE
---------- ----------------------- ----------
1000 李華 男 20
1001 accp 男 20
1002 admin 男 30
8.SQL函式單行函式:從表中查詢的每一行只返回一個值,可出現在select子句,where子句中
日期函式
數字函式
字元函式
轉換函式:ToChar(),ToDate(),ToNumber()
其他函式:
Nvl(exp1,exp2):表示式一為null時,返回表示式二
Nvl2(exp1,exp2,exp3):表示式一為null時返回表示式三,否則返回表示式二
Nullif(exp1,exp2):兩表示式相等時,返回null,否則返回表示式一分組函式:基於一組行來返回
Avg,Min,Max,Sum,Count
Group by,having分析函式
Row_number,rank,dense_rank示例:select u.user_name,sum(oi.order_num*oi.order_price) as total,row_number() over (order by sum(oi.order_num*oi.order_price) desc) as sort from order_item_tbl
oi,user_tbl u,order_tbl o where oi.order_id = o.id and o.user_id = u.id group by u.user_name;
三.鎖和資料庫物件
1.鎖:資料庫用來控制共享資源併發訪問的機制。鎖的型別:行級鎖,表級鎖行級鎖:對正在被修改的行進行鎖定。行級鎖也被稱之為排他鎖。在使用下列語句時,Oracle會自動應用行級鎖:insert,update,delete,select…… for updateselect……for update允許使用者一次鎖定多條記錄進行更新。使用commit or rollback釋放鎖。表級鎖:lock table user_tbl in mode mode;表級鎖型別:行共享 row share行排他 row exclusive共享 share共享行排他 share row exclusive排他 exclusive死鎖:兩個或兩個以上的事務相互等待對方釋放資源,從而形成死鎖2.資料庫物件oracle資料庫物件又稱模式物件資料庫物件是邏輯結構的集合,最基本的資料庫物件是表資料庫物件:
表,序列,檢視,索引序列用於生成唯一,連續序號的物件。建立語法:create sequence user_id_seqstart with 1000increment by 1maxvalue 2000minvalue 1000nocyclecache 1000;--指定記憶體中預先分配的序號訪問序列:select user_id_seq.currval from dual;select user_id-seq.nextval from dual;
更改刪除序列:alter sequence user_id_seq maxvalue 10000;--不能修改其start with 值drop sequence user_id_seq;在Hibernate中訪問序列:
<generator class="sequence">
<param name="sequence"> user_id_seq
</param>
</generator>
檢視以經過定製的方式顯示來自一個或多個表的資料建立檢視:create or replace view user_tbl_view (vid,vname,vage)as select id,user_name,age from user_tbl[with check option]|[with read only];建立帶有錯誤的檢視:create force view user_tbl_force_view asselect * from user_table;--此時user_table可以不存在建立外聯接檢視:create view user_stu_view asselect u.id,u.user_name,u.password,s.ddressfrom user_tbl u,stu_tbl swhere u.s_id(+)=s.id;--哪一方帶有(+),哪一方就是次要的刪除檢視:drop user_stu_view;
索引用於提高SQL語句執行的效能索引型別:唯一索引,點陣圖索引,組合索引,基於函式的索引,反向鍵索引
建立標準索引:create index user_id_index on user_tbl(id) tablespace schooltbs;重建索引:alter index user_id_index rebuild;刪除索引:drop index user_id_index;
建立唯一索引:create unique index user_id_index on user_tbl(id);建立組合索引:create index name_pass_index on user_tbl(user_name,password);建立反向鍵索引:create index user_id_index on user_tbl(id) reverse;
相關推薦
Oracle資料庫語句大全(一)
一.入門部分 1. 建立表空間create tablespace schooltbs datafile ‘D:\oracle\datasource\schooltbs.dbf’ size 10M
Oracle資料庫基礎(一)
Oracle 是一個數據庫管理系統,是Oracle公司的核心產品。其在資料安全性與安整性控制方面的優越效能,以及跨作業系統、跨硬體平臺的資料操作能力。基於“客戶端/伺服器”(Client/Server)系統結構。 主要特點: &
Oracle資料庫學習(一)安裝和簡單使用
新公司的新專案,需要用到Oracle資料庫,所以現在便來解除此資料庫,不得不說,這個資料庫還這是麻煩。 安裝倒是簡單,就是中間會遇到各種問題。 安裝步驟參考:https://blog.csdn.net/fancheng614/article/details/78172894 安裝完成後,中間不要有太大的
Oracle 常用語句大全
一、基礎必會篇 1、說明:建立資料庫 CREATE DATABASE database-name 2、說明:刪除資料庫 drop database dbname 3、說明:備份 sql server — 建立 備份資料的 device USE master EXEC sp_a
MYSQL 資料庫語句大全完整版
常用操作資料庫的命令1.show databases; 檢視所有的資料庫2.create database test; 建立一個叫test的資料庫3.drop database test;刪除一個叫test的資料庫5.show tables; 在選中的資料庫之中檢視所有的表4.use test;選中庫 ,在建
Oracle常用語句大全
Oracle 常用語句大全 1. Oracle安裝完成後的初始口令? internal/oracle sys/change_on_install system/manager scott/tiger sysman/oem_temp 2.
oracle許可權語句大全
1.系統預設的幾個使用者: sys ——–網路管理使用者,具有最高資料庫管理許可權 system——本地管理使用者,許可權次於sys scott——-普通使用者,預設是鎖住的(不可用) 預設情況下:scott 密碼是 tiger (自己設定)
oracle資料庫語句級觸發器和行級觸發器的使用
行級觸發器對DML語句影響的每個行執行一次,觸發器表(如:您的定義觸發器的emp表)變化一個記錄(I、D、U)就執行一次。 語句級觸發器對每個DML語句執行一次,完成一次“事務”觸發一次。 如果在TABLE表中插入的資料為500行,那麼這個表上的語句級觸
查詢資料庫語句大全
注:以下操作均建立在上篇文章sql Server操作1的資料基礎之上 一、實驗目的 熟悉SQL語句的基本使用方法,學習如何編寫SQL語句來實現查詢 二、實驗內容和要求 使用SQL查詢分析器查詢資料,練習查詢語句的使用,掌握SELECT 語句的完整結構,包括簡單查
oracle資料庫選擇題(一)
一、單選題 1. 使用Oracle資料庫,必須啟動哪個服務( ) A.OracleHOME_NAMETNSListener B.OracleServiceSID C.OracleMTSRecoveryService D.Oracle
Oracle資料庫,當前時間轉為星期的語句和查詢某一欄位轉為星期顯示的一種方法。
1、當前時間的星期 select to_char(sysdate,'day') from dual ; 2、指定欄位的星期 select decode (substr(to_char(t.欄位,'d','NLS_DATE_LANGUAGE
oracle資料庫 SQL語句、內建函式大全
1、數值函式 函式 返回值 樣例 顯示 CEIL(n) 大於或等於數值n的最小整數 SELECT CEIL(10.6) FROM TABLE_NAME; 11 FLOO
oracle資料庫使用sql語句獲取一段連續的日期
oracle資料庫,使用sql語句獲取一段連續的日期: SELECT TO_CHAR ( TO_DATE ('2018-11-08', 'yyyy-mm-dd') + ROWNUM - 1, 'yyyy-mm-dd' ) AS sjsj, ROWNUM
Oracle資料庫常見的增刪改查操作語句大全
回收站: show recyclebin (命令列語句) 清空回收站: purge recyclebin 回收站還原已刪除表: flashback table table_name to before dorp 獲取系統時間: sysdate 平均值:avg條件為真:whe
oracle資料庫SQL查詢語句練習一
1、 選擇部門30中的所有員工。 2、 列出所有辦事員(CLERK)的姓名,編號和部門編號。 3、 找出佣金高於薪金的員工。 4、 找出佣金高於薪金的60%的員工。 5、 找出部門10中所有經理(MANAGER)和部門20中所有辦事員(CLERK)的詳
資料庫基本----SQL語句大全
資料庫基本 ---- SQL語句大全一、基礎 1 、說明:建立資料庫 Create DATABASE database - name &nb
oracle資料庫把某行資料轉成一列資料
select running_time, running_user, algorithm_id, algorithm_list_id, name1, name2, ROWNUM, wm_concat(t
【oracle 流程控制】oracle資料庫流程控制語句控制PL/SQL語句
1、條件語句 if else判斷 #宣告變數 declare employee_sa number; begin select count(*) into employee_sa from employees where salary>6000; if employee_sa
ORACLE 資料庫的基本操作語句
1.簡單的表操作 建立一個簡單的表 create table student( name varchar2(20), age number(3) ); 插入新記錄 insert into student values('Tom'
Oracle 資料庫 體系結構(一):儲存結構
目錄 為什麼要學習體系結構? 體系結構的定義 Oracle 物理結構 Oracle 邏輯結構 總結 為什麼要學習體系結構? 之前的文章有講解到 MySQL 、MongoDB 資料庫,這些資料庫我們安裝好了就能拿來用,比如直接可以做一些:增、刪、改