MySQL数据库学习------更新中

MySQL数据库学习------更新中

数据库的三层结构

安装数据库就是安装数据库管理系统(DBMS)

image-20230426213541861

数据库-表的本质仍然是文件

数据在数据库中存储方式是表

表的一行称之为一条记录----->在java程序中,一行记录往往用一个对象映射

SQL语句分类

DDL:数据定义语言

DML:数据操作语言

DQL:数据查询语言

DCL:数据控制语言

创建表create

CREATE TABLE student(
	id INT NOT NULL DEFAULT 1,
	NAME VARCHAR(20) NOT NULL DEFAULT '',
	chinese FLOAT NOT NULL DEFAULT 0.0,
	english FLOAT NOT NULL DEFAULT 0.0,
	math FLOAT NOT NULL DEFAULT 0.0
);

插入表insert

NSERT INTO student(id,NAME,chinese,english,math) VALUES(1,'韩顺平',89,78,90);
INSERT INTO student(id,NAME,chinese,english,math) VALUES(2,'张飞',67,98,56);
INSERT INTO student(id,NAME,chinese,english,math) VALUES(3,'宋江',87,78,77);
INSERT INTO student(id,NAME,chinese,english,math) VALUES(4,'关羽',88,98,90);
INSERT INTO student(id,NAME,chinese,english,math) VALUES(5,'赵云',82,84,67);
INSERT INTO student(id,NAME,chinese,english,math) VALUES(6,'欧阳锋',55,85,45);
INSERT INTO student(id,NAME,chinese,english,math) VALUES(7,'黄蓉',75,65,30);

select语句

#查询表中所有学生的信息
SELECT * FROM student
#查询表中所有学生的名字和对应的英语成绩信息
SELECT `name`,english FROM student
#过滤表中重复数据
SELECT DISTINCT * FROM student
SELECT DISTINCT english FROM student

对查询的列进行运算

#统计每个学生的总分
SELECT `name`,(chinese+english+math) FROM student;
#在所有学生的总分加十分
SELECT `name`,(chinese+english+math+10) FROM student;

给列求别名

#使用别名表示学生分数
SELECT `name`,(chinese+english+math+10) AS total_score FROM student;

where子句经常使用的运算符

#查询姓名为赵云的学生成绩
SELECT * FROM student WHERE `name`='赵云'
#查询英语成绩大于90分的同学
SELECT * FROM student WHERE `english`>90
#查询大于总分大于200分的同学
SELECT * FROM student WHERE (`english`+`chinese`+`math`)>200

and

#查询math大于60,并且(and) id大于4的学生成绩
SELECT * FROM student WHERE `math`>60 AND id > 4

#查询英语成绩大于语文成绩的学生成绩
SELECT * FROM student WHERE english > chinese;
#查询大于总分大于200分并且数学成绩小于语文成绩的同学
#韩%姓以韩开头的
SELECT * FROM student WHERE (`english`+`chinese`+`math`)>200
	AND math < chinese AND `name` LIKE '赵%'

其他

#英语成绩在80-90之间的
SELECT * FROM student WHERE `english` >=80 AND `english` <= 90
SELECT * FROM student WHERE `english` BETWEEN 80 AND 90
#数学成绩为89,90,91
SELECT * FROM student 
	WHERE `math` = 89 OR `math` = 90 OR `math` = 91
SELECT * FROM student 
	WHERE `math` IN (89 ,90,91)	

order by

#使用order by 子句排序查询结果
#asc 升序 desc 降序
#对数学成绩排序后输出
SELECT * FROM student 
	ORDER BY math;
#按总分从高到底顺序输出
SELECT `name`,(math+english+chinese)AS total_score FROM student 
	ORDER BY total_score DESC;
INSERT INTO student(id,NAME,chinese,english,math) VALUES(8,'韩信',89,78,99);
#对姓李的学生成绩输出(升序)where + order by
SELECT `name`,(math+english+chinese)AS total_score FROM student 
	WHERE `name` LIKE '韩%'
	ORDER BY total_score;

猜你喜欢

转载自blog.csdn.net/Johnor/article/details/130895860