目录
一、安装mysql
下载
地址:https://dev.mysql.com/downloads/mysql/5.5.html
注册或登录Oracle账户下载
解压添加环境变量
复制到文件路径至bin的到
此电脑——高级系统设置——高级——环境变量——
点下面的path
新建粘贴路径即可
一路确定
创建文件
环境配置完成后,打开文件夹,创建一个新的文件后缀名为 .ini 的 my.ini空白文件以初始化mysql数据库。
复制这段话贴进去(其中两个路径改一下,注意是//不是/)
[mysql]# 设置mysql客户端默认字符集default-character-set=utf8[mysqld]# 设置3306端口port = 3306# 设置mysql的安装目录basedir = D:\\software_learning\\database\\mysql-8.0.30-winx64# 设置mysql数据库的数据的存放目录datadir = D:\\software_learning\\database\\mysql-8.0.30-winx64\\data# 允许最大连接数max_connections=20# 服务端使用的字符集默认为8比特编码的latin1字符集character-set-server=utf8# 创建新表时将使用的默认存储引擎default-storage-engine=INNODB# 创建模式sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
保存退出
启动mysql
以管理员运行cmd窗口
切换盘符,进入mysql文件夹下的bin目录下
2. mysqld --initialize 初始化mysql。
在生成的datd文件夹下找到后缀.err文件,用记事本打开
2022-09-13T07:56:04.929927Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: (ab8w7j6aa,J
查看初始账号、密码也就是下面:(密码是无空格的连续的,首可能是各种奇怪的符号)
账号:root
密码:(ab8w7j6aa,J
安装mysql
如果之前有安装过可以先在cmd命令窗口输入sc delete mysql 删除之前的mysql服务(非必要,可忽略)
接着在文件的bin目录下输入mysqld --install 命令;
成功后net start mysql启动
修改初始密码
alter user ‘root’@‘localhost’ identified with mysql_native_password by ’ 这里填写新密码 ';
例如:
alter user ‘root’@‘localhost’ identified with mysql_native_password by ’ 123456 ';
至此mysql安装启动完毕
如果要停止:
net stop mysql
二、sqlyog安装
上面的服务别关闭
sqlyog下载
http://ms-mcms/upload/1/editor/1576571660432.zip
更新上面链接不行了:https://github.com/webyog/sqlyog-community/wiki/Downloads
下载解压后
进入文件夹找到.exe文件,双击安装
后面都是选下一步
安装后运行,需要注册密钥,下面三选一即可
姓名(Name):cr173
序列号(Code):8d8120df-a5c3-4989-8f47-5afc79c56e7c
姓名(Name):cr173
序列号(Code):59adfdfe-bcb0-4762-8267-d7fccf16beda
姓名(Name):cr173
序列号(Code):ec38d297-0543-4679-b098-4baadf91f983
新建连接
刚刚安装mysql的你改过的用户名密码
连接即可可以看到localhost下的所有数据库
三 补充
3.1 常用的数据库命令
net start mysql – 开启服务
连接数据库:mysql -u root -p – 回车后加密码,
2.查看数据库:show databases
3.创建数据库:create database dbname
4.切换数据库:use dbname
5.查看所有表:show tables
6.修改数据库:alter database dbname character set utf8
7.删除数据库:drop database dbnamenet stop mysql # 关闭服务
sql语言中 – 是单行注释(两个-),是多行注释
3.2 基本操作
3.2.1 常用的sql语言命令
创建表:create table tbname(id int, name varchar(20), sex char(1)); # 数字代表位数
2.查看表信息:desc tbname; – 查看表字段
3.查询数据:select * from tbname;
4.插入数据:insert into tbname (字段名) values (字段值); – insert into tbname (id, name, sex) values (1, ‘小明’, 0);
5.修改数据:update tbname set name=‘小明’, age = 2 where id = 3; – set后修改多个加逗号,指定id的,不指定所有的都要被改
6.删除数据:delete from tbname where id = 3; – 小心where的限定条件,不然全删了
7.修改表(添加字段):alter table tbname add achievement double(5, 2); – achievement 是字段名
8.修改表(改变字段):alter table tbname change name username varchar(20) – name是字段名
9.修改表(删除字段):alter table tbname drop name
10.删除表:drop table tbname
3.2.2 sql语句示例
-- 下面是一个完整的例子CREATE TABLE students (id INT,student_name VARCHAR(20),sex CHAR(1),age INT,achievement DOUBLE(5, 2),team_id INT,graduation_data DATE);INSERT INTO students VALUES (1, 'XiaoMing',0,7,100,1,'2022-9-26');INSERT INTO students VALUES (2, 'XiaoHong',1,0,199,1,'2022-9-26');INSERT INTO students VALUES (3, 'XiaoLiang',0,15,60,2,'2022-9-26');INSERT INTO students VALUES (4, 'XiaoZhang',0,10,88,2,'2022-9-26');INSERT INTO students VALUES (5, 'XiaoLi',1,2,110.5,3,'2022-9-26');INSERT INTO students VALUES (6, 'XiaoLiu',0,6,30,3,'2022-9-26');INSERT INTO students VALUES (7, 'ZhangSan',0,3,NULL,4,'2022-9-26');INSERT INTO students VALUES (8, 'Wangwu',0,6,50,4,'2022-9-26');INSERT INTO students VALUES (9, 'XiaoMing',0,6,40,4,'2022-9-26');INSERT INTO`students` students VALUES (10, 'XiaoEr',0,6,40,NULL,'2022-9-26');-- select只查询数据库返回值,并不会对本身的数据库进行更改SELECT * FROM studentsSELECT id, student_name, sex FROM students; -- 只选择那几列SELECT student_name, age+18 AS '18_years_later' FROM students; -- 给age的数值都加了18,字段名变成18_years_later,(AS可以省略)-- 按条件查询SELECT student_name, age FROM students WHERE age >= 3 AND age <= 15 -- where后可以简写为 BETWEEN 3 AND 15SELECT student_name, age FROM students WHERE age <> 6 -- <>表示不等于,直接使用!=也是可以的SELECT * FROM students WHERE achievement IS NULL;SELECT * FROM students WHERE age = 3 OR age = 10;SELECT * FROM students WHERE age IN (3, 15, 18); -- or SELECT * FROM tbname WHERE NAME LIKE '%x%'; -- 字段包含x的内容,'%x',分别是'x%'以x结尾,以x开头-- 排序SELECT * FROM students ORDER BY achievement; -- 通过achievement对students表数据升序排序SELECT student_name, achievement FROM students ORDER BY achievement DESC; -- 默认不加按照升序排列ASC,降序末尾加DESCSELECT student_name, achievement FROM students WHERE achievement >= 60 ORDER BY achievement DESC; -- 加了条件筛选出来再排SELECT student_name, age, achievement FROM students ORDER BY age, achievement DESC; -- 根据多个规则排序时,用逗号隔开,根据先后顺序排,且没加就是ASC,表示先按照age升序,按照achievement降序
3.2.3常用函数
-- 常用函数SELECT student_name,LOWER(student_name) FROM students; -- 把student_name的所有值转换为小写,并且显示为了LOWER(student_name)SELECT student_name,UPPER(student_name) '转换为大写' FROM students; -- 可以取别名,不然默认是UPPER(student_name)SELECT SUBSTR(student_name, 5, 2) FROM students; -- substr(开始,长度)截取的字符串即 substringSELECT student_name FROM students WHERE SUBSTR(student_name, 5, 1) = "M"; -- 截取的是M这个字符的student_nameSELECT student_name, LENGTH(student_name) FROM students; -- 获取student_name长度,默认是student_name旁边length(student_name)显示SELECT student_name, IFNULL(achievement, 0) FROM students; -- 获取achievement,如果是null就变为0 -- 聚合函数SELECT SUM(achievement) FROM students; -- 计算总成绩SELECT AVG(achievement) FROM students; -- 求平均成绩SELECT MAX(age) FROM students; -- 求最大年龄SELECT MIN(age) FROM students; -- 求最小年龄SELECT COUNT(*) FROM students; -- 查询学生数量SELECT COUNT(achievement) FROM students; -- 有成绩的学员综合SELECT COUNT(achievement) FROM students WHERE sex = 0; -- 查询有成绩的男同学(sex=0)SELECT DISTINCT student_name FROM students; -- 去掉重复数据显示SELECT COUNT(DISTINCT student_name) FROM students; -- 统计去除重复后的总数SELECT sex, COUNT(*) FROM students GROUP BY sex; -- 分组,查询不同性别的人数SELECT team_id, SUM(achievement) FROM students GROUP BY team_id; -- 按小组计算总成绩SELECT team_id, SUM(achievement) FROM students GROUP BY team_id HAVING SUM(achievement) > 100; -- 筛选,总成绩大于100的小组-- where 是直接对数据的条进行筛选,having是用在分组后进行筛选-- limit关键字SELECT * FROM students LIMIT 5; -- 找到前 个学生SELECT * FROM students LIMIT 2,8; -- 查询第3个元素到第8个元素(即从第2个开始,取8个)SELECT * FROM students ORDER BY achievement DESC LIMIT 5; -- 学员表中排序成绩前五的学生-- 顺序综合写法SELECT team_id, SUM(achievement)FROM studentsWHEREsex = 0GROUP BY team_idHAVING SUM(achievement) > 100ORDER BY SUM(achievement) DESCLIMIT 2;-- 约束CREATE TABLE teams (id INT,team_name VARCHAR(20))INSERT INTO teams (id, team_name) VALUES (1, '老鹰队');INSERT INTO teams (id) VALUES (2);SELECT * FROM teams;-- 查询发现第二条数据的team_name是空-- 1.所以创建表的时候需要非空约束来保证字段的值不能为空;也就是在后面加一个not nullteam_name VARCHAR(20) NOT NULL-- 也就是DROP TABLE teams;CREATE TABLE teams (id INT,team_name VARCHAR(20) NOT NULL)INSERT INTO teams (id, team_name) VALUES (1, '老鹰队');INSERT INTO teams (id) VALUES (2); -- 这样插入就会报错,不会成功SELECT * FROM teams;-- 2.默认约束:保证字段即使字段不插入数据,也会有一个默认值team_name VARCHAR(20) DEFAULT '无名队'DROP TABLE teams;CREATE TABLE teams (id INT,team_name VARCHAR(20) DEFAULT '无名队')INSERT INTO teams (id, team_name) VALUES (1, '老鹰队');INSERT INTO teams (id) VALUES (2); -- 这样插入就不会报错,会有默认值'无名队'SELECT * FROM teams;-- 3.主键约束:保证数据不为空,且唯一id INT PRIMARY KEY AUTO_INCREMENTDROP TABLE teams;CREATE TABLE teams (id INT PRIMARY KEY, -- id变成主键约束了team_name VARCHAR(20))INSERT INTO teams (id, team_name) VALUES (1, '老鹰队');INSERT INTO teams (id, team_name) VALUES (1, '老虎队'); -- 就会报错了,除非1改成2SELECT * FROM teams;DROP TABLE teams;CREATE TABLE teams (id INT PRIMARY KEY AUTO_INCREMENT, -- id变成自增的了,注意必须要PRIMARY KEYteam_name VARCHAR(20))INSERT INTO teams (team_name) VALUES ('老鹰队');INSERT INTO teams (team_name) VALUES ('老虎队'); -- 自动增加了SELECT * FROM teams;-- 4.外键约束:限制两个表的关系,一个表的外键必须为另一个表的主键,可以为空FOREIGN KEY(team_id) REFERENCES teams(id) -- 定义一对多关系SELECT * FROM students-- 如果想要students表里面的team_id和teams表里面的team_id进行一个关联,学生里面的作为外键,teams里面的作为主键DROP TABLE students -- 先删了之前的学生表,重新创建CREATE TABLE students (id INT,student_name VARCHAR(20),sex CHAR(1),age INT,achievement DOUBLE(5, 2),team_id INT, -- 仅仅是int,graduation_data DATE,FOREIGN KEY(team_id) REFERENCES teams(id) -- students表中作为外键的字段team_id,关联的主键的那个表teams的主键id);DESC students -- team_id的位置Key写的是MUL,表示是外键INSERT INTO students VALUES (1, 'XiaoMing',0,7,100,1,'2022-9-26');INSERT INTO students VALUES (2, 'XiaoHong',1,0,199,1,'2022-9-26');INSERT INTO students VALUES (3, 'XiaoLiang',0,15,60,2,'2022-9-26');INSERT INTO students VALUES (4, 'XiaoZhang',0,10,88,2,'2022-9-26');SELECT * FROM students-- 这个时候插入下面的要报错,因为外键插入了主键不含有的值3,4(外键的约束)INSERT INTO students VALUES (5, 'XiaoLi',1,2,110.5,3,'2022-9-26');INSERT INTO students VALUES (6, 'XiaoLiu',0,6,30,3,'2022-9-26');-- 所以必须要再加入值才能添加成功INSERT INTO teams (team_name) VALUES ('老猫队');INSERT INTO teams (team_name) VALUES ('老熊队');INSERT INTO students VALUES (5, 'XiaoLi',1,2,110.5,3,'2022-9-26');INSERT INTO students VALUES (6, 'XiaoLiu',0,6,30,3,'2022-9-26');INSERT INTO students VALUES (7, 'ZhangSan',0,3,NULL,4,'2022-9-26');INSERT INTO students VALUES (8, 'Wangwu',0,6,50,4,'2022-9-26');INSERT INTO students VALUES (9, 'XiaoMing',0,6,40,4,'2022-9-26');INSERT INTO students VALUES (10, 'XiaoEr',0,6,40,NULL,'2022-9-26'); -- 外键可以是null但不能是主键里没有的SELECT * FROM students -- 外键可以是null但不能是主键没有的-- 多对多,关联表-- 创建学生表和教师表CREATE TABLE teachers(id INT PRIMARY KEY AUTO_INCREMENT,teacher_name VARCHAR(20))SELECT * FROM teachers;INSERT INTO teachers (teacher_name) VALUES ("李老师");INSERT INTO teachers (teacher_name) VALUES ("张老师");SELECT * FROM teachers;SELECT * FROM students;-- 右键对students表点改变表在students表勾选id为主键,不然下面创建关系表会报错DESC students-- 创建关系表CREATE TABLE teacher_to_student(id INT PRIMARY KEY AUTO_INCREMENT, -- 主键teacher_id INT, student_id INT, -- 两个外键FOREIGN KEY(teacher_id) REFERENCES teachers(id),FOREIGN KEY(student_id) REFERENCES students(id))INSERT INTO teacher_to_student (teacher_id, student_id) VALUES (1,1);SELECT * FROM teacher_to_student-- 多表查询-- 多表不推荐的方式SELECT students.`student_name`, teams.`team_name`FROM students, teams WHERE students.`team_id` = teams.`id`;-- 别名SELECT s.`student_name`, t.`team_name`FROM students s, teams t WHERE students.`team_id` = t.`id`;-- 内连接SELECT s.`student_name`, t.`team_name`FROM students s INNER JOIN teams tON s.`team_id` = t.`id`-- 左外连接SELECT s.`student_name`, t.`team_name`FROM students s LEFT JOIN teams t -- 就这儿改成leftON s.`team_id` = t.`id`-- 右外连接SELECT s.`student_name`, t.`team_name`FROM students s RIGHT JOIN teams t -- 就这儿改成rightON s.`team_id` = t.`id`-- 三个表关联SELECT s.`student_name`, t.`team_name`FROM teacher_to_student tsINNER JOIN teachers t ON ts.`teacher_id` = t.`id`INNER JOIN students s ON ts.`student_id` = s.`id`-- 子查询SELECT student_name,achievement FROM students WHERE achievement > (SELECT AVG(achievement) FROM students)
表关系分类
一对多
多对多关系
多表查询的推荐方式
SQL语言在功能上主要分为如下3大类:
来源地址:https://blog.csdn.net/lanmy_dl/article/details/126836254