一、创建用户和授予权限
1.1 环境:
Oracle 11g xe
第三方图形客户端: PLsql
Windows 10
1.2 sql 语句
创建用户:create user 用户名 identified by 用户名;
分配权限或角色:grant connect,resource to 用户名;
取消权限或角色:revoke 权限/角色 from 用户名;
1.3 常用系统预定义角色
connect:临时用户
resource:更为可靠的正式用户
注意:角色是具有名称的一组权限的组合。
二、sql 语言的总结
2.1 sql可以做什么?
2.2 sql语言的分类
- DQL (数据查询语句)select
- DML(数据操作语句)insert、update、delete
- DDL(数据定义语句)create、alter、drop
- DCL(数据控制语句)grant、revoke
- TCL(事务控制语句)savepoint、rollback、set transaction、commit
【注意】数据操作语句针对表中的数据,而数据定义语句针对数据库对象(表、索引、视图、触发器、存储过程、函数、表空间等)
2.3 select 查询语句
2.3.1 字段别名
2)别名如果含有空格或其他特殊字符或大小写敏感,需用双引号引起来;
3)as 可以省略。
2.3.2 去除重复行
在搜索字段名前,加一个 distinct 关键字
例如:select distinct deptno from emp;
2.3.3 排序---order by
排序包括升序(asc,默认升序,该关键字可不写)和 降序(desc)
例如:select empno,ename,sal from emp order by sal; --------以sal升序进行排序
select empno,sal from emp order by sal desc; --------以sal降序进行排序
2.3.4 where 字句
举例:
select * from emp where deptno = 10;
select * from emp where ename = ‘SMITH’;
select * from emp where hiredate = ‘02-4月-81’;
注意:
1)字符串和日期值要用单引号括起来
2)字符串大小写敏感
3)日期值格式敏感!!!
其他关键字:
2.3.5 逻辑运算符
例如:select * from emp where deptno = 10 and sal > 1000;
2.3.6 sql优化问题
and:把检索结果较少的条件放到后面
or :把检索结果较多的放到后面
2.3.7 四种运算符
重要:算术 > 连接 > 比较 > 逻辑
优先级:
- * /
- + -
- ||
- >,<,>=,<=,<>
- is [not] null,like [not] in
- [not] between… and…
- not
- and
- or
2.3.8 使用函数
函数分为:
多行函数:sum(),avg() ----仅适用数值型。 count(),max(),min() -----适用任何类型数据
2.3.8.1 单行函数
字符函数:
initcap(char) -----首字母大写
lower(char) -----转换为小写
upper(char) -----转换为大写
ltrim(char,set) -----左剪裁
rtrim(char,set) -----右剪裁
replace(char,search_str,replace_str) -----字符串替换
instr(char,substr[,pos]) -----查找字符串位置,例如: instr(‘worldwide’,’d’)
substr(char,pos,len) -----取子字符串
concat(char1,char2) -----连接字符串
数值函数:
日期函数:
转换函数:
其他函数: