mysql没法修改数据_MySQL学习笔记之数据的增、删、改实现方法
本文實(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ù)的語法格式:
update 表明 set 列名=表達(dá)式 … where 條件
說明: 如果where 后面沒有條件,則相當(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字段在原來的基礎(chǔ)上加100
update employee set sal=sal+100 where name='wu'
三、刪除數(shù)據(jù)
刪除數(shù)據(jù)語法:
delete from 表明 where 條件
刪除數(shù)據(jù)原則:
1、 如果不使用where 子句,將刪除表中所有數(shù)據(jù)
2、 delete語句不能刪除某一列的值(可使用update)
3、 delete僅僅刪除記錄,不刪除表本身,如要?jiǎng)h除表,使用drop table語句
4、 同insert和update一樣,從一個(gè)表中刪除一條記錄將引起其他表的參照完整性問題
5、 刪除表中的數(shù)據(jù)也可以使用truncate table語句
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)
希望本文所述對(duì)大家MySQL數(shù)據(jù)庫計(jì)有所幫助。
總結(jié)
以上是生活随笔為你收集整理的mysql没法修改数据_MySQL学习笔记之数据的增、删、改实现方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: db2 两个结构相同的表_从两个工作表提
- 下一篇: mysql header files_编