插入数据
插入全部数据: insert into 表名 values (数据);
INSERT INTO table_name
VALUES (value1, value2, ...);
insert into t_studentinfo values(1,'x博文','男',20);
插入部分列的数据 insert into 表名 (列名1,列名2,...) values(值1,值2,...);
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
insert into t_studentinfo (id,name,age) values(2,'大勇',21);
insert into t_studentinfo (sex,name) values('男','bowen');
修改数据
修改数据: updata 表名 set 列名 = 新的数据 where 条件;
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
#全部更新为21
update t_studentinfo set age = 21;
#将名字为bowen的年龄更改为11
update t_studentinfo set age = 11 where name = 'bowen';
#将名字为大勇的性别更改为女
update t_studentinfo set sex = '女' where name = '大勇';
删除数据
删除数据: delete from 表名 where 条件;
DELETE FROM table_name
WHERE condition;
delete from t_studentinfo where sex = '男';
查询数据
从表中检索数据使用SELECT
语句,语法如下:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
查询所有列: select * from 表名;
select* from t_studentinfo;
查询指定列: select 列名,列名 from 表名;
select name,id from t_studentinfo;
给查询到的列起别名: select 列名 别名,列名 别名 from 表名;
select `name` 姓名,age 年龄 from t_studentinfo;
这样查询结果就会已别名的形式显示。
条件查询: where 条件表达式
相等: 列名 = 值
不相等: 列名 != 值 (只在mysql支持) 列名 <> 值
或: 条件1 or 条件2
并: 条件1 and 条件2
在两者之间: between 条件1 and 条件2;
在范围内: in (范围);
不在范围内: not in (范围);
大于 > ,小于< ,大于等于>= ,小于等于<=
#查询01课程大于60分的学生的信息
select * from student where S in (select S from sc where C='01' and Score > 60);
#查询01课程小于等于60分的学生的信息
select * from student where S in (select S from sc where C='01' and Score <= 60);
#查询01课程小于等于60分或者没选01课程的学生的信息
select * from student where S not in (select S from sc where C='01' and Score > 60);
模糊查询
模糊查询: where 列名 like 模糊表达式;
% :表示匹配任意0到n个字符
_ :匹配任意1个字符
select * from t_studentinfo where name like '胡%';
select * from t_studentinfo where name like '胡__';
select * from t_studentinfo where name like '%小%';
select * from t_studentinfo where name like '_大_';
分页查找
分页查找: limit offset,rows; (offset是起始位置 rows是偏移量(页面大小))
select * from student limit 3 , 2;
select * from student limit 0 , 5;