如何在数据字典中修改Oracle的表列名
如何在数据字典中修改Oracle的表列名 在Oracle数据库中没有提供直接修改表中列名称的功能,但在实际使用时常需要修改表的列名和列顺序这一情况下,本文给出了从数据字典中直接修改表列的名称和顺序的方法,供大家参考! AD:51CTO学院:IT精品课程在线看!
如何在数据字典中修改Oracle的表列名
在Oracle数据库中没有提供直接修改表中列名称的功能,但在实际使用时常需要修改表的列名和列顺序这一情况下,本文给出了从数据字典中直接修改表列的名称和顺序的方法,供大家参考!
AD:51CTO学院:IT精品课程在线看!
在Oracle数据库中没有提供直接修改表中列名称的功能,但在实际使用时常需要修改表的列名和列顺序,在不得已的情况下,有些Oracle的使用者用重新创建一个新的具有正确列名和顺序的数据库表,再将旧表的数据转储进来,最后删除旧表并将新表重命名为旧表的方法来完成此功能。此方法的最大问题是要求有双倍的存储空间、较大的回滚段和较长的时间,如果表中数据量较大,这项工作开销会很大。实际上我们可以从数据字典中直接修改表列的名称和顺序。下面是实现的具体步骤:
1、以internal用户名登录Oracle数据库,并创建一测试表。
SQL>CREATE TABLE SCOTT.TEST AS SELECT EMPNO, ENAME FROM SCOTT.EMP; SQL>DESC SCOTT.TEST Name Type Nullable Default Comments ------- ------------ ------- EMPNO NUMBER(4) Y ENAME VARCHAR2(10) Y Copy after login |
下面需要要把SCOTT.TEST表中EMPNO和ENAME两列调换顺序,并把ENAME列更名为EMP_NAME,EMPNO改为EMP_NO。
2、查询表中列的实际存储位置或表。
SQL>SET LONG 9999 Copy after login |
由于TEXT列是LONG类型,只有“SET”之后才能完全显示。
<p>SQL>SELECT TEXT FROM ALL_VIEWS WHERE VIEW_NAME = ‘USER_TAB_COLUMNS’;</p> Copy after login |
数据字典视图USER_TAB_COLUMNS中存储有表列的定义信息,从该语句的查询结果可以看出,列定义信息是存储在表SYS.COL$中的,即如果修改表中列的定义,应该在SYS.COL$表中修改。
3、从数据字典视图ALL_OBJECTS中查找对象SCOTT.TEST对象ID。
<p>SQL> SELECT * FROM ALL_OBJECTS WHERE OWNER =‘SCOTT’ AND OBJECT_NAME=‘TEST’;</p> Copy after login |
4、根据SCOTT.TEST对象的ID,从SYS.COL$检索出表中列的定义信息。
SQL> SELECT OBJ#,COL#,NAME FROM SYS.COL$ WHERE OBJ# =13888; OBJ# COL# NAME ---------- ---------- ------- 13888 1 EMPNO 13888 2 ENAME Copy after login |
5、使用Update语句来进行修改。
<p>UPDATE SYS.COL$ SET COL# = 2,NAME=‘EMP_NO’ WHERE OBJ# = 13888 AND NAME=‘EMPNO’; UPDATE SYS.COL$ SET COL# = 1,NAME=‘EMP_NAME’ WHERE OBJ# = 13888 AND NAME =‘ENAME’; COMMIT;</p> Copy after login |
6、重启数据库服务。
由于数据字典是在数据库启动时加载到SQL中的,所以修改了它之后,如果使用“SELECT * FROM SCOTT.TEST; ”,会发现好像并没有修改。因此,修改完成之后,还需要重启数据库服务。
SQL>SHUTDOWN SQL>STARTUP Copy after login |
这时,再查看,就会发现修改已经成功。
<p>SQL> SELECT * FROM SCOTT.TEST; EMP_NAME EMP_NO ---------- ------ SMITH 7369 ALLEN 7499 WARD 7521 …… </p> Copy after login |
这种方法直接从数据库中进行表列定义的修改,存在一定风险,但它对于数据量特别大的表是非常有用的。充分利用数据字典功能,往往能够完成日常很难完成的工作。下面笔者写了一段简单的存储过程,可实现表中列的重命名。大家可以直接调用这个过程来完成列的重命名:
<p>SQL>exec altercolname (‘模式名称’,‘表名称’,‘原列名称’,‘新列名称’); create or replace procedure sys.altercolname (schmaname in varchar2, tabname in varchar2, oldcolname in varchar2, newcolname in varchar2) is n_schmaname varchar2(30); --模式名称 n_tablename varchar2(30); --表名称 n_oldcolname varchar2(30); --原来列名称 n_newcolname varchar2(30); --新的列名称 n_objnum number; begin n_schmaname := upper(schmaname); n_tablename := upper(tabname); n_oldcolname := upper(oldcolname); n_newcolname := upper(newcolname); SELECT OBJECT_ID INTO n_objnum FROM ALL_OBJECTS WHERE OWNER = n_schmaname AND OBJECT_NAME=n_tablename; UPDATE SYS.COL$ SET NAME=n_newcolname WHERE OBJ# = n_objnum AND NAME=n_oldcolname; COMMIT; end altercolname;</p> Copy after login |
- Oracle数据库密码文件的使用和维护
- 利用Oracle管理服务器将数据导入导出
- 全面认识Oracle数据库字符集
参考:
《Oracle中修改表列名,用SQL语句的方式》所用的修改表列名的方式是一个集成操作,执行该文所说的方式的具体过程,其实质是在做本文中里提到的那些内部修改操作。即《Oracle中修改表列名,用SQL语句的方式》的操作是表象,而本文中所做的操作才是本质。
Oracle中修改表列名,用SQL语句的方式

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

Oracle views can be exported through the EXP utility: Log in to the Oracle database. Start the EXP utility, specifying the view name and export directory. Enter export parameters, including target mode, file format, and tablespace. Start exporting. Verify the export using the impdp utility.

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.
