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数据库计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值