Oracle数据库.docx

上传人:b****2 文档编号:24572959 上传时间:2023-05-29 格式:DOCX 页数:23 大小:27.54KB
下载 相关 举报
Oracle数据库.docx_第1页
第1页 / 共23页
Oracle数据库.docx_第2页
第2页 / 共23页
Oracle数据库.docx_第3页
第3页 / 共23页
Oracle数据库.docx_第4页
第4页 / 共23页
Oracle数据库.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

Oracle数据库.docx

《Oracle数据库.docx》由会员分享,可在线阅读,更多相关《Oracle数据库.docx(23页珍藏版)》请在冰豆网上搜索。

Oracle数据库.docx

Oracle数据库

一、SQL语句分为五类2

二、Oracle常用字段类型:

3

三、函数5

数字函数5

转换函数6

其它函数7

四、多表连接和子查询8

多表连接:

9

五、数据操作与事务控制11

SQL语句分为五类11

六、数据库的主要对象12

七、Oracle数据库开发应用程序15

用户管理16

SQL:

StructuredQueryLanguage:

结构化查询语言。

SQL分类(SELECT、DML、DDL、DCL、事务控制语句);

RDBMS:

关系数据库管理系统

用户:

Sys:

超级管理员,权限最高;

System:

管理员;

Scott:

用户。

Oracle卸载过程:

_______________________________________________________________________________

1、停止Oracle所有的服务;

2、卸载所有Oracle所有产品,开始—>程序—>Oracle-Orahome10g—>OracleInstallationProducts—>UniversalInstaller;

3、运行regedit,选择HKEY_LOCAL_MACHINE—>SOFTWARE,删除所有ORACLE入口;

4、运行regedit,选择HKEY_LOCAL_MACHINE—>SYSTEM—>CurrentContralSet—>Service,删除所有ORACLE入口;

5、运行regedit,选择HKEY_LOCAL_MACHINE—>SYSTEM—>CurrentContralSet—>Service—>Eventlog—>Application,删除所有ORACLE入口;

6、删除Oracle目录;

7、重启计算机(若有不可删除的,重启之后即可删除)。

_______________________________________________________________________________

一、SQL语句分为五类

查询语句:

Select

DML语句(数据操作语言):

Insert/Update/Delete/Merge

DDL语句(数据定义语言):

create、alter、drop、truncate

DCL语句(数据控制语言):

grant、revoke

事务控制语句:

commit、rollback、savepoint

二、Oracle常用字段类型:

Char(n):

定长字符串

Varchar2(n):

变长字符串

Number(m,p):

数字类型

Date:

日期类型

1、创建表格

CREATETABLEstudent

