oracle基础篇一

----------------------------------------------安装:-----------------------------------
1、Oracle11gR2(11.2.0.1.0).iso解压后===》setup.exe===》全局数据库名:orcl 、管理口令:123456、确认口令

2、plsql.rar解压后===>setup.exe ===>成功后输入:用户名:sys/system,密码:123456


--------------------------------------------------------------------创建新用户授权导入数据 .sql--------------------------------------------------------------------------

conn sys/123456 as sysdba;--连接dba用户 or sqlplus "/as sysdba"

select sysdate from dual;--查询当前的语言环境 英文or中文

alter session set nls_date_language=english;--更改为英文环境

start c:\summit2.sql;--导入summit2.sql到数据库

-----------------------------------------------------------------------数据库基本概念--------------------------------------------------------------------------

--数据:指要存储的信息,文字、图形、图像、声音
--数据库:存储数据的仓库,发展阶段
--人力手工整理存储数据
--使用磁盘文件存储数据
--关系型数据库
--优点:有组织的存储、可共享、冗余小、易扩展
--数据库管理系统(Database Management System):科学地组织和存储数据,有效地获取和维护数据
--数据库系统(Database System):是数据库和数据库管理系统的的集合(DBMS、DB、Application Software、User、Database Manager)
--Oracle:甲骨文股份有限公司,全球最大的企业软件供应公司
--Oracle是最大的数据库厂商提供的关系型数据库产品
--Oracle 发展历程:
--1979年,Oracle公司推出Oracle 2
--1980年,Oracle 3
--1992年,Oracle 7
--1999年,Oracle 8i
--2001年,Oracle 9i
--2004年,Oracle 10g
--2007年,Oracle11g


----------------------------------------------------查询select-------------------------------------------
一、基本查询
1.查看所有信息
select * from 表名;

select * from stu;

2.显示指定字段
select 字段名1, 字段名2,。。。 from 表名
select first_name, age from stu;

3.给字段取别名: as 可以省略 ,如果别名为 关键字 必需加双引号


select first_name as 姓, age as 年龄 , sex as "select" from stu 表名;


4.给表取别另:as不加
select 表别名.字段名, 表别名.字段名,... from 表名 表的别名

select s.id , s.last_name from stu s;

5、字段的联接: ||

select 字段字名1||字段字名2||'字符串',.... 别名 from 表名;


select first_name||' '||last_name||'---'||age 姓名 from stu;

习题1:在s_emp表中,查找出LAST_NAME列和FIRST_NAME列中间加一个空格连在一起输出,其输出的列名为my$name的sql语句

6、删除重复

SELECT DISTINCT id, last_name FROM stu;

习题2:请查找出表S_DEPT中,NAME列不重复的内容

二.按条件查
select * from 表名 where 条件表达式

条件表达式: 真、假

对SQL语句返回的数据集进行筛选
--紧跟在FROM子句后
--由一个或多个限定条件组成,限定条件由表达式、比较运算符和字面值组成
--所有字符串和日期需要用单引号括起来,数值不需要单引号
--日期在Oracle里有特定的格式,'DD-MON-YY'(具体看日期的显示格式),否则作为一个字符串。

1.比较运算符:= , < , > ,<=,>=, != 或 <>

select * from 表名 where 字段名 比较运算符 值


select * from stu where sex='女';
select * from stu where age=20;
select * from stu where mathe>60;
select * from stu where first_name <> '王';// !=

习题3:到s_emp表中,查找出月工资大于2000的员工?
习题4:到s_emp表中,查找出title为 Warehouse Manager的员工?

2.通配符 (模糊查询)like / not like
字符串的值是区分大小写,其它的不区分大小写

%:0、1、多个字符
字段名字符类型

select * from 表名 where 字段名 like '含有通配符的字符串'

select * from stu where last_name like '%w%';

-:1个字符

select * from stu where last_name like 'w_';

select * from stu where last_name not like 'w_';

