插入 删除和更新记录

一、插入数据

1、为表的所有字段插入数据

语法:

insert into 表名(字段1,字段2,……)values(值1,值2,……)

示例:

在tsubject表中插入一条记录?

insert into tsubject(subjectid,subjectname,bookname,publisher) values('0007','英语','大学英语走遍美国','清华出版社');

提示:当给表中所有字段添加字段值时,可以省略字段名。

insert into tsubject values('0008','高等数学','高等数学教材','清华出版社');

2、为表的指定字段插入数据

语法:

insert into 表名(字段1,字段2,……) values(值1,值2,……)

示例:

在tsubject表中添加课程id为0009,课程名为'安卓开发'?

insert into tsubject(subjectid,subjectname) values('0009','安卓开发'); insert into tsubject(subjectname,subjectid) values('0010','C#入门');

3、同时向表中插入多条记录

语法:

insert into 表名(字段1,字段2,……) values(值1,值2,……),(值1,值2,……),……

示例:

insert into tsubject(subjectname,subjectid) values('数据结构','0011'),('计算机基础','0012')

4、将查询结果插入到表中

语法:

insert into 表名(字段1,字段2,……) select 字段1,字段2,…… from 表名 where 条件;

示例:

create table st(subjectid varchar(4),subjectname varchar(30)); insert into st(subjectid,subjectname) select subjectid,ubjectname from tsubject where publisher is null;

二、更新数据

1、根据本表的条件更新记录

语法:

update 表名 set 字段名1=表达式1,字段名2=表达式2,...... where 条件

示例:

将tstudent表中专业班级为.net开发的学生姓名后加注net字样。

update tstudent set sname=concat(sname, 'net') where class='.net开发'; update tstudent set sname = left(sname, 3) where class ='.net开发';

根据学生的出生日期,在学生姓名后加注大一、大二、大三、大四的字样。(1985年大四、1986年大三、1987年大二、1988年大一)。

update tstudent set sname=concat(sname, case year(birthday)%5 when 0 then '大四' when 1 then '大三' when 2 then ‘大二' else '大—' end) where year(birthday)>=1985 and year(birthday)<=1988; update tstudent set sname=left(sname, 3) where year(birthday)>=1985 and year(birthday)<=1988;

2、根据另一个表的条件更新

语法:

update 表1 join 表2 on 表1和表2的连接条件 set 字段名1=表达式1, 字段名2=表达式2,...... where 条件

示例:将成绩是不及格的学生的姓名后加*号?

update tstudent s join tscone sc on s.studentid=sc.StudentID set s.sname=concat(sname, '*') where sc.mark<60; update tstudent s join tscore sc on s.studentid=sc.StudentID set s.sname=left(sname, 3) where sc.mark<60;

注意:同时更改两张表的列

示例:将分数低于60分的学生,加5分,并在学生姓名后添加标记'+'

update tstudent s join tscore sc on s.studentid=sc.StudentID set s.sname=concat(sname,'+'), sc.mark=sc.mark+5 where sc.mark<60; update tstudent s join tscore sc on s.studentid=sc.StudentID set s.sname=left(sname,3), sc.mark=sc.mark-5 where sc.mark<60;

子查询也能实现相同的功能

示例:将成绩有大于98分的学生姓名后添加“#”标记?

update tstudent set sname = concat(sname, '#') where studentid in (select studentid from tscore wrere mark>=98)

三、删除数据

1、根据本表的条件删除记录

语法:

delete from 表名 where 条件;

示例:

删除学号小于00010的学生?

delete from tstudent where studentid<'00010';

2、根据另一张表的条件删除

语法:

delete 表别名1 from 表名1 表别名1 join 表名2 表别名2 on 表1和表2的连接条件 where 条件;

示例:

删除成绩小于60分的学生?

delete s from tstudent s join tscore sc on s.studentid=sc.studentid where sc.mark<60;

提示:使用子查询也可以实现上述功能

示例:删除成绩大于90分的学生记录?

delete from tstudent where studentid in (select studentid from tscore where mark>90);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值