sql的一點總結<一>

sql總結sql

1.常見的數據庫對象有哪些?
表(table) 視圖(view) 序列(sequence) 索引(index) 同義詞(synonym)
存儲過程(procedure) 存儲函數(function) 觸發器(trigger)數據庫

2.表:數據的主要存儲方式,由行和列組成
視圖:存儲起來的select語句。
對視圖中數據的DML操做,會致使建立視圖使用的表中的數據的修改。
create view emp_vu
as
select department_id,avg(salary) dept_avg_sal
from employees
group by department_id;
--with read only
select * from emp_vu;
序列:提供了一系列有規律的數值,經常使用來做爲表的主鍵的值
create sequence emp_id_seq
start with 1001
increment by 1
maxvalue 10000
--minvalue
--cycle/nocycle
--cache/nocache
1)nextval / currval
select emp_id_seq.currval from dual;

select emp_id_seq.nextval from dual;

create table emp(
id number(10),
name varchar2(15)
)

insert into emp
values(emp_id_seq.nextval,'BB');

select * from emp;

裂縫:①多個表共用一個序列②出現回滾③出現異常

索引(index):當使用索引做用的列做爲查詢條件進行查詢時,能夠提升查詢的效率。
--如何建立索引:①自動建立(聲明爲主鍵或惟一性約束的列) ②手動建立
create index emp_sal
on employees(salary);

3.重點:表
DDL:CREATE TABLE;ALTER TABLE;TRUNCATE TABLE;DROP TABLE;RENAME .. TO ..
不可回滾,即意味着:自動提交
--1.建立表
--1.1「白手起家」
create table dept(
dept_id number(10),
dept_name varchar2(15),
location_id varchar2(10),
birth Date
)微信

select * from dept;

--1.2基於現有的表,建立
create table emp1
as
select employee_id id,last_name name,hire_date,salary
from employees
--where department_id = 80;
where 1=2;

select * from emp1;
--1)對現有的表的複製/空表
create table emp_copy
as
select * from employees
--where 1=2;函數

select * from emp_copy;spa

--2.修改表
--2.1增長一個列
ALTER table emp
add(salary number(10,2) default 2000);對象

select * from emp;
--2.2修改現有的列
alter table emp
modify(salary number(15,2));blog

insert into emp(id,name)
values(1004,'CC');索引

--2.3重命名現有的列
alter table emp
rename column salary to sal;rem

--2.4刪除現有的列
alter table emp
drop column sal;it

--3.重命名現有的表
rename emp to employee;

select * from employee;

--4.清空表
truncate table employee;

rollback;

--5.刪除表
drop table employee;

 

DML:增、刪、改、查
--增insert into ...
--1.一條一條的添加
select * from dept;

insert into dept
values(,,,);

insert into dept(dept_id,location_id,dept_name)
values(,,);
--2.導入數據
insert into dept(dept_id,location_id,dept_name)
select department_id,location_id,department_name
from departments;

alter table dept
modify(dept_name varchar2(20));

--刪
delete from dept
where dept_id < 40;

--改
update dept
set location_id = 1700
where dept_id = 20;

commit;

--查詢(重中之重)
select .... --分組函數(count / max / min / avg / sum)
from ...,....--多表鏈接
where ...--過濾條件和 多表的鏈接條件(若不寫,會出現笛卡爾積的錯誤)
group by ...--凡是在select中出新了分組函數,則沒有使用分組函數的列要做爲group by的條件
having avg(..) ...--分組函數做爲過濾條件,要使用having
order by ... asc/desc;

--存儲起來的pl/sql語句:
declare
--聲明變量、記錄類型、遊標

begin
--執行部分

exception
--異常處理部分
end;
--存儲過程(procedure) :沒有返回值
create or replace procedure ... (param1 param1_type,param2 param2_type)
is

begin

end;


--存儲函數(function) :有返回值
create or replace function ...(param1 param1_type,param2 out param2_type)
return 變量類型
is

begin
...
return ...;
end;

--觸發器(trigger)
create or replace trigger ...
after/before
update/delete/.. on 表名
begin

end;

 

 

<!-- Start -->

獲知及時信息,請關注個人我的微信訂閱號:0與1的那點事

 

<!-- End -->

 

本文爲博主原創文章,轉載請註明出處!

http://www.cnblogs.com/libingbin/

感謝您的閱讀。

相關文章
相關標籤/搜索