MySQL的操作

原文地址https://www.cnblogs.com/wupeiqi/articles/5713315.html

  a. 安装MySQL服务端
  b. 安装MySQL客户端
  b. 【客户端】连接【服务端】
  c. 【客户端】发送命令给【服务端MySQL】服务的接受命令并执行相应操作(增删改查等)

1
2
3
4
5
6
7
8
9
下载
        http://dev.mysql.com/downloads/mysql/
安装
        windows:
            点点点
        Linux:
            yum install mysql-server
        Mac:
            点点点

Window版本

1、下载

2、解压

如果想要让MySQL安装在指定目录,那么就将解压后的文件夹移动到指定目录,如:C:\mysql-5.7.16-winx64

3、初始化

MySQL解压后的 bin 目录下有一大堆的可执行文件,执行如下命令初始化数据:

1
2
3
cd c:\mysql-5.7.16-winx64\bin
 
mysqld --initialize-insecure

4、启动MySQL服务

执行命令从而启动MySQL服务

5、启动MySQL客户端并连接MySQL服务

由于初始化时使用的【mysqld --initialize-insecure】命令,其默认未给root账户设置密码

输入回车,见下图表示安装成功:

到此为止,MySQL服务端已经安装成功并且客户端已经可以连接上,以后再操作MySQL时,只需要重复上述4、5步骤即可。但是,在4、5步骤中重复的进入可执行文件目录比较繁琐,如想日后操作简便,可以做如下操作。

a. 添加环境变量

将MySQL可执行文件添加到环境变量中,从而执行执行命令即可

如此一来,以后再启动服务并连接时,仅需:

b. 将MySQL服务制作成windows服务

上一步解决了一些问题,但不够彻底,因为在执行【mysqd】启动MySQL服务器时,当前终端会被hang住,那么做一下设置即可解决此问题:

注册成服务之后,以后再启动和关闭MySQL服务时,仅需执行如下命令:

1
2
3
4
5
# 启动MySQL服务
net start mysql
 
# 关闭MySQL服务
net stop mysql

Linux版本

安装:

1
yum install mysql-server  

服务端启动

1
mysql.server start

客户端连接

1
2
3
4
5
6
7
连接:
    mysql -h host -u user -p
 
    常见错误:
        ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2), it means that the MySQL server daemon (Unix) or service (Windows) is not running.
退出:
    QUIT 或者 Control+D

三、数据库操作

1、显示数据库

1
SHOW DATABASES;

默认数据库:
  mysql - 用户权限相关数据
  test - 用于用户测试数据
  information_schema - MySQL本身架构相关数据

2、创建数据库

1
2
3
4
5
# utf-8
CREATE DATABASE 数据库名称 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
 
# gbk
CREATE DATABASE 数据库名称 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

3、使用数据库

1
USE db_name;

显示当前使用的数据库中所有表:SHOW TABLES;

4、用户管理

1
2
3
4
5
6
7
8
9
10
创建用户
    create user '用户名'@'IP地址' identified by '密码';
删除用户
    drop user '用户名'@'IP地址';
修改用户
    rename user '用户名'@'IP地址'; to '新用户名'@'IP地址';;
修改密码
    set password for '用户名'@'IP地址' = Password('新密码')
  
PS:用户权限相关数据保存在mysql数据库的user表中,所以也可以直接对其进行操作(不建议)

5、授权管理

1
2
3
show grants for '用户'@'IP地址'                  -- 查看权限
grant  权限 on 数据库.表 to   '用户'@'IP地址'      -- 授权
revoke 权限 on 数据库.表 from '用户'@'IP地址'      -- 取消权限
 对于权限
 对于数据库
 对于用户和IP
 示例

特殊的:

1
flush privileges,将数据读取到内存中,从而立即生效。
 忘记密码

四、数据表基本

1、创建表

1
2
3
4
create table 表名(
    列名  类型  是否可以为空,
    列名  类型  是否可以为空
)ENGINE=InnoDB DEFAULT CHARSET=utf8
 是否可以为空
 默认值
 自增
 主键
 外键

2、删除表

1
drop table 表名

3、清空表

1
2
delete from 表名
truncate table 表名

4、修改表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
添加列:alter table 表名 add 列名 类型
删除列:alter table 表名 drop column 列名
修改列:
        alter table 表名 modify column 列名 类型;  -- 类型
        alter table 表名 change 原列名 新列名 类型; -- 列名,类型
  
添加主键:
        alter table 表名 add primary key(列名);
删除主键:
        alter table 表名 drop primary key;
        alter table 表名  modify  列名 int, drop primary key;
  
添加外键:alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
删除外键:alter table 表名 drop foreign key 外键名称
  
修改默认值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
删除默认值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;

5、基本数据类型

MySQL的数据类型大致分为:数值、时间和字符串

 View Code

二进制数据:TinyBlob、Blob、MediumBlob、LongBlob

更多参考:

  • http://www.runoob.com/mysql/mysql-data-types.html
  • http://dev.mysql.com/doc/refman/5.7/en/data-type-overview.html

五、表内容操作

1、增

1
2
3
insert into 表 (列名,列名...) values (值,值,值...)
insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)
insert into 表 (列名,列名...) select (列名,列名...) from 

2、删

1
2
delete from 
delete from 表 where id=1 and name'alex'

3、改

1
update 表 set name = 'alex' where id>1

4、查

1
2
3
select from 
select from 表 where id > 1
select nid,name,gender as gg from 表 where id > 1

5、其他

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
a、条件
    select from 表 where id > 1 and name != 'alex' and num = 12;
 
    select from 表 where id between and 16;
 
    select from 表 where id in (11,22,33)
    select from 表 where id not in (11,22,33)
    select from 表 where id in (select nid from 表)
 
b、通配符
    select from 表 where name like 'ale%'  - ale开头的所有(多个字符串)
    select from 表 where name like 'ale_'  - ale开头的所有(一个字符)
 
c、限制
    select from 表 limit 5;            - 前5行
    select from 表 limit 4,5;          - 从第4行开始的5行
    select from 表 limit 5 offset 4    - 从第4行开始的5行
 
d、排序
    select from 表 order by 列 asc              - 根据 “列” 从小到大排列
    select from 表 order by 列 desc             - 根据 “列” 从大到小排列
    select from 表 order by 列1 desc,列2 asc    - 根据 “列1” 从大到小排列,如果相同则按列2从小到大排序
 
e、分组
    select num from 表 group by num
    select num,nid from 表 group by num,nid
    select num,nid from 表  where nid > 10 group by num,nid order nid desc
    select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid
 
    select num from 表 group by num having max(id) > 10
 
    特别的:group by 必须在where之后,order by之前
 
f、连表
    无对应关系则不显示
    select A.num, A.name, B.name
    from A,B
    Where A.nid = B.nid
 
    无对应关系则不显示
    select A.num, A.name, B.name
    from inner join B
    on A.nid = B.nid
 
    A表所有显示,如果B中无对应关系,则值为null
    select A.num, A.name, B.name
    from left join B
    on A.nid = B.nid
 
    B表所有显示,如果B中无对应关系,则值为null
    select A.num, A.name, B.name
    from right join B
    on A.nid = B.nid
 
g、组合
    组合,自动处理重合
    select nickname
    from A
    union
    select name
    from B
 
    组合,不处理重合
    select nickname
    from A
    union all
    select name
    from B
posted @ 2020-03-15 15:46  Alex东  阅读(89)  评论(0编辑  收藏  举报