DML数据操作语言

1. DML数据操作语言

1.1 插入语句

语法:

insert 
	into 
		表名 (列名1,
         列名2,
		...) 
	values (1,2,
	...);

特点:

  • 字段类型和值类型一致或兼容,而且一一对应。
  • 可以为空的字段,可以不用插入值,或用null填充。不可以为空的字段,必须插入值。
  • 字段个数和值的个数必须一致。
  • 字段可以省略,但默认所有字段,并且顺序和表中的存储顺序一致。
#1.插入的值的类型要与列的类型一致或兼容
insert
	into
		beauty(id,
		NAME,
		sex,
		borndate,
		phone,
		photo,
		boyfriend_id)
	values(13,
	'唐艺昕',
	'女',
	'1990-4-23',
	'1898888888',
	null,
	2);

#2.不可以为null的列必须插入值。可以为null的列如何插入值?
#方式一:
insert
	into
		beauty(id,
		NAME,
		sex,
		borndate,
		phone,
		photo,
		boyfriend_id)
	values(13,
	'唐艺昕',
	'女',
	'1990-4-23',
	'1898888888',
	null,
	2);

#方式二:
insert
	into
		beauty(id,
		NAME,
		sex,
		phone)
	values(15,
	'娜扎',
	'女',
	'1388888888');

#3.列的顺序是否可以调换
insert
	into
		beauty(NAME,
		sex,
		id,
		phone)
	values('蒋欣',
	'女',
	16,
	'110');

#4.列数和值的个数必须一致
insert
	into
		beauty(NAME,
		sex,
		id,
		phone)
	values('关晓彤',
	'女',
	17,
	'110');

#5.可以省略列名,默认所有列,而且列的顺序和表中列的顺序一致
#方式一:
insert
	into
		beauty
	values(18,
	'张飞',
	'男',
	null,
	'119',
	null,
	null);

#方式二:
/*
语法:
insert 
    into 
    	表名 set 
    	列名=值,
    	列名=值,
    	...
*/
insert
	into
		beauty set
		id = 19,
		NAME = '刘涛',
		phone = '999';

#两种方式比较
#1、方式一支持插入多行,方式二不支持
insert
	into
		beauty
	values(23,
	'唐艺昕1',
	'女',
	'1990-4-23',
	'1898888888',
	null,
	2) ,
	(24,
	'唐艺昕2',
	'女',
	'1990-4-23',
	'1898888888',
	null,
	2) ,
	(25,
	'唐艺昕3',
	'女',
	'1990-4-23',
	'1898888888',
	null,
	2);

#2、方式一支持子查询,方式二不支持
insert
	into
		beauty(id,
		NAME,
		phone) select
			26,
			'宋茜',
			'11809866';

insert
	into
		beauty(id,
		NAME,
		phone) select
			id,
			boyname,
			'1234567'
		from
			boys
		where
			id < 3;

1.2 修改语句

  • 修改单表的记录语法:
update 
	表名
set1 = 新值1,2 = 新值2,
    ...
where 
	筛选条件;
  • 修改多表的记录(sql92语法):
update1 别名,2 别名
set=,
	...
where 
	连接条件
and 
	筛选条件;
  • 修改多表的记录(sql99语法):
update1 别名
inner|left|right join2 别名 on 
	连接条件 set=,
	...
where 
	筛选条件;
#1.修改单表的记录
#案例1:修改beauty表中姓唐的女神的电话为13899888899
update
	beauty
set
	phone = '13899888899'
where
	NAME like '唐%';

#案例2:修改boys表中id号为2的名称为张飞,魅力值10
update
	boys
set
	boyname = '张飞',
	usercp = 10
where
	id = 2;

#2.修改多表的记录
#案例1:修改张无忌的女朋友的手机号为119
update
	boys bo
inner join beauty b on
	bo.`id` = b.`boyfriend_id` set
	b.`phone` = '119',
	bo.`userCP` = 1000
where
	bo.`boyName` = '张无忌';

#案例2:修改没有男朋友的女神的男朋友编号都为2号
update
	boys bo
right join beauty b on
	bo.`id` = b.`boyfriend_id` set
	b.`boyfriend_id` = 2
where
	bo.`id` is null;

1.3 删除语句

  • 单表的删除
delete 
from 
	表名 
where 
	筛选条件
  • 多表的删除(sql92语法):
delete1的别名,2的别名
from1 别名,2 别名
where
	连接条件
	and 筛选条件;
  • 多表的删除(sql99语法):
delete1的别名,2的别名
from1 别名
inner|left|right join2 别名 on 
	连接条件
where 
	筛选条件;
  • truncate
truncate 
    table 
    	表名;
#1.单表的删除
#案例:删除手机号以9结尾的女神信息
delete
from
	beauty
where
	phone like '%9';

#2.多表的删除
#案例:删除张无忌的女朋友的信息
delete
	b
from
	beauty b
inner join boys bo on
	b.`boyfriend_id` = bo.`id`
where
	bo.`boyName` = '张无忌';

#案例:删除黄晓明的信息以及他女朋友的信息
delete
	b,
	bo
from
	beauty b
inner join boys bo on
	b.`boyfriend_id` = bo.`id`
where
	bo.`boyName` = '黄晓明';

#3.truncate语句
#案例:将魅力值>100的男神信息删除
truncate
	table
		boys;

/*
delete与truncate的区别:
    delete可以加where条件,truncate不能加。
    truncate删除,效率高一丢丢。
    假如要删除的表中有自增长列,
        如果用delete删除后,再插入数据,自增长列的值从断点开始,
        而truncate删除后,再插入数据,自增长列的值从1开始。
    truncate删除没有返回值,delete删除有返回值。
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值