Oracle数据库基本操作
Data files
Online redo log files
Parameter file
Backup files
Archive log files
Password file
Alert and trace log files
基本概念
• 数据库逻辑结构
Logical
Database
Physical
Schema
Tablespace
Data file
Segment
Extent
Oracle data block
OS block
基本概念
• 用户,角色,权限
Users Jenny David Rachel
Roles
HR_MGR
HR_CLERK
Privileges
Delete employees. Insert employees.
通过代码操作数据
• 与存储过程互动:
create or replace procedure Test( BanJieType in varchar2, cur_OUT_f OUT GLOBALPKG.outlist) as begin --TODO OPEN cur_OUT_f FOR select * from 临时表; end Test;
• 这个别名在哪配置?
(C:\Oracle\product\11.1.0\client_1\network\admin\tnsnames.o ra)
• 有没有图形化配置工具?(NETCA)
管理常用的对象
通过PL/SQL Developer,可以方 便的管理一些常用的对象
管理常用的对象
• 表:
管理常用的对象
基本概念
• SQL语句------常用函数
– – – – – – – – 时间转字符串: to_char(sysdate,'yyyymmddhh24miss') 字符串转时间:to_date(‘1981-10-10’) 数字转换:TO_NUMBER( '9999') 字符串截位: SELECT SUBSTR('ABCDEFG',3,4) "Substring" FROM DUAL; 字符补全: SELECT (L/R)PAD(‘MORRISON’,12,‘ab’) “PAD example" FROM DUAL; (L/R)RIM: SELECT (L/R)TRIM('BROWNINGyxXxy','xy') "RIM example" FROM DUAL; TRIM:SELECT TRIM (0 FROM 0009872348900) "TRIM Example" FROM DUAL; NVL: SELECT last_name, NVL(TO_CHAR(commission_pct), 'Not Applicable') "COMMISSION" FROM employees
• 视图:
创建,修改视图: CREATE OR REPLACE VIEW 视图名称 AS SELECT ***********************
• 序列:
NEXTVAL CURRENTVAL
管理常用的对象
• 触发器:
CREATE TRIGGER hr.salary_check BEFORE INSERT OR UPDATE OF salary, job_id ON hr.employees FOR EACH ROW WHEN (new.job_id <> 'AD_VP') CALL check_sal(:new.job_id, :new.salary, :new .last_name);
CREATE OR REPLACE PACKAGE GLOBALPKG AS TYPE outlist IS REF CURSOR; TRANCOUNT INTEGER := 0; IDENTITY INTEGER; END;
Database db = DatabaseFactory.CreateDatabase("WebAudit_ConnectionString"); DbCommand cmd = db. GetStoredProcCommand (存储过程名称); db.AddInParameter(cmd, “参数名”, DbType.String, 参值); if (DatabaseSettings.GetDataBaseType() == DatabaseSettings.DataBaseType.oracle) cmd.Parameters.Add(new OracleParameter("cur_OUT_f", OracleType.Cursor, 0, ParameterDirection.Output, string.Empty, DataRowVersion.Default, true, null)); db.ExecuteDataView(cmd);
用户
CREATE USER EpointSystemSupport IDENTIFIED BY "11111" DEFAULT TABLESPACE "EPOINTSYSTEMSUPPORT" TEMPORARY TABLESPACE "TEMP" QUOTA UNLIMITED ON "EPOINTSYSTEMSUPPORT"; GRANT "CONNECT" TO EpointSystemSupport; GRANT "RESOURCE" TO EpointSystemSupport; ALTER USER EpointSystemSupport DEFAULT ROLE "CONNECT", "RESOURCE";
导入导出
• 导入:
– imp dbuser/oracle file=oradb.dmp log=oradb.log full=y buffer=4096000 commit=y ignore=y feedback=10000
• 导出
– exp dbuser/oracle file=dbuser.dmp log=dbuser.log owner=dbuser buffer=4096000 feedback=10000
管理常用的对象
• 函数:
管理常用的对象
• 存储过程:
管理常用的对象
• 任务:
通过代码操作数据
• 原始做法:使用OracleConnection,定义好连接字符串,然后创建 OracleCommand,执行相应的命令。 • 改进做法:使用Microsoft的EnterpriseLibrary:
Process Monitor (PMON)
Database Writer (DBWn)
LogWriter (LGWR)
Archive log files
Checkpoint (CKPT)
Control files Data files Redo log files
Archiver (ARCn)
Control files
基本概念
• SQL语句------Oracle特有的写法
– MERGE MERGE INTO bonuses D USING (SELECT employee_id, salary, department_id FROM employees WHERE department_id = 80) S ON (D.employee_id = S.employee_id) WHEN MATCHED THEN UPDATE SET D.bonus = D.bonus + S.salary*.01 WHEN NOT MATCHED THEN INSERT (D.employee_id, D.bonus) VALUES (S.employee_id, S.salary*0.1);
Select employees.
Update employees.
注意区分
• SYS, SYSTEM, SYSMAN是用户 • SYSDBA , SYSOPER 是系统权限 • DBA 是角色
基本概念
• 常用对象
数据表
视图
一个模式名 存储过程
类型定义
触发器
函数
序列
任务
权限
默认表空间
基本概念
• SQL语句------Oracle特有的写法
内存结构
System Global Area (SGA)
进程 Background processes
存储结构
数据库文件
SGA Shared pool Streams pool Large pool
Java pool
Database buffer cache
Redo log buffer
System Monitor (SMON)
表空间
CREATE TABLESPACE EpointSystemSupport DATAFILE '/oracle/oradata/orcl/EpointSystemSupport.dbf' SIZE 50M AUTOEXTEND ON NEXT 5M MAXSIZE UNLIMITED LOGGING PERMANENT EXTENT MANAGEMENT LOCAL AUTOALLOCATE BLOCKSIZE 8K SEGMENT SPACE MANAGEMENT MANUAL FLASHBACK ON;
– ROWNUM 用于控制查询结果的行数(类似SQLServer中 的TOP) 比如:Select * from 表名/视图名 where rownum<=10 – 字符串连接 使用 || 比如: ‘aaa’||’bbbb’ – 空表:DUAL Oracle中的查询语句必须包含FROM 比如:Select getdate() from dual – 取得系统时间sysdate