程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> MySQL學習筆記之數據的增、刪、改實現方法

MySQL學習筆記之數據的增、刪、改實現方法

編輯:關於MYSQL數據庫

本文實例講述了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相關內容感興趣的讀者可查看本站專題:《MySQL索引操作技巧匯總》、《MySQL日志操作技巧大全》、《MySQL事務操作技巧匯總》、《MySQL存儲過程技巧大全》、《MySQL數據庫鎖相關技巧匯總》及《MySQL常用函數大匯總》

希望本文所述對大家MySQL數據庫計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved