Oracle 11g 数据库基础教程
课堂笔记
系统服务
操作系统通过服务来管理安装好的Oracle 11g数据库产品的运行
在运行Oracle数据库前,要检查数据库服务有没有启动好
开始»控制面板»管理工具»服务
OracleServiceORCL:数据库服务(数据库实例),是Oracle核心服务,是数据库启动的基础,只有该服务启动,Oracle数据库才能正常启动。(必须启动)
OracleOraDb11g_home1TNSListener:监听服务,该服务只有在远程访问数据库才需要。(必须启动)
OracleDBConsoleorcl: Oracle控制台服务,即企业管理器服务。只有该服务启动了,才可以使用Web方式的企业管理器管理数据库。(非必须启动)
文件体系结构
C:\app\Administrator
oradata:以数据库为单位,存放数据库的物理文件,包括数据文件、控制文件和重做日志文件。
product:存放Oracle 11g数据库管理系统相关的软件,包括可执行文件、网络配置文件、脚本文件等。
安装好数据库后,自动有3个用户:
系统用户:sys、system
普通用户:scott
用户名/密码: Scott/123456
把字体放大:
鼠标光标对准SQL Plus»右键单击»属性»弹出“SQL Plus”属性列表
启动数据库控制OEM
开始»所有程序»Oracle-OraDb11_g_home1»Database Control-orcl
口令:123456
OEM控制台的URL格式为:https://主机名或主机IP地址:要连接的数据库的服务端口号/em
2.2.2 启动SQL Plus
(1)菜单命令方式启动SQL Plus
开始»所有程序» Oracle-OraDb11_g_home1»应用程序开发»SQL Plus
(2)命令行方式启动SQL Plus
开始»所有程序»附件»命令提示符
13.1.4 用户的锁定与解锁
如果遇到scott用户被锁定,解锁步骤如下:
①conn system/123456;
②alter user scott account unlock;
③alter user scott identified by 123456;
2.2.3SQL Plus常用命令
①conn[ect]命令先断开当前连接,然后建立新的连接(切换用户)
②如果要以sys(特权用户)的身份连接,必须要带as sysdba或as sysoper
SQL>conn sys/123456 as sysdba
③disconn[ect]命令作用是断开与数据的连接,但不退出SQL Plus环境
SQL>disconn
④exit命令作用是断开与数据库的连接,并且完全退出SQL Plus环境
SQL>exit
⑤建表、解锁»带分号‘;’
(1)linesize:设置行显示的长度,默认值为80。如果输出行的长度超过80个字符,则换行显示
SOL>set linesize 130;
(2)serveroutput:设置是否显示执行dbms_output.put_line命令输出的结果。
若该变量值为on,则显示输出结果,否则不显示输出结果。默认值为off。
SOL>show serveroutput
SOL>set serveroutput on
(3)time:设置是否在SQL Plus命令提示符之前显示时间(on),默认值为off,不显示。
SOL>show time
SOL>set time on
(4)timig:设置是否显示SQL语句的执行时间,默认值为off,不显示SQL语句的执行时间。
SOL>show timing
SOL>set timing on
(5)spool:将SQL Plus屏幕内容存放到文本文件夹中(记录屏幕数据)
SQL>spoold:\t1.txt 路径/文件名
SQL>spooloff (必须结束)
(6)desc[ribe]:显示任何数据库对象的结构信息(描述表结构)
SQL>descemp;
2.3 SQL Daveloper
创建数据库连接
测试»连接
课堂内容:
select * from emp;
selectempno,ename,to_char(hiredate,'yyyy-mm-dd hh:mi:ss')from emp;
6.2 表的创建与管理
--创建表
--创建部门表
create table department
(
deptid number(2) primary key,
dname varchar2(20) unique,
loc varchar2(20)
);
--创建雇员表(Employee),包括员工号、员工名、工作职位、主管领导、雇佣日期、基本工资、补贴、部门号等员工信息。
create table employee
(
empid number(4) primary key,
ename char(10) not null,
job char(10),
mgr number(4) ,
hiredate date,
baseSal number(7,2),
comm number(7,2),
deptid number(2) constraint fk_deptidreferences department(deptid)
);
--修改表
--添加约束
alter table employee add constraint ck_sal check(baseSal between 1000 and 3000);
--修改表结构 (添加列,修改列,删除列)
--修改部门表,添加一列tele ,字符类型 长度13
alter table department add tele char(13);
--修改部门表,修改tele ,长度15
alter table department modify tele char(15);
--删除列 tele
alter table department drop column tele;
--查看用户下的表
select * from cat;
--用数据字典:user_tables
desc user_tables
select table_name,tablespace_name,status fromuser_tables where table_name='DEPARTMENT';
在Oracle中可以通过查询数据字典视图获取用户信息
6.3 索引的创建与管理
--创建索引
--在emp表 ename列上创建索引 (单个列上创建索引)
create index idx_ename on emp(ename);
--多个上列创建索引
create index emp_idx_j on emp(ename,job);
--创建基于函数的索引
create index emp_fun_idx onemp(UPPER(ename));
--删除索引
drop index emp_fun_idx;
6.4 视图的创建与管理
创建视图前要在SQL Plus先授权
授权:SQL> grantcreate view to scott;
--创建视图
--简单视图:查询的数据来自一个表
--复杂视图:查询的数据包括多个表
--1,创建只读视图,建立一个10部门员工的只读视图dep_10。
create or replace view dep_10
as
select * from emp where deptno=10
with read only;
select * from dep_10
--2,在emp建立视图emp_v1,包括empno,ename,job,sal
create or replace view emp_v1
as
select empno,ename,job,sal from emp;
--视图操作(简单视图可以进行增删改查)
insert into emp_v1values(8000,'chenxin','SALESMAN',2300);
select * fromemp order by empno desc
--复杂视图(复杂视图不可以进行增删改查)
--创建一个包含各部门的部门号、部门人数和部门平均工资的视图
create or replace view sal_avg as
select deptno,avg(sal) avgsal ,count(*)total
from emp
group by deptno;
课堂作业:
一、创建表
1,建立学生表student
学号: sno 数字类型 长度2 主键
姓名: sname 字符类型 长度8 非空
年龄 age 数字类型 长度2
出生日期 birthdate 日期类型
create table student
(
sno number(2) primary key,
sname varchar2(8) not null,
age number(2),
birthdate date
);
2,班级表class
班级号 cno 数字类型 长度2 主键
班级名 cname 字符类型 长度20
班级人数 rs 数字类型 长度2
学号 sno 数字类型 长度2 外键
create table class
(
cno number(2) primary key,
cname char(20) ,
rs number(2),
snonumber(2) references student(sno)
);
二、修改表
1) 修改表student,使 age 在13到15之间
alter table student addconstraint ck_age check(age between 13 and 15);
2) 修改表student,添加电话tele 字符 长度17
alter table student add telechar(17);
3) 删除tele字段
alter table student dropcolumn tele;
6.5 序列
--创建序列
--查看部门表dept数据
select* from dept;
create sequence dept_seq
increment by 10
start with 50;
--使用序列 nextval 和 currval 两个属性
insert into dept values(dept_seq.nextval,'管理部','aaaaaaa');
--查看序列当前值
select dept_seq.currval fromdual;
insert into dept values(dept_seq.nextval,'纪检部','ccccccc');
--删除序列
drop sequence dept_seq;
7.1数据插入
数据操作 insert , update ,delete , select
日期类型数据插入方法
select * from emp;
1)原始数据插入方法
insert into emp(empno,ename,hiredate,sal)
values(8000,'chenxin',to_date('2018-06-02','yyyy-mm-dd'),1800);
2)带有时分秒的数据插入
insert intoemp(empno,ename,hiredate,sal)
values(8001,'liang',to_date('2010-06-0310:11:12','yyyy-mm-dd hh:mi:ss'),1800);
--查看
selectempno,ename,to_char(hiredate,'yyyy-mm-dd hh:mi:ss'),sal from emp;
3)insertinto 表1 select * from 表2
--创建部门表 department
create table department
(deptid number(2) primary key,
deptName char(10),
deptloc varchar2(20)
);
--往表里插入数据
insert into department select * from deptwhere deptno in (10,30);
select * fromdepartment;
4) createtable 表1 as select * from 表2
create table ndept as select * fromdept;
select * from ndept;
光标放在选中的行的最前边 键盘按Shift+ End 瞬间整行选中
--修改数据
update dept set loc='bbbbb'where deptno=50;
select * from dept;
update dept setdname='yirong',loc='bbbbb' where deptno=60;
commit; 提交
--删除数据
delete from dept wheredeptno=50;
--整个表的数据都删除,不能回滚
truncate table ndept;
rollback; 回退
SQL Plus中事务是隐形开始的
当发出commit或rollback命令时事务才算结束
Oracle SQL Developer 工具»首选项»代码编译器
可以调整字体还有显示行数
课堂作业:
第一步:在SQL Plus中授权
SQL*Plus: Release 11.2.0.1.0Production on 星期六 6月 2 10:33:45 2018
Copyright (c) 1982, 2010,Oracle. All rights reserved.
请输入用户名: system/123456
ERROR:
ORA-28002: the password willexpire within 7 days
连接到:
Oracle Database 11gEnterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP,Data Mining and Real Application Testing options
SQL> create user stu_user identified by stu123;
用户已创建。
SQL> grant connect,resource to stu_user;
授权成功。
SQL> grant create view,create any index,create sequence tostu_user;
授权成功。
第二步:在SQL Developer中新建连接
--(2)创建表并插入数据
--创建book表
create table book
(
no char(10) primary key,
title varchar2(80) not null,
author varchar2(20) not null,
publish varchar2(20) not null,
pub_date date not null,
price number not null
);
--创建reader表
create table reader
(
rno char(10) primary key,
rname char(8) not null
);
--创建borrow表 联合主键
createtable borrow
(
no char(10) not null,
rno char(10) not null,
borrow_date date,
constraint pk_no_rno primary key(no,rno)
);
--往book表中插入数据
select * from book;
insert into book(no,title,author,publish,pub_date,price)
values (100001,'Oracle 9i数据库系统管理','李代平','冶金工业出版社',to_date('2003-01-01','yyyy-mm-dd'),38);
insert into book(no,title,author,publish,pub_date,price)
values (100002,'Oracle 9i中文版本入门与提高','赵松涛','人民邮电出版社',to_date('2002-07-01','yyyy-mm-dd'),35);
insert into book(no,title,author,publish,pub_date,price)
values (100003,'Oracle 9i开发指南:PL/SQL程序设计','Joan Casteel','电子工业出版社',to_date('2004-04-03','yyyy-mm-dd'),49);
insert into book(no,title,author,publish,pub_date,price)
values (100004,'数据库原理辅助与提高','盛定宇','电子工业出版社',to_date('2004-03-01','yyyy-mm-dd'),34);
insert into book(no,title,author,publish,pub_date,price)
values (100005,'Oracle 9i中文版本实用培训教程','赵伯山','电子工业出版社',to_date('2002-01-01','yyyy-mm-dd'),21);
insert into book(no,title,author,publish,pub_date,price)
values (100006,'Oracle 8实用教程','翁正科等','电子工业出版社',to_date('2003-07-08','yyyy-mm-dd'),38);
--往reader表中插入数据
select * from reader;
insert intoreader(rno,rname) values (200001,'张兰');
insert intoreader(rno,rname) values (200002,'李凤');
insert intoreader(rno,rname) values (200003,'孟欣');
insert into reader(rno,rname)values (200004,'谢非');
insert intoreader(rno,rname) values (200005,'刘英');
--往borrow表中插入数据
selectno,rno,to_char(borrow_date,'yyyy-mm-dd hh:mi:ss') from borrow;
insert into borrow(no,rno,borrow_date)
values (100001,'200001',to_date('2004-08-1010:06:14','yyyy-mm-dd hh:mi:ss'));
insert into borrow(no,rno,borrow_date)
values (100002,'200002',to_date('2004-08-1010:06:27','yyyy-mm-dd hh:mi:ss'));
insert into borrow(no,rno,borrow_dat