1. 查看数据库:查询MySQL中有哪些数据库。
show databases;
2. 创建数据库:创建一个数据库,名字叫java。
create database java;
3. 使用(调取数据库)数据库:表示之后的操作全是在数据库java中进行的。
use java;
4.建表:在java数据库中建立一个新的表,名字叫exam_result。
create table exam_result(id int,name varchar(50),
chinese decimal(3,1),
math decimal(3,1),
english decimal(3,1));
5. 查看表结构:查看标的组织结构,包含哪些信息。
desc exam_result;
6.删除表:删除某一个表存储的信息。
drop table exam_result;
7.create:新增,在表中添加要存储的信息。
-
多行插入
insert into exam_result values (1,'xuheng',80,80.5,99), (2,'zhanghangfei',90,83.5,89), (3,'lijian',69,88.5,99), (4,'qushihan',80,82.5,59), (5,'fuxiaoyang',90,80.5,79), (6,'lidailin',89,89.5,69), (7,'chaoqiluo',69,80.5,69), (8,'majie',70,80.5,89);
-
单行插入
insert into exam_result values (8,'likui',56,65,59);
8. retrieve:查询,查询指定的信息。
-
查询所有学生信息。
select * from exam_result;
-
查询学生的学号,姓名,语文成绩的信息。
select id,name,cinese from exam_result;
-
查询学生的学号姓名,语数英的总成绩。
select id,name,chinese+math+english as total from exam_result;
-
查询语数英总成绩前三名的学生信息。
select id,name,chinese+math+english as total from exam_result order by total desc limit 3;
-
查询学号,姓名,语文成绩,按照语文成绩由高到低查询学生的信息。
select id,name,chinese from exam_result order by chinese desc;
-
条件查询:查找数学成绩大于85分的学生。
select * from exam_result where math >85 ;
-
条件查询:数学成绩在[85,90]之间的学生。
select * from exam_result where chinese between 85 and 90;
-
模糊查询:
- ‘x%’:查询以姓名以‘x’开头的学生。
- ‘%i’:查询姓名以‘i’结尾的学生。
- ‘%i%’:查询姓名中含有‘i’的学生信息。
以‘x’开头 select * from exam_result where name like 'x%'; 以‘i’结尾 select * from exam_result where name like '%i'; 包含‘i’的 select * from exam_result where name like '%i%';
9. update:修改,修改信息。
新增一列,来存储学生的总成绩信息。
alter table exam_result add(total decimal(4,1));
update exam_result set total=(chinese+english+math);
由于likui的学号重复,将其更改为9号。
update exam_result set id=9 where name =likui;
-
delete:删除
由于李逵同学考试中作弊,开除学籍,删除他的信息。
delete from exam_result where name='likui';