MySQL所使用的SQL语言是用于访问数据库的最常用标准化语言。MySQL软件采用了双授权政策,分为社区版和商业版,由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,故一般中小型网站的开发都选择MySQL作为网站数据库。以下为,自学途中整理的一些笔记以及遇到的一些问题。

常用指令

  1. 连接数据库
    mysql -uroot -p
    输入密码
    注:-u后跟用户
  2. 选择数据库
    show databases;
    use xxx;
  3. 查看当前数据库下所有表
    show tables;
  4. 查看当前用户
    select user();
  5. 查看当前所选数据库
    select database();
  6. 查看表结构
    desc xxx;

用户授权

在使用MySQL的过程中,MySQL本身已经创建了一个root用户,如果想自己建立一个用户,需给该用户分配一定的权限。

  1. 创建用户 create user ‘username’@’host’ identified by ‘password’
  2. 授权 grant all privileges on . to ‘username’@’host’ identified by ‘password’ with grant option
    flush privileges
  3. 设置或更改密码 set password for ‘username’@’host’=password(“newpassword”)
  4. 撤销用户权限 revoke privilege on databasename.tablename from ‘username’@’host’
  5. 查看权限 show grants for ‘username’@’host’
  6. 删除用户 drop user ‘username’@’host’

SQL语句介绍

  • 查询语句:select
  • DML语句:insert、update、delete
  • DDL语句:create、alter、drop、truncate
  • DCL语句:grant、revoke
  • 事务控制语句:commit、rollback、savepoint

DDL语句

DDL语句是操作数据库对象的语句
例:

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
#建表语句
create table test(
id int primary key,
price decimal,
name varchar(255),
date datetime,
message text
)
修改表结构语句
#增加列定义
alter table test
add(
sex varchar(10) default '男'
)
#修改列定义
alter table test
modify id varchar(255) primary key
#删除列定义
alter table test
drop sex
#重命名数据表
alter table test
rename to test_2
#删除表
drop table test

数据库约束

  • not null
  • unique
  • primary key
  • foreign key
  • check(MySQL不支持check约束)

例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#职位表
create table posit(
id int auto_increment primary key,
name varchar(255) not null
);
#职员表
create table employee(
id int primary key,
name varchar(255) not null,
sex varchar(255) default'男',
birth datetime not null,
age int not null,
posit_id int,
foreign key(posit_id) references posit(id)
);

DML语句

DML主要操作数据表里的数据

  • insert into
  • update
  • delete from

例:

1
2
3
4
5
6
7
8
9
#插入数据
insert into teacher(id,name) values(1,'张三')
#修改数据
update teacher
set name = '李四'
where id = 1
#删除数据
delete from student
where student = 1

三. 查询语句

多表查询

例:
select e.id,e.name,p.name from employee e,posit p

模糊查询

例:
select * from student where name like '张%'
注:_代表一个字符,%代表任意个字符

多表连接查询

  • 交叉连接
  • 自然连接
  • using子句连接
  • on子句连接
  • 左、右、全外连接(MySQL不支持全外连接)

例:

1
2
3
4
5
# on子句连接最常用
select s.* ,t.name
from student s
join teacher t
on s.id = t.id

子查询

  • 出现在from语句后当成数据表(行内视图)

    1
    2
    3
    select *
    from (select * from student) s
    where s.id > 3
  • 出现在where语句后面作为过滤条件的值

    1
    2
    3
    select * from student s
    where s.id >
    (select t.id from teacher t where t.name = '张三')

四. 集合运算

  • union运算(并)
    select 语句 union select语句
  • minus运算(差)
    select 语句 minus select语句
  • intersect运算(交)
    select 语句 intersect select语句

注:MySQL只支持union运算

五. 数据库函数

用于处理数据或复杂的计算

  • 单行函数

    • char_length(‘xx’):字符长度
    • sin(int x):计算sin值
    • adddate(‘1998-04-05’,200):为指定日期添加指定天数
    • curdate():获取当前日期
    • curtime():获取当前时间
    • MD5(‘xx’):MD5加密函数
  • 多行函数(组函数)

    • avg():计算多行的平均值
    • count():计算多行的总条数
    • max():计算多行的最大值
    • min():计算多行的最小值
    • sum():计算多行的总和

常见问题

远程登录

  • 在装有MySQL的机器上登录MySQL mysql -u root -p密码
  • 执行use mysql;
  • 执行update user set host = ‘%’ where user = ‘root’;
  • 执行FLUSH PRIVILEGES;

最后更新: 2018年10月31日 09:04

原始链接: https://www.lousenjay.top/2018/01/25/MySQL学习笔记/