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 emp1asselect employee_id id,last_name name,hire_date,salaryfrom employees--where department_id = 80;where 1=2;select * from emp1;--1)对现有的表的复制/空表create table emp_copyasselect * from employees--where 1=2;
select * from emp_copy;
--2.修改表--2.1增加一个列ALTER table empadd(salary number(10,2) default 2000);
select * from emp;--2.2修改现有的列alter table empmodify(salary number(15,2));
insert into emp(id,name)values(1004,'CC');
--2.3重命名现有的列alter table emprename column salary to sal;
--2.4删除现有的列alter table empdrop column sal;
--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 deptvalues(,,,);
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_namefrom departments;
alter table deptmodify(dept_name varchar2(20));
--删delete from deptwhere dept_id < 40;
--改update deptset location_id = 1700where dept_id = 20;
commit;
--查询(重中之重)select .... --分组函数(count / max / min / avg / sum)from ...,....--多表连接where ...--过滤条件和 多表的连接条件(若不写,会出现笛卡尔积的错误)group by ...--凡是在select中出新了分组函数,则没有使用分组函数的列要作为group by的条件having avg(..) ...--分组函数作为过滤条件,要使用havingorder 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/beforeupdate/delete/.. on 表名begin
end;
<!-- Start -->
获知及时信息,请关注我的个人微信订阅号:0与1的那点事
<!-- End -->
本文为博主原创文章,转载请注明出处!
http://www.cnblogs.com/libingbin/
感谢您的阅读。
转载于:https://www.cnblogs.com/libingbin/p/6391243.html