文章目录
一、插入语句
1.1方式一
语法:
insert into 表名(字段名,...) values(值,...);
特点:
1、要求值的类型和字段的类型要一致或兼容
insert into beauty(id,name,sex,borndate,phone,photo,boyfriend_id)
values (13,'晓彤','女','1997-4-15','17853219640',null,2)
2、字段的个数和顺序不一定与原始表中的字段个数和顺序一致 但必须保证值和字段一一对应
3、假如表中有可以为null的字段,注意可以通过以下两种方式插入null值
①字段和值都省略
insert into beauty(id,name,sex,borndate,phone,boyfriend_id)
values (13,'晓彤','女','1997-4-15','17853219640',2)
②字段写上,值使用null
insert into beauty(id,name,sex,borndate,phone,photo,boyfriend_id)
values (13,'晓彤','女','1997-4-15','17853219640',null,2)
4、字段和值的个数必须一致
5、字段名可以省略,默认所有列,且列的顺序和表中的顺序一致
insert into beauty
values (13,'晓彤','女','1997-4-15','17853219640',null,2)
1.2方式二
语法:
insert into 表名 set 字段=值,字段=值,...;
#案例:
insert into beauty
set id= 19, name = 'anna',phone = '13978435672';
两种方式 的区别:
1.方式一支持一次插入多行,方式二不支持,语法如下:
insert into 表名【(字段名,..)】 values(值,..),(值,...),...;
#案例:
insert into beauty
values (13,'晓彤','女','1997-4-15','17853219640',null,2), (14,'晓红','女','1999-3-17','19853219640',null,2);
2.方式一支持子查询,方式二不支持,语法如下:
insert into 表名
查询语句;
#案例:
insert into beauty(id,name,phone)
select 26,'宋倩','1892457098';
insert into beauty(id,name,phone)
select id,boyname,'1234567'
from boys where id<3;
二、修改语句
2.1修改单表的记录 ★
语法:
update 表名
set 字段=值,字段=值
【where 筛选条件】;
2.2 修改多表的记录【补充】
语法:
sql92语法(只能内连)
update 表1 别名 ,表2 别名
set 字段=值,字段=值
【where 筛选条件】;
sql99语法
update 表1 别名
left|right|inner join 表2 别名
on 连接条件
set 字段=值,字段=值
【where 筛选条件】;
三、删除语句
3.1 使用delete
3.1.1 除单表的记录★
语法:
delete from 表名 【where 筛选条件】【limit 条目数】
3.1.2 级联删除[补充](多表的删除)
语法:
sql92语法:
delete 别名1,别名2
from 表1 别名,表2 别名
【where 筛选条件 and 筛选条件】
sql99语法:
delete 别名1,别名2
from 表1 别名
inner|left|right join 表2 别名
on 连接条件
【where 筛选条件】
3.2 使用truncate
语法:truncate table 表名
两种方式的区别【面试题】★
1.truncate删除后,如果再插入,标识列从1开始
delete删除后,如果再插入,标识列从断点开始
2.delete可以添加筛选条件
truncate不可以添加筛选条件
3.truncate效率较高
4.truncate没有返回值
delete可以返回受影响的行数
5.truncate不可以回滚
delete可以回滚