到现在还不会Mysql? 手摸手带你入门mysql系列一

237 阅读2分钟

mysql

1.mysql 的下载跟安装

  下载地址: dev.mysql.com/downloads/m…

安装报错 服务名无效解决方案

进入Mysql 下面的 bin 目录 执行 mysqld --initialize 初始化

然后 net start mysql 就可以了

2.图形可视化工具下载

下载地址:dev.mysql.com/downloads/w…

1、修改密码和修改加密方式

  mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码' PASSWORD EXPIRE NEVER;

  mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY  '新密码';

  mysql> FLUSH PRIVILEGES;

 

   建表

  • id: 主键
  • datatype 数据类型
  • pk 是否主键 Y
  • nn 不为空
  • Ai 自动增加
// 往 users 表里面添加一条数据
// password 是关键字需要 用``包起来
insert into users (username,·password·,nickname) values ('zhangsan','123456','李四');

查询

// 查询全部的users 表 (对性能不好)
select * from users 

// 条件查询  根据username,nickname 查询 username = 'zhangsan' 并且 nickname ='张三' 的数据
select username,nickname from users where username = 'zhangsan' and nickname = '张三'


// 根据 users 表的 id 进行排序  desc(降)   
select * from users order by id desc

// 查询blogs表的总数
select count(id) as `count` from blogs

// 查询 blogs表 id降序 2条数据
// limit 当前查询数据数量 2
// offset 跳过的数据数量 2  // 默认值 0 查询第一页,2相对查询第二页
select * from blogs by id desc limit 2 offset 2
​

修改

// 更新blogs表 id 为 1的数据 content 内容
update blogs set content = '内容内容' where id = 1

// 更新多条数据 更新blogs 表 id 为 1的数据 将content字段对应的值更新为 我是说,title更新为 你是说
update blogs set content = '我是说', title = '你是说' where id = 1

删除

// 删除blogs表 id 为 1 的数据
delete from blogs where id = 1 ;

外键

  • 创建外键
  • 更新限制 & 删除级联
  • 连表查询

alter table 编辑表

  • 比如 blogs 表 关联 users 表
  • 用blogs 表的userid 关联 users表的id
  • 点击 foreign Keys
  • foreign key name 为关联的 name
  • referenced table 为当前关联的数据表
  • colucmn 当前关联的数据表哪个字段进行关联
  • referenced colucmn 你要关联的表的字段
  • foreign Key options

on Update 更新的时候关联

on Delete 删除的时候关联

CASCADE 比如删除了用户,那么将会删除这个用户对应的 文章

链表查询

// 查询出来的 blogs 列表会包含 users.id 对应的 users表的信息
select * from blogs inner join users on users.id = blogs.userid

// 查询出来的 blogs 列表会包含 users.id 对应的 users表的 username,nickname信息
select blogs.*, users.username,users.nickname 
from blogs inner join users on users.id = blogs.userid

// 过滤条件,查询username 为 lisi 的blogs列表
select blogs.*, users.username,users.nickname 
from blogs inner join users on users.id = blogs.userid
where users.username = 'lisi'