只能改大。沒(méi)有數(shù)據(jù)可能直接用 alter table table_name modify column datatype;
我們提供的服務(wù)有:成都網(wǎng)站制作、做網(wǎng)站、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、鼓樓ssl等。為1000多家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的鼓樓網(wǎng)站制作公司
如果有數(shù)據(jù),改小的話可以會(huì)丟失數(shù)據(jù)。
根據(jù)字段類型決定
alter table 表名 modify 字段名 varchar2(長(zhǎng)度); 或
alter table 表名 modify 字段名 number(長(zhǎng)度 );
比如:
表:stu(name varchar2(20)) 要將字段name的長(zhǎng)度改為10
表中有一條數(shù)據(jù):name(中國(guó)華西村刀光劍影) 長(zhǎng)度超過(guò)10,截取的時(shí)候必然要丟失數(shù)據(jù)。
當(dāng)然 如果表中的數(shù)據(jù)長(zhǎng)度都小于10,則可以用sql語(yǔ)句段來(lái)直接搞定。
begin
alter table stu add (name2 varchar2(10)); ? 增加新字段
update stu set name2=substr(trim(name),1,10); ?賦值給新字段
alter table stu drop(name); ? ? ? ? ? ? ? ? ? ? ? ? ? 刪除原字段
alter table stu rename column name2 to name; 將新字段改名end;
因?yàn)闃I(yè)務(wù)需要 修要修改某個(gè)字段數(shù)據(jù)類型有number( ) 變?yōu)閚umber( )型
要是沒(méi)有數(shù)據(jù)的話直接用以下語(yǔ)句即可
alter?? table tb_test modify permile number( );
但是有數(shù)據(jù)的話 就不能用上面方法了
alter table tb_test add permile_temp number( )
update tb_test set permile_temp=permile;
alter table drop column permile;
alter table test rename column permile_temp to permile;
這種方法會(huì)使列名發(fā)生變化 而且字段順序增加 有可能發(fā)生行遷移 對(duì)應(yīng)用程序會(huì)產(chǎn)生影響
以下方法是比較好的方法
不用使列名發(fā)生變化 也不會(huì)發(fā)生表遷移 但這個(gè)有個(gè)缺點(diǎn)是表要更新兩次
如果數(shù)據(jù)量較大的話 產(chǎn)生的undo和redo更多 前提也是要停機(jī)做
要是不停機(jī)的話 也可以采用在線重定義方式來(lái)做
以下是腳本:
alter table tb_test add permile_temp number;
Add/modify columns
alter table tb_test modify PERMILE null;
update tb_test set permile_temp=permile permile=null;
mit;
alter table tb_test modify permile number( );
update tb_test set permile=permile_temp permile_temp=null;
mit;
alter table tb_test drop column permile_temp;
alter table tb_test modify PERMILE not null;
lishixinzhi/Article/program/Oracle/201311/17913
發(fā)現(xiàn)clob類型比較特殊,和其他字段類型不同,不可以從其他字段類型直接轉(zhuǎn)換為clob(blob也一樣),可以通過(guò)long類型作為中間轉(zhuǎn)換的橋梁,即先將varchar2轉(zhuǎn)換為long,然后再將long轉(zhuǎn)換為clob,即可。
SQL alter table test modify (loc long );
Table altered
SQL alter table test modify (loc clob );
Table altered
2、假設(shè)要修改字段有數(shù)據(jù),則可以使用以下兩種方法;
方法一:
alter table batchintfloadlog rename column resultinfo to resultinfo_temp;
alter table batchintfloadlog add resultinfo clob;
update batchintfloadlog set resultinfo=trim(resultinfo_temp);
alter table batchintfloadlog drop column resultinfo_temp;
方法二:
create table batchintfloadlog_temp ?as select * from batchintfloadlog where 1=2;?
alter table batchintfloadlog_temp modify (resultinfo long);?
alter table batchintfloadlog_temp modify (resultinfo clob);?
insert into batchintfloadlog_temp select * from batchintfloadlog;
drop table batchintfloadlog;?
rename batchintfloadlog_temp to batchintfloadlog;
首先方法是使用RENAME關(guān)鍵字:
修改字段名:alter table 表名 rename column 現(xiàn)列名 to 新列名;
修改表名:alter table 表名 rename to 新表名
增加字段語(yǔ)法:alter table tablename add (column datatype [default value][null/not null],….);
說(shuō)明:alter table 表名 add (字段名 字段類型 默認(rèn)值 是否為空);
例:alter table sf_users add (HeadPIC blob);
例:alter table?sf_users add (userName varchar2(30) default?'空' not null);
修改字段的語(yǔ)法:alter table tablename modify (column datatype [default value][null/not null],….);
說(shuō)明:alter table 表名 modify (字段名 字段類型?默認(rèn)值 是否為空);
例:alter table sf_InvoiceApply modify (BILLCODE number(4));
刪除字段的語(yǔ)法:alter table tablename drop (column);
說(shuō)明:alter table 表名 drop column 字段名;
例:alter table sf_users drop column HeadPIC;
字段的重命名:
說(shuō)明:alter table 表名 rename ?column? 列名 to 新列名?? (其中:column是關(guān)鍵字)
例:alter table sf_InvoiceApply rename column PIC to NEWPIC;
表的重命名:
說(shuō)明:alter table 表名 rename to? 新表名
例:alter table?sf_InvoiceApply rename to??sf_New_InvoiceApply;
語(yǔ)句:
alter table tableName rename column oldCName to newCName; -- 修改字段名
alter table tableName modify (cloumnName 數(shù)據(jù)類型); -- 修改數(shù)據(jù)類型
例如:
1、創(chuàng)建表:
CREATE TABLE Student(
id varchar2(32) primary key,
name varchar2(8) not null,
age number
);
2、修改字段名:
alter table Student rename column name to StuName;
3、修改數(shù)據(jù)類型:
alter table Student modify (id varchar2(64));
清醒時(shí)做事,糊涂時(shí)讀書,大怒時(shí)睡覺(jué),獨(dú)處時(shí)思考;做一個(gè)幸福的人,讀書,旅行,努力工作,關(guān)心身體和心情,成為最好的自己
新聞標(biāo)題:oracle怎么修改字段 oracle怎么修改字段類型
標(biāo)題URL:http://chinadenli.net/article10/hgsego.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、云服務(wù)器、網(wǎng)站收錄、軟件開(kāi)發(fā)、網(wǎng)站導(dǎo)航、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)