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
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
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、求包含%的雇员姓名