MySQL 增删改语句

# DML语言
/*
数据操作语言:
	插入:insert
	修改:update
	删除: delete
*/

一、插入语句 insert
/*
语法:
方式一:
	insert into 表名(列名,。。) values(值1,值2,。。。。)
方式二:
	insert into 表名 set 列名=值1,列名=值2,。。。。
*/
#1. 插入值的类型要与列的类型一致或者兼容
use girls;
insert into beauty(id,name,sex,borndate,phone,photo,boyfriend_id)
	values(13,"唐艺昕","女","1990-01-01","188882222",null,2);
select * from beauty;


#2.不可以为null的列必须插入值,可以为null的列如何插入值?
#方式1:
insert into beauty(id,name,sex,borndate,phone,photo,boyfriend_id)
	values(13,"唐艺昕","女","1990-01-01","188882222",null,2);

#方式2:
insert into beauty(id,name,sex,borndate,phone,boyfriend_id)
	values(14,"郑爽","女","1990-01-01","188882222",2);
select * from beauty;

insert into beauty(id,name,sex,borndate,phone)
	values(15,"娜扎","女","1991-01-01","188882222");

#3.列的顺序是否可以交换
insert into beauty(name,sex,borndate,id,phone,boyfriend_id)
	values("蒋欣","女","1988-01-01",16,"188882222",2);
select * from beauty;

#4.列数和值必须一致

#5.可以省略列名,默认所有列,而且列的顺序和表中的顺序一致
insert into beauty
	values(17,"张飞","男",null,"188882222",null,null);


#方式二
insert into beauty set id=18,name="刘涛",sex="女",borndate="1980-05-23",phone="181012586",boyfriend_id=6;



方式一 可以插入多行
insert into beauty(id,name,sex,borndate,phone,photo,boyfriend_id) values
	(19,"唐艺昕1","女","1990-01-01","188882222",null,2),
	(20,"唐艺昕2","女","1990-01-01","188882222",null,2),
	(21,"唐艺昕3","女","1990-01-01","188882222",null,2);


方式一支持子查询,方式二不支持
insert into beauty(id,name,sex) select 22,"宋茜","110";



二、修改:update
/*
1.修改单表的记录
语法:
	update 表名 set 列=值,列=值,。。。。where 筛选条件

2.修改多表的记录
语法:
sql92语法:
	update 表1 别名,表2 别名 set 列=值,列=值,。。。 where 连接条件 and 筛选条件;

sql99语法:
	update 表1 别名 inner|left|right join 表2 别名 on 连接条件 set 列=值,列=值,。。。 【where筛选条件】  
	
*/
# 1.修改单表的记录
#案例:修改beauty中姓唐的女神的电话为13535266
update beauty set phone="13535266" where name like "唐%";
select * from beauty;


三、删除: delete
/*
方式一:delete
	语法:
	单表的删除
		delete from 表名 where 筛选条件;
	多表的删除
语法:
sql92语法:
	delete 别名1,别名2 from 表1 别名1,表2,别名2  where 连接条件 and 筛选条件;

sql99语法:
	delete 别名1,别名2 from 表1 别名 inner|left|right join 表2 别名 on 连接条件【where筛选条件】  
	
方式二:truncate清空数据
语法: truncate table 表名;
*/

#方式一:delete
# 单表的删除
案例:删除手机尾号以6结尾的女神信息
delete from beauty where phone like "%6";

  

猜你喜欢

转载自www.cnblogs.com/ivyharding/p/11546220.html