mysql基础 insert update delete

一、insert使用 (多行、单行、部分)

1、使用如下这种方式插入数据有如果表结构变化就会不安全(s_id设置了主键自增,所以可以设置null)

//必须设置所有列的值。有主键(s_id)自增可以设置为null也能继续自增
方式一:INSERT INTO `test_cnb`.`student`   -- 
		VALUES (Null, '赵雷', '1990-01-01', '男');
//建议使用方式二,s_id主键省略了
方式二:INSERT INTO `test_cnb`.`student` 
		(`s_name`, `s_birth`, `s_sex`) 
		VALUES ('赵雷', '1990-01-01', '男');

2、插入语句如果数量、类型、长度、是否可为Null(或有默认值)出现问题都会插入失败
3、对于增删改都会造成更新索引,从而造成在此时的select语句非常慢(什么时候更新索引)

如果有索引的表,建议使用:(这样就可以提升整体性能)
insert LOW_PRIORITY(低优先级) into 表 。。。

4、插入多条语句建议如下(提升数据库性能)

INSERT INTO `test_cnb`.`student` 
		(`s_name`, `s_birth`, `s_sex`) 
		VALUES ('赵雷', '1990-01-01', '男'),  -- 英文逗号
				('赵雷2', '1990-11-11', '男');

二、uodate 使用

UPDATE `test_cnb`.`student` SET `s_name`='赵雷', `s_birth`='1990-01-01', `s_sex`='男' WHERE (`s_id`='01');

1、update语句更新多行,如果出现错误将都回滚
2、update ignore 表 。。。 (即使发生错误也更新,不建议使用)

三、delete 使用

delete from `test_cnb`.`student`。。。

1、删除只能删除表数据,而不能删除表
2、删除表中所有的行,建议使用(TRUNCATE TABLE );速度更快,删除原来的表在建一张,而不是逐行删除数据

TRUNCATE TABLE student;

truncate table和delete table 的区别

猜你喜欢

转载自blog.csdn.net/jue6628/article/details/103466388