SQL PL/SQL
Database 数据库
表(table)
SQL(structured query language结构化查询语言)
1 DDL(data definition language数据定义语言)
create table alter table drop table
定义table structure
列(column)数据类型(data type)宽度(width)约束(constraint)
2 DML(data manipulation language 数据操作语言)
insert(插入) update(修改) delete(删除)
行(row) data
3 TCL(transaction control language 事务控制语言)
commit (提交)
rollback(回滚)
4 DQL(data query language数据查询语言)
select
5 DCL(data control language数据控制语言)
grant(授予权限)
revoke(回收权限)
数据库厂商提供 RDBMS software
oracle oracle10g mysql
MS sql server
IBM DB2
SYBASE ASE
DBA(database administrator 数据库管理员):
install software RDBMS(relationship database management system关系型数据库管理系统)--〉database(create database open)
-->登录数据库
远程登录20机器,使用该机器上的命令
telnet 192.168.0.20 伪终端
openlab/open123
登录数据库
client程序和数据库服务器在一台机器
ORACLE_SID
%sqlplus openlab/open123
client程序和数据库服务器在两台机器
%proc ip:port:oracle servername(作用等价于ORACLE_SID):username:password
telnet 192.168.0.23
openlab/open123
solaris的vi的删字符不能在编辑模式下按backspace键,需要转到命令模式下按x键删字符
dept department
location 位置
emp employee
bonus 奖金
salary 月薪
job 职位
mgr 领导的员工号
hiredate 入职日期
create table emp_mine
as
select * from emp_hiloo;
create table dept_mine
as
select * from emp_hiloo;
DQL
select
源表 结果集
1投影操作 列 select
2选择操作 行
3连接操作(多表查询)
select语句
子句
select from where group by having order by
from-->where-->select (distinct)
select colname,colname
from tablename
sql语句执行
sqlplus把语句通过连接给oracle server process(oracle的一个进程),server process对其进行语法语义分析,产生执行计划,按执行计划产生结果,将结果集返回给sqlplus.
双引号 表示列别名,不可以用来表达字符串,列名中包含空格以及大小写敏感时用双引号
单引号 表达字符串
(15000+null)*12 --> null
空值转换函数
nvl(p1,p2)
if p1 is null then
return p2;
else
return p1;
end if;
where子句
where 条件表达式 (列名 比较运算符 值)
sql条件表达式
=单值运算符
(between and) (where >= and <=) 范围 [a,b]
in 多值运算符 (=any)(where = or = or = ) 离散值 集合
通配符
_ 任意一个字符
% 0或任意多个字符
'S' 'S_' 'S%'
like
is null
否定形式
= <> != ^=
is null is not null
like not like
between and not between and
in not in
not in salary not in (3000,5000)
where salary <> 3000
and salary <> 5000
not in <>all (跟所有的都不相等)
select语句
1select子句可以跟列名,表达式,函数
2select后面的distinct表示去重,distinct之后和from之前的所有列联合去重。
3where执行在select子句之前,不可以用列别名;where子句后列名的位置可以是算术字符表达式,尽量不用表达式。where子句中的条件表达式针对字符类型,字符值是大小写敏感的。where子句后列名的位置可以是函数,尽量不用函数。where后面可以跟多个条件表达式,用and或者or连接。
[3000,5000] 3000,5000
关于null值的讨论
1 null在表中不占存储空间,含义为空,没有.算术表达式中含有null值,结果一定为null.
如果列的数据类型为number,null不是0,可以理解为无穷大
2若该列有多个null值,distinct处理该列的结果是保留一个null
3 400 = null null=null(不成立),判断列的取值是否为null,必须用is null,不能用=null
4 400 <> null null<>null(不成立)
5 若用not in,集合中包含null值,一定没有记录返回。集合中包含null值对in没有影响
课堂练习
1列出每个员工的名字和薪水
select ename,salary from emp_hiloo
2列出每个员工的名字和年薪
select ename,salary*12 "Ann Sal" from emp_hiloo
3列出每个员工的名字和总收入
select ename,
(salary+nvl(bonus,0))*12 tot_sal
from emp_hiloo
4返回结果如下
zhangwuji's department no.is 10.
liucansong's department no.is 30.
.
.
'''s fdjaslkj ' 若要表达字符',用两个''即可表示'本身
select ename||'''s department is '
||deptno||'.'
from emp_hiloo;
5 有哪些不同的奖金?
select distinct bonus from emp_hiloo;
6列出工资大于5000的员工的年薪?
select ename,salary*12 ann_sal
from emp_hiloo
where salary > 5000
7列出年薪大于60000的员工的年薪?
同6
8哪些员工的职位是salesman
哪些员工的职位是Manager
select ename,job
from emp_hiloo
where job = 'salesman'
select ename,job
from emp_hiloo
where job = 'Manager'
9哪些员工的职位是salesman,大小写不知道
函数 upper('salesman') --> 'SALESMAN'
lower
select ename,job
from emp_hiloo
where upper(job) = 'SALESMAN'
select ename,job
from emp_hiloo
where lower(job) = 'salesman'
10 列出员工的工资在[3000,5000]之间的
列出员工的工资是3000,5000的。
11哪些员工的职位的第二个字符是a
select ename,job
from emp_hiloo
where job like '_a%'
哪些员工的职位的第二个字符是_
select ename,job
from emp_hiloo
where job like '__%'
select ename,job
from emp_hiloo
where job like '_\_%' escape '\'
第一个_是通配符,\_看成_本身。
12 哪些员工没有奖金
select ename,bonus
from emp_hiloo
where bonus is null;
select ename,bonus
from emp_hiloo
where bonus = null(写法不正确)
13 哪些员工有奖金
select ename,bonus
from emp_hiloo
where bonus is not null
13 10部门有哪些职位?
select distinct job
from emp_hiloo
where deptno = 10
14 列出员工,工资在[3000,5000],职位是salesman,没有奖金,名字的第二个字符是a,不在20,30部门
select ename,salary,job,bonus,deptno
from emp_hiloo
where ename like '_a%'
and deptno not in (20,30)
and job = 'salesman'
and bonus is null
and salary between 3000 and 5000
Database 数据库
表(table)
SQL(structured query language结构化查询语言)
1 DDL(data definition language数据定义语言)
create table alter table drop table
定义table structure
列(column)数据类型(data type)宽度(width)约束(constraint)
2 DML(data manipulation language 数据操作语言)
insert(插入) update(修改) delete(删除)
行(row) data
3 TCL(transaction control language 事务控制语言)
commit (提交)
rollback(回滚)
4 DQL(data query language数据查询语言)
select
5 DCL(data control language数据控制语言)
grant(授予权限)
revoke(回收权限)
数据库厂商提供 RDBMS software
oracle oracle10g mysql
MS sql server
IBM DB2
SYBASE ASE
DBA(database administrator 数据库管理员):
install software RDBMS(relationship database management system关系型数据库管理系统)--〉database(create database open)
-->登录数据库
远程登录20机器,使用该机器上的命令
telnet 192.168.0.20 伪终端
openlab/open123
登录数据库
client程序和数据库服务器在一台机器
ORACLE_SID
%sqlplus openlab/open123
client程序和数据库服务器在两台机器
%proc ip:port:oracle servername(作用等价于ORACLE_SID):username:password
telnet 192.168.0.23
openlab/open123
solaris的vi的删字符不能在编辑模式下按backspace键,需要转到命令模式下按x键删字符
dept department
location 位置
emp employee
bonus 奖金
salary 月薪
job 职位
mgr 领导的员工号
hiredate 入职日期
create table emp_mine
as
select * from emp_hiloo;
create table dept_mine
as
select * from emp_hiloo;
DQL
select
源表 结果集
1投影操作 列 select
2选择操作 行
3连接操作(多表查询)
select语句
子句
select from where group by having order by
from-->where-->select (distinct)
select colname,colname
from tablename
sql语句执行
sqlplus把语句通过连接给oracle server process(oracle的一个进程),server process对其进行语法语义分析,产生执行计划,按执行计划产生结果,将结果集返回给sqlplus.
双引号 表示列别名,不可以用来表达字符串,列名中包含空格以及大小写敏感时用双引号
单引号 表达字符串
(15000+null)*12 --> null
空值转换函数
nvl(p1,p2)
if p1 is null then
return p2;
else
return p1;
end if;
where子句
where 条件表达式 (列名 比较运算符 值)
sql条件表达式
=单值运算符
(between and) (where >= and <=) 范围 [a,b]
in 多值运算符 (=any)(where = or = or = ) 离散值 集合
通配符
_ 任意一个字符
% 0或任意多个字符
'S' 'S_' 'S%'
like
is null
否定形式
= <> != ^=
is null is not null
like not like
between and not between and
in not in
not in salary not in (3000,5000)
where salary <> 3000
and salary <> 5000
not in <>all (跟所有的都不相等)
select语句
1select子句可以跟列名,表达式,函数
2select后面的distinct表示去重,distinct之后和from之前的所有列联合去重。
3where执行在select子句之前,不可以用列别名;where子句后列名的位置可以是算术字符表达式,尽量不用表达式。where子句中的条件表达式针对字符类型,字符值是大小写敏感的。where子句后列名的位置可以是函数,尽量不用函数。where后面可以跟多个条件表达式,用and或者or连接。
[3000,5000] 3000,5000
关于null值的讨论
1 null在表中不占存储空间,含义为空,没有.算术表达式中含有null值,结果一定为null.
如果列的数据类型为number,null不是0,可以理解为无穷大
2若该列有多个null值,distinct处理该列的结果是保留一个null
3 400 = null null=null(不成立),判断列的取值是否为null,必须用is null,不能用=null
4 400 <> null null<>null(不成立)
5 若用not in,集合中包含null值,一定没有记录返回。集合中包含null值对in没有影响
课堂练习
1列出每个员工的名字和薪水
select ename,salary from emp_hiloo
2列出每个员工的名字和年薪
select ename,salary*12 "Ann Sal" from emp_hiloo
3列出每个员工的名字和总收入
select ename,
(salary+nvl(bonus,0))*12 tot_sal
from emp_hiloo
4返回结果如下
zhangwuji's department no.is 10.
liucansong's department no.is 30.
.
.
'''s fdjaslkj ' 若要表达字符',用两个''即可表示'本身
select ename||'''s department is '
||deptno||'.'
from emp_hiloo;
5 有哪些不同的奖金?
select distinct bonus from emp_hiloo;
6列出工资大于5000的员工的年薪?
select ename,salary*12 ann_sal
from emp_hiloo
where salary > 5000
7列出年薪大于60000的员工的年薪?
同6
8哪些员工的职位是salesman
哪些员工的职位是Manager
select ename,job
from emp_hiloo
where job = 'salesman'
select ename,job
from emp_hiloo
where job = 'Manager'
9哪些员工的职位是salesman,大小写不知道
函数 upper('salesman') --> 'SALESMAN'
lower
select ename,job
from emp_hiloo
where upper(job) = 'SALESMAN'
select ename,job
from emp_hiloo
where lower(job) = 'salesman'
10 列出员工的工资在[3000,5000]之间的
列出员工的工资是3000,5000的。
11哪些员工的职位的第二个字符是a
select ename,job
from emp_hiloo
where job like '_a%'
哪些员工的职位的第二个字符是_
select ename,job
from emp_hiloo
where job like '__%'
select ename,job
from emp_hiloo
where job like '_\_%' escape '\'
第一个_是通配符,\_看成_本身。
12 哪些员工没有奖金
select ename,bonus
from emp_hiloo
where bonus is null;
select ename,bonus
from emp_hiloo
where bonus = null(写法不正确)
13 哪些员工有奖金
select ename,bonus
from emp_hiloo
where bonus is not null
13 10部门有哪些职位?
select distinct job
from emp_hiloo
where deptno = 10
14 列出员工,工资在[3000,5000],职位是salesman,没有奖金,名字的第二个字符是a,不在20,30部门
select ename,salary,job,bonus,deptno
from emp_hiloo
where ename like '_a%'
and deptno not in (20,30)
and job = 'salesman'
and bonus is null
and salary between 3000 and 5000