DB数据库知识.docx
- 文档编号:2523594
- 上传时间:2023-05-03
- 格式:DOCX
- 页数:19
- 大小:20.59KB
DB数据库知识.docx
《DB数据库知识.docx》由会员分享,可在线阅读,更多相关《DB数据库知识.docx(19页珍藏版)》请在冰点文库上搜索。
DB数据库知识
scott密码是tiger
sys密码是change_on_install
system密码是manager
sysman密码是oem_temp
1.createuser[用户名]identifiedby[密码];--创建用户
--如何修改用户密码:
alteruser[用户名]identifiedby[新密码]
2.grantconnectto[用户名]--赋予用户登录权限
grantcreatesessionto[用户名]
解锁用户:
alteruserscottaccountunlock;
3.grantresourceto[用户名]--赋予CRUD
4.grant[权限命]on[表名]to[用户名]
revoke[权限命]on[表名]from[用户名]
grant[权限命]on[表名]to[用户名]withgrantoption
[权限包括]select,update,insert,delete,all
5.查看数据库字符集
select*fromnls_database_parameters
--[数据类型]
number-1038次方---1038次方
char定长,最多存储2000字符,查询效率高
varchar2可变长,最多存储4000字符,查询效率低
clob最大存储4G
blob存储二进制数据,4G
number(8,2);101.23
createtable[表名](
字段数据类型[notnullorprimarykey,如果没有notnull,默认可以为空],
字段数据类型[notnullorcheck,如果没有notnull,默认可以为空],
字段数据类型[notnullorunique,如果没有notnull,默认可以为空]
)
-----------------------------------------------------------------------------------
修改表的操作
添加字段
altertable[表名]add(字段名,数据类型)
删除字段
altertable[表名]dropcolumn[字段名]
修改字段
altertable[表名]modify(字段名,数据类型)
给已有的表重命名
rename[原表名]to[新表名]
添加主键
altertable[表名]addconstraint[约束名]primarykey(字段);--如果有多个,称之为联合主键
添加外键
altertable[从表名]addconstraint[约束名]foreignkey(从表名的外键字段)references主表(主键字段)ondeletecascade
删除约束
altertable[从表名]dropconstraint[约束名]
创建表
createtable[表名]asselect*from[表2]--复制表2
createtable[表名]asselect*from[表2]where1=2--复制表结构
createtable[表名]asselect[字段1],[字段2]from[表名]
插入数据
insertinto[表名]values(值1,值2,.......值n);
insertinto[表名](字段1,字段2,....字段n)values(值1,值2,.......值n);
insertinto[表名]select[字段1,字段2,.....字段n]from[表名]where字段='值';
insertinto[表名]([字段1,字段2,.....字段n])select[字段1,字段2,.....字段n]from[表名]where字段='值';
insertallinto[表名1]values(1004,'haha');
into[表名2]values(1005,'heihei')select*fromdual;
---删除数据
deletefrom[表名]where字段='值';--速度慢,要写日志:
把日志写在回滚表中
truncatetable[表名]--速度快,不需要写日志
droptable[表名]
dropuser[用户名]cascade
dropview[视图名]
1.下面的语句用来查询哪些对象被锁:
selectobject_name,machine,s.sid,s.serial#
fromv$locked_objectl,dba_objectso,v$sessions
wherel.object_id = o.object_idandl.session_id=s.sid;
2.下面的语句用来杀死一个进程:
altersystemkillsession'24,111';(其中24,111分别是上面查询出的sid,serial#)
修改数据
update[表名]set[字段]='值'where字段='值';
update[表名]set[字段]='值',[字段1]='值'where字段='值';
查询数据
select字段1,字段2,......字段nfrom[表名];
select字段1,字段2,......字段nfrom[表名]where字段='值';
select字段1,字段2,......字段nfrom(select字段1,字段2,
......字段nfrom[表名]where字段='值')where字段='值'
select字段1,字段2,......字段nfrom[表名]where字段=select字段1from[表名];
组函数--聚合函数
MAX,MIN,AVG,COUNT,SUM,WM_CONCAT
常用的函数
substr()upper()lower()instr()lpad()rpad()trim()
to_char()to_date()trunc()round()nvl()nvl2()
initcap()next_day()length()replace()mod()
------------------
case
when表达式then值1
when表达式then值2
else默认值
end
decode(字段,
'值1','值1结果',
'值2','值2结果',
'值3','值3结果',
'默认值'
)
-----case和decode区别:
decode只能判断等值的表达式
case可以判断区间表达式
联合查询
左连接:
select*from[表名1]leftouterjoin[表名2]on表名1.字段=表名2.字段
select*from[表名1],[表名2]where表名1.字段=表名2.字段(+)
右连接
select*from[表名1]rightouterjoin[表名2]on表名1.字段=表名2.字段
select*from[表名1],[表名2]where表名1.字段(+)=表名2.字段
全链接
select*from[表名1]fullouterjoin[表名2]on表名1.字段=表名2.字段
---去重复,(有重复的数据只显示一次)
select*fromemp20
union
select*fromemp
---不去重复(有重复的数据全部显示)
select*fromemp20
unionall
select*fromemp
---(差集[emp20中除了交集以外的部分])
select*fromemp20
minus
select*fromemp
---交集
select*fromemp20
intersect
select*fromemp
分组:
groupby
排序:
orderbydesc降序默认升序asc
分组之后过滤:
having
单行过滤:
where
select*fromemp
where.....
groupby.....
having......
orderby.....
使用groupby要注意的事项:
select后面的字段必须由:
组函数或者是分组的字段
---分页
select*from
(selectrownumno,e.*from
(select*fromemporderbysaldesc)
ewhererownum<=10)whereno>1;
---创建视图
CREATE[ORREPLACE][FORCE|NOFORCE]VIEWview[(aliaslist)]
ASsubquery
[WITHCHECKOPTION[CONSTRAINTcname]
[WITHREADONLY]
ORREPLACE表示如果视图已经存在则重新创建
FORCE表示创建视图而不管基表是否存在,NOFORCE表示只有基表存在的情况下才创建视图(默认情况)
view表示视图名
aliaslist表示别名列表(别名之间用逗号隔开,别名的个数需要和子查询中选择的字段或表达式的个数一致)
subquery表示子查询
WITHCHECKOPTION表示只有可访问的行在视图中才能被插入或更新
cname表示为约束指定的名称
WITHREADONLY表示在该视图中不可执行DML操作
insertinto[视图名]values('值1','值2',.....'值n');//此数据插入视图对应的表中
update[视图名]set[字段]='值'where[字段]='值';
deletefrom[视图名]where[字段]='值';
如果视图中包含下面的内容,就不可以从视图中删除数据
组函数
GROUPBY子句
DISTINCT关键字
伪列ROWNUM关键字
如果视图中包含下面的内容,就不可以在视图中更新数据:
组函数
GROUPBY子句
DISTINCT关键字
伪列ROWNUM关键字
用表达式定义的字段
如果视图中包含下面的内容,就不可以在视图中插入数据:
组函数
GROUPBY子句
DISTINCT关键字
伪列ROWNUM关键字
用表达式定义的字段
基本中的NOTNULL字段不在视图中
/*
创建序列
*/
createsequenceseq_semple
incrementby1
startwith1
maxvalue9999
nocycle
nocache
--修改序列
altersequenceseq_semple
incrementby2
maxvalue9999
nocycle
nocache
insertintotestvalues(seq_semple.nextval,'小明','123');
insertintotestvalues(seq_semple.nextval,'小花','1234');
insertintotestvalues(seq_semple.nextval,'小丽','234');
insertintotestvalues(seq_semple.nextval,'小红','12345');
commit;
select*fromtest;
selectseq_semple.nextval,seq_semple.currvalfromdual;
selectseq_semple.currvalfromdual;
--删除序列
dropsequenceseq_semple;
/*
索引
*/
--创建索引
createtablet(
tidnumberprimarykey,
tnamevarchar2(20),
telnumberuniquenotnull--默认自动创建索引
)
--创建索引
createindexi_test1
ontest(tname,tpsw)
--删除索引
dropindexi_test1;
--系统权限
createuserjmidentifiedbyjm123;
createusertomidentifiedbytom123;
--让用户jm需要登录;并且让用户同时具有赋权限的功能
grantcreatesessiontojmwithadminoption
--让jm给tom赋权限(登录)
grantcreatesessiontotom;
--收回jm的登录权限,但是,tom依然可以登录;(不是级联回收)
revokecreatesessionfromjm;
--对象权限emp表在scott【方案】,给mqx用户赋予访问(select)emp表的权限
grantselectonscott.emptomqx;
--给jm赋予查emp表的权限,同时权限下放
grantselectonscott.emptojmwithgrantoption;
grantselectonscott.emptotom;
--回收jm的访问emp表的权限(tom不能访问到emp表,证明是级联回收)
revokeselectonempfromjm;
--如何创建角色
createrolejoy;
--给角色授权
grantcreatetable,createview,createsequence,
createproceduretojoy;
--给用户授予角色
grantjoytojm;
--创建存储过程
--publicvoidshow(){System.out.println();}[/as]
createorreplaceprocedurep_pro
is
begin
insertintotestvalues(2,'狗狗','234');
end;
--执行存储过程
begin
p_pro;
end;
/*publicstaticvoidshow(Stringmsg){
System.out.println(msg);
}
publicstaticvoidmain(Stringargs[]){
Stringmsg="HelloWorld";
show(msg);
}
*/
--有参数的存储过程
createorreplaceproceduresp_pro(
iidnumber,
inamevarchar2,
ipassvarchar2
)
is
begin
insertintotestvalues(iid,iname,ipass);
end;
--执行存储过程
begin
sp_pro(3,'猪猪','345');
end;
--根据ID查找动物的名字;
createorreplaceproceduresp_p
(
iidinnumber,
outnameoutvarchar2,
outtpswoutnumber
)
is
begin
selecttname,tpswintooutname,outtpswfromtestwheretid=iid;
endsp_p;
/*
inti=0;
Stringstr="a";
Strings;
Scannerinput=newScanner(System.in);
s=input.next();
*/
declare
onamevarchar2(20);--接受返回来的值
outpswvarchar2(20);
iidnumber:
='&请输入要查询的ID';
begin
sp_p(iid,oname,outpsw);
dbms_output.put_line('动物的名称:
'||oname||'动物的编号:
'||outpsw);
--System.out.println("HelloWorld!
");
exception
whenno_data_foundthen
dbms_output.put_line('朋友输入的数据有误,请重新输入');
end;
commit;
select*fromtest;
truncatetabletest;
--删除存储过程
dropprocedure[存储过程名];
--查询所有存储过程
select*fromuser_objectswhere
object_type='PROCEDURE';
--创建函数
createorreplacefunctionf_m1
returnvarchar2
is
begin
return'我真是一个天才啊!
';
end;
/*21:
332016/1/10
publicStringfindById(intid){
Stringmsg=null;
if(id==1){
msg="星期一";
}elseif(id==2){
msg="星期二";
}
returnmsg;
}
*/
--执行函数
selectf_m1fromdual;
createorreplacefunctionf_m2(iddnumber)
returnvarchar2
is
outnamevarchar2(20);
begin
selecttnameintooutnamefromtestwheretid=idd;
returnoutname;
end;
--执行函数【命令窗口执行】
varsnamevarchar2;
callf_m2
(2)into:
sname;
--查询所有函数名称
selectobject_namefromuser_objects
whereobject_type='FUNCTION';
--创建触发器
createorreplacetriggert_test
beforeinsertontest
foreachrow
begin
--dbms_output.put_line('用户名'||:
new.tname);
--dbms_output.put_line('密码'||:
new.tpsw);
insertintomsgtablevalues(seq_se.nextval,'宠物名:
'||:
new.tname||'宠物编号'||:
new.tpsw);
end;
--调用存储过程向test表中插入数据
begin
sp_pro(13,'美国摩登考拉No.1','888');
end;
altertablemsgtablemodify(msgvarchar2(50));
commit;
--删除触发器
droptriggert_test;
select*frommsgtable;
/*
游标
*/
declare
cursorculisselect*fromtest;
srowtest%rowtype;
begin
opencul;
fetchculintosrow;
whilecul%foundloop
dbms_output.put_line(srow.tid||srow.tname||srow.tpsw);
fetchculintosrow;
endloop;
closecul;
end;
declare
v_numnumber:
=10086;
v_contextvarchar2(20)default'本人很聪明!
';
v_ipconstantnumber(3,1):
=10.1;
v_salemp.sal%type;
v_empemp%rowtype;
begin
v_context:
='我们都是好孩子';
dbms_output.put_line(v_context);
end;
declare
v_rowemp%rowtype;
begin
select*intov_rowfromempwhereempno=7369;
dbms_output.put_line(v_row.ename);
end;
declare
agenumber:
=20;
begin
age:
='&请输入你的年龄';
ifage<20then
dbms_output.put_line('我还不够法定结婚年龄!
');
elseifage>22then
dbms_output.put_line('我刚大学毕业!
');
else
dbms_output.put_line('我该的啦!
');
endif;
endif;
end;
for语法:
for循环变量in开始值..结束值loop
执行语句;
endloop;
--如:
declare
inumber;
begin
foriin1..111loop
dbms_output.put_line(i);
endloop;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- DB 数据库 知识