数据库之Oracle

一. 用户的管理

1. 用户就是好比公司的某个人,而权限是这个人能在公司做什么,他的角色就是说明他的职位。

2. 用户的权限分为:

系统权限:对别的用户的管理操作。

对象权限:对保存的数据的管理操作。

3. 对用户的增删改查

增:例如我创建一个用户名为”atongmu“密码为“123”的用户:create user c##atongmu identified by 123;

删:不能自己删除自己,需要dba权限才可以,这样有了之后,删除用户:drop user  c##atongmu cascade;

改:一般就是改去密码:alter user c##scott identified by root;

查:当前用户下所有的表:select * from user_tables;

显示当前数据库的所有表:select * from tab;

select * from dba_users; 查看数据库里面所有用户,前提是你是有dba权限的帐号,如sys,system
                  select * from all_users;  查看你能管理的所有用户!
                  select * from user_users; 查看当前用户信息 !

4. 用户权限管理

(什么是权限:就是你能不能操作某条SQL语句)

增:grant connect to C##用户名;

grant 操作名 on 表名 to 用户名 ;

grant 操作名 on 表名 to 用户名 with grant option;

删:revoke 操作名 on 表名 from 用户名;

revoke select on emp from us_test;

5. 一个用户的综合应用案例

(1)  创建用户,默认创建在哪个数据库里面,但是默认没有任何权限,不能登录任何数据库的,需要授权。

(2)  为其制定相应的权限,需要sys/system用户赋予权限: grant connect to xiaoming;

(3)  创建表create table TestTable(userId varchar2(30),userName varchar2(30));

(4)  grant resource to xiaoming;

(5)  查看表的列:desc test;

(6)  grant select on c##scott.emp to xiaoming;(sys,system,表所有者,scott)

(7)  select * from c##scott.emp;

(8)  收回权限(有权限的人都可以收回):revoke select on emp from xiaoming

(9)   权限传递:grant select on emp to xiaoming with grant option

(11)  如果授权在上级被会收,下一级也会被回收

(12)   把测试用户删除掉:drop user C##用户名 cascade;

二. 数据表管理

1 表和列的命名的规则

必须以字母开头,长度不能超过30个字符,不能使用oracle的保留字,只能使用下面的字符a-zA-Z0-9$#_等。

2 数据类型

number(5,2):5位数字,2位小数.范围(-10^38,10^38)

char:定长,2000字符,字符串char(5)     查询极快,浪费空间

varchar2:变长,4000个字符(8000个字节)

clob(character large object) 字符型大对象,最大4G

date:年月日 时分秒

blob:二进制数据电影,图片,音乐,4G,不会放到数据库里面,文件服务器

3 表操作

增: create table student(id,varchar2(30),username varchar2(30));

删:drop table 表名

改:rename 旧表名 to 新表名;

查:select 表名 from user_tables;//当前用户的表

三. Oracle的查询

(数据库中,我们一般用的最多的就是查询)

1 纯查询语句

select 列限定 from 表限定 where 行限定。

去除重复行(distinct):select distinct 列限定 from 表名 where 行限定(z只能单行查询)。

like 模糊行限定。

in:枚举查询: select * from emp where empno in(12,56,90);

where条件的组合查询(and,or) : select * from emp where (sal>500 or job='MANAGER') and ename                    like 'J%';

2 多表查询

笛卡尔乘积的原理:select * from emp,dept;

逻辑外键多表联查: select e.name,d.name, from emp e,dept  d where e.deptno=d.deptno

3 子查询/嵌套查询

什么叫子查询:多个select 关键词在同一个查询语句中,这种情况下,就是子查询.把内部select查询到的结果当成一张表,在通过外面的select语句查询出最终的结果。

例如:select * from emp where job in(select distinct job from emp where deptno=20);

4 SQL函数查询

字符函数:

lower(字符):把字符串转化为小写。

upper(char):将字符串转化为大写格式。

length():返回字符串的长度。

substr(char,m,n):取字符串的字串。

replace(char,search_s,replace_s) : 后换前

instr(char_1,char_2,[,n[,m]]):取得chr_2,在char_1中起始位置下标

数学函数:

abs(n):取绝对值

round(n,[m]):四舍五入,n为数据,m为四舍五入到第几位

trunc(n,[m]):截取,截取到小数点的第几位

mod(m,n):对m用n取摸(余数)

floor(n):向下取整

ceil(n):向上取整

5 分页查询

rownum分页 :select * from (select ta.*, rownum rn from (select ename, sal from emp order by sal desc) ta where rownum <=10) where rn >= 6;

RowID分页: select * from emp where rowid in(select rid from (select rownum rn,rid ,sal from(select rowid rid,sal from order by sal desc)where rownum<10)where rn>5)order by sal;

rownum()函数分页:select * from (select t.*,row_number() over (order by sal desc) rkfrom emp t)where rk<10 and rk>1;

 

最新文章

  1. CSS知识图--转载
  2. 深入学习jQuery选择器系列第八篇——过滤选择器之伪子元素选择器
  3. Evolution项目(1)
  4. String Mybatis 多数据源配置
  5. 【转】虚拟机VMware3种网络模式(桥接、nat、Host-only)的工作原理
  6. String与InputStream相互转换
  7. Brn系列网上商城发布指南
  8. URLEncode与URLDecode总结与实现
  9. C++基础笔记(二)C++对C的扩展
  10. UIViewAnimationOptions swift 2
  11. typedef和自定义结构体类型
  12. if
  13. 关于T-SQL重编译那点事,内联函数和表值函数在编译生成执行计划的区别
  14. Java学习日记-6 继承
  15. UIWebView 使用要注意的几点
  16. leetcode之旅(10)-Roman to Integer
  17. ubuntu base make 未找到命令
  18. NOIP2018备考——DP专题练习
  19. Android.mk 用法介绍
  20. 『编程题全队』Alpha 阶段冲刺博客Day4

热门文章

  1. DevExpress控件GridControl使用 z
  2. 1.【nuxt起步】-nuxt是什么?
  3. Utuntu 和 window共享文件
  4. 控制CUP占用率曲线
  5. 1079. Total Sales of Supply Chain (25)【树+搜索】——PAT (Advanced Level) Practise
  6. python(22)- 递归和函数式编程
  7. android中的MD5、Base64、DES/3DES/ADES加解密
  8. 项目部署到niginx title乱码问题
  9. java 回文字符串
  10. oracle死锁的检测查询及处理