习题5:到s_emp表中,查找出LAST_NAM中含有m的,工资大于1000的员工?

3.算数运算符 (可以出现在where,但要与比较运算符一起 , select--from)
+,-,*,/ 字段为数值类型

select * from stu where (mathe+chinese) > 120;


4.逻辑运算符:and 、or、 in ()、between... and

select * from stu where mathe>60 and mathe<80;//在60与80之间的, 字段可以是不同的

select * from stu where chinese < 60 or chinese >80; //小于60 或 大于80

select * from stu where grades in (1,3);//查出括号列出的取值

select * from stu where mathe between 60 and 80;//小值在前大值在后包含60,80, 对一个字段,字段是数值类型的

习题6:到s_emp表中,查找出工资大于1000并小于1500的员工?


5.null, 值未知(还没有赋值), 注意:空格不是null

select * from 表名 where 字段名 is null; //is not null

select * from stu where first_name is not null;
select * from stu where first_name is null;

习题7:到s_emp表中,查找出comments为空的记录?

六、排序
select * from 表名 where 条件表达式 order by 排序字段名1,排序字段名2,... asc/desc

--ORDER BY子句,在整个SELECT语句中始终位于最后,后面可以是列名、列的别名、表达式、顺序号
--ORDER BY后面可以跟多列,表示先按第一列排序,如果第一列值相同再按第二列排序,如果第二列值也相同,则按第三列排序,依次类推
--ASC表示升序(可省略),DESC表示降序,空值是最大的

select * from 表名 order by 列数 asc/desc
select * from stu order by 字段名1 asc,字段名2 desc,... ;
select * from 表名 whele 条件表达式 order by 字段名 asc/desc

select * from stu order by age;
select * from stu order by 7 desc;
select * from stu order by mathe, chinese desc ;//当第一个字段相同时,按第二个字段排

select * from stu where sex='女' order by mathe desc;

习题8:s_emp表,查找出员工的id、last_name、manager_id、年薪,请按照员工的manager_id升序排列,年薪降序排列

七、聚合函数:from的前面, select 后面, 数值类型, 字段只能有一个

count(), max(), min(),sum(), avg()

1.查表的所有记录个数 count(字段名)或count(*)

select count(sex) 总数 from stu;
select count(*) 总数 from stu;
select count(*) "1班人数" from stu where grades=1;


2.max(数值类型字段),min(数值类型字段)
select max(mathe) from stu where sex='男';
select min(mathe) , max(mathe)from stu where sex='男';

3.sum(数值类型字段), avg(数值类型字段)
select sum(mathe) from stu;
select sum(mathe)/count(*) from stu;
select avg(mathe) from stu;

习题8:到s_emp表中,查找出最高的工资与最小的工资,员工的工资总和与平均工资, 并求出员工总数?

八、rownum:查询结果中每一行的编号,
select from 的中间,
where :
1. = 1
2. <= 数值
3. < 数值

select rownum, last_name, id from stu where age=20 and rownum<=2 ;
select * from stu where age=20 and rownum =1;
select * from stu where age =20 and rownum<=2;

select * from stu where rownum<5;
select * from stu where age=20 and rownum>0;


显示语文成绩的前3名
select * from (select * from stu order by chinese desc) where rownum<=3;

思考:
习题9:到s_emp表中,查找出工资在3到5名的员工?


八、分组查询
select 分组字段1,分组字段2 , 聚合函数() from 表名 group by 分组字段1,分组字段2 having 条件 order by 分组字段/聚合函数;


select 分组字段1,分组字段2 , 聚合函数() from 表名 where 条件表达式 group by 分组字段1,分组字段2 order by 分组字段/聚合函数;


条件表达式:不能出现聚合函数
条件 :可以出现聚合函数

select grades, sex, count(*) from stu group by grades, sex;

select grades, max(mathe) from stu group by grades;


--1.如果有分组:select 与from之间只能出现:分组字段名与聚合函数
--2.如果分组,又排序:排序只能出现:分组字段名与聚合函数
--3.如查分组,条件语句为:having , having中只能出现: 分组字段名与聚合函数, 只能在 group by 的后面

--4.如果分组中使where做为条件语句,where中只能出现:分组字段名


select grades, max(age) from stu group by grades having max(age)>30 order by max(age)
select grades, max(age) from stu group by grades having grades<2 order by max(age)

select grades, max(age) from stu where grades<2 group by grades order by max(age);


习题1:对s_emp中的title进行分组,并显示部门的总人数与最高工资

数学的平均分大于75人

select last_name,avg(mathe) from stu group by last_name having avg(mathe)>75

数学平均大于75分的班级

select grades,avg(mathe) from stu group by grades having avg(mathe)>75;

按班级分组,并且将数学成绩大于50分的求平均 平均并排序
select grades, avg(mathe) from stu where mathe>50 group by grades order by avg(mathe) desc ;


习题2、对s_emp表中salary+commission_pct的总工资大于1500的人

select * from s_emp where sum(salary+commission_pct)>1500

select last_name, sum(salary+commission_pct) zgz from s_emp group by last_name having sum(salary+commission_pct)>1500

select last_name, salary, commission_pct, sum(salary+commission_pct) from s_emp group by last_name, salary, commission_pct having sum(salary+commission_pct)>1500;
注:只要判断条件中有聚合函数,就要使用分组

九、函数

1、lower(字段名)--小写, upper()--大写, initcap()--首字母大写

select lower(last_name) from stu where lower(last_name) like 'w%' ;
select upper(last_name) from stu where upper(last_name) like 'W%';
select initcap(last_name) from stu where initcap(last_name) like 'W%';

注:字段为字符类型

习题1:对s_emp表中last_name中含有v的记录以last_name小写,first_name大写,userid的开头字母大小显示
select lower(last_name), upper(first_name), initcap(userid) from s_emp where upper(last_name) like '%V%'

select lower(last_name), upper(first_name), initcap(userid) from s_emp where lower(last_name) like '%v%'

习题2:对s_emp表中last_name中含有大写V的记录以last_name小写,first_name大写,userid的开头字母大小显示
select lower(last_name), upper(first_name), initcap(userid) from s_emp where last_name like '%V%'

习题3:对s_emp表中last_name中含有小写v的记录以last_name小写,first_name大写,userid的开头字母大小显示
select lower(last_name), upper(first_name), initcap(userid) from s_emp where last_name like '%v%'


2、截取
substr(字符串的字段名, 开始截取的位置, 截取长度)

select substr(last_name,2, 3) from stu;

习题1:在表s_emp中查找fires_name的前两个字符中含有ma的,并显示last_name从第3个到5个字符。
select substr(last_name, 3, 3), first_name from s_emp where lower( substr(first_name, 1,2 )) like 'ma%';

select substr(last_name, 3, 3), first_name from s_emp where lower( substr(first_name, 3,3 )) like 'ber';

3、字符串长度
length(字符串的字段)

select last_name, length(last_name) from stu where length(last_name)>2;


4、round()四舍五入,trunc()截取 数值

select round(avg(mathe), 2) avg from stu;

select trunc(avg(mathe), 2) avg from stu;
习题1:在s_emp表中,查找部门的对工资涨11.12345%的平均工资,并保留3小数。 (四舍五入,与不做四舍五入)

select title, round( avg(salary*11.12345 + salary) , 3), trunc(avg(salary*11.12345 + salary), 3 ) from s_emp group by title;


十、子查询:
--子查询的位置可以出现在where子句、having子句、from子句
--子查询还可以嵌套其他子查询,允许多层嵌套

1、< 、<=、>、>=、= 、<>、关系运算符时,
注:
1、一个select 的查询结果 要做为 另一个select的条件
2、子查询句的返回集只能有一个字段
3、子查询的返回字段只能有一个值

select * from 表名 where 字段 关系运算符(select 字段 from 表名)


select * from stu where chinese > (select avg(mathe) from stu);
select * from stu where mathe= (select max(mathe) from stu );
select * from stu where mathe< (select max(mathe) from stu where sex='男' );

习题:查找s_emp表中,比平均工资高的人, (平均工资)


select last_name, salary from s_emp where salary > ( select avg(salary) from s_emp )
select last_name, salary, avg(salary) from s_emp where salary > ( select avg(salary) from s_emp ) group by last_name, salary

2、all, any , in
注:、
注:
1、一个select 的查询结果 要做为 另一个select的条件
2、子查询句的返回集只能有一个字段
3、子查询的返回字段只能有多个值,需关系运算符后加 all, any , in

all:所有
select * from stu where id >= all (select id from stu where mathe>60); // 1,3,4,11
1、>all 比最大的大
2、<all 比最小的小

any:一个
select * from stu where id <= any (select id from stu where mathe>60);

1、>any 比最小的大
2、<any 比最大的小

in:相等
select * from stu where id in (select id from stu where mathe>60); // 1,3,4,12


select * from stu where id =any (select id from stu where mathe>60); // 1,3,4,12

习题:查询出比最多班级人数少的班级

select grades, count(*) from stu group by grades having count(*) <any (select count(*) from stu group by grade


十一、多表查询 (有关联)

1. select * from 表名1 别名1, 表名2 别名2 where 连接与查询条件

n个表,条件有n-1个

select * from stu, course; //两两的相积

select * from stu , course where stu.id = course.id ;
select s.last_name, s.first_name, c.cname from stu s , course c where s.id = c.id ;


2、内联接inner join
select * from 表名1 inner join 表名2 on 联接的条件 where 查询查条件

select * from stu inner join course on stu.id=course.id;
select * from stu , course where stu.id=course.id;
select * from stu inner join course on stu.id=course.id where stu.id>1;

3、外联接
select * from 表名1 left join 表名2 on 联接的条件 where 查询查条件

1)左联接 left join
select * from stu left join course on stu.id = course.id;
左表全部显示,右表只显示满足条件的
2)右联接 right join

select * from stu right join course on stu.id = course.id;
右表全部显示,左表只显示满足条件的
3)全联接 full join
select * from stu full join course on stu.id = course.id;
右表全部显示,左表全部显示


4、子查询可以转换为多表查询 (子查询的返回结果只有一个)

在stu表中查找出比小红年龄大的人
select * from stu where age > (select age from stu where last_name='小红');


select s1.* from stu s1,stu s2 where s1.age>s2.age and s2.last_name='小红';


select s1.last_name, s1.age , s1.id from stu s1, stu s2 where s1.age>s2.age and s2.last_name='海' ;

习题9:从表S_CUSTOMER,S_REGION中查找出顾客表的NAME、REGION_ID列和区域表的name列,REGION_ID为5号区域

select s1.name,s1.region_id,s2.name from s_customer s1 full join s_region s2 on s1.region_id = s2.id where s2.id = 5;select c.name, c.region_id, r.name from s_customer c inner join s_region r on c.region_id=r.id where c.region_id=5
select c.name, c.region_id, r.name from s_customer c left join s_region r on c.region_id=r.id where c.region_id=5
select c.name, c.region_id, r.name from s_customer c right join s_region r on c.region_id=r.id where c.region_id=5
select c.name, c.region_id, r.name from s_customer c full join s_region r on c.region_id=r.id where c.region_id=5
select c.name, c.region_id, r.name from s_customer c , s_region r where c.region_id=r.id and c.region_id=5


习题11:请统计出表S_ORD中,11号销售人员(SALES_REP_ID=11)的订单数量,以及他的TOTAL平均值
select sales_rep_id,count(*) "订单数量",avg(total) "total平均值" from s_ord group by sales_rep_id having sales_rep_id = 11 ;


UNION 操作符用于合并两个或多个 SELECT 语句的结果集。

请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。

SQL UNION 语法
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2

SQL UNION ALL 语法
SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2

另外,UNION 结果集中的列名总是等于 UNION 中第一个 SELECT 语句中的列名。

两个select语句的字段类型匹配,而且字段个数要相同,字段类型名
在进行表链接后会筛选掉重复的记录,所以在表链接后会对所产生的结果集进行排序运算,删除重复的记录再返回结果

创建表:

create table 表名(

字段名1 数据类型 约束条件,

字段名2 数据类型 约束条件,
.。。。
字段名n 数据类型 约束条件

);

表名: 开头必是字母,1--30字符, 字母,数字,下划线,$ ,#
字段名1 表名唯一, 关键字不能为表名


插入表记录
1. insert into 表名 values(值1, 值2, 值3,值4,...)

insert into myTA values(1000, '李四张山', '男');

2.insert into 表名(字段名1, 。。。) values(值1,。。。)
insert into myTA(name) values('赵武');

修改表数据
update 表名 set 字段名1=值(表达式), 字段名2=值(表达式),。。。 where 修改条件
update myTA set sex='女';//
update myTA set sex='F' where name='abcdefse' ;
update myTA set id = id+11, sex='MF' where name='李四张山';


删除
1.delete from 表名 where 删除条件 ;//删除表记录

truncate table 表名; //重新建立表结构,
delete from 表名;//没有修改表结构

2.drop table 表名; //删除整个表的


约束
1、主键 primary key 唯一值,并不能为空,一个表只能有一个主键

create table myB(
id number(4) primary key,
name varchar2(8),
sex char(2)
);


2、非空约束 not null , 不能为空


create table myC(
id int primary key,
name varchar2(8) not null,
sex char(2) null
);


3、唯一约束 unique,赋值时值是唯一的,但空值可以出现多次

create table myD(
id int primary key,
name varchar2(8) unique ,
sex char(2) null
);

primary key == unique not null
create table myD(
id int primary key,
name varchar2(8) unique not null,
sex char(2) null
);

4、检查约束:check(),满足条件的才可以赋值成功

create table myE(
id int primary key,
name varchar2(8) unique ,
age int check(age>20 and age<60),
age int check(age beween 20 and 60),
sex char(2) check( sex in ('M','F') )

);

5、外键, 外键的值必需是主键中以存在的值


主表的字段名是唯一或主键
向子表中插入数据时,值必须是主表存的值, 但可以是空值(多个)


create table mySoct(
sid int,
mathc number(4),
chinec number(4),
constraint 外键名 foreign key(从表字段名) references 主表名(主表的字段名)
);

先删除从表,再删除主表

create table status(
status_id number(3) primary key,
status_name varchar2(8)
);

insert into status values(101, '创建');
insert into status values(102, '接受');
insert into status values(103, '单派');
insert into status values(104, '备餐');
insert into status values(105, '送出');
insert into status values(106, '结账');
insert into status values(107, '无人接单');

select * from status;


create table order_header(
order_id number(1),
order_code number(15),
status_id number(3),
order_date varchar2(8),
product_id varchar2(4),
qty number(3),
amount number(5),

foreign key(status_id) references status(status_id)
);
insert into order_header values(1, 201107010001, 101, '20110701' ,'D101',1, 80);
insert into order_header values(2, 201107010002, 106, '20110701' ,'D118',5, 125);
select * from order_header;

每个业务日期的“状态为'单派','送出','结账”的订单金额合计值。
select order_header.order_code, order_header.status_id, order_header.qty*order_header.amount from status inner join order_header on status.status_id=order_header.status_id
where status_name in ('单派','送出','结账')

表结构
1.加添表结构
alter table 表名 add(新字段名 数据类型 约束, sex char(2) 。。。);


2.修改表结构:

alter table 表名 modify (字段名 数据类型 约束,。。。);
数据类型:类型, 类型的取值范围大


3.删除表结构

alter table 表名 drop column 字段名;//删除一列



alter table 表名 drop ( 字段名, 字段名,。。。);//删除多列

4.修字段名
alter table 表名 rename column 旧字段名 to 新字段名;

5.修改表名
rename 旧表名 to 新表名;

6.显示表结构:
desc 表名

日期类型:date

1.to_date('日期字符串','格式字符串')

to_date('2000-02-5 12:30:05','yy-mm-dd hh24:mi:ss')

sysdate当前系统时间
insert into w1 values(1005, 'www', sysdate);

3.to_char( 日期字段名, '字符串的格式')

select * from myGg where to_char(eb,'dd-mm-yyyy' )='03-10-2018';

2.months_between(日期数据, 日期数据),两个日期之间的月份
select months_between(日期数据, 日期数据) from myGg;

4.to_char( 数字段名, '字符串的格式')

select to_char(pr, '$999,999,999.99') from mygf;

L:当地货币符号

$:美元符号
9:一个数字
0:强制0显示


用户: system / sys

1.select * from dba_users; 显示所有用户

1.创建用户: create user A identified by 123456;

2.权限分配:
DBA:所有全部
resource:可以创建实体,不可以创建数据库
connect :可以登录
分配权限
grant connect, resource to 用户名;//

grant all on 表名 to 用户名;//all 增,删,改,查
grant select,update on 表名 to 用户名;

管理员:
select * from 用户名.表名

commit;提交

删除权限
revoke connect from 用户名;

3.用户的切换
conn 用户/密码

4.修改用户密码
alter user 用户名 identified by 新密码;

5.删除用户

drop user 用户名;
注:在用户名下以有表,或。。, 在删除用户前必需先将表删除,再删除用户


视图:用来保存查询结束
1、创建视图: 注,视图的列必是表中存在的列名,列名不能重复
函数,算数运算,rumber... 但可以给它们取一个别名
如:
create view mydd as select mathe+10 as mathe from stu inner join course on stu.id = course.id where sex='女';
select * from mydd;

create view 视图名 as 查询语句;
create view stu_mathe as select first_name||''||last_name as "姓名", mathe from stu where mathe>60;

select * from stu_mathe;
如果用户是普通用户,需要先赋权限
grant create view to 用户名;

create view 新视图名 as select * from 表名 where name like 'd%';


2、修改视图
create or replace view 视图名 as 查询语句;
create or replace view 原视图名 as select * from 表名 where name like '%d%';

3、删除视图

drop view 视图名;

索引: 用于提高查询效率 , 表记录多时(10W)
查询结束在 5%,使用

create index 索引名 on 表名(字段名);

create index stu_index_first on stu(first_name);

select * from stu where first_name='王';


序列:是一个计数器
1、定义
create sequence 序列名
start with 1 //开始值
increment by 1 //步长
minvalue 1 //最小值
maxvalue // 最大值 , nomaxvalue
cycle //循环,nocycle
cache //缓存, nocache

create sequence mys
start with 1 //开始值
increment by 1 //步长
minvalue 1 //最小值
maxvalue // 最大值 , nomaxvalue
cycle //循环,nocycle
cache //缓存, nocache

2.使用, 主键
insert
update
序列名.nextval

id, name, age;
1 ddd 20
2 ddd 20
3 ddd 20
insert into stu values(mys.nextval,'ddd', 20)
insert into stu values(mys.nextval,'ddd', 20)
insert into stu values(mys.nextval,'ddd', 20)
mys.currval

同义词:另一个用户的表的别名 .

create synonym 同义词名 for 用户名.表名


两个普通用户,一个用户使用另一个用户中的表,视图...


1.第一个普通用户,需要具有connect, resource ,create synonym , 管理员授权

2.以第一个用户登录,创建同义词
create synonym 同义词名 for 第二个用户名.表名

3.以第二个用户登录,将对表的操作权限给第一个用户
grant select on 表名 to 第一个用户名

grant all on 表名 to 第一个用户名

4.以第一个用户登录,使用同义词
select * from 同义词名

drop synonym 同义词名

猜你喜欢

转载自www.cnblogs.com/fqqwz/p/11624934.html