插入数据
命令格式:insert [into] 表名(字段列表)values(值列表),(值列表),....
ex:
--创建student临时表(name,gender,age,mobile)
create temporary table if not exists student(
id int not null auto_increment,
name varchar(20) not null,
gender varchar(6) not null default 'male',
age int not null default 18,
mobile varchar(11) not null unique key,
primary key(id)
)engine=innodb charset=utf8 comment='this is student info table' auto_increment=1000;
--向表中插入一条数据
insert into student(name,gender,age,mobile) value('张三','male',25,'13888888888');
--向表中插入多条数据
insert into student(name,mobile) values('李四','13888888889'),('王五','13888888880');
--replace 插入数据
replace into insert table(name,mobile) value('赵二','1388888884');
--insert 另外一种插入数据方式,replace同样适用
insert into student set name='钱三',mobile="88888888888";
ps:如果要插入的值列表包含所有字段并且顺序一致,则可以省略字段列表。
查询数据
命令格式:select 字段列表 from 表名 [其他条件]
ex:
--显示student表中所有记录只需学生名称及手机号
select name,mobile from student;
ps:字段列表可以是多张表的字段,字段列表可以用代替,表示返回结果集显示所有字段,不提倡使用,效率低、占用内存、可能需要过滤操作
删除数据
命令格式:delete from 表名 [条件子句]
ex:
--删除名叫张三的学生
delete from student where name = '张三';
--清空学生表
delete from student;
ps:truncate和delete区别是:truncate是先删除表然后再重新创建,delete仅仅只是删除表中的数据
更新数据
命令格式:update 表名 set 字段名=新值, [字段名=新值] [更新条件]
ex:
--更新名字叫李四的学生的年纪为20
update student set age=20 where name = '李四';
ps:注意更新数据时,不要忘记加上更新条件,否则整张表记录都会被更改








网友评论