oracle語法
數據定義DDL
create alter drop truncate
數據操縱DML
insert select delete update
事務控制TCL
commit savepoint rollback
數據控制DCL
grant revoke
cmd連接數據庫
sqlplus [email protected]
查看所在庫 select name from v$database
sys超級用戶
system管理員
scott/tigrt測試用戶
若scott/tigher用戶被鎖=====
conn sys/123456 as sysdba;
alter user scott account unlock; //解鎖
conn scott/tigher //彈出一個修改密碼的對話框,修改密碼
修改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)
oracle語法