首页>软件资讯>常见问题

常见问题

Toad使用教程

发布时间:2022-12-20 10:19:23人气:1189

TOAD FOR ORACLE

主要内容:

连接

查询(单表、多表、嵌套、函数)’追加(单行、多行、表间)

删除(记录、整表)

更新(直接、嵌套)


Toad for Oracle 连接

知识点:

系统级的监控经须用sss用户或system用户登录-

用sys用户登录时,Ccameat as选择SYSDBA:用systen用户登录,Ccanect as选挥Nornsl,T则会报铅。

Sys是系统内置的一个管理用户,这个用户只能用来做管理。syster本质上讲是一个一能用户被赋予了管理员汉限-


创建用户

Database-Create-User

创建表

Database-Create-Table

添加数据

Database-Schens Brawser

删除用户

Database-Manitor-Database Browser


用SQL进行单表查询

查询所有的记录

查询所有记录的某些字段

查询某些字段不同记录

单条件的查询

组合条件的查询

排序查询

分组查询

字段运算查询

变换查询显示


查询所有的记录

在【命令编辑区】执行输入“select * from scott.emp”,然后单击【执行】按钮,出现图所示的emp数据表所有记录。


查询所有记录的某些字段

在【命令编辑区】输入“select empno, ename, job fromscott.emp”,然后单击【执行】按钮,将显示emp数据表的empno、ename和job字段


查询某些字段不同记录

在上图所示的job字段中,可以发现有相同的数据,为了查询有多少种不同的job,在【命令编辑区】输入“select

distinct job from scott.emp”,然后单击【执行】按钮,出现结果


单条件的查询

输入“select empno, ename, job from scott.emp wherejob=' MANAGER’”,然后单击【执行】按钮,出现字符型字段条件查询的结果,查询的是job为MANAGER的记录。


单条件的查询

输入“select empno, ename,sal from scott. emp wheresal<=2500”,然后单击【执行】按钮,出现数字型字段条件查询的结果,查询的是满足sal小于等于2500的记录。


单条件的查询


比较运算符

Toad使用教程 比较运算符1.png

Toad使用教程 比较运算符2.png

Toad使用教程 比较运算符3.png

like和not like适合字符型字段的查询。%代表任意长度的字符串,_下划线代表一个任意的字符,like 'm%'代表rm开头的任意长度的字符串,like 'm代表m开头的长度为3的字符串。


组合条件的查询

输入“select * from scott.emp where job>=’CLERK'sal<=2000”

组合条件的查询

输入“select empno, ename,job from scott.emp where notjob=’CLERK’”


排序查询

输入“select * from scott.emp where job<='CLERK’orderby job asc, sal desc”


分组查询GROUP BY

SELECT column_name, aggregate_function(column_name)FROM table_name

WHERE column_name operator valueGROUP BY column_name

现在,我们希望查找每个客户的总金额(总订单),怎么办?

如果省略 GROUP BY会出现什么情况?

分组查询

您是否想到下面的结果集?

4.png

为什么不能使用上面这条SELECT语句呢?解释如下;上面的SELECT 语句指定了两列(Customer和 SUM(OrderPrice) )。"SUM(OrderPrice)”返回一个单独的值《“OrderPrice列的总计,而“Customer”返回6个值(每个值对应“Orders”表中的每一行)。因此,我们得不到正确的结果。不过,您已经看到了. GROUP BY语句解决了这个问题。

分组查询

select count(*)合计, job,sal from scott. empgroup by job, sal

having sal<=2000order by job, sal

注:从效率考虑的话,不需要的记录在GROUP BY之前过滤掉.


字段运算查询

输入 select empno,ename,sal,mgr,sal+90000 from

scott.emp


变换查询显示(别名)

输入“select empno编号, ename姓名,job工作, sal薪水from scott. emp”


用SQL进行多表查询

无条件多表查询

等值多表查询

非等值多表查询


无条件多表查询

无条件多表查询是将各表的记录以“笛卡尔”积的方式组合起来。如scott.dept表共有4条记录,scott. emp表共有14条记录,其“笛卡尔”积将有4*14—56条记录。

select emp.empno,emp.ename,emp.deptno,dept.dname,dept.locfrom scott.emp,scott.dept;


等值多表查询

等值多表查询将按照等值的条件查询多个数据表中关联的教据。要求关联的多个数据表的某些字段具有相同的属性,即具有相同的数据类型、宽度和取值范围。

select emp.empno, emp.ename, emp.deptno, dept.dname,dept.locfrom scott.emp,scott.dept

where scott.emp.deptno-scott.dept.deptno;


非等值多表查询

在非等值多表查询中,可以使用比较运算符来组合查询条件。

select emp.empno, emp.ename, emp.deptno, dept.dname,dept.locfrom scott.emp,scott.dept

where scott.emp.deptno!=scott.dept.deptno and scott.emp.deptno=10;


用SQL进行嵌套查询

简单嵌套查询

带【in】的嵌套查询

带【any】的嵌套查询

带【some】的嵌套查询

带【all】的嵌套查询

带【exists】的嵌套查询

并操作的嵌套查询

交操作的嵌套查询

差操作的嵌套查询


简单嵌套查询

select查询语句里可以嵌入select查询语句,称为嵌套查询。有些书上将内嵌的select语句称为子查询,子查询形成的结果又成为父合询的条件。子查询可以嵌套多层,子查询操作的数据表可以是父查询不操作的数据表。子查询中不能有order by分组语句。

select emp.empno,emp.ename,emp.job,emp.salfrom scott.emp

where sal>=(select sal from scott.emp where ename='BLAKE");


带【in】的嵌套查询

select emp.empno,emp.ename,emp.job,emp.salfrom scott.emp

where sal in (select sal from scott.emp where ename=WARD");


带【any】的嵌套查询

select emp.empno,emp.ename,emp.job,emp.salfrom scott.emp

where sal >any(select sal from scott.emp where job='MANAGER');

带any的查询过程等价于两步的执行过程。

1〕执行“select sal from scott.emp where job=MANAGER""2)查询到3个薪水值2975、2850和2450,父查询执行下列语句。where sal >2975 or sals2850 or sals2450


带【some】的嵌套查询

select emp.empno,emp.ename,emp.job,emp.salfrom scott.emp

where sal =some(select sal from scott.emp where job="MANAGER'y;


带【all】的嵌套查询

select emp.empno,emp.ename,emp.job,emp.salfrom scott.emp

where sal >ali(select sal from scott.emp where job='MANAGER");

带all的嵌套查询与【some】的步骤相同

1)执行“select sal from scott.emp where job=1MANAGER""2)查询到3个薪水值2975、2850和2450,父查询执行下列语句。where sal >2975 and sals2850 and sal>2450;


带【exists】的嵌套查询

select emp.empno,emp.ename,emp.job.emp.salfrom scott.emp,scott.dept

where exists

(select * from scott.emp where scott.emp.deptno=scott.dept.deptno);


并操作的嵌套查询

并操作就是集合中并集的概念。属于集合A或集合B的元素总和就是并集。(select deptno from scott.emp)

union

(select deptno from scott.dept);


交操作的嵌套查询

交操作就是集合中交集的概念。属于集合A且属于集合B的元素总和就是交集。

(select deptno from scott.emp)intersect

(select deptno from scott.dept);


差操作的嵌套查询

差操作就是集合中差集的概念。属于集合A且不属于集合B的元素总和就是差集。

(select deptno from scott.dept)minus

(select deptno from scott.emp);


用SQL进行函数查询

【ceil】函数

【floor】函数

【mod】函数

【power】函数

【round】函数

【sign】函数

【avg】函数

【count】函数

【min】函数

【 max】函数

【sum】函数


【ceil】函数

【ceil】函数用法: ceil (n),取大于等于数值n的最小整数。select sal,sal/100.ceil(sal/100, from scott.emp;


【floor】函数

【floor】函数用法: floor (n),取小于等于数值n的最大整数。select sal,sal/100,floor (sal/100) from scott.emp;


【mod】函数

【mod】函数用法: mod (m, n),取m整除n后的余数。select sal,mod(sal,1000), mod(sal , 100), mod(sal ,10)from scott.emp;


【power】函数

【power】函数用法: power (m,n),取m的n次方。

select sal, power(sal,2).power(sal,3)

from scott.emp;


【round】函数

【round】函数用法: round (m, n),四含五入,保留n位。select sal,round(sal/100,2) ,round(sal/1 000,2)

from scott.emp;


【sign】函数

【sign】函数用法: sign (n)。n>0,取l; n=O,取O:n<O,取-l。select sal,sal-3000,sign(sal-3000)

from scott.emp;


【avg】函数

【avg】函数用法: avg(字段名),求平均值。要求字段为数值型。select avg(mgr)平均薪水

from scott.emp;


【count】函数

【count】函数用法: count(字段名)或count (*),统计总数。select count(distinct job )工作类别总数

from scott.emp;


【min】函数

【min】函数用法: min(字段名),计算数值型字段最小数。select min(sal)最少薪水

from scott.emp;


【max】函数

【max】函数用法: max(字段名),计算数值型字段最大数。select max(sal)最高薪水

from scott.emp;


【sum】函数

【sum】函数用法: sum(字段名),计算数值型字段总和。select sum(sal)薪水总和

from scott.emp;


用SQL录入数据

o单行记录的录入o多行记录的录入

表间数据复制


单行记录的录入

1.语法

insert into数据表(字段名1,字段名2,…-) values(字段名l的值,字段名2的值,-…-…)。

由于字段的类型不同,在书写字段值的时候要注意格式.数信型字段,可以直接写值。

字符型字段,其值上要加上单引号。

口期型字段,其值上要加上单引号,同时还要注意年、月、口的排列次序。

在数据的插入语句中,插入列排序和插入值要——对应。字符型和口期型字段要加上单引号,空列必须有伯。

2实例

在scott.cmp数据表里共包含了3种类型的字段。

empno.number(4) . NMOJT NULL,数值型,长度为4,不能为空。ename,varchar2(10,字符型,长度为10。

hiredate. date,日期型。

我们以在这3个字段中插入记录为例进行说明。

对于日期型的数据,往往会感觉为难。因为不知道年、月、日的排列顺序和格式,这里教给大家几个方法。首先查询范例数据表中的数据,然后“依胡户画瓢”就可以了。


单行记录的录入

(1)在【命令编辑区】输入“select empno,ename,hiredate fromscott.emp;"、然后单击【执行】按钮,出现下图所示的结果。有的计算机系统默认的日期型数据格式应该为“28-9月-2012”。

在TOAD中显示是YYYY/MM/DD的格式,实际数据内部格式是DD一MM月- YYYY的格式

原因TOAD内置日期格式与系统格式不一致,可通过自定义方式更改

alter session set

nls_date_format= 'yyyy-dd-mm hh24:mi:ss;

(2)在【命令编辑区】输入“insert into scott.emp(empno, ename,hiredate) values (7999,JONE',28-9月-2012';”,正常插入数据。

alter session set

nls date format- 'yyry-dd-mm;后再进行insert inloscott.emp(empno, ename,hiredate] values (8000,"JONE,201205011;也能正确插入数据,但之前28-9月-2012'的格式不能正确插入


多行记录的录入

在数据的录入中,经常需要将从女据表中查询到的数据稍傲修改成批录入的情况,这就是多行数据的录入。

1,语法

insert into数据表(字段名1.字段名2,-…-)

lselect(字段名l或运算,字段名2或运算。----) from数据表where条件)

实际上,首先利用子查询语句查询结果,然后再利用insert语句将结果插入数据表,子查询和insert中的数据表既可以相同,也可以不同,但要求查淘结果的字段和insert插入的数据表中字段属性完全一致

2实例

执行以下语句。

insert into scott. eimplempnIc,enamme,hiredate)

iselect empno+100. ename,hiredate fron scott. empwhcre crmpno>=7999

);

2条数据被插入。执行查询查看结果

表间数据复制

可以从一个故据表中选择需要的数据插入到全新的数据表中。1)在【命令编辑区】执行以下语句。

