DB数据库知识
- 格式:docx
- 大小:25.05 KB
- 文档页数:11
scott密码是 tiger sys 密码是change_on_install system 密码是 manager sysman密码是oem_temp 1.create user [用户名] identified by [密码];--创建用户 --如何修改用户密码:
alter user [用户名] identified by [新密码] 2.grant connect to [用户名]--赋予用户登录权限 grant create session to [用户名] 解锁用户:alter user scott account unlock; 3.grant resource to [用户名] --赋予CRUD 4.grant [权限命] on[表名] to [用户名] revoke [权限命] on[表名] from [用户名] grant [权限命] on[表名] to [用户名] with grant option [权限包括] select,update,insert,delete,all 5.查看数据库字符集 select * from nls_database_parameters
--[数据类型] number -10 38次方--- 10 38次方 char 定长,最多存储2000字符,查询效率高 varchar2 可变长,最多存储4000字符,查询效率低 clob最大存储4G blob 存储二进制数据,4G number(8,2); 101.23
create table [表名]( 字段数据类型[not null or primary key,如果没有not null,默认可以为空] , 字段数据类型[not null or check ,如果没有not null,默认可以为空] , 字段数据类型[not null or unique ,如果没有not null,默认可以为空] ) ----------------------------------------------------------------------------------- 修改表的操作 添加字段 alter table [表名] add (字段名,数据类型) 删除字段 alter table [表名] drop column [字段名] 修改字段 alter table [表名] modify (字段名,数据类型) 给已有的表重命名 rename [原表名] to [新表名] 添加主键 alter table [表名] add constraint [约束名] primary key(字段);--如果有多个,称之为联合主键 添加外键 alter table [从表名] add constraint [约束名] foreign key (从表名的外键字段) references 主表(主键字段) on delete cascade
删除约束 alter table [从表名] drop constraint [约束名] 创建表 create table [表名] as select * from [表2] -- 复制表2 create table [表名] as select * from [表2] where 1=2 --复制表结构 create table [表名] as select [字段1],[字段2] from [表名] 插入数据 insert into [表名] values (值1,值2,.......值n); insert into [表名] (字段1,字段2,....字段n) values (值1,值2,.......值n); insert into [表名] select [字段1,字段2,.....字段n] from [表名] where 字段='值'; insert into [表名] ([字段1,字段2,.....字段n]) select [字段1,字段2,.....字段n] from [表名] where 字段='值'; insert all into [表名1] values (1004,'haha'); into [表名2] values (1005,'heihei') select * from dual; ---删除数据 delete from [表名] where 字段='值'; --速度慢,要写日志:把日志写在回滚表中 truncate table [表名] --速度快,不需要写日志 drop table [表名] drop user [用户名] cascade drop view [视图名]
1.下面的语句用来查询哪些对象被锁: selectobject_name,machine,s.sid,s.serial# fromv$locked_objectl,dba_objects o ,v$session s where l.object_id = o.object_id and l.session_id=s.sid; 2.下面的语句用来杀死一个进程: alter system kill session '24,111'; (其中24,111分别是上面查询出的sid,serial#)
修改数据 update [表名] set [字段]='值' where 字段='值'; update [表名] set [字段]='值' , [字段1]='值' where 字段='值';
查询数据 select 字段1,字段2,......字段n from [表名]; select 字段1,字段2,......字段n from [表名] where 字段='值'; select 字段1,字段2,......字段n from (select 字段1,字段2, ......字段n from [表名] where 字段='值' ) where 字段='值' select 字段1,字段2,......字段n from [表名] where 字段 = select 字段1 from [表名]; 组函数--聚合函数 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] left outer join [表名2] on 表名1.字段=表名2.字段 select * from [表名1],[表名2] where 表名1.字段=表名2.字段(+) 右连接 select * from [表名1] right outer join [表名2] on 表名1.字段=表名2.字段 select * from [表名1],[表名2] where 表名1.字段(+)=表名2.字段 全链接 select * from [表名1] full outer join [表名2] on 表名1.字段=表名2.字段
---去重复,(有重复的数据只显示一次) select * from emp20 union select * from emp ---不去重复(有重复的数据全部显示) select * from emp20 union all select * from emp ---(差集[emp20中除了交集以外的部分]) select * from emp20 minus select * from emp
---交集 select * from emp20 intersect select * from emp
分组:group by 排序:order by desc降序默认升序asc 分组之后过滤:having 单行过滤:where
select * from emp where ..... group by ..... having ...... order by .....
使用 group by 要注意的事项: select 后面的字段必须由:组函数或者是分组的字段
---分页 select * from (selectrownum no, e.* from (select * from emp order by saldesc) e where rownum<=10) where no >1;
--- 创建视图 CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view[(aliaslist)] AS subquery [WITH CHECK OPTION [CONSTRAINT cname] [WITH READ ONLY] OR REPLACE表示如果视图已经存在则重新创建 FORCE表示创建视图而不管基表是否存在,NOFORCE表示只有基表存在的情况下才创建视图(默认情况) view表示视图名 aliaslist表示别名列表(别名之间用逗号隔开,别名的个数需要和子查询中选择的字段或表达式的个数一致)