# 对于数据表的操作(对数据表的操作使用alter)
show tables;
# 显示当前数据库中所有的数据表
create table table_name(
id int unsigned auto_increment primary key not null,
name varchar(50)
);
# 创建一个数据表,int unsigned无符号整型,auto_increment:自动增长
# primary key:主键, not null:不为空
# 创建表的时候,需要定义数据表的字段名,以及属性(varchar(50))
# 注意(name varchar(50),后面没有逗号)
desc table_name;
# 查看使用的数据库所选择的数据表的字段
alter table table_name add 字段名 字段属性(int/varchar(n));
# 给相应的数据表添加字段
alter table table_name change 字段名(原名) 字段名(新名) 字段类型 约束(not null);
# 对数据表进行字段修改(同时名字也修改)
alter table table_name modigy 字段名 类型 约束;
# 对数据表进行字段的修改(不修改字段名)
alrer table table_name drop 字段名;
# 删除数据表中的某个字段
drop table table_name;
# 删除数据表
# 对数据表中的数据进行操作
insert into table_name values(id,'name','gender',score...),(id,'name','gender',score)....;
# 这样是对照表的字段名,进行数据填充,必须所有的字段名都有
insert into table_name(name) values('name'),('name')...;
# 这里是这填充相应字段的数据
update table_name set 字段名=值 where id = n;
# 对数据进行修改,where进行限定
delete table_name where id = n;
# 对数据进行删除
# 对数据进行查询
select * from table_name;
# 查询表中的所有数据
select name,gender,... from table_name;
# 从数据表中查询对应的数据
select * from table_name where id in (3,5,6);
# 查询表中id=3,id=5,id=6的数据
select * from table_name where id between 3 and 5;
# 查询数据表中连续的3到5的数据
select * from table_name where id > 5;
# 查询数据表中id>5的数据
select * from table_name where id >5 and gender ="男";
# 可以使用and来进行俩个条件同时限制的查询
# 优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符
# and比or先运算,如果同时出现并希望先算or,需要结合()使用
select * form table_name order by score (asc/desc);
# 将查询的结果集进行排序,默认是asc(升序),然后是desc(降序)
select * from talbe_name limit 0,count;
# 进行查询结果的分页操作
# 0是从第几条开始,默认是0
# count是每页有几条数据
# 聚合函数使用的查询
select max(score) from people where gender = '男';
# 选择男生里最高的成绩
select num(score) from people where gender='男';
# 查询男生的总成绩
select avg(score) from people where gender='男';
select sum(score)/count(*) from people where gender='男';
# 查询男生的平均成绩
select gender from people group by gender;
# 根据性别进行分组,并查看分组结果
# 分组查询的用法
select gender, group_concat(id) from people group by gender;
# 根据性别进行分组,并且查询对应组的id
# select gender,count(*) from people group by gender;
# 根据性别进行分组,并且根据查询出每组对应的人数
select gender,group_concat(name) from people group by gender having id>2;
# 这里的having是和where的效果一样的,但是只适用于group by
select gender,count(*) from students group by gender with rollup;
# 这里的with rollup是专门在最后新增一行,来记录当前表中该字段对应的操作结果
# 连接查询的规则
select * from 表1 inner/left/right join 表2 on 表1.列 运算符 表2.列
select * from people inner join book on people.book_id = book.id
# 内连接就是查询表和表之间满足连接条件的数据
# 数据库的左右内连接查询
mysql> select * from book;
+----+----------------+--------+-------------+
| id | name | author | create_time |
+----+----------------+--------+-------------+
| 1 | <三国演义> | 1985 | 李四 |
| 2 | <西游记> | 1888 | 张三 |
| 3 | <红楼梦> | 1898 | 王五 |
| 4 | <西游记> | 1888 | 张三 |
+----+----------------+--------+-------------+
4 rows in set (0.00 sec)
mysql> select * from people;
+----+--------+--------+-------+-------+---------+
| id | name | gender | class | score | book_id |
+----+--------+--------+-------+-------+---------+
| 1 | 张三 | 男 | a | NULL | 3 |
| 2 | 李四 | 男 | b | NULL | 3 |
| 3 | 王六 | 女 | c | NULL | 3 |
| 4 | 周七 | 女 | d | NULL | 1 |
+----+--------+--------+-------+-------+---------+
4 rows in set (0.00 sec)
mysql> select * from people inner join book on people.book_id = book.id;
+----+--------+--------+-------+-------+---------+----+----------------+--------+-------------+
| id | name | gender | class | score | book_id | id | name | author | create_time |
+----+--------+--------+-------+-------+---------+----+----------------+--------+-------------+
| 4 | 周七 | 女 | d | NULL | 1 | 1 | <三国演义> | 1985 | 李四 |
| 1 | 张三 | 男 | a | NULL | 3 | 3 | <红楼梦> | 1898 | 王五 |
| 2 | 李四 | 男 | b | NULL | 3 | 3 | <红楼梦> | 1898 | 王五 |
| 3 | 王六 | 女 | c | NULL | 3 | 3 | <红楼梦> | 1898 | 王五 |
+----+--------+--------+-------+-------+---------+----+----------------+--------+-------------+
4 rows in set (0.00 sec)
mysql> select * from people left join book on people.book_id =book.id;
+----+--------+--------+-------+-------+---------+------+----------------+--------+-------------+
| id | name | gender | class | score | book_id | id | name | author | create_time |
+----+--------+--------+-------+-------+---------+------+----------------+--------+-------------+
| 4 | 周七 | 女 | d | NULL | 1 | 1 | <三国演义> | 1985 | 李四 |
| 1 | 张三 | 男 | a | NULL | 3 | 3 | <红楼梦> | 1898 | 王五 |
| 2 | 李四 | 男 | b | NULL | 3 | 3 | <红楼梦> | 1898 | 王五 |
| 3 | 王六 | 女 | c | NULL | 3 | 3 | <红楼梦> | 1898 | 王五 |
+----+--------+--------+-------+-------+---------+------+----------------+--------+-------------+
4 rows in set (0.00 sec)
mysql> select * from people right join book on people.book_id =book.id;
+------+--------+--------+-------+-------+---------+----+----------------+--------+-------------+
| id | name | gender | class | score | book_id | id | name | author | create_time |
+------+--------+--------+-------+-------+---------+----+----------------+--------+-------------+
| 1 | 张三 | 男 | a | NULL | 3 | 3 | <红楼梦> | 1898 | 王五 |
| 2 | 李四 | 男 | b | NULL | 3 | 3 | <红楼梦> | 1898 | 王五 |
| 3 | 王六 | 女 | c | NULL | 3 | 3 | <红楼梦> | 1898 | 王五 |
| 4 | 周七 | 女 | d | NULL | 1 | 1 | <三国演义> | 1985 | 李四 |
| NULL | NULL | NULL | NULL | NULL | NULL | 2 | <西游记> | 1888 | 张三 |
| NULL | NULL | NULL | NULL | NULL | NULL | 4 | <西游记> | 1888 | 张三 |
+------+--------+--------+-------+-------+---------+----+----------------+--------+-------------+
6 rows in set (0.00 sec)