mysql数据库配置环境变量及CRUD

mysql -u root -p cmd启动mysql
1.配置环境变量
高级设置 -- 环境变量 -- 在Path地址栏后面加入mysql安装地址
数据库常用操作可参考https://github.com/coco369/knowledge
数据库的CRUD表示create,drop,update,select
分别表示:创建,删除,更新,查找
create table student(
 id int auto_increment,
 name varchar(30) not null,
 primary key(id) 
)engine=InnoDB default charset=utf8;

这里的drop是删除整个数据库,
删除表里的内容一般使用delete
delete from student where name='xxx';insert插入内容
insert into student (name) values('coco');update更改数据
update student set name='xxx' where id=1;select查找数据
select * from student where id in(1,2)查询第一个或者第二个
select * from student order by(-id);按id的倒叙排列
select * from student;查找表的所有内容
select * from student where id=1,name=''
select * from student limit n;查找n行
select * from student limit n offset 1;查找2到第n行
select * from student s join grade g on g.id=s.id where g.g_name='python';外键调用
mysql> select * from student;
+----+--------+-------------+------+
| id | s_name | s_tel       | g_id |
+----+--------+-------------+------+
|  1 | 周杰伦 | 13441244541 |    1 |
|  2 | 孙燕姿 | 13441244542 |    1 |
|  3 | 古天乐 | 13441244543 |    1 |
|  4 | 周星驰 | 13441244544 |    1 |
|  5 | 张家辉 | 13441244545 |    1 |
|  6 | 熊大   | 14441244541 |    2 |
|  7 | 熊二   | 14441244542 |    2 |
|  8 | 熊三   | 14441244543 |    2 |
|  9 | 渣渣辉 | 15441244541 |    3 |
| 10 | 黄大牌 | 15441244542 |    3 |
| 11 | 王大锤 | 16441244541 |    4 |
| 12 | 白元芳 | 16441244542 |    4 |
+----+--------+-------------+------+
==对学生个数统计==:
mysql> select count(*) as '学生个数',g.g_name as '班级名称' from 
student s join grade g on s.g_id=g.id group by(g.id);

