oracle语法

 

数据定义DDL
create alter drop truncate

数据操纵DML
insert select delete update

事务控制TCL
commit savepoint rollback

数据控制DCL
grant revoke

 

cmd连接数据库
sqlplus system/123@ORCLEHELLO1
查看所在库 select name from v$database

sys超级用户
system管理员
scott/tigert测试用户

若scott/tigert用户被锁=====
conn sys/123456 as sysdba;
alter user scott account unlock; //解锁
conn scott/tigert //弹出一个修改密码的对话框,修改密码

修改sys/system用户密码=====
进入sqlplus 进行密码重置

sqlplus/as sysdba

alter user sys indentified by newpassword;
conn sys/newpassword as sysdba;

//用sys帐号更改system帐号密码
alter user system identified by newpassword;
conn system/newpassword

 

导出
exp username/password file="src.dmp"
导入
imp username/password file="src.dmp"

 

创建表空间 表名
create tablespace news datafile 'D:\oradata\0\01.dbf' size 50m;
删除表空间
drop tablespace news;

查看表空间
select * from v$tablespace;

查看扩展信息
select tablespace_name,autoextensible from dba_data_files;

自增长 edit
alter database datafile 'src' autoextend on;

添加文件
alter tablespace news
add datafile 'D:\oradata\0\02.dbf' size 50m;

删除表空间和文件
drop tablespace news including contents and datafiles;

创建用户
create user c##thie identified by thie default tablespace news;

授权
grant connect,resource to c##thie;

对表空间授权
grant unlimited tablespace to c##thie;

char 固定长度字符
varchar2 可变长度字符
number 整数,浮点
number(3,1) 99.1 number(3) 999 number 无限制
date 日期
blob 二进制对象
clob 字符格式的大型对象
bfile 将二进制数据存储操作系统文件中

新建表
create table teacher //教师表
(tno number(4) not null, //教师编号4 不能为空
tname varchar2(14) not null, //教师名字 字符14 不能为空
tid char(18), //身份证号 字符18
gender char(2), //性别 字符2
birthdate date) //生日 日期
/

增加列
alter table teacher add(
sal number(7,2),hirdate date,wechat varchar2(30));

查看表
desc teacher;

修改列名
alter table teacher rename column 要改的列名 to 新列名;


alter table teacher modify(tname varchar2(30));

alter table teacher drop column wechat;

添加约束
主键(列数据唯一 不能为空)(primary key)
alter table teacher add constraint pk_teacher primary key(tno);
唯一(列数据唯一 允许为空 只能一个空值)(unique)
alter table teacher add constraint uk_teacher_tname unique(tname);

检查(限制取值范围)(check)
alter table teacher add constraint ck_teacher_gender check(gender in('男','女'));

外键(关联表)(foreign key)
alter table teacher add constraint fk_teacher_deptno_dept foreign key(deptno) references dept(deptno);

删除约束
alter table dept
drop constraint deptno;

不重复distinct
select distinct deptno from teacher where deptno is not null

 

插入
insert into teacher (tno,tname,tid,gender) values(1,'11','111','男')

commit;

查看表
select * from teacher;

删除
delete from teacher;

commit;

创建序列
create sequence sq_teacher
start with n increment by 1;

初始化序列
select sq_teacher.nextval from dual;

当前序列
select sq_teacher.currval from dual;

修改列为10
update teacher set deptno=10;

指定修改目标
update teacher set sal=sal+2000 where
tname in('陈老师','韩老师');

删除所有姓蔣的老师
delete from teacher where tname like '蒋%';

删除所有教师和部门
delete from teacher;
delete from dept;

回退
rollback;

截断
truncate table teacher;


select * from teacher t where t.job='研发' or t.job='讲师' order by t.sal desc


select 'insert into dept values('||deptno||','||dname||','||loc||')' from dept

select 'insert into dept values('||deptno||','''||dname||''','''||loc||''')' from dept

select * from TEACHER t where sal between 10000 and 20000

select t.tname,t.deptno,(select dname from dept where dept.deptno=t.deptno)as deptname from teacher t

select t.tname,t.deptno,(select dname from dept where deptno = t.deptno) from teacher t where t.deptno in(select deptno from dept where t.deptno=deptno and dname in('人力部','招生部'))

select tname,(select dname from dept where deptno=t.deptno) from TEACHER t where deptno in(select deptno from dept where dname='招生部') and gender='男'

select tname,teacher.deptno,dept.dname from teacher inner join dept on teacher.deptno=dept.deptno where dept.dname='招生部' and gender='男'

select tname,dname,dept.deptno from teacher inner join dept on dept.deptno=teacher.deptno

select t1.tno,t1.tname,t1.mgrno,t2.tname from teacher t1 left join teacher t2 on t1.mgrno=t2.tno

select t1.tno,t1.tname,dept.dname as 部门,t2.tname as 导师 from teacher t1 left join teacher t2 on t1.mgrno=t2.tno left join dept on dept.deptno=t1.deptno

select round(sal,-1 ) from teacher

select sysdate+1/24 from dual

select to_number('112'), to_char(1122), to_date('1986-1-3','yyyy-mm-dd')from dual

select to_char(sysdate,'yyyy-mm-dd hh24-mi-ss') from dual

select sal,comm,(sal+nvl(comm,0))as 总和 from teacher

select sal,comm,(sal+nvl(comm,0))as 总和,(sal+nvl2(comm,comm+1000,0))as 总和 from teacher

select sal,comm,(sal+nvl(comm,0))as 总和,(sal+nvl2(comm,comm+1000,0))as 总和,decode(comm,null,0,2300,2600,0) from teacher

select deptno,sum(sal),avg(sal),count(0),max(sal),min(sal) from teacher group by deptno having count(*)>10

select tname,deptno,sal,
rank() over(partition by deptno order by sal desc) rank,
dense_rank() over(partition by deptno order by sal desc) dense_rank,
row_number() over(partition by deptno order by sal desc) row_number from teacher


select * from (select rownum as rn,tname from teacher where rownum<=10) t where t.rn>=5

select * from (select e.*,rownum rn from (select * from teacher order by sal desc) e where rownum<=9) where rn >=5

各部门最高工资的人员
select t.tname,t.sal,t.deptno,(select dname from dept where dept.deptno=t.deptno)as 部门 from teacher t join (select max(sal) maxsal,deptno from teacher group by deptno) d
on(t.sal=d.maxsal and t.deptno=d.deptno)


返回boolean判断是否存在exists
select t.tname,t.deptno from teacher t
where exists (select 'a' from dept where dept.deptno=t.deptno)



 

posted @ 2017-05-28 16:16  m97i  阅读(121)  评论(0编辑  收藏  举报