oracle总结--增删改查

时间:2022-01-27 06:42:48

oracle的执行计划
SQL> EXPLAIN PLAN FOR SELECT * FROM emp;
已解释。
SQL> SELECT plan_table_output FROM TABLE(DBMS_XPLAN.DISPLAY('PLAN_TABLE'));
或者:
SQL> select * from table(dbms_xplan.display);

select distinct * from student; --排除重复对象
select * from(select e.*,rownum rw from emp e) where rw>=8 and rw<=10;
--分页查询
select * from (select e.*,dense_rank() over(order by sal desc) rank from emp e) where rank=5; ---查出指定条件的一个排名

select job , max(sal) from emp having max(sal)>2000 group by job
-----------------------数据库group by和having

select months_between(sysdate,hiredate) from emp; 查看入职日期时间
select last_day(sysdate) from dual; 查看本月的最后一天

select substr(ename,0,1) from emp where regexp_like(substr(ename,0,1),'^([C-M]+)$'); -------截取首字母为C到M的值

update students set s=trim(ssss); ---把数据复制到新的字段上去

update students set sold=to_date('2013/06/30', 'yyyy-mm-dd hh24:mi') where sid =1;
------------------------修改表数据

insert into testes(create_time) values(now())   --mysql数据库进行插入当前时间操作(格式:2017-08-14 10:35:11)

alter table students add (ssss number(20)); ------添加表字段

alter table students rename column ssss to ss; ------修改表字段

alter table students drop column sss; ----------删除表字段

show recyclebin; ------查看回收站 purge recyclebin;-------------清空回收站
show recyclebin; -------闪回查看 flashback table 表名 to before drop; ---闪回数据库

savepoint a; 定义保存点 rollback to savepoint a; ---回退回保存点

set transaction isolation level SERIALIZABLE(read only(只读)、read committed(读取提交数据)); ---设置事物的隔离级别

创建两张关联表create table AA
(
aid number(10) primary key ,
sname varchar2(40) not null,
hibernate date
);
create table BB(
bid number(10) primary key,
dname varchar2(50),
constraint id_fk foreign key(bid) references AA(aid)
);

或者:创建两张表,然后修改表结构 alter table BB add contraint id_fk foreign key(bid) references AA(aid);
alter table grade add constraint id_fk foreign key (id) references student (id)

mysql中时间走系统时间,设置成timestamp    默认设置成CURRENT_TIMESTAMP  跟着系统时间自增

设置外键

alter table 表1 add constraint fk_stu foreign key (表1字段) references 表2(表2字段)

设置主键

alter table 表add constraint pk_student primary key(表字段);

创建序列

create sequence reqtea start with 1;