Oracle/PLSQL常用操作語句

SQL
–創建表
create table 表名(字段名 字段類型);
create table user(
id number,
name varcahr2(20),
sex vrachr2(1)
);

–刪除表
drop table 表名;
drop table user;

–添加主鍵
alter table 表名 add constraint 約束名 primary key(id);
alter table user add constraint pk_user primary key(id);

–刪除主鍵
alter table 表名 drop constraint 約束名;
alter table user drop constraint pk_user;

–創建一般索引
create index 索引名 on user(創建索引的字段);
create index in_user on user(name);

–創建唯一索引(主鍵oracle默認爲其創建唯一索引)
create unique 索引名 on user(創建索引的字段);
create unique index in_user_o on user(id);

–刪除索引
drop index 索引名;
drop index in_user_o;

–表名註釋
comment on table user is ‘用戶’;

–字段註釋
comment on column user.name is ‘用戶名’;

–插入數據
insert into user(id,name) values(1,‘Jack’);
insert into user values(2,‘Jack’,‘M’);

–更新數據
update user set name = ‘Jet’ where id = 1;

–刪除數據

delete from user where name = ‘Jet’;

–刪除所有數據
truncate table user;
delete from user;

–增加字段
alter table user add (createdate date);

–修改字段類型
alter table user modify(createdate varchar2(20));

–刪除字段
alter table user drop column createdate;

–修改字段名稱
alter table user rename column name to username;

–創建序列
create sequence s_user_id start with 1 increment by 1 nomaxvalue nominvalue nocycle nocache;–自增序列

–刪除序列
drop sequence s_user_id;

–查詢序列
—直接用會報爲在當前會話中定義
select s_user_id.currval from dual;
/*
currval 表示序列的當前值,新序列必須使用一次nextval 才能獲取到值,否則會報錯
nextval 表示序列的下一個值。新序列首次使用時獲取的是該序列的初始值,從第二次使用時開始按照設置的步進遞增
*/
–truncate(數據特多情況)
truncate table user;
—不可回滾
–DML(如update,delete,insert等)語句在爲commit前可以回滾,DDL(create,drop,truncate等)語句不可回滾,因在執行後數據庫自動爲其提交一個commit;
create table ad(num1 int);
insert into ad values(1);
select * from ad;
savepoint s_1;–創建回滾點1
insert into ad values(2);
savepoint s_2;–創建回滾點1
select * from ad;
insert into ad values(3);
rollback to s_2;–回滾
select * from ad;
commit;
–排序函數dense_rank() over (partition by xxxx(以某個字段作爲分組) order by xxxx(以某個字段作爲排序))
select h_sc.student_no,
h_sc.course_no,
h_sc.core,
dense_rank() over(partition by h_sc.course_no order by h_sc.core desc) rk from hand_student_core h_sc
select trunc(add_months(sysdate,-1),‘month’)from dual 查詢上月第一天
Select trunc(last_day(add_months(sysdate,-1))) from dual
查詢上月最後一天

PLSQL
–創建遊標
CURSOR emp_ded IS
SELECT e.department_id
FROM employees e;

–ref coursor 定義變量爲遊標類型
–不可用for rec in c_cursor 遍歷,可以用loop … end loop;
type c_cursor is ref cursor;

–循環遍歷遊標
FOR rec IN emp_ded
LOOP
emp_d := rec.department_id;
emp_test(emp_d,
nup,
isalary,
asalary);
IF nup = 0
OR nup IS NULL THEN
RAISE ex;
ELSE
dbms_output.put_line(emp_d || ’ num is ’ || nup);
END IF;
END LOOP;

–Group BY 分組函數
SELECT AVG(salary)
,department_id
FROM employees
GROUP BY department_id
having AVG(salary) > 10000;

having 後面跟限定條件

–創建存儲過程
PROCEDURE emp_test(emp_did IN employees.department_id%TYPE,
np OUT NUMBER,
misalary OUT employees.salary%TYPE,
masalary OUT employees.salary%TYPE) IS
BEGIN
SELECT COUNT(*) AS nup,
MIN(e.salary),
MAX(e.salary)
INTO np,
misalary,
masalary
FROM employees e
WHERE e.department_id = emp_did;
END emp_test;

–創建FUNCTION
FUNCTION area(side_one IN INTEGER,
side_two IN INTEGER,
side_three IN INTEGER) RETURN INTEGER IS
z INTEGER;
m INTEGER;
BEGIN
z := 0.5 * (side_one + side_two + side_three);
m := sqrt(z * (z - side_one) * (z - side_two) * (z - side_three));
RETURN m;
END area;

–創建自定義異常處理
PROCEDURE in_emp IS
ex EXCEPTION;
begin
EXCEPTION
WHEN ex THEN
dbms_output.put_line(emp_d);
END in_emp;
–數據庫自帶異常處理
在這裏插入圖片描述
–常用函數
在這裏插入圖片描述在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章