数据库中数据是存放在表格当中的,上篇博客我们详细介绍了如何链接数据库、调用数据库、查看数据库中所有的表格,那么这篇博客我们来看看如何对关系型数据库中表格数据进行增删改查

一、insert 操作(增)

往表格里插入样本值

写法一:  insert into 表名(字段1、字段2.....)values(值1,值2......);
写法二:insert into 表名values(值1,值2........);
写法三:insert into 表名(字段1,字段2.....)
									values(值1,值2....),
									(值1,值2.......),
									(值1,值2........)...        #插入多组的值.
写法四:insert into 表名(name,age,sex)
                         select name,age,sex from stars;

二、update操作(改)

更新表格数据

update 表名 set 字段1=值1,字段2=值2......   where  条件     #注意若不见条件,会修改所有数据库中的数据,在实际应用中很危险

三、delete操作(删)

删除表格数据

#删除表中的数据之后,自增主键的值不会重新开始
         delete from 表名 where   条件       #不加条件会删除表格中的所有数据,谨慎使用 
#清空表,清空后自增主键从1开始
        truncate  table 表名                     #等同于delete from 表名
#delete和truncate的区别就是自增主键的值是否从1开始。

四、select操作(查)

基本结构:select 字段名 from 表名
1、基础查询

select username,password  from  user;
select username as  用户名,password as 密码  from user;  #可以给字段起别名
select × from  user; #查询所有字段
select 2018,username,password from user;
select  distinct username from user;#去除重复记录

2、条件查询
a、常用的关系运算符:>,>=,<,<=,=,!=,<>,between and

select username,password from user where uid <10;
select username,password from user where uid !=10;
select username,password from user where uid between 10 and 20;

b、常用的逻辑运算符有:and 、or、not

select username,password from user where uid<100 and uid>20;
select username,password from user where uid>100 or uid<20;

c、常用的集合运算符:in、not in

select username,password from user where uid in (2,3,4);
select username,password from user where uid not in (2,3,4);

d、判空:is null、is not null

select username,password from user where username is null;

e、模糊查询:like

select  * from user where username like “王_”;# _代表一个字符
select * from user where username like'王%';# %代表任意长度字符

3、排序(order by)
排序属性有:asc(升序、默认)、desc(降序)

select * from user order by  age asc;
select * from user oder by age desc;
select name,age from php_user_history oder by age desc,name;#age有相同的记录上按照name升序排列

4、限制结果集(limit):
limit n #取前面n条记录

select * from php_user limit 3; #取前三条记录;
select * from php_user limit 4,2# 从第四条开始取,一共取2条

5、集合函数
count:统计结果中的记录个数;max:最大值;min:最小值;avg:均值;sum:求和;

select count(*) num from user;
select count(distinct age) num from user;  #去除重复记录
注意:集合函数不能直接使用在where后面的条件里,但是可以在子查询中

6、分组(group by)
将结果集分组统计,规则:
(1)出现了group by的查询语句,select后面的字段只能是集合函数和group by后面有的字段,不要跟其他字段
(2)对分组进行过滤,可以用having

select uid,count(*) num from php_forum group by uid
select uid,title,count(*) num from forum group by uid having count(*)>=2;

五、子查询&多表查询

由于子查询和多表查询比较难,所以此处和第四项分开进行描述
1、子查询嵌入到其他查询语句中查询语句,子查询只可以出现在from,where中
子查询别用select *,exists 除外

select title from forum where uid in (select id from php_user where name='王三');

2、多表查询
a、多表查询必须有链接,否则结果没有意义;
b、多表查询有两种写法:隐式标准sql(链接条件写到where字句中) 和 显式 内连接
(1)标准sql语句

select title,content,name,publish_time  
		from user u ,forum f              #给表格起一个别名,书写方便
   		where u.id =f.uid ;                 #where后写链接条件
(一个;为一个语句,在mysql中,换行不算一个语句,必须以“;”结束才算一个语句)
select  title,content,name,publist_time
        from user u, forum f
   		where u.id =f.uid and name=’王昆‘;
select  a.username,,c.title
   		from bbs_user a,bbs_category b,bbs_forum c
   		where b.cid=c.cid  and c.uid=a.uid

(2)内连接(inner join)

select a.uid,username,titile,conten
        from bbs_user a  inner join bbs_form b   on a.uid=b.uid   #关联条件
        where  a.uid<5;
select username,name,title
		 from bbs_user a inner join bbs_forum c on  c.uid=a.uid
		 inner join bbs_category b on  c.cid=b.cid;

(3)外链接(left join、right join)
两张表关联查询时,根据以哪个表为主可以分为左外链接和右外链接,左外链接以左表为主,如果右边的表里没有匹配的记录,则添加一个万能记录与之匹配。

select * from user  u left join forum f on  u.id =f.uid;#以user为主表
select * from user  u right join forum f on  u.id =f.uid;#以forum为主表
mysql的增删改查操作基本就这些了,去实际中多操作吧。