+----------+----------+
| 学生个数 | 班级名称 |
+----------+----------+
|        5 | python   |
|        3 | java     |
|        2 | php      |
|        2 | h5       |
+----------+----------+
==添加学生成绩==
mysql> alter table student add s_grades int;
+----+--------+-------------+------+----------+
| id | s_name | s_tel       | g_id | s_grades |
+----+--------+-------------+------+----------+
|  1 | 周杰伦 | 13441244541 |    1 |       70 |
|  2 | 孙燕姿 | 13441244542 |    1 |     NULL |
|  3 | 古天乐 | 13441244543 |    1 |     NULL |
|  4 | 周星驰 | 13441244544 |    1 |     NULL |
|  5 | 张家辉 | 13441244545 |    1 |     NULL |
|  6 | 熊大   | 14441244541 |    2 |     NULL |
|  7 | 熊二   | 14441244542 |    2 |     NULL |
|  8 | 熊三   | 14441244543 |    2 |     NULL |
|  9 | 渣渣辉 | 15441244541 |    3 |     NULL |
| 10 | 黄大牌 | 15441244542 |    3 |     NULL |
| 11 | 王大锤 | 16441244541 |    4 |     NULL |
| 12 | 白元芳 | 16441244542 |    4 |     NULL |
+----+--------+-------------+------+----------+
==添加值==
mysql> update student set s_grades=90 where id=2;
Query OK, 1 row affected (0.11 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update student set s_grades=90 where id=3;
Query OK, 1 row affected (0.08 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update student set s_grades=92 where id=4;
Query OK, 1 row affected (0.13 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update student set s_grades=93 where id=5;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update student set s_grades=94 where id=6;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update student set s_grades=95 where id=7;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update student set s_grades=95 where id=8;
Query OK, 1 row affected (0.10 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update student set s_grades=96 where id=9;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update student set s_grades=96 where id=10;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update student set s_grades=97 where id=11;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update student set s_grades=100 where id=12;
+----+--------+-------------+------+----------+
| id | s_name | s_tel       | g_id | s_grades |
+----+--------+-------------+------+----------+
|  1 | 周杰伦 | 13441244541 |    1 |       80 |
|  2 | 孙燕姿 | 13441244542 |    1 |       90 |
|  3 | 古天乐 | 13441244543 |    1 |       90 |
|  4 | 周星驰 | 13441244544 |    1 |       92 |
|  5 | 张家辉 | 13441244545 |    1 |       93 |
|  6 | 熊大   | 14441244541 |    2 |       94 |
|  7 | 熊二   | 14441244542 |    2 |       95 |
|  8 | 熊三   | 14441244543 |    2 |       95 |
|  9 | 渣渣辉 | 15441244541 |    3 |       96 |
| 10 | 黄大牌 | 15441244542 |    3 |       96 |
| 11 | 王大锤 | 16441244541 |    4 |       97 |
| 12 | 白元芳 | 16441244542 |    4 |      100 |
+----+--------+-------------+------+----------+
12 rows in set (0.00 sec)
==查询成绩平均值,最大值,最小值==
mysql> select avg(s_grades) from student;
+---------------+
| avg(s_grades) |
+---------------+
|       93.8333 |
+---------------+
1 row in set (0.00 sec)

mysql> select max(s_grades) from student;
+---------------+
| max(s_grades) |
+---------------+
|           100 |
+---------------+
1 row in set (0.06 sec)

mysql> select min(s_grades) from student;
+---------------+
| min(s_grades) |
+---------------+
|            88 |
+---------------+
1 row in set (0.00 sec)
mysql> select * from StudentInfo;
+----+--------+------------+------+
| id | i_name | i_phone    | i_id |
+----+--------+------------+------+
|  1 | xxz    | 1355667763 |    1 |
|  2 | xxx    | 1355667763 |    2 |
|  3 | zxx    | 1355664463 |    3 |
|  4 | zyx    | 1355664463 |    4 |
|  5 | zyz    | 1355664463 |    5 |
|  6 | xyz    | 1355664463 |    6 |
|  7 | yyz    | 1355664463 |    7 |
|  8 | yyy    | 1355664463 |    8 |
|  9 | zyy    | 1355664463 |    9 |
+----+--------+------------+------+
**join on语句**
mysql> select * from student;
+----+--------+----------+-------+------------+---------+--------+------+
| id | s_name | s_addr   | s_sex | s_tel      | s_yuwen | s_math | g_id |
+----+--------+----------+-------+------------+---------+--------+------+
|  1 | 王大锤 | 人民公园 | 男    |  111111111 |      60 |     60 |    1 |
|  2 | 白元芳 | 人民公园 | 男    |  111112211 |      60 |     60 |    1 |
|  3 | 周杰伦 | 人民公园 | 男    |  211112211 |      60 |     60 |    1 |
|  4 | 孙燕姿 | 人民公园 | 男    |  214112211 |      60 |     60 |    1 |
|  5 | 周星驰 | 人民公园 | 男    | 2141122110 |      69 |     60 |    2 |
|  6 | 张学友 | 人民公园 | 男    | 2141122120 |      69 |     60 |    2 |
|  7 | 刘德华 | 人民公园 | 男    | 2141122120 |      99 |     60 |    2 |
|  8 | 梁朝伟 | 人民公园 | 男    |  244112210 |      99 |     60 |    2 |
|  9 | 古天乐 | 人民公园 | 男    |  245112210 |      99 |     90 |    2 |
+----+--------+----------+-------+------------+---------+--------+------+
mysql> select s.s_name as '学生',i.i_name as '家长姓名' ,i.i_phone as '家长电话'
     from StudentInfo i join student s on i.i_id=s.id group by(s.id);
+--------+----------+------------+
| 学生   | 家长姓名 | 家长电话   |
+--------+----------+------------+
| 王大锤 | xxz      | 1355667763 |
| 白元芳 | xxx      | 1355667763 |
| 周杰伦 | zxx      | 1355664463 |
| 孙燕姿 | zyx      | 1355664463 |
| 周星驰 | zyz      | 1355664463 |
| 张学友 | xyz      | 1355664463 |
| 刘德华 | yyz      | 1355664463 |
| 梁朝伟 | yyy      | 1355664463 |
| 古天乐 | zyy      | 1355664463 |
+--------+----------+------------+
阅读更多

没有更多推荐了,返回首页