sqlite 修改表的列

2021-10-24 04:45:35 字數 3528 閱讀 5035

3. 重建表修改列

4. 總結

軟體都是在不斷的公升級和變化的,在軟體的世界列唯一不變的就是變化;同樣對於資料庫來講,也存在乙個資料庫公升級或者降級的問題,本文來討論一下sqlite資料庫表列修改的方法。

假如在我們的專案資料庫中存在乙個表叫做company,記錄公司所有的員工資訊,建立表的**如下:

create table if not exists company

( id primary key not null

, name text not null

, age integer not null

, address text not null

, phone integer not null

);

建立完成表之後,我們檢視表的結構資訊,如下:

sqlite>

.schema company

create table company

( id primary key not null

, name text not null

, age integer not null

, address text not null

, phone integer not null

);

接著我們向表中存放幾個員工資訊:

insert into company

(id, name, age, address, phone)

values(0

,'jack',20

,'beijing'

,'123456789');

insert into company

(id, name, age, address, phone)

values(1

,'tom',21

,'shanghai'

,'12332145678'

);

插入之後的資料如下:

sqlite> select * from company;

id|name|age|address|phone

0|jack|

20|beijing|

123456789

1|tom|

21|shanghai|

12332145678

突然,我們在員工入職的時候發現了乙個問題,乙個員工的手機號是這種"0755-67899876", 顯然這種情況無法使用integer來儲存了,我們需要對錶進行公升級,那麼應該怎麼辦呢?

最先想到的命令就是alter命令修改邊,我們看下sqlite的這個命令有什麼功能:

sqlite 的 alter table 命令不通過執行乙個完整的轉儲和資料的過載來修改已有的表。您可以使用 alter table 語句重新命名表,使用 alter table 語句還可以在已有的表中新增額外的列。

在 sqlite 中,除了重新命名表和在已有的表中新增列,alter table 命令不支援其他操作。

也就是sqlite中支援如下的操作:

create table test

( id integer primary key not null

, age integer not null

);

重新命名表的語法如下:

alter table database_name.table_name rename to new_table_name;
重新命名的過程如下:

sqlite>

.tables

company test

sqlite> alter table test rename to testbak;

sqlite>

.tables

company testbak

語法如下:

alter table database_name.table_name add column column_def...

;

執行過程如下:

sqlite> alter table testbak add column name text not null

;sqlite>

.schema testbak

create table if not exists "testbak"

( id integer primary key not null

, age integer not null

, name text not null

);

顯然,從上面的分析我們可以看出,sqlite無法直接懟已經存在的列進行修改;如果要修改已經存在的列,那麼可以使用如下的方法:

重新命名表。

重新建立表。

插入舊表中的資料。

刪除舊的表。

下面使用例子來說明整個過程。

執行過程如下:

sqlite> alter table company rename to company_tmp;

sqlite>

.tables

company_tmp testbak

使用create table company重新建立表:

create table company

( id primary key not null

, name text not null

, age integer not null

, address text not null

, phone text not null

);

insert into company

(id, name, age, address, phone)

select id, name, age, address, phone

from company_tmp;

插入資料之後的表內容如下:

sqlite> select * from company;

id|name|age|address|phone

0|jack|

20|beijing|

123456789

1|tom|

21|shanghai|

12332145678

sqlite> drop table company_tmp;

sqlite>

.tables

company testbak

sqlite不支援修改已經存在的列的屬性,如果需要修改可以使用重新建立表的方式來完成。

oracle修改表增加列刪除列修改列

tag oracle修改表 sql增加列 sql刪除列 sql修改列 1.增加列 alter table table name add column datatype default expr column datatype.例如 sql alter table emp01 add eno numb...

oracle修改表增加列刪除列修改列

oracle修改表 sql增加列 sql刪除列 sql修改列 1.增加列 alter table table name add column datatype default expr column datatype.例如 sql alter table emp01 add eno number 4...

修改表中的列

更改列的名稱應當使用系統的儲存過程 sp rename,而不是 alter table 語句。sp rename 用於修改當前資料庫中使用者所建立物件的名稱,包括表 索引 列 別名資料型別或 microsoft.netframewoek 公共語言執行 cir 時使用者定義資料型別。sp rename...