MySQL数据库知识点目录
- Mysql数据库(端口:3306)
Mysql数据库(端口:3306)
1. 创建数据库
-- utf-8
CREATE DATABASE db_name DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
-- gbk
CREATE DATABASE db_name DEFAULT CHARSET gbk COLLATE gbk_chinese_ci;
2. 用户管理
-- 创建用户
create user '用户名'@'IP地址' identified by '密码';
-- 删除用户
drop user '用户名'@'IP地址';
-- 修改用户
rename user '用户名'@'IP地址'; to '新用户名'@'IP地址';;
-- 修改密码
set password for '用户名'@'IP地址' = Password('新密码');
-- 或者用update更新user
update user set password=password('123') where user='root' and host='localhost';
-- 修改需要刷新才生效
flush privileges;
3. 授权管理
-- 查看权限
show grant for '用户名'@'IP地址';
-- 授权
grant 权限 on database1.table1 to '用户名'@'IP地址';
-- 取消权限
revoke 权限 on database1.table1 from '用户名'@'IP地址';
4. 修改表的列
-- 添加列
alter table table_name add column int;
-- 默认添加到最后一列
alter table t1 add age int default 0;
-- 添加到第一列
alter table t1 add addr char(12) first;
-- 添加到addr列后面
alter table t1 add phone after addr;
-- 删除列
alter table t1 drop phone;
-- 修改列,只修改类型
alter table t1 modify column phone INTEGER;
-- 列名和类型同时修改
alter table t1 change column name myname varchar(20);
5. 键的操作
-- 添加主键
alter table t1 add primary key(id);
-- 删除主键
alter table t1 drop primary key(id);
alter table t1 modify id int, drop primary key;
-- 添加外键
alter table 从表 add constraint 从键名称 foreign_key 从表(外键字段)
-- 删除外键
alter table table_name drop foreign_key key_name
6. 设置默认值
-- 设置默认值
alter table t1 alter age set default 18;
-- 删除默认值
alter table t1 alter age drop default;
7. SQL语句基础
- select语句
格式:select 字段 from 表名; 全部字段可以用 *
- where 用于限制查询的结果
格式:
where 字段='xxx';查询条件> < >= <= = !=
-
与(AND)或(OR)
-
在(IN)不在(NOT IN)
-
空(NULL)非空(NOT NULL)
-
全部(ALL) 任一(ANY)
-
在[a,b]之间
格式:between a and b
- 排重DISTINCT
格式:
select DISTINCT 字段 from 表名;
- insert语句
-- 插入数据
insert into 表名(列名,列名) values(值,值);
insert into tb1(id, name) values(1, 'luck');
- update语句
-- 更新数据
update tb1 set name = 'zhangsan' where id > 1;
- delete语句
-- 删除数据
delete from tb1 where id=1 and name = 'luck';
8. 排序
- ORDER BY语句
格式:
select 字段 from 表名 where 条件 ORDER BY 字段;
- 升序(ASC)与降序(DESC)
格式:
select 字段 from 表名 where 条件 ORDER BY 字段 ASC;
格式:
select 字段 from 表名 where 条件 ORDER BY 字段 DESC;
- 多项排序
格式:
select 字段 from 表名 where 条件 ORDER BY 字段 ASC|DESC,字段ASC|DESC;
-- 升序
select * from 表 order by 列 asc;
-- 降序
select * from 表 order by 列 desc

本文详细梳理了MySQL数据库的基础知识,包括创建数据库、用户管理、授权、表操作、SQL语句、排序、聚合函数、关联查询等内容。深入探讨了存储引擎如InnoDB和MyISAM的区别,以及事务隔离级别、主从复制实现过程和高并发环境下的解决方案。通过对MySQL常用数据类型的介绍,助读者全面理解MySQL数据库。
3100

被折叠的 条评论
为什么被折叠?



