oracle1

oracleSQL

SQLstrunctured query language(结构化查询语言)

文件 FILE

DATABASE(数据库)

SQL

DDL(datadefinition language 数据定义语言结构画表头)

createtable (创建表)(column列  数据类型data type,宽度 width

                constraint 约束)

altertable (修改表结构)

droptable (删除表)

DML(datamanipulation language 数据操作语言数据填行)

insert插入数据

update修改数据

delete删除数据

TCL(transactioncontrol language 事务控制语言)

commit(提交)

rollback(回滚)

DQL(dataquery language 数据查询语言)

select

DCL(datacontrol language 数据控制语言)

grant(授权)

revoke(回收权限)

databaseobject 数据库对象

table(表)index(索引) view(视图) sequence(顺序号序列号)

RDBMS(relationshipdatabase management system 数据库管理系统)

ORACLE  oracle 10g software my sql

IBMdb2

MS  SQL Server

---------DBA(database administrator)

installoracle 10g(安装 oracle10g 软件)

createdatabase(创建数据库 )

startup(open 打开状态)

---------SD(softwaredeveloper)

--->登录数据库(建立数据库连接)

--->SQL语句

console

telnet192.168.0.26

telnet192.168.0.23

osopenlab/open123 % $ shell提示符

sqlplus

dbopenlab/open123 SQL>select

echo$ORACLE_SID (环境变量)

oracledb server (oracle数据库服务器)的名字 open状态

sqlplus

setenvORACLE_SID hiloo

ERROR:

ORA-01034:ORACLE not available 

没有hiloo这个oracle数据库服务.

cmd(windows)shell(unix)都是命令解释器

C/S   client/server  (客户端/服务器)

客户端程序和服务器端程序在同一台机器上sqlplus

客户端程序和服务器端程序在不同的机器上 jdbc(tcp/ip)

客户端程序和服务器端程序在同一台机器上

D:>setORACLE_SID=hiloo

D:>setORACLE_SID

ORACLE_SID=hiloo

%setenv ORACLE_SID hiloo

%echo $ORACLE_SID

ORA-00955:name is already used by an existing(存在) object

createtable test(c1 number)

tablecreated

desctest (describe test 显示表结构)

insert

1row inserted

源表 from后面的表名  结果集 select语句的查询结果

select语句select子句 from子句

select

投影操作(select子句) 选择操作连接操作

select子句列名,表达式,函数

算术表达式  字符表达式(字符串拼接)

select* from dept_hiloo

empno员工编号

mgr该名员工领导的员工编号

语法检查,语义检查,生成执行计划,执行该计划,产生结果集.

select表达式列别名 from tabname

字符串用''

列别名用"", 空格或者大小写敏感

insert包含null,没值,算术表达式中包含null值,结果一定为null.null可以按无穷大理解.

nvl(p1,p2)返回值 nvl(bonus,0) 空值转换函数 p1,p2的数据类型一致

pl/sql程序函数

ifp1 is null then

   return p2;

else

   return p1;

endif;

distinct不同区别去重

distinctdeptno,job from emp_hiloo distinct之后,from之前的所有列联合去重

selectfrom where

where子句实现选择操作where 条件表达式列名比较运算符值

wheresalary > 5000

where子句执行在select子句之前,所以列别名不能用于where子句

表达式比较运算符值(尽量不用)

betweenand 闭区间范围 >= and <=

in(集合) <=> =any(跟集合里的任意一个值相等) <=> =or =

ORA-01797:this operator(运算符) must be followed(跟) by ANY or ALL

c1between 10 and 20

c1in (10,20)

like像字符

通配符 % 0或多个字符    _任意一个字符

'S'  like 'S%'  like 'S_'

_%有两个含义一个代表通配符,另一个代表它本省

isnull 

否定形式

isnull is not null

=<> !=

betweenand  not between and 比小的小比大的大

like  not like

in  not in

notin <=> <>all (跟集合里的所有值都不相等) <=> <> and <>

建连接

DDLDML TCL

selectdistinct from where

fromwhere select distinct

where数据类型  比较运算符(betweenand in like is null) null

练习

1列出员工名称以及工资

selectename,salary from emp_hiloo

2列出员工名称以及年薪

selectename,salary*12  from emp_hiloo

3列出员工名称以及一年的总收入

selectename,(salary+nvl(bonus,0))*12 tot_sal from emp_hiloo

4公司里有哪些不同的职位

selectdistinct job from emp_hiloo

5公司里每个部门有哪些不同的职位

selectdistinct distinct deptno,job from emp_hiloo

6工资大于5000的员工的名称和工资

selectename,salary from emp_hiloo where salary > 5000

7工资大于5000的员工的名称和年薪

selectename,salary*12 from emp_hiloo where salary > 5000

8年薪大于60000的员工的名称和年薪

selectename,salary*12 from emp_hiloo where salary > 5000

9哪些员工的职位是clerk

selectename,job from emp_hiloo where job = 'clerk'

10哪些员工的职位是Manager

selectename,job from emp_hiloo where job = 'Manager'

11哪些员工的职位是clerk,不知道clerk的大小写

selectename,job from emp_hiloo where lower(job) = 'clerk'

12找出工资在5000到10000之间的员工的名称和年薪

selectename,salary*12 from emp_hiloo

wheresalary between  5000 and  10000

13哪些员工的职位是clerk或Manager或salesman

selectename,job from emp_hiloo

wherejob in ('clerk','Manager','salesman')

14哪些员工的名字的第二个字符是a.

selectename,job from emp_hiloo

whereename like '_a%'

15哪些员工的职位的前两位字符是j_ (j_salesman符合条件)

转义

selectename,job from emp_hiloo

wherejob like 'j\_%'escape ''

16哪些员工没有奖金

selectename,bonus from emp_hiloo

wherebonus is null

17哪些员工有奖金

selectename,bonus from emp_hiloo

wherebonus is not null

关于null的讨论

insert包含null,没值,算术表达式中包含null值,结果一定为null.null可以按无穷大理解.

distinctbonus,bonus包含多个null值,结果集包含一个null值.

判断一个列的取值是否为空,用isnull

null= null 不成立 1 <> null 不成立null <> null 不成立

对于in来说,集合里面是否包含null值,对结果集没影响

对于not in来说,集合里面包含null值,结果集一定是norows selected.(没有任何记录)

原文地址:https://www.cnblogs.com/baiduligang/p/4247564.html