数据的增删改查
插入语句insert
方式一 values
语法
Insert into 表名(列名,.....) values (值,........);
特点
- 插入的值的类型要与列的类型一致或兼容
Insert into beauty (id,sex,phone)
Values (13,’女’,’17811111111’);
- 不能为Null的列必须插入值,可以为null的插入值方式有两种
- 写列名,值里写null
Insert into beauty (id,sex,photo) Values (11,’女’,null);
- 不写列名,不写值
Insert into beauty (id,sex) Values (12,’女’);
- 列的顺序可以调换,值的顺序要与上方顺序一致
- 列数和值的个数必须一致
Insert into beauty (sex,id)
Values (’女’,44);
- 可以省略列名,默认所有列
Insert into beauty values (15,’123’,’男’,null,17811111111);
方式二 set
语法
Insert into 表名
Set 列名 = 值,.........;
Insert into beauty
Set id = 22,sex=’男’;
values和set的区别
- 方式一支持插入多行
Insert into beauty(id,sex)
Values (12,’女’),
(13,’男’);
- 方式一支持子查询
Insert into beauty (id,name)
Select id,name
From boys where id<3;
修改语句update
修改单表的记录
语法
Update 表名
Set 列 = 新值,......
Where 筛选条件;
执行顺序为 先 update —where —set
修改beauty表中姓王的人的电话为17855555555,id为2
Update beauty
Set phone = 17855555555,id = 2
Where name = ‘王%’;
修改多表的记录
Sql92语法
Update 表1 别名1,表2 别名2,...
Set 列名 = 值,。。。。
Where 链接条件
And 筛选条件;
Sql99语法
Update 表1 别名1
Inner/left/right join 表2 别名2....
On 链接条件
Set 列名 = 值,。。。。
where 筛选条件;
修改张三的女朋友的手机号为1122344
Update boys bo
Inner join beauty bt
On bo.id = bt.id
Set phone = ‘1122344’
Where bo.boyname = ‘张三’;
删除
方式一 Delete
单表的删除
语法
Delete from 表名 where 筛选条件;
删除id 为2的数据
Delete from beauty where id = 2;
多表的删除
Sql92语法
Delete 别名1,别名2...
from 表1 别名1,表2 别名2,...
Where 链接条件
And 筛选条件;
Sql99语法
Delete 要删除的信息所在表别名1,要删除的信息所在表别名2,.....
From 表1 别名1
Inner/left/right join 表2 别名2....
On 链接条件
where 筛选条件;
删除张三的女朋友的信息
Delete bt
From beauty bt
Inner join boy bo
On bo.boyfriend_id = bt.id
Where bo.boyname=’张三’;
删除张三和他女朋友的信息
Delete bt,bo
From beauty bt
Inner join boy bo
On bo.boyfriend_id = bt.id
Where bo.boyname=’张三’;
方式二Truncate
语法
Truncate table 表名;
删除beauty表
truncate table beauty;
delete和truncate的区别
- Delete 可以加where筛选条件,truncate不能
- 如果要删除的表中有自增长列,
- 使用delete删除后,再插入数据,自增长的值从断点开始,
- 使用truncate 从1开始
- Truncate 没有返回值