create table scott.testas

(

secleet distinet empIo,ename,hiredafrom scott. emp

uhere empnD>=7000);

2)

select  from scatt. test


用SQL删除数据

删除记录

整表数据删除


删除记录

使用【delete】命令可以利除数括。使用【truncate】命令可以删除整表数据但保留结构。

删除记录的语法:delete from数据表where条件。

输入“delete from scott.test where empno>=7500 and empno<=8000;

sclect * from scott.test表中的数据已经被刮除,此时用ROLLBACK会发生什么?

使用EOLLRACK后再度查询select * from scott.test,数据回滚


整表数据删除

truncate table命令将快速刮除数据表中的所有记录,但保留数掘表结构。这种快速删除与delcte fron 数据表的别除全部效据表记录不一样,delete命令删除的数据将存储在系统回滚段中,需要的时候,数据可以回滚恢复,而truncate命令删除的数据是不可以恢复的-

truneate table seott.test

rollback;

select  from scott.test;

数据已经完全制除


用SQL更新数据

直接赋值更新

嵌套更新


直接赋值更新

1.语法

update数据麦

set字段名1=新的赋值,字段名2=新的赋值。…----where条件

2.实例

在【命令编辑区】执行以下语句。

update scott.emp

set empno-8888, ename=TOM”, hiredate=' 03-9月-2002where empno-8000


嵌套更新

1,语法

update数据表

set字段名1=iselect字段列表from 数据表where条件),字段名2=(select字段列表froa数据表where条件),…-…-.

2实例

update scott.empset sal=

select sal+300 from scott. empwhere empnIo-8888

注意如果sal没值,则

sal+300=null.故此例先使sal=300再进行嵌套操作



上一条:Toad常用使用技巧有哪些?

下一条:Toad for Oracle_数据库专家的最佳生产力工具