--Oracle --day:2010-4-8 author:luobing create or replace procedure PROC_INSERT_BLDAREN(rownums IN number) is begin truncate table BI_BAOLIAO_DAREN; insert into BI_BAOLIAO_DAREN (ID,USERID,USERNAME,BAOLIAONUM,CREDITS) select bi_baoliao_sequence.nextval,bl.* from (select b.userid,b.username,count(b.id),sum(b.credits) credits from bi_baoliao b group by b.userid,b.username order by credits desc) bl where rownum <=rownums; END PROC_INSERT_BLDAREN; ________________________________DDL(Data Definition Language 数据对象定义语言)___________________________ CREATE : 在数据库中创建新的数据对象 ALTER : 修改数据库中对象的数据结构 DROP : 删除数据库中的对象 DISABLE/ENABLE TRIGGER : 修改触发器的状态 UPDATE STATISTIC : 更新表/视图统计信息 TRUNCATE TABLE : 清空表中数据 COMMENT : 给数据对象添加注释 RENAME : 更改数据对象名称 __________________________________________________________________________________________________________ --查询日期字段 --方法1 select to_char(date1,'yyyy-mm-dd mm:hh:ss') "Date" from table1; --方法2 select cast(date1 as timestamp) "Date" from table1; select * from tb_shop; create table tb_shopType ( Id number(10) primary key, typeName varchar2(10) not null ); create table tb_shop ( Id number(10) primary key, shopId varchar2(20) unique not null, shopName varchar2(20) not null, price number(6,2) check(price>0) not null, --添加check约束 ==>constraint ch_price check(price>0), shopTypeId number(10) not null, -- ==>可以写成 references tb_shopType(Id), manufactoruingdate date not null, constraint fk_shopTypeId foreign key(shopTypeId) references tb_shopType(Id) ); --向表中添加一个新列 alter table tb_shop add memo varchar2(100); --修改表中列名为memo的类型 alter table tb_shop modify memo varchar2(50); --删除表中的一列 alter table tb_shop drop column memo; --删除表中所有的行记录 truncate table tb_shop; --删除表对象 drop table tb_shop;
________________________________________DML(Data Manipulation Language 数据操作语言)________________ Insert :将数据插入到表或视图 Delete :从表或视图删除数据 Select :从表或视图中获取数据 Update :更新表或视图中的数据 Merge : 对数据进行合并操作(插入/更新/删除) ______________________________________________________________________________________________________ --Insert 语句 --普通 insert into tb_shopType values(1,'家电类'); --插入日期值 insert into tb_shop values(1,'s003','电脑',3400.00,1,'11-3月-10'); insert into tb_shop values(2,'s002','LenoveG50',4500.00,1,'11-3月-10'); --Update 语句 --更新 产品日期为 11-3月-10 的商品的价格+100; update tb_shop t set t.price=t.price+100 where t.manufactoruingdate='11-3月-10'; update tb_shop t set t.shopName='LenovoG450' where t.shopname='LenoveG50'; --Delete 语句 --删除 商品中日期为 11-3月-10 且名称为LenovoG450的 商品 delete tb_shop t where t.shopName='LenovoG450' and t.manufactoruingdate=to_date('2010-3-11','yyyy-mm-dd'); --使用了TO_DATE函数
________________________________________DQL(Data Query Language 数据查询操作语言)________________ select:用来查询数据的 __________________________________________________________________________________________________________________ --Select 语句 --单行子查询 (查询公司中工资最高的员工的信息) Select first_name,last_Name,salary from employees where salary=(select max(salary) from employees); --多行子查询 必须使用多行运算符(IN ,NOT IN ,EXISTS NOT EXISTS,ALL ANY) --查询工资 大于部门编号为20的所有员工薪水 的员工信息 select first_name,last_name,salary,department_id from employees where salary>=all(select salary form employees where deparement_id=20); --多列子查询 (查询个部门中工资最低的员工的信息) select first_name,last_name,salary,deparment_id from employees where (salary,deparment_id) in(select min(salary),department_id from employees group by department_id) order by department_id; --相关子查询 (查询负责管理其他员工的管理员信息) select employee_id,first_name,last_name,department_id from employees a where exists ( select * from employees b where b.manager_id=a.employee_id) order by department_id,employee_id; --________________________DDL,DML 中使用子查询___________________________ --1.Create Table {使用employees中的几个新列,创建一个新表,并插入department_Id为90 和110 的部门的员工 create table emp (empId,ename,hireDate,deptId) as select a.employee_id,a.first_name||a.last_name,a.hire_date,a.department_id from employees a where a.department_id in(90,110); --2.Insert {将一个表中的数据插入到另一个表中(将表中部门编号为20 的所有员工信息添加到 emp中) insert into emp select a.employee_id,a.first_naem||a.last_name,a.hire_date,a.department_id from employees a where a.department_id=20; --3 Delete {删除emp中 位于Marketing 部门的雇员信息} delete emp where emp.deptid=(select department_id from departments where department_name='Marketing'); --4 Update {将员工206的hiredate 和 depti 的之设置成 员工100的 } update emp set(hiredate,deptid)=(select hiredate,deptid from emp where emp.empid=206) where emp.empid=100;
______________________________DCL(Data Control Language 数据对象权限控制语言)________________________________________________ GRANT : 赋予用户某种控制权限 REVOKE :取消用户某种控制权限 ______________________________________________________________________________________________________________________________ --Grant (授权) --1: Select {授予查询权限 grant select on emp to scott; --2: Select {授予emp表的某empid,ename列的查询权限 必须使用视图} create view view_emp as select empid,ename from emp; grant select on view_emp to scott; --3: Update {授予scott对某empid,ename列的更新权限 grant update(empid,ename) on emp to scott; --4: ALL { 授予emp表对象的所有权限给scott} grant all on emp to scott; --5: Insert update delete select 权限 和上面 一样 grant insert /*(update/delete/insert)*/ on emp to scott; --Revoke (撤销权限) --1: delete 撤销 scott 对emp 的delete权限 Revoke delete on emp to scott; 连接查询 – 左连接通用写法:select * from a left join b on a.id=b.id – 右连接通用 写法:select * from a right join b on a.id=b.id – 全连接通用 写法:select * from a full join b on a.id=b.id – 左连接Oracle 写法:select * from a,b where a.id=b.id(+) – 右连接Oracle 写法:select * from a,b where a.id (+) =b.id --自连接select w.ename||' work for '||m.job from emp w,emp m where w.empno=m.empno;
___________________________________TCL(Transaction ControlLanguage 事务控制语言)___________________________ COMMIT : 保存已完成事务动作结果 SAVEPOINT : 保存事务相关数据和状态用以可能的回滚操作 ROLLBACK : 恢复事务相关数据至上一次COMMIT操作之后 SET TRANSACTION : 设置事务选项 ___________________________________________________________________________________________________________ --Commit --1 在 sqlplus中 要commit; commit; --Rollback --1 在 insert Update delete 后 是用 Rollback rollback; --2 有 savepoint 时候 撤销 保存点后的所有操作 savepoint p1; rollback to p1; --或者下面的 rollback to savePoint p1;