๐Ÿ’ป Programming/Oracle 11g (64)

ํŠน์ • ํ…Œ์ด๋ธ”์˜ ์ œ์•ฝ์‚ฌํ•ญ( not null, fk ๊ณผ ๊ฐ™์€ )์˜ ์ด๋ฆ„, ํ…Œ์ด๋ธ”๋ช…, ์†Œ์œ ์ž์˜ ๋ชฉ๋ก์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

Below query command queries constraints'(not null, fk) name , owner, table name, column name, and position of the column.


SQL > select * from user_cons_columns where table_name='table_name';

ํ…Œ์ด๋ธ” ๋ฐฑ์—…์„ ํ• ๋•Œ์— export / import ๋ฅผ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๋‹จ์ˆœํ•˜๊ฒŒ rename ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ create ~ as select ~ ๋ฅผ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์˜ค๋Š˜์€ ํ…Œ์ด๋ธ” ๋ช…์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์ƒˆ๋กœ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•จ์œผ๋กœ์จ ๋ฐฑ์—…์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ ์ด ๋ฐฉ๋ฒ•์€ blob๋ฐ์ดํƒ€๊ฐ€ ๋งŽ์€ ๊ฒฝ์šฐ์— ์ƒ๋‹นํžˆ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.


Step by Step

1) Query table name and index names linked to the table you want to backup

2) Rename table name

3) Rename indexes' name ( if specified explicitly when it is created )

4) Rename constraints' name ( if specified explicitly when it is created )

4) Use the same DDL used to create the renamed table


โ€ป This way is very simple and fast then using "create table ~ as select * from ~" statement when the table has a huge blob data.



1) ๋ณ€๊ฒฝํ•˜๊ณ ์žํ•˜๋Š” ํ…Œ์ด๋ธ”์— ์–ด๋–ค ์ธ๋ฑ์Šค๋“ค์ด ์žˆ๋Š”์ง€ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

SQL > select index_name, table_name from user_indexes;


2) ํ…Œ์ด๋ธ”๋ช…์„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.

SQL > rename old_table to new_table;


3) ์ธ๋ฑ์Šค๋ช…์„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ( ์ธ๋ฑ์Šค๋ช…์ด ์ž๋™์ƒ์„ฑ๋œ ๊ฒƒ์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋งŒ )

SQL > alter index pk_old_table rename to pk_new_table;


4) ์ œ์•ฝ์‚ฌํ•ญ๋ช…์„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ( ์ œ์•ฝ์‚ฌํ•ญ๋ช…์ด ์ž๋™์ƒ์„ฑ๋œ ๊ฒƒ์ด ์•„๋‹Œ ๊ฒฝ์šฐ์—๋งŒ )

SQL > select * from user_cons_columns where table_name='{table_name};

SQL > alter table {table name} rename constraint {constraint name} to {new constraint name}


5) ๊ธฐ์กด ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ–ˆ๋˜ DDL์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ด๋ธ”์„ ์ƒˆ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

SQL > create table .........



์˜ค๋Š˜์€ ํ…Œ์ด๋ธ” ๋ช… ๋ณ€๊ฒฝํ•˜๋Š” ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


ํ…Œ์ด๋ธ” ๋ช…์„ ๋ณ€๊ฒฝํ•  ๋•Œ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ rename ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

Use rename command to rename a table to a new one.


SQL > rename {old table name} to {new table name}

Table renamed.


๋ณ€๊ฒฝ์ด ์™„๋ฃŒ๋˜์—ˆ๋Š”์ง€ ์•„๋ž˜์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•ด์„œ ํ™•์ธํ•ด๋ณด์„ธ์š”.

Check if renaming was successful using below query. Below statement queries the table names owned by you.


SQL > select table_name from user_tables;


You would see renamed table name if it was successful.





์ผ๋ฐ˜์œ ์ €๋กœ ๋‚ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ธ๋ฑ์Šค ๋ชฉ๋ก์„ ์กฐํšŒํ•˜๋ ค๋ฉด ์•„๋ž˜ ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

Querying index list owned by me.


SQL > select * from user_indexes;

Better to use below query to know which index is from which table because there are so many information on the user_indexes table.

์œ„ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์ƒ๋‹นํžˆ ๋งŽ์€ ์–‘์˜ ์ •๋ณด๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์–ด๋Š ํ…Œ์ด๋ธ”์— ์–ด๋Š ์ธ๋ฑ์Šค๊ฐ€ ์žˆ๋Š”์ง€๊ฐ€ ๊ถ๊ธˆํ•˜๋‹ค๋ฉด ์•„๋ž˜ ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

SQL > select index_name, table_name from user_indexes;