MySQL(3):CRUD语句(1)——增、删、改
2016-12-25 21:33
232 查看
增删改查(CRUD)
-- 是指在做计算处理时的增加(Create)、读取查询(Retrieve)、更新(Update)和删除(Delete)几个单词的首字母简写。
-- 主要被用在描述软件系统中数据库或者持久层的基本操作功能。
增 insert语句
插入的数据应与字段的数据类型相同(或能转换成那种类型的数据)。
数据的大小应在列的规定范围内,例如:不能将一个长度为80的字符串加入到长度为40的列中。
在values中列出的数据位置必须与被加入的列的排列位置相对应。
养成好习惯:字符和日期型数据应包含在单引号中。数字不应该加单引号!
插入空值,不指定或insert into table value(null)
create database mydb6;
use mydb6;
create table users (age int);
insert into users values (30); -- 没有引号!
create table users2 (id int, age int, name varchar(64));
insert into users2 (id, age, name) values (1,18,'qcyfred');
insert into users2 (id, name ) values (2, ''); -- 注意这里,没有空格! 显示的是NULL,其实是''。
insert into users2 (id, name ) values (3, ' '); -- 注意,这里打了空格!
insert into users2 (id, name ) values (4,null);
insert into users2 (id) values (5);
+------+------+---------+
| id | age | name |
+------+------+---------+
| 1 | 18 | qcyfred |
| 2 | NULL | NULL |
| 3 | NULL | |
| 4 | NULL | NULL |
| 5 | NULL | NULL |
+------+------+---------+
测试:
select id from users2 where name = null;
-- 居然是空集!
select id from users2 where name = ''; -- 空字符串
+------+
| id |
+------+
| 2 |
| 3 |
+------+
select id from users2 where name = ' '; -- n个空格
+------+
| id |
+------+
| 2 |
| 3 |
+------+
都等价于''……因为是varchar,变长,最后的空格会被回收。
insert into users2 (name) values ('qcy ');
mysql> select * from users2 where name = 'qcy
+------+------+---------------+
| id | age | name |
+------+------+---------------+
| NULL | NULL | qcy |
+------+------+---------------+
1 row in set (0.00 sec)
mysql> select * from users2 where name = 'qcy';
+------+------+---------------+
| id | age | name |
+------+------+---------------+
| NULL | NULL | qcy |
+------+------+---------------+
1 row in set (0.00 sec)
改 update语句
create table employee(
id int,
name varchar(20),
sex bit,
birthday date,
salary float,
entry_date date,
resume text
);
insert into employee (id, name, sex, birthday, salary, entry_date, resume) values (1,'qcy',1,'1993-05-06',100.3,'2016-12-20','个人简历');
insert into employee (id, name, sex, birthday, salary, entry_date, resume) values (2,'qcy2',0,'1993-05-06',100.3,'2016-12-25','个人简历');
insert into employee (id, name, sex, birthday, salary, entry_date, resume) values (3,'qcy3',0,'1993-05-06',100.3,'2016-12-25','个人简历');
update employee set salary = 1000;
update employee set salary = salary + 100 where id = 2;
update employee set salary = salary + 100 where name = 'qcy3';
update employee set entry_date = now() where id = 1;
删 delete语句
delete from employee where salary > 1000;
不用where,就会删除表中所有的记录!!!
DML语句基本上都一样… data modify language
DDL语句…可能不一样…… data define language
update employee set salary = salary + 100 where id = 2;
update employee set salary = salary + 100 where id = 2;
使用delete语句仅删除记录,不删除表本身。如要删除表,使用drop table语句。
同insert和update一样,从一个表中删除记录将引起其它表的参照完整性问题,在修改数据库数据时,头脑中应该始终不要忘记这个潜在的问题。
删除表中数据也可使用TRUNCATE TABLE 语句,它和delete有所不同,参看mysql文档。
truncate table 表名,可以删除表的记录,速度快,但不能回滚
保存点。
savepoint aa; -- 保存点 怎么回事?
delect from employee where id = 1;
rollback to aa; -- 无法恢复了
MySQL 数据库,DML自动提交。不是手动提交。
MySQL数据库控制台,DML默认自动提交事务!
DDL肯定自动提交……
set autocommit = false;
savepoint aaa;
delete from employee where id = 1;
rollback to aaa;
-- 是指在做计算处理时的增加(Create)、读取查询(Retrieve)、更新(Update)和删除(Delete)几个单词的首字母简写。
-- 主要被用在描述软件系统中数据库或者持久层的基本操作功能。
增 insert语句
插入的数据应与字段的数据类型相同(或能转换成那种类型的数据)。
数据的大小应在列的规定范围内,例如:不能将一个长度为80的字符串加入到长度为40的列中。
在values中列出的数据位置必须与被加入的列的排列位置相对应。
养成好习惯:字符和日期型数据应包含在单引号中。数字不应该加单引号!
插入空值,不指定或insert into table value(null)
create database mydb6;
use mydb6;
create table users (age int);
insert into users values (30); -- 没有引号!
create table users2 (id int, age int, name varchar(64));
insert into users2 (id, age, name) values (1,18,'qcyfred');
insert into users2 (id, name ) values (2, ''); -- 注意这里,没有空格! 显示的是NULL,其实是''。
insert into users2 (id, name ) values (3, ' '); -- 注意,这里打了空格!
insert into users2 (id, name ) values (4,null);
insert into users2 (id) values (5);
+------+------+---------+
| id | age | name |
+------+------+---------+
| 1 | 18 | qcyfred |
| 2 | NULL | NULL |
| 3 | NULL | |
| 4 | NULL | NULL |
| 5 | NULL | NULL |
+------+------+---------+
测试:
select id from users2 where name = null;
-- 居然是空集!
select id from users2 where name = ''; -- 空字符串
+------+
| id |
+------+
| 2 |
| 3 |
+------+
select id from users2 where name = ' '; -- n个空格
+------+
| id |
+------+
| 2 |
| 3 |
+------+
都等价于''……因为是varchar,变长,最后的空格会被回收。
insert into users2 (name) values ('qcy ');
mysql> select * from users2 where name = 'qcy
+------+------+---------------+
| id | age | name |
+------+------+---------------+
| NULL | NULL | qcy |
+------+------+---------------+
1 row in set (0.00 sec)
mysql> select * from users2 where name = 'qcy';
+------+------+---------------+
| id | age | name |
+------+------+---------------+
| NULL | NULL | qcy |
+------+------+---------------+
1 row in set (0.00 sec)
改 update语句
create table employee(
id int,
name varchar(20),
sex bit,
birthday date,
salary float,
entry_date date,
resume text
);
insert into employee (id, name, sex, birthday, salary, entry_date, resume) values (1,'qcy',1,'1993-05-06',100.3,'2016-12-20','个人简历');
insert into employee (id, name, sex, birthday, salary, entry_date, resume) values (2,'qcy2',0,'1993-05-06',100.3,'2016-12-25','个人简历');
insert into employee (id, name, sex, birthday, salary, entry_date, resume) values (3,'qcy3',0,'1993-05-06',100.3,'2016-12-25','个人简历');
update employee set salary = 1000;
update employee set salary = salary + 100 where id = 2;
update employee set salary = salary + 100 where name = 'qcy3';
update employee set entry_date = now() where id = 1;
删 delete语句
delete from employee where salary > 1000;
不用where,就会删除表中所有的记录!!!
DML语句基本上都一样… data modify language
DDL语句…可能不一样…… data define language
update employee set salary = salary + 100 where id = 2;
update employee set salary = salary + 100 where id = 2;
使用delete语句仅删除记录,不删除表本身。如要删除表,使用drop table语句。
同insert和update一样,从一个表中删除记录将引起其它表的参照完整性问题,在修改数据库数据时,头脑中应该始终不要忘记这个潜在的问题。
删除表中数据也可使用TRUNCATE TABLE 语句,它和delete有所不同,参看mysql文档。
truncate table 表名,可以删除表的记录,速度快,但不能回滚
保存点。
savepoint aa; -- 保存点 怎么回事?
delect from employee where id = 1;
rollback to aa; -- 无法恢复了
MySQL 数据库,DML自动提交。不是手动提交。
MySQL数据库控制台,DML默认自动提交事务!
DDL肯定自动提交……
set autocommit = false;
savepoint aaa;
delete from employee where id = 1;
rollback to aaa;
相关文章推荐
- MySQL_CRUD基础语句
- MySQL_CRUD基础语句
- MySQL_CRUD基础语句
- MySQL(4):CRUD语句(2)——基本查询
- MySql的基本常识和crud语句大全。。。。。
- MySQL_CRUD基础语句
- MySQL(7):CRUD语句(3)——子查询和多表查询、蠕虫复制
- MySQL基本语句和连接字符串
- MYSQL语句回顾
- 从Roller学到的mysql操作语句,记录一下方便拷贝
- 二十行语句实现从Excel到mysql的转化
- 从一个MySQL的例子来学习查询语句
- 用sql语句取出mysql 数据库中表的字段的说明信息
- html中select语句读取mysql表中内容
- mysql的一些语句总结
- 通过Mysql 的语句生成后门木马
- html中select语句读取mysql表中内容
- 通过Mysql 的语句生成后门木马的方法!
- mysql数据导入导出语句
- 对于MYSQL数据操作语句的总结