Mysql基础命令
设置mariadb初始化密码
#进入mysql
#进入mysql库 use mysql;
#update user set password=password(“密码”)where user=‘root’
创建库,表 create database create table
create dabatase +库名 创建库;
use +库名; #进入库
# create table +表名(id int,name varchar(50))
create table t1 (id int,name varchar(50)); #创建表 序号 和名字
插入信息 insert into
#insert into +表名(id/name) values(字段值)
#insert into (表名) values(值)
#insert into 表名(字段名 id/name) values () 指定列插入
#insert into 表名 set 字段名=值; 设置字段对应的数据
insert into t1(id/name) values( 1 ,'lining');
insert into t1 set name="lining"
更改表信息
alter table 表名 modify id vachar(值)#更改表信息
alter table t1 add gender varchar(20) first(增加到开头) #把gender增加到开头
alter table t1 add gender varchar(20) after name; #增加到name后面
alter table 表名 add gender(#年龄) carchar(值) #增加一列 年龄
alter table 表名 change id emp_id int #修改id 为emp_id也可以修改约束
alter table t1 drop id(#列名); #删除id列
update 表名 set 字段名(id)=数据(1) where 条件; #更新数据
delete from t1 where id=1; #删除id为1的列 在删除前先select * from t1 where id=1; 查看一下
drop table t1(表名); #删除表
drop database 库名; #删除库
查询信息
select * from +表名; #查看整个表
select * from t1 where id=1; #查看ip为1的列
#select * from t1 where 条件 and name="" 查看t1表 id为1 名字为“”
select * from t1 where id=1 and name is not null(#关键字); #查看t1表id=1 名字不等于null的
select * from t1 where id = 1 and name = “lining” #查看t1表id=1 名字等于李宁的
select * from t1 where id=1 or name = "lining"; #查找t1表里id=1和name=lining的列
select * from t1 order by id; #排序
select * from t1 order by id limit 1; #查看第一行
select * from t1 order by id limit 2,2; #查看从第二行之后的两行
desc t1(表名); #查看表结构
show create table t1\G #查看创建t1表的信息
show create database lining #查看创建lining库的信息
show create user root@'localhost' \G #查看创建的用户信息
show processlist\G #常看进程(当前正在登陆的mysql)
status \G #查看数据库状态
kill (8,2,1,5,6,4) #吧进程号为()踢出mysql
create table t1(select * from mysql.user where 1=2(为真则成功,为假则失败));#克隆t1表mysql.user的信息
授权
grant all on 库名.表名 to '主机'@'ip' identified by '密码';
grant all on *.* to 'copy'@'%' identified by 'by';
flush privileges; 刷新权限
函数
avg() # 求平均值
count() # 求记录总数
distinct() # 去重
max() # 最大值
min() # 最小值