1. 程式人生 > 其它 >插入,修改,刪除資料庫表(DDL)

插入,修改,刪除資料庫表(DDL)

-- 修改表的結構:
-- 增加一列:
alter table s_student add score double(5,2); -- 增加一列score 5指的是總位數 2指的是小數的位數

執行結果:

-- 往score裡新增資料

update s_student set score = 123.5678 where son = 1; -- 在son等於1裡新增成績

執行結果:

注:因為scoredouble(5,2)5指的是總位數 2指的是小數的位數,四捨五入最後為123.57

如果想要score這一列,新增到最前面,可以這樣:

alter table s_student add

score double(5,2)first;

執行結果:

將score這一列,新增到誰(sex)後面:

alter table s_student add score double(5,2)after sex;

執行結果:

想要刪除一列:

alter table s_student drop score;

執行結果:

想要修改(modify,change)一列:

alter table s_student modify score float(4,1); -- modify修改是列的型別定義,但是不會改變列的名字

執行結果:

注:本來是double(5,2)現在是float(4,1)

alter table s_student change score score1 double(5,1); -- change修改列名和列的型別定義

執行結果:

注:score名字被修改為score1,float(4,1)被修改為double(5,1)

刪除表:

drop table s_student;

這裡全都是對資料庫物件進行操作,屬於DDL