oracle数据库-SQL语言开发与运用(二)

一:需要用到的工具,PLSQL 、secureCRT、xFTP(实战01系列最后一节,没太明白。)

二:实战02省略,大多内容我用不上

 三:实战03

1.插入数据

1.1直接通过PLSQL的图形化工具,在表格里输入数据。

1.2需要表中的列名

select * from table;

insert into table(列名1,列名2,列名3) values (1,22,‘string’);

commit;

1.3省略列名

select * from sql04;

insert into sql04 values(1,22,' ');

commit;

1.4取变量名称

select * from sql03;

insert into sql03 values(&sql03_id,&sql03_name,);

1.5从另一张表直接复制数据过来

-----------------------------无条件插入(所有数据都插入)-----------

insert into sql05 select * from sql04;

commit;

-------------------------有条件的插入--------------------------------

insert into sql05 select * from sql04 where sql04_price>200

2.update语句。

2.1 图形化界面操作

2.2 通过命令的方式

update sql02 set sql02_number=20002 where sql02_id=2;

update sql02 set sql02_status=' 20002 ' where sql02_id=2;字符型的需要加引号。

2.3 修改多列

update sql02 set sql02_id=222,sql02_status ='OK' where sql02_id=2;

2.4有子查询的条件查询

update sql04

        set (sql04_desc,sql04_count)=

              (select sql04_desc,sql04_count from sql04 where sql04_id =2)

where sql04_id =3;

3.delete 语句

3.1删除某条记录

delete from sql04 where sql04_id =1;

3.2删除两条记录

delete from sql04 where sql04_id =2 and sql04_id=3;

delete from sql04 where sql04_id in (2,3);

delete 不会释放占用的物理空间,可以用truncate table

4.pl/sql综合应用,学习和创建PL/SQL 对象

select * from yg;

四:实战04

SQL语言之查询语言:查询限制排序、联接查询、子查询。

1、SQL查询限制排序

1.1算术运算符

加减乘除,对列名进行操作。

1.2比较运算符

>  <  >= <= =

1.3列连接 ||,把两列的内容拼接在一起。

1.4字符串的连接,select ename|| 'is a' || job from emp;

1.5去除重复行 select distinct job from emp;

1.6构造表达式

实现的效果就是批量删除表。

select ' drop table ' ||table_name || ' ;' from all_tables where owner ='ITPUX';

 1.7使用like运算符,实现模糊查询

select * from bm where department_name like '人%';

1.8复合条件

and,or,in,

select * from yg where job_id='AD_VP' and salary =17000;

select * from yg where job_id='AD_VP' or job_id ='IT_PROG';

select * from yg where job_id in ('AD_VP','IT_PROG');

1.9使用&替换

select * from yg where salary=&salary;

select * from yg where salary=&salary and job_id='&job_id';

1.10使用查询排序

----order by 列名 asc升序排列,desc 降序排列

select * from emp order by sql;

select * from emp order by sql desc;

select * from emp order by sql,deptno desc;(仅以第一列排序,后面的列不生效,后面的列如果有空格,会部分有效)

select * from gw order by 3;(按照第三列排序)

2、SQL的联接查询

Join操作基本分为3大类:外连接(左连接、右连接、全连接)、自然连接、内连接。

 目前有两张表

2.1使用自然连接。(只会出=条件成立的那条记录)

自然连接是一种特殊的等价连接,它将表中具有相同名称的列进行自动记录匹配。

NATURAL JOIN:在连接条件中使用等于(=)运算符比较被连接列的列值。

 select * from Itpux11 natural join Itpux12;语句执行得到以下结果。

2.2内连接 inner join

默认跟自然连接类似

有三种写法:

2.2.1 第一种写法:

select Itpux11.name,Itpux11.sex,Itpux12.grade

from Itpux11

inner join Itpux12 on Itpux11.name=Itpux12.name 该语句实现的效果和自然连接一样

 

 2.2.2第二种写法(常用)

select Itpux11.name,Itpux11.sex,Itpux12.grade

from Itpux11,Itpux12

where Itpux11.name=Itpux12.name;

2.2.3第三种写法

select name,Itpux11.sex,Itpux12.grade

from Itpux11

inner join Itpux12 using(name);

现将数据修改为:

2.3左外连接(left outer join)

左边是全部的,右边符合条件的,没有的用空值代替。

2.3.1 第一种写法

select Itpux11.name,Itpux12.name,Itpux11.sex,Itpux12.grade

from Itpux11 left outer join Itpux12 on Itpux11.name=Itpux12.name;

 得到的结果如下:

2.3.2 第二种写法

select Itpux11.name,Itpux12.name,Itpux11.sex,Itpux12.grade

from Itpux11 left join Itpux12 on Itpux11.name=Itpux12.name;

2.3.3 第三种写法(用+ oracle9i及以前,放左就是右连接,放右代表左连接)

select Itpux11.name,Itpux12.name,Itpux11.sex,Itpux12.grade

from Itpux11 ,Itpux12 where Itpux11.name=Itpux12.name(+);

2.4右外连接(right join/right outer join)

2.4.1 第一种写法

select Itpux11.name,Itpux12.name,Itpux11.sex,Itpux12.grade

from Itpux11 right outer join Itpux12 on Itpux11.name=Itpux12.name;

右边是全部,左边符合条件的,没有的用空值替代。得到的结果如下:

 2.4.2 第二种写法

select Itpux11.name,Itpux12.name,Itpux11.sex,Itpux12.grade

from Itpux11 right join Itpux12 on Itpux11.name=Itpux12.name;

2.4.3 第三种写法

select Itpux11.name,Itpux12.name,Itpux11.sex,Itpux12.grade

from Itpux11 ,Itpux12 where Itpux11.name(+)=Itpux12.name;

2.5全连接(full join/full outer join)

包含两个表的全部行,不符合条件的用空值替代。

2.5.1第一种写法

select Itpux11.name,Itpux12.name,Itpux11.sex,Itpux12.grade

from Itpux11 full (outer) join Itpux12 on Itpux11.name=Itpux12.name;

2.6笛卡尔积(cross join),交叉连接,左右两边相乘的结果作为记录条数(和乘法表一样)。

select Itpux11.name,Itpux12.name,Itpux11.sex,Itpux12.grade

from Itpux11 cross join Itpux12 ;(左乘右)

2.7 SEMI JOIN,用于exists

select * from Itpux11 natural join Itpux12;

select * from Itpux11

   where exists

        (select * from Itpux12

                        where Itpux11.name=Itpux12.name

                         and Itpux12.grade > 20);

2.8 ANTI JOIN

用!= 或者not in表示

select *

      from itpux11

where name not in (select name from itpux12 where itpux12.grade > 20)

order by name;   

2.9 SELF JOIN 自身连接

select y1.last_name,y2.last_name

     from yg y1,yg y2

   where y1.manager_id=y2.employee_id

            and y1.last_name like 'R%';

原文地址:https://www.cnblogs.com/maowuyu-xb/p/11462966.html