前面几篇文章介绍了bbed的使用语法规则,这篇介绍如何更改字符类型的数据。
yangobj表是由scott.emp经过CTAS 创建的表。
SQL> select * from yangobj;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ------------------------------ ---------- ---------- ------------------ ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
14 rows selected.
比如要修改empno=7521的员工名字 WARD,首先找到次记录在数据块中的地址:dba 4,396
SQL> select dbms_rowid.ROWID_OBJECT(rowid) data_object_id#,
2 dbms_rowid.ROWID_RELATIVE_FNO(rowid) rfile#,
3 dbms_rowid.ROWID_BLOCK_NUMBER(rowid) block#,
4 dbms_rowid.ROWID_ROW_NUMBER(rowid) row#,
5 rowid
6 from yangobj where empno=7521;
DATA_OBJECT_ID# RFILE# BLOCK# ROW# ROWID
--------------- ---------- ---------- ---------- ------------------
52526 4 396 2 AAAM0uAAEAAAAGMAAC
关闭数据库,使用bbed进行数据块的修改,也可以不关闭,不过要进行
alter sysetm flush shared_pool;
alter sysetm flush buffer_cache;
清理缓存中以前查询的数据。
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
使用BBED 进行修改:
定位到 dba 4,396
BBED> set dba 4,396
DBA 0x0100018c (16777612 4,396)
OFFSET 8072
找到WARD 的offset 为 8072
BBED> find /c WARD
File: /opt/oracle/oradata/orcl/users01.dbf (4)
Block: 396 Offsets: 8072 to 8135 Dba:0x0100018c
------------------------------------------------------------------------
57415244 0853414c 45534d41 4e03c24d 630777b5 02160101 0103c20d 3302c206
02c11f2c 000803c2 4b640541 4c4c454e 0853414c 45534d41 4e03c24d 630777b5
修改WARD为yang,注意如果要修改的字符串中字符的个数。如果是varchar2 类型的,修改前与修改后要一致;如果是char类型的,修改后的数据不能超过定义时char(N)中N的数值。(文章结尾有一个例子)
BBED> modify /c yang dba 4,396 offset 8072
File: /opt/oracle/oradata/orcl/users01.dbf (4)
Block: 396 Offsets: 8072 to 8135 Dba:0x0100018c
------------------------------------------------------------------------
79616e67 0853414c 45534d41 4e03c24d 630777b5 02160101 0103c20d 3302c206
02c11f2c 000803c2 4b640541 4c4c454e 0853414c 45534d41 4e03c24d 630777b5
使用dump命令查看结果
BBED> dump /v dba 4,396 offset 8072
File: /opt/oracle/oradata/orcl/users01.dbf (4)
Block: 396 Offsets: 8072 to 8135 Dba:0x0100018c
-------------------------------------------------------
79616e67 0853414c 45534d41 4e03c24d l yang.SALESMAN..M
630777b5 02160101 0103c20d 3302c206 l c.w.........3...
02c11f2c 000803c2 4b640541 4c4c454e l ...,....Kd.ALLEN
0853414c 45534d41 4e03c24d 630777b5 l .SALESMAN..Mc.w.
查看block 4,396的校验
BBED> sum dba 4,396
Check value for File 4, Block 396:
current = 0x27cd, required = 0x24df
current 与reqired 必须一致。
BBED> sum dba 4,396 apply
Check value for File 4, Block 396:
current = 0x24df, required = 0x24df
重新启动数据库,sqlplus 中进行验证。。
SQL> startup
ORACLE instance started.
Total System Global Area 1224736768 bytes
Fixed Size 2020384 bytes
Variable Size 318770144 bytes
Database Buffers 889192448 bytes
Redo Buffers 14753792 bytes
Database mounted.
Database opened.
SQL> conn yang/yang
Connected.
SQL> col ename for a15
SQL> col job for a10
SQL> set linesize 120
SQL> set pagesize 1000
SQL> select * from yangobj;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- --------------- ---------- ---------- ------------------ ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 yang SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
14 rows selected.
SQL> select * from yangobj where empno=7521;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- --------------- ---------- ---------- ------------------ ---------- ---------- ----------
7521 yang SALESMAN 7698 22-FEB-81 1250 500 30
SQL>
附上一个例子:
修改varchar2类型数据原数据与新数据长度不一导致查询结果中其他字段值也被修改了。
SQL> ---修改后查询
SQL> select * from yangobj where empno=7521;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- ---------- ---------- ------------------ ---------- ---------- ----------
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
SQL> alter system flush buffer_cache;
SQL> select * from yangobj;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ------------------------------ --------------- ---------- ------------------ ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 Yang lALESMAN