一份超长的MySQL学习笔记(正在更新)

database

学习资源

视频资料:韩顺平讲MySQL

MySQL安装(Windows):MySQL安装+初始化操作

MySQL本地服务配置(Windows):制作MySQL的Windows服务+创建用户及授权

数据库

备份数据库

mysqldump -u root -p -B 数据库名 > d:\文件名.sql

恢复数据库

SOURCE d:\bak.sql

备份数据库表

mysqldump -u root -p  数据库名 表1 表2 > d:\文件名.sql

创建

#创建

CREATE TABLE `user`(

id INT,

`name` VARCHAR(255),

`password` VARCHAR(255),

`birthday` DATE)

CHARACTER SET utf8 COLLATE utf8_bin ENGINE INNODB;

删除

drop table `actor`;

修改

--在password后面添加salary列

ALTER TABLE `user`

ADD salary VARCHAR(32) NOT NULL DEFAULT ""

AFTER PASSWORD;

--修改列birthday字段的结构

ALTER TABLE `user`

MODIFY birthday VARCHAR(60) NOT NULL DEFAULT "";

--删除name字段

ALTER TABLE `user`

DROP NAME;

--重命名表

RENAME TABLE `user` TO `student`;

--修改表的字符集为utf8

ALTER TABLE student CHARACTER SET utf8;

--修改列名salary为user_name

ALTER TABLE student

CHANGE salary user_name VARCHAR(32) NOT NULL DEFAULT "";

DESC `user`;--显示表结构,查看所有的列

CRUD

insert

INSERT INTO `goods`(id,good_name,price)

VALUES (1,"华为手机",2000);

INSERT INTO `goods`(id,good_name,price)

VALUES (2,"苹果手机",3000);

update

UPDATE `goods`SET price = 1000;

UPDATE `goods`SET price = 2000 WHERE good_name="华为手机";

UPDATE `goods`SET price=price+5000 WHERE good_name="苹果手机";

#如果需要修改多个字段,可以通过 set 字段1=值1,字段2=值2...

delete

DELETE FROM `goods` WHERE price=2000;

select

#查找表内所有内容

SELECT * FROM student;

#按列查找表内容

SELECT id FROM student;

SELECT `id`,english FROM student;

#去除重复数据(只有这一行每一列的数据相同的时候才会去重)

SELECT DISTINCT english FROM student;

#使用表达式对查询的列进行运算

#在select语句中可使用as语句

SELECT `name`AS`名字`,(chinese+english+math) AS `COUNT` FROM student;

#条件查询

SELECT * FROM student

WHERE `name` = "赵云";

SELECT * FROM student

WHERE `english` > 90;

SELECT * FROM student

WHERE (chinese + english + math) > 200;

#order by

#升序

SELECT `name`,(chinese + english + math)AS`count` FROM student

WHERE (chinese+english+math)>200

ORDER BY math ASC;

#降序

SELECT `name`,(chinese + english + math)AS`count` FROM student

WHERE (chinese+english+math)>200

ORDER BY math DESC;

#多条件升降序查询

SELECT `name`,(chinese+english+math)AS`count` FROM student

WHERE `name` LIKE "张%"

ORDER BY `count` DESC;

where语句中常用到的运算符

MySQL函数

合计/统计函数

#count(返回查询结果的行数)

#统计满足条件的某列有多少个,但是会排除为null的情况

SELECT COUNT(*) FROM student;

SELECT COUNT(*) FROM student

WHERE (math+chinese+english)>200;

SELECT COUNT(chinese) FROM student

WHERE chinese>90;

#SUM函数

#仅对数值起作用,否则会报错

SELECT SUM(math) FROM student;

SELECT SUM(math),SUM(chinese),SUM(english) FROM student;

SELECT SUM(chinese+english+math) FROM student;

SELECT SUM(chinese)/COUNT(*) FROM student;

#AVG函数

SELECT AVG(math) FROM student;

SELECT AVG(math+english+chinese) FROM student;

#MAX函数

SELECT MAX(math) FROM student;

#MIN函数

SELECT MIN(math) FROM student;

分组统计

--按照部门查询工资平均值和最大值

SELECT AVG(sal),MAX(sal),deptno FROM emp GROUP BY deptno;

--多条件分组

SELECT AVG(sal),MAX(sal),deptno , job FROM emp GROUP BY deptno , job;

--查找平均工资低于2000的部门

SELECT AVG(sal) , deptno FROM emp GROUP BY deptno HAVING AVG(sal) < 2000;

字符串函数

加密和系统函数

--演示加密函数和系统函数

查询增强

WHERE

-- 查询增强

-- 使用where子句

-- 在MySQL中,日期类型可以直接比较

SELECT * FROM emp WHERE hiredate <= "1991-12-11"; -- 需要注意格式

-- 模糊查询like

-- %表示0~多个任意字符

-- _表示单个任意字符

SELECT ename,sal FROM emp WHERE ename LIKE "S%" ;

SELECT ename,sal FROM emp WHERE ename LIKE "__o%";

-- 显示没有上级的员工信息

SELECT * FROM emp WHERE mgr IS NULL;

-- 查询表结构

DESC emp;

ORDER BY

-- 使用ORDER BY

-- 按照工资从低到高显示信息

SELECT * FROM emp ORDER BY sal; -- 默认降序

SELECT * FROM emp ORDER BY deptno ASC; -- 升序

SELECT * FROM emp ORDER BY deptno ASC , sal DESC;

分页查询

-- 分页查询

SELECT * FROM emp ORDER BY empno LIMIT 0,3;-- 第一页

SELECT * FROM emp ORDER BY empno LIMIT 3,3;-- 第二页

分组增强

-- 增强 GROUP BY 的使用

SELECT COUNT(*) FROM emp GROUP BY job;-- 各个岗位的人数

SELECT COUNT(*),COUNT(comm) FROM emp;-- 雇员总数、获得补助的雇员数

多子句查询

--统计各部门平均工资,并且大于1000的按照平均工资降序排序,取出前两行数据

SELECT deptno,AVG(sal) AS avg_sal

FROM emp

GROUP BY deptno

HAVING avg_sal > 1000

ORDER BY avg_sal DESC

LIMIT 0,2;

多表查询

多表笛卡尔集

-- 多表查询

SELECT * FROM emp,dept;

SELECT * FROM emp;

SELECT * FROM dept;

-- 显示雇员名称和雇员所在部门的名称

-- 从第一张表取出一行,与第二张表中的每一行进行组合,返回结果包含两张表的所有列

SELECT ename,sal,dname,emp.deptno FROM emp,dept WHERE emp.deptno = dept.deptno;

-- 显示部门10的部门名、员工名、工资

SELECT ename,sal,dname,emp.deptno FROM emp,dept WHERE emp.deptno = dept.deptno AND emp.deptno = 10;

-- 显示各个员工的姓名、工资和工资级别

SELECT ename,sal,grade FROM emp,salgrade WHERE sal BETWEEN losal AND hisal;

自连接

自连接是指在同一张表的连接查询,即将同一张表看成两张表

-- 自连接

SELECT worker.ename AS "职员名",boss.ename AS "上级名"

FROM emp worker,emp boss

WHERE worker.mgr = boss.empno;

以上是 一份超长的MySQL学习笔记(正在更新) 的全部内容, 来源链接: utcz.com/z/536096.html

回到顶部