MySQL (四) 基本SQL语句
【表记录的操作语句】
首先我们建建一张表,之后我们在这张表的基础之上完成表记录的操作
create table xs ( 学号 char(6) not null primary key ,
姓名 char(4) not null ,
专业 char(10) default '计算机' , 出生日期 date not null , 总学分 tinyint(1) null , 照片 blob null , 备注 text null , ) ;
【表记录的操作】
1、插入操作
use test_database ① insert into xs values('08011' , '王林' , '计算机' , '1994-02-10' , 50 , null , null) ; ② insert into xs (学号 , 姓名 , 专业 , 出生日期 , 总学分) values('08011' , '王林' , '计算机' , '1994-02-10' , 50) ; ③ 由于专业一项设有默认值那么可以如下几种方式 insert into xs (学号 , 姓名 , 出生日期 , 总学分) values('08011' , '王林' , '1994-02-10' , 50) ; 或者 insert into xs values('08011' , '王林' , '1994-02-10' , 50) ; 或者 insert into xs values('08011' , '王林' ,default , '1994-02-10' , 50 , null , null ) ; 或者 insert into xs set 学号='081101' , 姓名='王林' , 专业=default , 出生日期=''1994-02-10' , 总学分=50 ;
2、替换旧记录
默认由 primary key 来标定要替换的记录
replace into xs values ('081101' , '刘华' , '通信' , '1995-09-10' , 50 , null , null ) ;
3、插入图片
①方式1:通过制定图片路径来存储,这只是提供了一个图片位置的索引,并没用将图片真正的插入到数据库中,这种方法很常用
insert into xs values('081101' , '刘华' , '通信' , '1995-09-10' , 50 , 'D:\IMAGE\picture.jpg' , null ) ;
②方式2:直接将图片对象插入到数据表中 ,需要使用 load_file 函数
insert into xs values('081101' , '刘华' , '通信' , '1995-09-10' , 50 , load_file('D:\IMAGE\picture.jpg') , null ) ;
4、修改记录
①修改单个表
update xs set 总学分 = 总学分+10; update xs set学号 ='081251' , 备注 ='转专业学习' where 姓名 = '罗林琳';
注释:如果不指定where条件,将对表中的所有记录执行set操作
②修改多个表
设有两张表

update user1 , user2 set user1.password='1111' , user2.password='2222' when user1.id = user2.id ;
即如果在两张表中存在同一个人的话,那么在两张表中同时进行修改
5、删除表记录
①从单个表中删除记录
delete from table1 where 姓名='张三';
delete from xs where 总学分<50;
②从多个表中删除表记录
delete from t1, t2 using t1, t2, t3 where t1.id=t2.id and t2.id=t3.id;
③清除表记录
truncate table table- name

浙公网安备 33010602011771号