MySQL数据库基础操作之CRUD
D#### 增(写入数据)
具体字段
语法:
//① 方案
//要求:域个数与数据表字段个数完全一致
//主键id字段:直接设置null即可,其自己会填充
insert into 表名 values (null,field域信息,域信息,域信息...)
② 方案
//根据具体字段进行信息添加(可以添加完整域的个数,也可以添加部分域个数)
insert into 表名 (字段名,字段名) values (域信息,域信息)
//栗子
INSERT into school VALUE (null,'ace',17,'computer');
INSERT into school (name,age) VALUE ('zhangsan','22');
查(查看数据)
一个查询语句的完整形式如:
select 字段,字段,字段
[from 子句]
[where 子句]
[order by 子句]
[limit 子句]
;
字段限制查询
查询语句的基本结构:
select 字段 from 表名
//查询数据表全部记录\全部字段信息:
select * from school;
//查询指定字段
select name,hobby from school;
排序查询
相当于在字段限制查询之后增加了order排序语句
select 字段 from 表名 order by 字段 [asc]/desc //asc升序(不写则默认为升序) desc降序
SELECT * from school order by id ASC; //id升序查询全部记录
SELECT * from school order by id DESC; //id降序查询全部记录
//注意:一般数值型信息都可以排序使用
限制条数查询
相当于在字段限制查询、order排序语句之后增加了limit限制语句
limit 条数 //根据条数获得记录信息
select * from school Order by id limit 5; //按照升序查找前5条信息
where 子句限制查询
语法形式:
select .... from 表名 where 查询条件;
说明:
1. 查询条件,类似php语言中的判断条件,也就是说,where相当于if。
2. 查询条件的作用是:针对from子句的表"进行一行一行筛选”,以筛选出符合条件的行。
3. 查询条件中,可以使用很多的运算符,包括:算术运算符,比较运算符,逻辑运算符,等等。
示例:
where id > 10; //比较运算符
where age - 18 >= 0; //算术运算符,比较运算符
where id < 20 and age >= 18; //比较运算符,逻辑运算符
where year % 4 = 0 and year % 100 != 0 || year % 400 = 0;
//算术运算符,比较运算符,逻辑运算符
其他特殊运算符
- like模糊查找运算符:
用于判断某个字符型字段的值是否包含给定的字符。
语法形式:
xxx字段 like ‘%关键字%’
其中:%表示"任意个数的任意字符”。
还可以使用"_”(下杠),表示"任意一个字符”。
如果不使用"%”或"_”,则 like 相当于等于(=)。比如:
xxx字段 like ‘关键字’
相当于:
xxx字段 = ‘关键字’
- between范围限定运算符:
用于判断某个字段的值是否在给定的两个数据范围之间。
语法形式:
xxx字段 between 值1 and 值2
其含义相当于: xxx字段 >= 值1 and xxx字段 <= 值2
- in运算符:
用于判断某个字段的值是否在给出的若干个"可选值”范围。
语法形式:
xxx字段 in (值1, 值2, ...... )
其含义是:该字段的值等于所列出的任意一个值,就算满足条件,比如:
籍贯 in (‘北京’,‘山东’,‘河北’, ‘江西’); //则某人籍贯为上述4个之一就ok。
- is运算符:
用于判断一个字段中的是"是否存在”(即有没有),只有两个写法,如下所示:
where content is null; //不能写成: content = null;
where content is not null; //不能写成: content != null;
具体栗子:
//where 条件限制 ,根据各种运算符进行限制查询记录信息
//A. 算数运算符
where id+3=8 通过算数运算符限制查询的记录的id值情况
//结果:id=5的记录信息会被查询出来
select * from student where id+3=8;
//B. 比较运算符 > < >= <= = !=
select * from student where id>=4;
select * from student where id=6;
//C. 逻辑运算符 && ||
select * from student where id>=4 && salary>=40000
select * from student where id<3 || id>7;
//D. 其他运算符
//like:模糊查询
select * from student where name like '川普%'; //字段以川普开始后接其他内容条件
select * from student where name like '%林%'; //字段内部包括林,林左右可以有其他内容
select * from student where name like '%林'; //字段内容以林结尾
//in:根据范围查询
select * from student where id in (3,7,9); //根据id范围查询
//between:限制值在某个区间
select * from student where id between 3 and 7; //区间查询
//is:一般用于判断字段是否等于null信息
select * from student where hobby is null; //hobby内容为null的
select * from student where hobby is not null; //hobby内容不为null的
- 多条件限制
多个查询条件一并使用效果:
各个条件(from where order by limit)按照顺序编辑
//举例:
select id,name from school where id>=2 order by id desc limit 3;
改(修改数据)
修改语法:
update 表名 set 字段=值,字段=值...; //修改数据表全部记录信息
update 表名 set 字段=值,字段=值 where 条件子句; //根据条件修改信息
条件子句 与 查询的 where 条件子句 语法完全一致
修改指定记录
//将id大于等于6的数据的介绍(introduce)修改为'hello';
update school set introduce='hello' where id>=6;
删(删除数据)
语法:
delete from 表名; //删除数据表全部记录信息
delete from 表名 where 条件子句; //根据条件删除信息
条件子句 与 查询的 where 条件子句 语法完全一致
举个栗子:
delete from student;
delete from student where id in (12,13,15);
总结
到目前我们了解了MySQL数据库的一些基础操作,其中包括了增删改查,这些都是比较基础的操作。但是千里长城都是从一块块砖垒起来的,所以不要嫌弃它们很基础,因为它们一样很重要。期待下一次的MySQL操作分享啦~