这篇文章主要介绍了 MySQL 学习笔记之数据的增、删、改实现方法, 简单介绍了 mysql 实现数据的增、删、改相关语法与使用技巧, 需要的朋友可以参考下
MySQL 是一个开放源码的小型关联式数据库管理系统,开发者为瑞典 MySQL AB 公司。MySQL 被广泛地应用在 Internet 上的中小型网站中。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了 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 数据库计有所帮助。
来源: http://www.phperz.com/article/17/0331/295187.html