CREATE TABLE category(
cid INT,
cname VARCHAR(100)
);
增
插入部分字段
insert into 表名(字段名1,字段名2) values (值1,值2);
例如 insert into category (cid,cname) values (1,“a”);
插入全部字段
insert into 表名 values (值1,值2,…);
insert into category values(1,“a”);
改
更改所有记录的指定字段
update 表名 set 字段 = 值;
例如 update category set cname = “baby”;
更新符合条件记录的指定字段
update 表名 set 字段 = 值 where 条件
例如 update category set cname = “xiaoming” where cid =1;
更新多个字段 用逗号隔开
update 表名 set 字段=值,字段=值 where 条件
例如 update category set cname = “aa”,cid=“2”where cid =“1”;
删
删除表记录
1 删除表中所有记录
delete from 表名;
2 删除表中所有记录
truncate table 表名;
区别
truncate:不能删除行数据,要删就要把表清空。释放空间,清空自动增长的值。
delete:可删除指定行数据。不释放空间,不会清空自动增长的值。
逐条删除表中符合条件的记录
delete from 表名 where 条件
查
查询表中所有记录
select * from 表名
查询表中符合要求的记录
select * from 表名 where 条件
where的用法
1 between …and…
显示在某一区间的值(含头含尾)
[100,200]
先写小值,再写大值
员工工资在10000-15000
2 IN(set)
显示在in列表中的值,例:in(100,200)
替代 or…or…or的结构
书名 是不是属于四大名著
bookName in(‘三国’,’红楼’,’水浒’,’西游’)
3 LIKE
模糊查询,Like语句中,
% 代表零个或多个任意字符,‘明%’ %小米
_ 代表一个字符,’王__’
例如:first_name like ‘_a%’;
4 IS NULL
判断是否为空
5 逻辑运算符
and 多个条件同时成立
or 多个条件任一成立
not 不成立 取反