MySQL基本命令

3 篇文章 1 订阅
1 篇文章 0 订阅

一、常见设置命令

1.先查看服务器防火墙开放的端口

firewall-cmd --zone=public --list-ports 

2.允许防火墙放行443端口

firewall-cmd --zone=public --add-port=443/tcp --permanent

3.重启防火墙

firewall-cmd --reload

二、开启远程访问权限

mysql -u root -p
# 输入密码

use mysql;

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '密码' WITH GRANT OPTION;

flush privileges;

三、基本命令

# 1.查看数据库
show databases;

# 2.切换到 test 数据库
use test;

# 3.查看所有表(必须切换到数据库)
show tables;

# 4.查看users表中字段
desc users;

# 5.查看users表中数据
select * from users;
# 6.退出
exit;

四、数据库操作

# 创建数据库
create database users;

# 删除数据库
drop database users;

五.表操作

# 删除表
drop table users;
# 查看表
select * from users;
# 创建表
create table user(
-> id int
-> username varchar(50)
-> age int
->) default charset = utf8;

六、数据库设计

类型说明备注
int整型(2的31次方 - 1)数值
float浮点型数值
char短(255字节)字符串
varchar不定长(255~65535)字符串
text长字符串(无限制)字符串

七、数据字段属性

属性说明
unsigned无符号
zerofill定长填充
auto_increment自增(主键)
primary key(id)主键id
default ‘name’默认值
not null不允许为空
null允许为空
comment ‘备注’添加备注

八、表设计

九、表操作

1.基本操作

# 增
insert into users(name,password) values("1","2");

# 删
delete from users where id=1

# 改
update users set password='123' where id=2

# 查
select * from users where id=2;
select name,password from users where id=2;

2.运算符

+ 、 - 、 * 、 / 、% 、 %% 、|| 、and 、 of 、 =!=>=<=>< 

# 例 id大于3 且 小于 5:
select * from users where id between 3 and 5

# 例 id=3 或 id=5 或 id=7:
select * from users where id in(3,5,7)

3.表查询

# 1.选择特定的字段
select id,name from users

# 2.给字段取别名
select id as a,name as b from users
select id a,name b from users

# 3.distinct关键字的使用
select distinct name from users
去除 users 表中 name 字段重复值

# 4.使用where条件查询
select * from users where id>3 and id<5

# 5.查询空置null
select * from users where age is null
select * from users where age is not null

# 6.in的使用
select * from users where id in(1,2,3)

# 7.like查询(搜索) %-> 匹配所有字符  _ 匹配一个字符 (name为字段名)
select * from users where name link '%查询内容%'  

# 8.嵌套正则
select * from users where name regexp '.*live.*' # 包含live
select * from users where name regexp '$456' # 排除456

# 9.使用order by 对查询结果进行排序
select * from users order by id ase # 升序
select * from users order by id desc # 降序

# 10.limit 限制输出条数
select * from users limit 3 # 输出前三条
select * from users limit 3,10 # 从 第三条开始 输出 十条

4.函数查询

# 1.concat() 连接
select concat(user,password) from user; # userpassword

# 2.rand() 随机数
select * from users order by rand() limit 3  # 根据随机数排序  输出前三条

# 3.count() 统计条数
select count(*) from users # *表示 按照主键 统计
 
# 4.sum() 求和
select sum(id) from users  # id 数据求和

# 5.avg() 求平均值
select avg(id) from users

# 6.max() 求最大值
select max(id) from users

# 7.min() 求最小值
select min(id) from users

# 8.group by 分组聚合
select * from users group by class # 以class 字段进行分组

select *,count(*) from users group by class # 以class 字段进行分组,并统计每组条数

5.多表查询

# 1.链表查询(条件字段是属性要一直)
select users.name,class.id from users,class where users.class_id = class.id

# 2.嵌套查询
select * from users where id in (select max(id) from users) # users表中 id 最大的一组数据

# 3.左连接
select class.name,count(users.id) from class left jion users on class.id = users.class_id group by class.id
# 左侧表全部输出 右侧表 如果没有 则输出null

# 4.if条件查询
if(count(suers.id), count(suers.id) , '失败时输出')

6.判断成绩是否及格

select (select count(*) from users where num>60) '及格' , (select count(*) from users where num<60) '不及格'

select sum(if(num > 60 ,1 ,0)) '及格', sum(if(num<60, 1, 0 )) '不及格' from users

select class_id from user oroup by class_id having class_id < 2

# 既: 查出班号小于2的班级的ID

十、举例

1.多表查询 并 模糊查询 返回 前三条

select a.*, b.id as user_id , b.nickname as user_nickname , b.avatar as user_avatar  
	from sort a inner join users b on a.userid = b.id  
 	where locate('搜索内容',a.name)
 	limit 3
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值