本文實例講述了MySQL學習筆記之數據的增、刪、改實現方法。分享給大家供大家參考,具體如下:
一、增加數據
插入代碼格式:
insert into 表明 [列名…] values (值…)
create table test21(name varchar(32));insert into test21 (name) values ('huangbiao');
插入原則:
1、插入的數據應與字段的數據類型相同
2、數據的大小應該在列的規定范圍內
3、在values中列出的數據位置必須與被加入的列的排列位置對應
例子:
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);
二、更新數據
更新數據的語法格式:
update 表明 set 列名=表達式 … where 條件
說明: 如果where 后面沒有條件,則相當于對整個表進行操作。
例子數據:
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字段設置為3000
update employee set sal=3000 where name='zs'
將名字為wu的用戶sal字段在原來的基礎上加100
update employee set sal=sal+100 where name='wu'
三、刪除數據
刪除數據語法:
delete from 表明 where 條件
刪除數據原則:
1、 如果不使用where 子句,將刪除表中所有數據
2、 delete語句不能刪除某一列的值(可使用update)
3、 delete僅僅刪除記錄,不刪除表本身,如要刪除表,使用drop table語句
4、 同insert和update一樣,從一個表中刪除一條記錄將引起其他表的參照完整性問題
5、 刪除表中的數據也可以使用truncate table語句
mysql 事務
1、 mysql控制臺是默認自動提交事務(dml)
2、 如果我們要在控制臺中使用事務,請看下面:
mysql 刪除數據是自動提交的
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)
希望本文所述對大家MySQL數據庫計有所幫助。
新聞熱點
疑難解答