Heim  >  Artikel  >  Datenbank  >  MySQL学习笔记之数据的增、删、改实现方法_MySQL

MySQL学习笔记之数据的增、删、改实现方法_MySQL

WBOY
WBOYOriginal
2016-10-11 14:02:531331Durchsuche

本文实例讲述了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数据库计有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn