1.創建表空間
create tablespance 表名
datafile ‘’;-- 放在的位置
size --設定大小
autoextend on
next ; --擴展大小
-- 2洗掉表空間
drop tablespance 表名;
-- 創建序列 increment by增長 start with 從
create sequence s_表名 increment by 增長 start whith 增長的長度
select s_表名.nextval from dual;
-- 3添加一條記錄
insert into 表名(列名,列明,列明) values (s_表明,'','')
commit;-- 提交資料
-- 4創建用戶
create user 用戶名;
identified by 密碼;
default tablespance 表空間;
-- 創建表 整數用number 字符型 varcahr();
create tanlespance person(
)
-- 5查詢表
select * from 表名;
-- 添加一列 add
alter table 表名 add 列名 列名資料型別;
-- 修改列型別 rename column
alter table 表名 rename column 列名資料型別 to 資料型別;
-- 洗掉一列 dorp column
alter table 表名 drop column 列名;
-- 添加一條資料 insert into
insert into 表名(列名,列名)values();
-- 修改一條記錄 update set
update 表名 set 列名=‘’,列名=‘’where 主鍵(id)= id的序號
-- 洗掉表中全部記錄
delete from 表名
-- 洗掉表結構
drop table 表名
-- 字符函式
select upper('bob')from dual;-- 小寫轉大寫
select upper ('BOB')from dual;--大寫轉小寫
-- 數之函式 round 四舍五入 trunc 直接截取
select round(6.666,2)from dual;
select trunc(6.666,1) from dual;
-- 轉換函式
-- fm去掉前面的零
select to_char(sysdate,'fm yyyy-mm-dd hh24:mi:ss')from dual;
-- 字串日期
select to_date('2021-6-29 21:58:52','fm yyyy-mm-dd hh24:mi:ss')from dual;
-- 第二天
一.條件運算式
1.給表中的員工起中文名 等值判斷 查名字 when指當時列名 then指修改的名
select 名字列名,
case 名字列名
when ‘’then ‘’
else ‘’
end
from 表名;
2.判斷表中工資 大于3000,大于1500且小于3000,其它低收入
select enmae,sal,
case
when sal>3000 then '高收入'
when sal>1500 then '中收入'
else '低收入'
end
from 表名;
3.多行函式【聚合函式】:作用于多行,回傳一個值 count(1)查詢總數量
max(sal)最大 min() 最小 sum()求和 avg()平均值
select count(1) from 表名;
4.分組查詢
分組查詢出各個部門的平均工資 部門 deptno 工資 sal
select deptno, avg(sal) from 表名 group by deptno;
查詢工資高于平均工資的名字
select a.avgSal,d.dname
from
(select deptno, avg(sal) avgSal from 表名 group by deptno) a,
dept d where a,deptno = d.deptno;
5.查詢出平均工資高于2000的的部門資訊
select deptno,avg(sal) as avgSal from
emp group by deptno having avg(sal)>2000;
6.查詢出每個部門工資高于800的員工平均工資
select deptno,avg(sal) from emp where avg(sal)>800 group by deptno;
視圖 view
--視圖可以修改嗎?
查詢表的欄位被修改了 可以設定視圖為只讀
索引 index
索引的使用原則: ?
在大表上建立索引才有意義 ?
在where子句后面或者是連接條件上的欄位建立索引 ?
表中資料修改頻率高時不建議建立索引
什么是pl/sql
declare
//說明部分
begin
陳述句序列
end;
常量和變數意義
參考變數
表名%type
記錄變數
表名%rowtype
游標 cursor
步驟:
1.打開游標
2.取一行游標的值
3.關閉游標
4.游標的結束方式
存盤程序
create or replace procedure helloworld is begin
dbms_output.put_line('helloworld'); end helloworld;
存盤程序(Stored Procedure)是在大型資料庫系統中,
一組為了完成特定功能的SQL 陳述句集,經編譯后存盤在資料庫中,
用戶通過指定存盤程序的名字并給出引數(如果該存盤程序帶有引數)
來執行它,存盤程序是資料庫中的一個重要物件,
任何一個設計良好的資料庫應用程式都應該用到存盤程序,
觸發器
觸發器可用于 ?
資料確認 ?
實施復雜的安全性檢查 ?
做審計,跟蹤表上所做的資料操作等 ?
資料的備份和同步
CREATE [or REPLACE]
TRIGGER 觸發器名
{BEFORE | AFTER} {DELETE | INSERT | UPDATE [OF 列名]}
ON 表名 [FOR EACH ROW [WHEN(條件) ] ] begin PLSQL 塊 End 觸發器名
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/353215.html
標籤:Oracle
上一篇:Spark的安裝及其配置