(st_classchar(4),

st_numchar(10),

st_namevarchar2(4),

st_sexchar

(2),

st_agenumber(3,0)

2、显示表格框架

describe/descstudent;

3、插入数据

INSERTINTOstudent(st_class,st_num,st_name,st_sex,st_age)

VALUES('J02','0245639','哈哈','男',15);

4、显示所有数据

select*fromstudent

5、删除表格

DROPTABLEstudent;

6、连接符||和字段别名表示

selectfirst_name||last_name||'的工资是:

'||salary员工的工资fromemployees

 

7、去掉重复值

selectdistinctdepartment_idfromemployees

8、筛选

selectfirst_name,last_name,salaryfromemployees

wherefirst_name='S';/wheresalary>1000;

<>:

不等于

between…and…在某范围之内

in(list)字符串在那个之间

like(%、_)

is[not]null;是否为空

and,or,not。

连接关系,优先级not>and>or;

在一定范围值内:

数字用wheresalarybetween2000and2300

字符串用wherefirst_nameIN('F');

模糊查询:

wherefirst_namelike'joan%'

wherefirst_namelike'joan_'

wheredepartment_idisnull/notnull

满足两个条件,用AND连接,满足其中一个,用OR连接。

wheredepartment_id=1

andsalary>1000

9、排序

升序:

orderbydepartment_idasc(asc默认可以不写)

降序:

orderbysalarydesc

多种排序:

orderbydepartment_idasc,salarydesc

10、注释语句(--…..)

select*fromstudent--查找表中所有内容

11、空指针:

null。

12、起别名

(1)selectfirst_nameAS“姓名”,last_name,salaryfromemployees

(2)selectfirst_nameAS姓名,last_name,salaryfromemployees

(3)selectfirst_name姓名,last_name,salaryfromemployees

13、重命名表格:

renameemployees(旧表名)toemployee(新表名);

14、向表中增加字段:

altertableemployeesadd(namevarchar2(20));

15、从表中删除字段:

altertableemployeesdrop(name);

16、删除记录:

deletefromemployeeswhereemployee_id=7;

17、删除表中全部记录(清空表格):

deletefromemployees;

18、数据修改:

updatesalary

sethightest_level=4000

wheregrade_level=5

19、修改字段属性:

altertableemployeemodify(department_idvarchar2(30)notnull)

三、函数

SQL函数分类:

单行函数,多行函数;

单行函数分为:

字符函数、数字函数、日期函数、转换函数和其它函数;

字符函数

1、大小写转换函数:

lowerupperinitcap

selectfirst_namefromemployeewherelower(first_name)='jacky'

selectfirst_namefromemployeewhereupper(first_name)='JACKY'

selectfirst_namefromemployeewhereinitcap(first_name)='Jacky'

2、字符处理函数

Concat:

连接两个字符串

selectconcat(last_name,first_name)fromemployee

substr:

取出字符串的字符串

selectfirst_name,substr(first_name,1,3)fromemployee

length:

求字符串长度

selectfirst_name,length(first_name)fromemployee

instr:

找到字符串的位置

selectfirst_name,instr(first_name,'c')fromemployee

lpad|rpad:

字符串填充补位函数

selectfirst_name,lpad(first_name,6,'*')fromemployee

trim(‘’fromfirst_name):

截取字符串两侧的字符(只能截取字符串两端的字符,不能截取中间的字符)

ltrim(‘marry’,‘ma’),截取字符串左端的字符,结果为:

ry,可以截取多个左端的字符;

rtrim(‘marry’,‘ry),截取字符串右端的字符,结果为:

ma,可以截取多个右端的字符;

selectfirst_name,ltrim(first_name,‘Ja’)fromemployee

selectfirst_name,rtrim(first_name,‘cy’)fromemployee

replace:

替换字符串

selectfirst_name,replace(first_name,'Ja','DD')fromemployee

数字函数

Round:

数字四舍五入

Selectsalary,round(salary,2),round(salary,1),round(salary,0),round(salary,-1),round(salary,-2)fromemployee

whereemployee_id=201001

trunc :

直接截掉数字

   selectsalary,trunc(salary,2),trunc(salary,1),trunc(salary,0),trunc(salary,-1),trunc(salary,-2)fromemployee

whereemployee_id=201001

mod:

求余数(取模)

selectsalary,mod(salary,10)fromemployee

whereemployee_id=201001

日期函数

Sysdate:

得到当前系统的时间(默认日期格式:

DD-MON月-YY)

selectsysdatedual;

日期加减一个数字得到一个新日期,即增加或减少多少天

selecthire_date,hire_date+10,hire_date-10fromemployee

日期和日期之间不能相加,能够进行相减!

相减得到两个日期相差的天数!

Month_Between:

比较日期之间相差的月份

selectmonths_between(sysdate,hire_date)fromemployee

add_months:

日期加上或减去几个月

selecthire_date,sysdate,add_months(hire_date,5)fromemployee

selecthire_date,sysdate,add_months(hire_date,-5)fromemployee

next_day:

取得从当前时期开始遇到的第一个指代星期几的日期

selecthire_date,sysdate,next_day(sysdate,4)fromemployee

last_day:

指定日期所在月份最后一天的日期

selecthire_date,sysdate,last_day(sysdate)fromemployee

round:

取得按年或者月四舍五入得到的次年日期,年的四舍五入以每年六月为基准,月的四舍五入以每月的15号为基准。

selecthire_date,sysdate,round(sysdate,'year')fromemployee

selecthire_date,sysdate,round(sysdate,'month')fromemployee

trunc:

取得按年或月截取获得的新日期

selecthire_date,sysdate,trunc(sysdate,'year')fromemployee

selecthire_date,sysdate,trunc(sysdate,'month')fromemployee

转换函数

对日期进行to_char转换

selectsysdate,to_char(sysdate,'YYYY-MM-DD')fromemployee

对数字进行to_char转换:

9不强制显示,0强制显示,L显示本地符号。

selectsalary,to_char(salary,'$999,999,999.99')fromemployee

selectsalary,to_char(salary,'L00,000.00')fromemployee

to_date函数

selectto_date('11-10月-10')fromemployee

selectto_date('2010-10-11','YYYY-MM-DD')fromemployee

to_number函数

selectto_number('12345')*10fromemployee、

其它函数

nvl(expr1,expr2):

用于将控制转换为一个替换值

selectmanager_id,nvl(manager_id,12345)fromemployee

nvl2(expr1,expr2,expr3):

如果expr1不为null,返回expr2,否则返回expr3

selectmanager_id"前",nvl2(manager_id,12345,4567)fromemployee

nullif(expr1,expr2):

比较两个表达式,如果相等返回null,如果不相等返回第一个表达式的值。

selectlength(first_name),length(last_name),nullif(length(first_name),

length(last_name))fromemployee

coalesce(expr1,expr2,….exprn):

查找表达式莉第一非空值表达式,并返回此表达式的值(括号里面表达式必须是同一类型的)。

selectcoalesce(email,last_name,first_name)fromemployee;

条件表达式(实现if-then-else的逻辑):

使用两种方法:

case表达式,decode函数

case表达式:

selectemployee_id,first_name,department,salary,

casedepartmentwhen1thensalary+100

when2thensalary+200

when3thensalary+300

elsesalary+500

end"NEWSALARY"

fromemployee;

decode函数:

selectemployee_id,first_name,department,salary,

decode(department,1,salary+100,2,salary+200,3,salary+300)"NEWSALARY"fromemployee;

分组函数

1、分组函数对多行输入值进行运算,得到多行对应的单个结果

AVG():

计算平均值,distinct去掉重复值后再进行计算。

selectavg(salary)"平均工资"fromemployee;

selectavg(distinctsalary)"平均工资"fromemployee;

count:

计算总个数非空的才计算在内。

selectcount(*)fromemployee;

selectcount(*)fromemployeewheredepartment=6;

注:

count(*)求出所有符合条件的记录数,count(字段)是求出所有符合条件并且字段值非空的记录数。

max(),min():

求最大值,最小值

selectmin(salary)fromemployee;

selectmin(salary)fromemployee;

sum():

求和

selectsum(salary)fromemployee;

2、使用groupby对数据进行分组

使用groupby可以将表分成多个组,groupby紧跟where语句之后,

分组函数在使用的时候,需要遵守一些规则:

I出现在select列表中的字段,如果出现位置不再组函数中,那么必须出现在groupby子句中。

selectdepartment部门,avg(salary)平均工资fromemployeegroupbydepartment;

II在group出现的字段可以不出现在查询语句中。

selectavg(salary)平均工资fromemployee

groupbydepartment;

IIIwhere子句中不允许出现出现组函数,组函数可以出现在查询列表中,或者groupby子句中,但是不允许出现在where子句中。

错误例子:

selectdepartment,avg(salary)fromemployeewhereavg(salary)>5000groupbydepartment;

3、使用having子句对分组结果进行限制

selectdepartment部门,avg(salary)平均工资fromemployee

groupbydepartmenthavingavg(salary)>3000;

四、多表连接和子查询

约束:

在表上强制执行的一些数据校验规则,被插入、修改或者删除的数据必须符合在相关字段上设置这些数据检验规则,也就是约束条件。

Oracle数据库中包括五类完整性约束:

1、NOTNULL非空:

只能定义在列级,保证列值不能为空。

2、UNIQUE唯一键:

保证值不允许重复(可以为空)。

3、PRIMARYKEY主键:

不允许有重复值,也不允许有空值。

4、FOREIGNKEY外键:

5、CHECK检查:

约束条件存放在数据字典中,同样需要命名;

约束条件命名有两种方式:

Oracle按照SYS-Cn的默认命名格式命名,或者有用户指定的约束条件命名。

在什么时候创建约束:

(1)、建表的时候

createtablestudent(numnumber(20)notnull,namevarchar2(20)notnull,sexvarchar2

(2),agenumber(3))

(2)、建表之后

查找定义的约束:

select*fromall_constraintswheretable_name='STUDENT'

删除约束:

altertablestudentdropconstraintSYS_C005402;

添加约束:

altertablestudentaddconstraintnum_uni_checkunique(num);

altertablestusubaddconstraintstu_sub_checkprimarykey(st_no,su_no)

创建外键连接:

createtablestusub(numnumber(20),sub_nochar(4),foreignkey(num)referencesstudent)

查询约束:

select*fromall_constraintswheretable_name='EMPLOYEE';

check约束:

createtablestuInfo(stu_idchar(10)primarykey,stu_namevarchar2(10)notnull,stu_agenumber

(2),constraintstu_age_checkcheck(stu_age>15))

启用约束:

altertablestuInfoenableconstraintstu_age_check;

关闭约束:

altertablestuInfodisableconstraintstu_age_check;

多表连接:

使用单个select语句从多个表中取出相关的数据。

selectdepartment.department_id,employee.em_namefromdepartment,employeewhereemployee.department_id=department.department_id;

使用别名:

selectd.department_id,e.em_namefromdepartmentd,employeeewheree.department_id=d.department_id;

注意:

一定要有where语句且where语句后面是正确的连接条件,如果漏写或者错误的连接条件,会产生错误的结果!

多表连接可以分为四类:

等值连接、非等值连接、外连接、自连接,还有SQL99新标准下的多表连接。

等值连接:

最常见的一种,连接条件中的两个字段通过等号连接建立等值关系。

Ø示例:

selectd.department_id,e.em_name,d.department_namefromdepartmentd,employeeewheree.department_id=d.department_id;

Ø通过and连接字句和查询条件

selectd.department_id,e.em_name,d.department_namefromdepartmentd,employeeewheree.department_id=d.department_idande.department_='1001';

Ø连接多个表:

通过and操作符连接,一般来说,n个表连接,至少需要n-1个连接。

selects1.st_name,s2.su_namefromstusubs3,students1,subjects2whe

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1