update:更新資料庫表中的記錄
基本語法
update
表set 欄位1
=值1, 欄位2
=值2,..
.where..
.
select * from sudents where id=1;
查詢結果
例項要求:更新students
表id=1
的記錄的name
和score
這兩個字段
update students set name=
'大牛'
, score=
66where id=1;
-- 查詢並觀察結果
select
*from students where id=
1;
查詢結果
update語句的where條件可以一次更新多條記錄
-- 更新id=5,6,7的記錄
update students set name=』小牛', score=
77where id>=
5and id<=7;
-- 查詢並觀察結果
select
*from students;
查詢結果
update與劇中,更新欄位時可以使用表示式例項
要求:把所有80分以下的同學的成績加10分
-- 更新score<80的記錄
update students set score=score+
10where score<80;
-- 查詢並觀察結果
select
*from students;
查詢結果
注:如果where條件沒有匹配任何記錄,update語句也不會報錯,也不會有任何記錄被更新
特別注意:
update語句可以沒有where條件,但是會將整個表所有記錄更新
SQL基礎 修改資料 INSERT
向資料庫中插入一條新紀錄時,就必須使用insert語句 語法insert into 表 欄位1 欄位2,values 值1,值2,students表 例項 向students表插入一條新紀錄 insert into students class id,name,gender,score values...
SQL修改資料
關聯式資料庫的基本操作就是增刪改查,即crud create retrieve update delete。其中,對於查詢,我們已經詳細講述了select語句的詳細用法。而對於增 刪 改,對應的sql語句分別是 insert 插入新記錄 update 更新已有記錄 delete 刪除已有記錄。ins...
MySQL連表Update修改資料
設想兩張表,如下 table a field id field name table b field id filed my name 現在希望將表b中的my name中的內容 拷貝 到表a中對應的name欄位中,表a中的記錄與表b中的記錄通過id對應。首先我們來連表查詢一下 select a.id...