介绍了MySQL一些基础指令,增删改查
MySQL
准备工作
-
添加环境变量
-
初始化MySQL
1
mysqld --initialize-insecure
该命令在安全性方面存在缺陷,主要用于开发和测试环境
-
注册MySQL服务
1
mysqld -install
-
启动/停止 MySQL服务
1
2
3net start mysql
net stop mysql -
修改默认账户密码
1
mysqladmin -u root password xxxx
-
登录MySQL
1
mysql -uroot -pxxxx [-h数据库服务器IP地址 -P端口号]
-
卸载MySQL
1
2
3net stop mysql
mysqld -remove mysql
SQL分类
-
DDL:Data Definition Language 定义数据库对象
-
DML :Data Manipulation Language 操作数据,进行增删改
-
DQL:Data Query Language 查询数据库中的记录
-
DCL:Data Control Language 创建数据库的用户,控制访问权限
基础语法
以分号结尾,不区分大小写
DDL
-
查询数据库
1 | show databases; |
-
创建数据库
1 | create database filename; |
-
切换数据库
1 | use filename; |
-
查看当前正在使用的数据库
1 | select database(); |
-
删除数据库
1 | drop database filename; |
表操作
1 | create table 表名( |
约束 | 描述 | 关键字 |
---|---|---|
非空约束 | 限制该字段值不能为null | not null |
唯一约束 | 保证字段的所有数据都是唯一的 | unique |
主键约束 | 主键是一行数据的唯一标识,要求非空且唯一 | primary key (auto_increment自增) |
默认约束 | 保存数据时,如果未指定该字段值,则采用默认值 | default |
外键约束 | 让两张表的数据建立连接,保证数据的一致性和完整性 | foreign key |
1 | creat table tb_user( |
-
查询
1 | -- 查看当下数据库的表 |
-
修改
1 | -- 为表example 添加字段 qq varchar(11) |
-
删除
1 | -- 删除 example 表 |
DML
-
insert语句
1 | -- 为 example 表的 username,name,gender 字段插入值 |
-
update语句
1 | -- 将 example 中ID为1的 name 字段 更新为 张三,username 字段更新为 hello |
-
delete语句
1 | delete from 表名 [where 条件]; |
DQL
-
基本查询
1 | -- 查询特定字段 name,entrydate 并返回 |
-
条件查询(where)
1 | -- select 字段列表 from 表名 where 条件列表 |
-
分组查询(groud by)
- 聚合函数 count max min avg sum
1 | -- select 聚合函数 from 表名 |
1 | -- select 字段列表 from 表名 [where 条件] group by 分组字段名[having 分组后的过滤的条件]; |
-
排序查询(order by)
1 | --select 字段列表 from 表名 [where 条件列表][group by 分组字段] order by 字段1 排序方法1 |
-
分页查询(limit)
1 | -- select 字段列表 from 表名 limit 起始索引,查询记录数; |