查看 | systemctl status mariadb | 是否安装数据库 |
---|---|---|
安装 | sudo apt install mariadb mariadb-server | 安装客户端和服务端 |
初始化mariadb | mysql_secure_installation | |
启动 | sudo systemctl start mariadb sudo , sudo systemctl enable mariadb.service | |
进入mariadb | sudo mysql -u root -p | |
停止 | systemctl stop mariadb |
查看数据库 | show databases; | |
---|---|---|
创建数据库 | create databases 数据库名 default charset utf8; | |
进入数据库 | use 数据库名; | |
删除库 | drop databases 数据库名; |
创建用户 | create user '用户名'@'用户主机IP(如:127.0.0.%)' identified by '密码'; | |
---|---|---|
删除用户 | drop user '用户名'@'IP地址'; | |
修改用户 | rename user '用户名'@'IP地址' to '新用户名'@'IP地址' ; | |
修改密码 | set password for '用户名'@'ip地址' = Password('新密码'); | |
用户授权 | grant select, insert, update on hunk.* to '用户名'@'%(IP)'; | all privileges(所有权限) |
查看授权 | show grants for '用户'@'IP地址'; | |
取消授权 | revoke 权限 on数据库.表 from '用户'@'IP地址'; |
ps:用户权限相关数据保存在MySQL数据库的user表中,所以可以直接对表进行操作--(不建议)
show tables;
use 表名;
create table 表名(表内容)engine=innodb default charset=utf8;
#例:
create table ti(
id int not null auto_increment primary key,
name char(10)
) engine=innodb default charset=utf8;
innodb #支持事物, 原子性操作
auto_increment: #表示自增
primary key: #表示约束(不能重复且不能为空);加速查找
delete form 表名; (不改变自增)
truncate table 表名;(清空自增)
drop table 表名;
insert into 表名(id, name) values (1, 'xxxx');
delete from 表名 where id=2
update 表名 set name='hunk' where name='huhua'
select * from 表名;
unique 自定义名 (外键id) constraint fk_标识名 foreign key(主表ID) references 外键表名(外键表ID)
tinyint, int, bigint
float, double, decimal(总位数, 小数点后位数)
char(字符长度) :字符没有占满自动填充空值,长度是指定的不变的===(0~255) vachar(字符长度):根据数据的长度可变,节省空间,查询速度慢于char === (0~255) text:(0~65535) PS: SQL优化建议,把不变的往前放, 把可变的往后放
date 日期, time 时间, datetime 日期时间 (YYYY-MM-DD HH:MM:SS )
create table t_l1(
id int not nill auto_increment primary key,
name varchar(32),
age int
)engine=innodb default charset=utf8
#增加 insert
insert into t_l1(name, age) values('alex', 12);
insert into t_l1(name, age) values('alex', 12), ('root', 18);
insert into t_l1(name, age) select name, age form t_lxx;
# 删除 delete
delete form t_l1;
delete form t_l1 where id=12;
delete form t_l1 where id=12 or name='alex';
#修改 update
update t_i1 set name='alex' where id=12 and name='xxx';
update t_i1 set name='alex', age=19 where id<12 and name='xxx';
#查询 select
select * from t_l1;
select id, name from t_l1;
select id, name from t_l1 where id>10 ro name='xxx';
select id, name as cname from t_l1 where id>10 ro name='xxx';
select name, age, 11 from t_l1;
# 其他查询语句 in not int between and
select name from t_l1 where id!=10;
select name from t_l1 where id in (1,3);
select name from t_l1 where id in (select id from 表名);
select name from t_l1 where id not in (1,3);
select name from t_l1 where id between 1 and 3;
# 通配符 % _
# 以a开头:
# 1.a% (任意个字符)
# 2.a_ (一个字符)
select * from t_l1 where name like "%a";
# 分页 limit
select * from t_l1 limit 10;
select * from t_l1 limit 0,10; #(0:起始位置, 10:取的条数)
# 排序 order by desc(大到小) asc(小到大)
select * from t_l1 order by id desc;
select * from t_l1 order by id asc;
本文章使用limfx的vscode插件快速发布