首页 > Linux操作系统 > Linux操作系统 > oracle学习笔记(1)
oracle学习笔记(1)
一、历史
先说说oracle的一点历史,自从oracle 8i后她就完全是有java语言编写的。现在企业用的比较多的是oracle 9i 和 oracle 10g(05年)。oracle 11g(07年)被称为是最好的吧! 但不是谁都换得起的(值不值的问题),毕竟企业级的服务是要付费的。
二、准备安装
以安装oracle 11g为例,安装之前,要先安装jdk,下载好后安装,注意环境变量的配置,记一下:在用户变量中添加三个用户变量
PATH C:\Java\jdk1.6.0_23\bin
CLASSPATH .; C:\Java\jdk1.6.0_23\jre\lib\rt.jar
JAVA_HOME C:\Java\jdk1.6.0_23
到命令行中测试一下 ………..
安装oracle 10g 或oracle 11g(都安装过),比较方便
三、启动数据库的方法
在这之前,说说oracle 有关的服务项,其中服务OracleServiceORCL是必须要开启的,另一个是OracleOraDb11g_home1TNSListener 与第三方工具有关,监听服务的重要在于此。其他的服务可以关掉。
启动数据库的方法
(1)在命令行中输入
sqlplus 进入黑窗口后,输入你要用的用户名和密码
或 sqlplus /nolog 看到,SQL>conn scott/密码
(2)命令行中 sqlplusw /nolog 进入oracle自带的sql*plus
也可以到所安装的oracle中看到 sql*plus
(3) 在url中http://localhost:5560/isqlplus/ 但要另启动的一个控制台服务。
四、解锁与连接某已锁用户
必须是系统system或sys的权限
SQL>conn system/密码
假设hr 用户没被解锁
SQL>alter user hr account unlock;
解锁以后要设置用户hr 的密码
SQL>alter user hr identified by passwd;
这样就可以使用hr 用户了
SQL>conn hr/passwd
显示 连接
SQL> select * from tab;
可以 查到到用户hr 下有哪些表……
五、select 语句总结
Select [distinct] * ,[column1,column2,….]
From
table,view,subquery
[
WHERE search_condition ]
[
GROUP BY group_by_expression ]
[
HAVING
search_condition ]
[
ORDER BY order_expression [ ASC | DESC ] ]
这里以hr 用户下的employees表为例
(1) Select [distinct] * ,[column1,column2,….]
①+ ,- ,* ,/
select first_name, salary*12+10000 total from employees;
select first_name, (salary+800)*13 total from employees;
select hire_date, hire_date+1, hire_date-1 from employees;
注:日期类型只有加和减
② || (连接符)
select first_name||last_name from employees;
③别名 ---- “”(别名中有空格必须有“”)
1) select first_name||last_name “name” from employees;
2) select first_name||last_name as name from employees;
3) select first_name||last_name “n a m e” from employees;
④空值 null( 不等于 0,空格)
select first_name||null|| last_name name from employees;
- 无影响
select first_name , salary*(1+null) from employees;
--- salary*(1+null)为空
⑤distinct(关键字来过滤掉多余的重复记录只保留一条)
select distinct department_id from employees;
select distinct department_id,job_id from employees;
注意 distinct的位置 ,下面是一个错误的例子
select job_id ,distinct department_id from employees;
可以去google 进一步弄清 distinct
(2) where 子句
① 比较运算符
select last_name,salary
from employees
where salary >= 6000;
select * from employees
where last_name = 'King';
② between A and B(包含A 和B,且A<=B)
select last_name,salary
from employees
where salary between 2500 and 3000;
--不会报错,但结果为空
select last_name,salary
from employees
where salary between 3000 and 2500;
--下面语句等价于 where salary = 3000 不建议,效率不高
select last_name,salary
from employees
where salary between 3000 and 3000;
③ in
select employee_id,last_name,department_id
from employees
where department_id in (10,20,30);
④like % _
select first_name,salary
from employees
where first_name like 'A%';
select first_name,salary
from employees
where first_name like '%a%';
select first_name,salary
from employees
where first_name like '_a%';
⑤转义符
select first_name,salary
from employees
where first_name like '%*_%' escape '*';
⑥is null
select last_name,salary,commission_pct
from employees
where commission_pct is null;
---------error! where commission_pct = null ;
---------error where commission_pct = ' ';
⑦ and , or ,not
优先级比较: 比较运算符 >not > and > or
select last_name,salary,job_id
from employees
where job_id='AD_ASST' or job_id = 'IT_PROG' and salary > 7000;
-- 同时满足salary > 8000
select last_name,salary,job_id
from employees
where (job_id='AD_ASST' or job_id = 'IT_PROG') and salary > 7000;
(3) GROUP BY group_by_expression
主要是分组函数的应用
avg, sum, count, max, min…..
--count
select count(*) from employees;
select count(employee_id) from employees;
注:count中用主键字段效率高,不建议用*.
--求平均工资
select avg(salary) avg_salary from employees;
--每个部门的平均工资
select department_id,avg(salary)
from employees
group by department_id;
注意:select 之后除了组函数外,其他的字段都要放到group by后面
只有用到组函数,才能用到group by
以下是反例和特例
--correct ,特例类型
select avg(salary)
from employees
group by department_id;
--error1 不是 GROUP BY 表达式
select department_id,salary
from employees
group by department_id;
--error2
select department_id,job_id,avg(salary)
from employees
group by department_id;
--correct
select department_id,job_id,avg(salary)
from employees
group by department_id,job_id;
---error3 此处不允许使用分组函数;where 中不能用组函数
select department_id,avg(salary)
from employees
where avg(salary)>8000
group by department_id;
(4) HAVING search_condition
having 子句只能用于group by 配合使用
select department_id,avg(salary)
from employees
group by department_id
having avg(salary) > 7000;
(5) order by …
descend(desc) 下降 ; ascend(asc)上升
select last_name,hire_date,department_id,salary
from employees
order by department_id, salary desc;
注:在order by后有多个字段时,以第一个字段为主,第二个字段在第一个的基础上排序,依次类推….
--correct
select first_name,salary,salary*12 total
from employees
order by total desc;
注: 只有在order by这个子句后可以用别名,其他的子句都不可以
Eg:
--error
select first_name, salary*12 total
from employees
where total > 20000
order by total desc;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/25436591/viewspace-702012/ ,如需转载,请注明出处,否则将追究法律责任。