MySQL學(xué)習(xí)筆記之?dāng)?shù)據(jù)的增、刪、改實(shí)現(xiàn)方法
本文實(shí)例講述了MySQL學(xué)習(xí)筆記之?dāng)?shù)據(jù)的增、刪、改實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
一、增加數(shù)據(jù)
插入代碼格式:
insert into 表明 [列名…] values (值…)
create table test21(name varchar(32)); insert into test21 (name) values ('huangbiao');
插入原則:
1、插入的數(shù)據(jù)應(yīng)與字段的數(shù)據(jù)類型相同
2、數(shù)據(jù)的大小應(yīng)該在列的規(guī)定范圍內(nèi)
3、在values中列出的數(shù)據(jù)位置必須與被加入的列的排列位置對(duì)應(yīng)
例子:
create table test22(id int,name varchar(32)); mysql> insert into test22 (id,name) values (3,'huangbiao'); mysql> insert into test22 (name,id) values ('huangbiao2',5); mysql> insert into test22 (name,id) values ('',51); mysql> insert into test22 (name,id) values (NULL,555); mysql> insert into test22 (id) values (15);
二、更新數(shù)據(jù)
更新數(shù)據(jù)的語(yǔ)法格式:
update 表明 set 列名=表達(dá)式 … where 條件
說(shuō)明: 如果where 后面沒(méi)有條件,則相當(dāng)于對(duì)整個(gè)表進(jìn)行操作。
例子數(shù)據(jù):
create table employee( id int, name varchar(20), sex bit, birthday date, salary float, entry_date date, resume text ); insert into employee values(1,'aaa',0,'1977-11-11',56.8,now(),'hello word'); insert into employee values(2,'bbb',0,'1977-11-11',57.8,now(),'hello word'); insert into employee values(3,'ccc',0,'1977-11-11',56.3,now(),'hello word');
將employee表的sal字段全部改為2000
update employee set sal=2000;
將名字為zs的用戶的sal字段設(shè)置為3000
update employee set sal=3000 where name='zs'
將名字為wu的用戶sal字段在原來(lái)的基礎(chǔ)上加100
update employee set sal=sal+100 where name='wu'
三、刪除數(shù)據(jù)
刪除數(shù)據(jù)語(yǔ)法:
delete from 表明 where 條件
刪除數(shù)據(jù)原則:
1、 如果不使用where 子句,將刪除表中所有數(shù)據(jù)
2、 delete語(yǔ)句不能刪除某一列的值(可使用update)
3、 delete僅僅刪除記錄,不刪除表本身,如要?jiǎng)h除表,使用drop table語(yǔ)句
4、 同insert和update一樣,從一個(gè)表中刪除一條記錄將引起其他表的參照完整性問(wèn)題
5、 刪除表中的數(shù)據(jù)也可以使用truncate table語(yǔ)句
mysql 事務(wù)
1、 mysql控制臺(tái)是默認(rèn)自動(dòng)提交事務(wù)(dml)
2、 如果我們要在控制臺(tái)中使用事務(wù),請(qǐng)看下面:
mysql 刪除數(shù)據(jù)是自動(dòng)提交的
mysql> set autocommit=false; Query OK, 0 rows affected (0.00 sec) mysql> savepoint aaa; Query OK, 0 rows affected (0.00 sec) mysql> delete from employee; Query OK, 3 rows affected (0.05 sec) mysql> select * from employee; Empty set (0.00 sec) mysql> rollback to aaa; Query OK, 0 rows affected (0.06 sec) mysql> select * from employee; +------+------+------+------------+--------+------------+------------+ | id | name | sex | birthday | salary | entry_date | resume | +------+------+------+------------+--------+------------+------------+ | 1 | aaa | | 1977-11-11 | 56.8 | 2014-11-10 | hello word | | 2 | bbb | | 1977-11-11 | 57.8 | 2014-11-10 | hello word | | 3 | ccc | | 1977-11-11 | 56.3 | 2014-11-10 | hello word | +------+------+------+------------+--------+------------+------------+ 3 rows in set (0.00 sec)
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL索引操作技巧匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲(chǔ)過(guò)程技巧大全》、《MySQL數(shù)據(jù)庫(kù)鎖相關(guān)技巧匯總》及《MySQL常用函數(shù)大匯總》
希望本文所述對(duì)大家MySQL數(shù)據(jù)庫(kù)計(jì)有所幫助。
- MySql學(xué)習(xí)筆記之事務(wù)隔離級(jí)別詳解
- mysql學(xué)習(xí)筆記之幫助文檔
- mysql學(xué)習(xí)筆記之?dāng)?shù)據(jù)引擎
- mysql學(xué)習(xí)筆記之基礎(chǔ)知識(shí)
- MySQL學(xué)習(xí)筆記之創(chuàng)建、刪除、修改表的方法
- MySQL學(xué)習(xí)筆記小結(jié)
- 一千行的MySQL學(xué)習(xí)筆記匯總
- MySQL學(xué)習(xí)筆記5:修改表(alter table)
- MySQL學(xué)習(xí)筆記4:完整性約束限制字段
- MySQL學(xué)習(xí)筆記1:安裝和登錄(多種方法)
- 超全MySQL學(xué)習(xí)筆記
相關(guān)文章
MySQL的存儲(chǔ)函數(shù)與存儲(chǔ)過(guò)程相關(guān)概念與具體實(shí)例詳解
MySQL存儲(chǔ)函數(shù)(自定義函數(shù)),函數(shù)一般用于計(jì)算和返回一個(gè)值,可以將經(jīng)常需要使用的計(jì)算或功能寫(xiě)成一個(gè)函數(shù),存儲(chǔ)函數(shù)和存儲(chǔ)過(guò)程一樣,都是在數(shù)據(jù)庫(kù)中定義一些SQL語(yǔ)句的集合2023-03-03linux實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)每天自動(dòng)備份定時(shí)備份
備份是容災(zāi)的基礎(chǔ),是指為防止系統(tǒng)出現(xiàn)操作失誤或系統(tǒng)故障導(dǎo)致數(shù)據(jù)丟失,而將全部或部分?jǐn)?shù)據(jù)集合從應(yīng)用主機(jī)的硬盤或陣列復(fù)制到其它的存儲(chǔ)介質(zhì)的過(guò)程。這篇文章主要介紹了linux實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)每天自動(dòng)備份定時(shí)備份,需要的朋友可以參考下2017-09-09MySQL數(shù)據(jù)庫(kù)查詢性能優(yōu)化的4個(gè)技巧干貨
這篇文章主要為大家介紹了MySQL數(shù)據(jù)庫(kù)查詢性能優(yōu)化的4個(gè)技巧干貨詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08mysql?count()函數(shù)不計(jì)算null和空值問(wèn)題
這篇文章主要介紹了mysql?count()函數(shù)不計(jì)算null和空值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08