oracle刪除表字段和oracle表增加字段
添加字段的語(yǔ)法:alter table tablename add (column datatype [default value][null/not null],….);
修改字段的語(yǔ)法:alter table tablename modify (column datatype [default value][null/not null],….);
刪除字段的語(yǔ)法:alter table tablename drop (column);
添加、修改、刪除多列的話(huà),用逗號(hào)隔開(kāi)。
使用alter table 來(lái)增加、刪除和修改一個(gè)列的例子。
創(chuàng)建表結(jié)構(gòu):
create table test1
(id varchar2(20) not null);
增加一個(gè)字段:
alter table test1
add (name varchar2(30) default ‘無(wú)名氏' not null);
使用一個(gè)SQL語(yǔ)句同時(shí)添加三個(gè)字段:
alter table test1
add (name varchar2(30) default ‘無(wú)名氏' not null,
age integer default 22 not null,
has_money number(9,2)
);
修改一個(gè)字段
alter table test1
modify (name varchar2(16) default ‘unknown');
另:比較正規(guī)的寫(xiě)法是:
-- Add/modify columns
alter table TABLE_NAME rename column FIELD_NAME to NEW_FIELD_NAME;
刪除一個(gè)字段
alter table test1
drop column name;
需要注意的是如果某一列中已經(jīng)存在值,如果你要修改的為比這些值還要小的列寬這樣將會(huì)出現(xiàn)一個(gè)錯(cuò)誤。
例如前面如果我們插入一個(gè)值
insert into test1
values ('1′,'我們很愛(ài)你');
然后曾修改列: alter table test1
modify (name varchar2(8));
將會(huì)得到以下錯(cuò)誤:
ERROR 位于第 2 行:
ORA-01441: 無(wú)法減小列長(zhǎng)度, 因?yàn)橐恍┲颠^(guò)大
高級(jí)用法:
重命名表
ALTER TABLE table_name RENAME TO new_table_name;
修改列的名稱(chēng)
語(yǔ)法:
ALTER TABLE table_name RENAME COLUMN supplier_name to sname;
范例:
alter table s_dept rename column age to age1;
附:創(chuàng)建帶主鍵的表>>
create table student (
studentid int primary key not null,
studentname varchar(8),
age int);
1、創(chuàng)建表的同時(shí)創(chuàng)建主鍵約束
(1)無(wú)命名
create table student (
studentid int primary key not null,
studentname varchar(8),
age int);
(2)有命名
create table students (
studentid int ,
studentname varchar(8),
age int,
constraint yy primary key(studentid));
2、刪除表中已有的主鍵約束
(1)無(wú)命名
可用 SELECT * from user_cons_columns;
查找表中主鍵名稱(chēng)得student表中的主鍵名為SYS_C002715
alter table student drop constraint SYS_C002715;
(2)有命名
alter table students drop constraint yy;
3、向表中添加主鍵約束
alter table student add constraint pk_student primary key(studentid);
相關(guān)文章
Oracle中行列轉(zhuǎn)換的實(shí)現(xiàn)方法匯總
行列轉(zhuǎn)換是指將行數(shù)據(jù)轉(zhuǎn)換為列數(shù)據(jù),或?qū)⒘袛?shù)據(jù)轉(zhuǎn)換為行數(shù)據(jù)的過(guò)程,本文主要介紹了Oracle中行列轉(zhuǎn)換的實(shí)現(xiàn)方法匯總,用PIVOT和UNPIVOT函數(shù)來(lái)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
oracle常見(jiàn)故障類(lèi)別及規(guī)劃解析
這篇文章主要介紹了oracle常見(jiàn)故障及規(guī)劃解析,小編覺(jué)得還是不錯(cuò)的,這里分享給大家,供需要的朋友參考。2017-10-10
Oracle在DML語(yǔ)句中使用returing?into子句
這篇文章介紹了Oracle在DML語(yǔ)句中使用returing?into子句的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
oracle插入字符串?dāng)?shù)據(jù)時(shí)字符串中有''單引號(hào)問(wèn)題
這篇文章主要介紹了oracle插入字符串?dāng)?shù)據(jù)時(shí)字符串中有'單引號(hào)問(wèn)題的相關(guān)資料,需要的朋友可以參考下2017-04-04
Oracle常用函數(shù)Trunc及Trunc函數(shù)用法講解
這篇文章主要介紹了Oracle常用函數(shù)Trunc及Trunc函數(shù)用法講解,需要的朋友可以參考下2017-11-11
Linux 創(chuàng)建oracle數(shù)據(jù)庫(kù)的詳細(xì)過(guò)程
這篇文章主要介紹了Linux 創(chuàng)建oracle數(shù)據(jù)庫(kù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
oracle冷備份恢復(fù)和oracle異機(jī)恢復(fù)使用方法
這篇文章主要介紹了oracle冷備份恢復(fù)和oracle異機(jī)恢復(fù)方法,冷備份發(fā)生在數(shù)據(jù)庫(kù)已經(jīng)正常關(guān)閉的情況下,下面是使用方法,需要的朋友可以參考下2014-03-03

