mysql一些简单的命令

进入mysql:

mysql -h localhost -u root -p

h=>host主机
user用户
password密码

如果在用navicat连接数据库时,出现“Client does not support authentication protocol requested by server; consider upgrading MySQL client”错误时,用下面的语句修改一下密码即可解决。

修改密码:

alter user ‘root’@‘localhost’ identified with mysql_native_password by ‘新密码’;

更多修改密码的方式:
https://www.jb51.net/article/109259.htm

退出命令行:
exit;

显示所有数据库:
show databases;

切换数据库:
use 数据库名称;

显示当前数据库下的所有表:
show tables;

创建数据库:
create database 数据库名称;

删除数据库:
drop database 数据库名称;

创建表(了解,用管理工具创建表更简单):
create table 表名称 (
列名1 数据类型1,
列名2 数据类型2,

);

比如:
create table students (
id int primary key,
name varchar(20) not null,
age int,
birthday datetime
);

not null表示列不能为空值
primary key表示主键列

int整型数据
varchar(20)字符串,表示20字符
datetime日期数据
text 长文本
char 单字符
decimal 浮点数(小数)
bit 位(相当于JS中的boolean)
…更多数据类型,使用管理工具了解。

修改表(了解):
删除已存在的列:
alter table 表名称 drop 列名;
添加新列:
alter table 表名称 add 列名 数据类型;

添加数据:
insert into 表名(列名1,列名2, …) values (值1, 值2);
insert into user values(1,‘dsh’,‘男’,18,‘2019-02-14’);

查询数据:
select * from 表名 where 条件;
*表示所有列。

select * from user where id = 2;
select 列名1,列名2,… from user where id = 2;

修改数据:
update 表名 set 列名1=新值1, 列名2=新值2 where 条件;

删除数据:(删除表数据)
delete from 表名 where 条件;

删除表:(删除表结构)
drop table 表名;

排序(order by语句):
select * from 表名 order by 列名1 asc, 列名2 desc;

asc正序,desc倒序

模糊查询(like语句):
select * from 表名 where 列名1 like ‘%xxxx%’;

取数据条数:
select count(*) as total from user;
select count(id) as total from user;
select count(distinct name) as total from user;

count()用来获取数据条数,列前加distinct关键字用来去除重复的数据。

分组(group by语句)
select * from 表名 group by 列名1;

其中:where语句可以配合order by语句,group by语句, having语句。
having语句用来对分组后的数据再筛选。
如:
select * from 表名 group by 列名1 having 条件;

where语句是group by分组前的筛选条件,而having是group by分组后的筛选条件。having语句只能配合group by 语句使用,没有group by语句,having不能使用。

限制取几条:
select * from user limit 2,4;

按区间查找:
select * from user where id between 2 and 4;

select * from user where id>=2 and id<=4;

or语句(或者):对同一列查询多个值
select * from user where name=‘dsh’ or name=‘admin’;

in语句:
select * from user where name in (‘dsh’,‘admin’);

and语句:对多列查询多个值。
select * from user where name=‘ccc’ and age=16;

like语句:

查询name中包含’d’字符的数据:
select * from user where name like ‘%d%’;

查询name中以’d’开头的数据:
select * from user where name like ‘d%’;

查询name中以’d’结尾的数据:
select * from user where name like ‘%d’;

查询id不等于1的数据:
select * from user where id!=1;
select * from user where id<>1;

查看表结构:
show columns from 表名;

查看表索引:
show index from user;

必须掌握
如何查询?
select * from 表名;
如何筛选?
select * from 表名 where 筛选条件;
如何排序?
select * from 表名 order by 列名;
如何添加?
insert into…
如何删除:
delete from 表名 where …;
如何修改?
update 表名 set 列名1=新值1, 列名2=新值2 where …;

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值