oracle之SQL结构化查询语言

一、SQL简介

    SQL语言的全称是Structured Query Language。

二、集成环境plsqldev

    全称PL/SQL Developer,它是提供给Oracle 数据库开发者开发存储过程的非常方便易用的集成环境 (IDE) . 它运行速度快,稳定,占用内存少,是很不错的小型工具。

三、登陆

    使用scott用户登录,如果忘记解锁:
   1)使用DBA登录
    2)解锁命令
    alter user scott account unlock;

   3)测试:
    select * from emp;
    向oracle发出命令,查询表emp的全部数据。emp的表属于scott用户。默认从当前用户找。
    如果使用其他用户登录,想查询其他用户的表则如下:
    select * from 用户.表名

四、服务

1、所有的服务改成"手动"
2、启动两个
  1)、监听服务:OracleOraDb10g_home1TNSListener
监听客户端的连接
  2)、数据库服务:OracleServiceORCL
命名规则:OracleService+实例名

五、常用命令

1、sqlplus
sqlplus /nolog
conn scott/tiger@orcl
show user
set linesize 150
set pagesize 20
passw
conn sys/abc123456@orcl as sysdba
select * from emp where ename=‘&ename’

a)alter user scott account unlock
使用管理账号
b)sqplusw
spool on
spool d:/test.txt
select * from emp;
spool off

六、常见错误

ORA-12541: TNS: 监听程序当前无法识别连接描述符中请求的服务

D:\oracle\product\10.2.0\db_1\network\ADMIN

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = PLSExtProc)
      (ORACLE_HOME = d:\oracle\product\10.2.0\db_1)
      (PROGRAM = extproc)
    )
  (SID_DESC =
   (GLOBAL_DBNAME = ORCL)
   (ORACLE_HOME = d:\oracle\product\10.2.0\db_1)
   (SID_NAME = ORCL)
    )
  )

七、SQL分类

1、结构化查询语言 (Structured Query Language),具有定义、查询、更新和控制等多种功能,是关系数据库的标准语言。
2、SQL分类:
数据操纵语言DML Data Manipulation Language :
SELECT INSERT UPDATE DELETE
数据定义语言DDL Data definition language :
CREATE ALTER DROP RENAME TRUNCATE
数据控制语言DCL Data Control Language :
GRANT REVOKE
Transaction:commit rollback savepoint

查看所有表:select table_name from user_tables;
查看表结构:describe dept;(或者desc dept;)
emp表雇员表
Empno: 雇员工号          Ename: 雇员名字
Job:工作。(秘书、销售、经理、分析员、保管)
Mgr(manager):经理的工号       Hiredate:雇用日期
Sal:  工资       Comm: 津贴      Deptno: 所属部门号
dept表部门表
Deptno:部门号  Dname:部门名字   Loc: 地址
salgrade表薪水等级
Grade:等级     losal:最低工资   hisal:最高工资
bonus表奖金
Ename:雇员名字,  job:工作,  
sal: 工资  comm:津贴

为表添加注释
comment on table emp is ‘雇员表';

为列添加注释
comment on column emp.Empno is '雇员工号';

1)Select-from-where句型
2)SELECT [DISTINCT] {*,column alias,..}
FROM table
Where 条件表达式

3)alias

Column alias
Column “alias”
Column as alias

a)SELECT语言(A)

1)检索单个列
select  col  from tableName;
2)检索多个列
select  col1, col2,col3  from tableName;
3)检索所有列
select  *  from tableName;
使用通配符的优点:书写方便、可以检索未知列
使用通配符的缺点:降低检索的性能
4)给检索出的列起个别名
select job  "gong zuo"   from emp;
select job  as  "gong zuo"   from emp;

select * from emp;
select empno from emp;
select empno empnumber from emp;
select empno “empnumber” from emp;
select empno as empnumber from emp;
select distinct empno from emp;

b)where (A)

1)条件比较
=,!=,<>,<,>,<=,>=,any,some,all
is null,is not null
between x and y
exists(sub-query)
in(list),not in(list)
like  _ ,%,escape ‘\‘   _\% escape ‘\’

select * from emp where comm is null;
select * from emp where comm is not null;
select ename, sal from emp where sal in (800, 1250, 1500, 2000);
select ename, sal from emp where ename in (‘SMITH’, ‘ALLEN’, ‘KING’);
select ename, sal from emp where sal between 1000 and 2500;
select ename, sal from emp where deptno <> 10;


2)逻辑复合条件
not,(and,or) and优先级高
列出deptno为10或者30,并且工资>2000的所有人。
select * from Emp where deptno=30 or deptno=10 and sal>2000;
这个命令列出的人中薪水有<2000的,为什么
计算次序问题的解决,最好用括号进行分组处理

SQL优化问题:
AND:  把检索结果较少的条件放到后面
OR:  把检索结果较多的条件放到后面

select ename, hiredate from emp where hiredate > ’20-2月-81’;
select ename, sal from emp where deptno = 10 and sal > 1000;
select ename, job , deptno from emp where deptno = 10 or job = ‘CLERK’;
select ename, sal from emp where sal not in (800, 1500, 2000);
列出deptno为10或者30,并且工资>2000的所有人。

八、练习

1、查询部门编号为10的员工信息
2、查询年薪大于3万的人员的姓名与部门编号
3、查询佣金为null的人员姓名与工资
4、查询工资大于1500 且 and 含有佣金的人员姓名
5、查询工资大于1500 或 or含有佣金的人员姓名
6、查询姓名里面含有 S 员工信息 工资、名称
7、求姓名以J开头第二个字符O的员工姓名的与工资
8、求包含%的雇员姓名

猜你喜欢

转载自m15047453160.iteye.com/blog/2029071