oracle使用sql語句增加字段示例(sql刪除字段語句)
添加字段的語法:alter table tablename add (column datatype [default value][null/not null],….);
修改字段的語法:alter table tablename modify (column datatype [default value][null/not null],….);
刪除字段的語法:alter table tablename drop (column);
添加、修改、刪除多列的話,用逗號(hào)隔開。
使用alter table 來增加、刪除和修改一個(gè)列的例子。
創(chuàng)建表結(jié)構(gòu):
create table test1
(id varchar2(20) not null);
增加一個(gè)字段:
alter table test1
add (name varchar2(30) default ‘無名氏' not null);
使用一個(gè)SQL語句同時(shí)添加三個(gè)字段:
alter table test1
add (name varchar2(30) default ‘無名氏' not null,
age integer default 22 not null,
has_money number(9,2)
);
修改一個(gè)字段
alter table test1
modify (name varchar2(16) default ‘unknown');
另:比較正規(guī)的寫法是:
-- 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′,'我們很愛你');
然后曾修改列: alter table test1
modify (name varchar2(8));
將會(huì)得到以下錯(cuò)誤:
ERROR 位于第 2 行:
ORA-01441: 無法減小列長(zhǎng)度, 因?yàn)橐恍┲颠^大
高級(jí)用法:
重命名表
ALTER TABLE table_name RENAME TO new_table_name;
修改列的名稱
語法:
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)無命名
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)無命名
可用 SELECT * from user_cons_columns;
查找表中主鍵名稱得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數(shù)據(jù)庫表實(shí)現(xiàn)自增主鍵的方法實(shí)例
Oracle數(shù)據(jù)庫中沒有自增字段,我們可以通過創(chuàng)建序列和觸發(fā)器的方式,間接地實(shí)現(xiàn)自增的效果,下面這篇文章主要給大家介紹了關(guān)于oracle數(shù)據(jù)庫表實(shí)現(xiàn)自增主鍵的相關(guān)資料,需要的朋友可以參考下2022-06-06
Oracle 數(shù)據(jù)庫特殊查詢總結(jié)
這篇文章主要介紹了Oracle 數(shù)據(jù)庫特殊查詢總結(jié)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
oracle join on 數(shù)據(jù)過濾問題
因?yàn)樵贔OR .. IN () LOOP 游標(biāo)中使用 所以不能采用下面的查詢語句做游標(biāo)2009-07-07
安裝oracle11g INS-30131執(zhí)行安裝程序驗(yàn)證所需的初始設(shè)置失敗的解決方法
這篇文章主要為大家詳細(xì)介紹了安裝oracle11g INS-30131執(zhí)行安裝程序驗(yàn)證所需的初始設(shè)置失敗的解決方法,感興趣的小伙伴們可以參考一下2016-07-07
Oracle數(shù)據(jù)庫中的LISTAGG函數(shù)使用示例及注意事項(xiàng)
這篇文章主要給大家介紹了關(guān)于Oracle數(shù)據(jù)庫中LISTAGG函數(shù)使用示例及注意事項(xiàng)的相關(guān)資料,listagg函數(shù)是Oracle?11.2推出的新特性,其主要功能類似于wmsys.wm_concat函數(shù),即將數(shù)據(jù)分組后把指定列的數(shù)據(jù)再通過指定符號(hào)合并,需要的朋友可以參考下2024-08-08
Oracle平臺(tái)應(yīng)用數(shù)據(jù)庫系統(tǒng)的設(shè)計(jì)與開發(fā)
Oracle平臺(tái)應(yīng)用數(shù)據(jù)庫系統(tǒng)的設(shè)計(jì)與開發(fā)...2007-03-03
Oracle官方工具SQL Developer的簡(jiǎn)單使用
這篇文章介紹了Oracle官方工具SQL Developer的簡(jiǎn)單使用,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
Oracle創(chuàng)建設(shè)置查詢權(quán)限用戶的方法
這篇文章主要給大家介紹了關(guān)于Oracle創(chuàng)建設(shè)置查詢權(quán)限用戶的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Oracle具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08

