Home Database Mysql Tutorial 通过在线重定义来增加大表列默认值

通过在线重定义来增加大表列默认值

Jun 07, 2016 pm 04:47 PM

其中提到11G之前,可以通过在线重定义表的方式,来进行列的增加(且有默认值)。

之前的文章讨论过11G中关于大表增加列的新特性  ,

其中提到11G之前,可以通过在线重定义表的方式,来进行列的增加(且有默认值)。

在线重定义权限需求:

grant create any table to 用户;
grant alter any table to 用户;
grant drop any table to 用户;
grant lock any table to 用户;
grant select any table to 用户;
grant create any trigger to 用户;
grant create any index to 用户;

1.SQL获取原始表的DDL

可通过如下设置,,将storage 子句去除,也可以不去执行

begin
    Dbms_Metadata.Set_Transform_Param(Dbms_Metadata.Session_Transform,
                                    'SQLTERMINATOR',
                                    True);
  Dbms_Metadata.Set_Transform_Param(Dbms_Metadata.Session_Transform,
                                    'STORAGE',
                                    False);
end;

获取原始表的DDL语句(这里例子为T)

set pagesize 5000
set long 50000
Select Dbms_Metadata.Get_Ddl(Object_Type => 'TABLE', Name => 'T')  from dual
union all
select Dbms_Metadata.Get_Dependent_Ddl(Object_Type => 'CONSTRAINT',Base_Object_Name => 'T')  from dual
union all
select Dbms_Metadata.Get_Dependent_Ddl(Object_Type => 'INDEX',Base_Object_Name => 'T')  from dual
union all
select Dbms_Metadata.Get_Dependent_Ddl('OBJECT_GRANT', 'T', 'SCOTT')  from dual;

2.将获取的语句中的 表名T,替换为T2,之后建立。
 
3.给t2表 增加字段(带有默认值)

alter table t2 add MrDai varchar2(10) default 'MrDai';

4.检查一下能否T表能否进行重定义,如果执行成功,则表示可以,否则将会报错显示为什么不可以

exec Dbms_Redefinition.Can_Redef_Table(USER, 'T');

5.开始重定义

注意:如原始表有未提交的事物,该过程会一直在等待,等待事件为enq: TX - row lock contention

exec  dbms_redefinition.start_redef_table(uname => USER,orig_table => 'T',int_table => 'T2',options_flag => DBMS_REDEFINITION.cons_use_pk);

6.完成重定义

exec dbms_redefinition.finish_redef_table(uname=>USER,orig_table=>'T',int_table=>'T2');

查看重定义以后的t

SQL> desc t;
 Name        Null?    Type
 ----------------------------------------- -------- ----------------------------
 OWNER          VARCHAR2(30)
 OBJECT_NAME        VARCHAR2(128)
 SUBOBJECT_NAME         VARCHAR2(30)
 OBJECT_ID      NOT NULL NUMBER
 .
 .
 .
 MRDAI          VARCHAR2(10)

已经添加列完成。

 

 

linux

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you drop a table in MySQL using the DROP TABLE statement? How do you drop a table in MySQL using the DROP TABLE statement? Mar 19, 2025 pm 03:52 PM

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

How do you create indexes on JSON columns? How do you create indexes on JSON columns? Mar 21, 2025 pm 12:13 PM

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? Mar 18, 2025 pm 12:00 PM

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)

How do you represent relationships using foreign keys? How do you represent relationships using foreign keys? Mar 19, 2025 pm 03:48 PM

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

See all articles