Mysql基本操作

1.创建用户并赋予权限

grant all privileges on *.* to username@localhost identified by 'password'
eg:grant all privileges on shark.* to 'username'@localhost identified by 'password'
eg:grant all privileges on shark.* to 'username'@'%' identified by 'password'

2.添加、删除用户

create user 'username'@'127.0.0.1' identified by 'password'
drop user 'username'@'127.0.0.1'

3.索引建立

1.create index tbname_fed1 on tbname(fed) //创建1个索引
2.create index tbname_ted1_fed2 on tbname(fed1,fed2) //创建多个索引
3.alter table tbname add index(字段1,字段2)

4.编码设置

1.alter database dbname character set utf8 //更改数据库编码
2.show variables like '%char%'; //查看数据库编码

5.数据导出

mysqldump -uroot -proot dbname [tbname] > filename.sql

6.数据导入

mysql> source ./filename.sql

7.导入文件数据

mysql>load data local infile './filename.sql' into table tbname(fed1,fed2...)

8.性能监听

#开启性能查看
set @@profiling = 1;
#查看性能总览
show profiles
#查看详细性能
show profile for query 2

9.解释执行语句性能

explain 查询语句(select * from tbname where fed = 'XXX')

10.更改数据表引擎

alter table tablename set engine = 'InnoDB'(引擎名称)

11.查询表状态

show table status from dbname where name = 'tablename'

12.常用语句

#显示已有数据库
Show databases;
#创建数据库
Create database 数据库名
#选择数据库
Use 数据库名
#创建数据表
Create table tbname(
Id int(10) unsigned not null auto_increment primary key,
Name varchar(10) not null,
Major varchar(10) not null defaule ‘社会学’);
#插入数据
Insert into tbname(fed1,fed2.....) values (vle1,vle2...),(vle1,vle2)....;
#查询数据
Select fed1,fed2 from tbname where (查询条件);
#更改数据
Update 数据表名 set 字段名=新值 where (更新条件);
#删除数据
Delete from 数据表名 where (删除条件);
#插入字段
Alter table 数据表名 add 字段名 字段类型 after|before 字段名
#修改字段
Alter table 数据表名 change字段名 新字段名 新字段类型
#删除字段
Alter table 数据表名 drop 字段名
#重命名数据表
Alter table 数据表名 rename 新数据表名
#删除数据表
Drop table 数据表名
#删除数据库
Drop database 数据库名
#查看数据库中所有表
Show tables from 数据库名
#显示数据表详情信息
Describe 数据库名.数据表名

13.联合查询

<----------聚合查询---------------->
#查询符合条件的条数
Select count(*) from 数据表名 where 查询条件; //返回符合查询条件的条数;
#查询符合条件且字段值不同的条数
Select count(distinct 字段名) from 数据表名 where 查询条件;
#查询最大、最小值、平均值
Select max|min|avg(字段名) from 数据表名 where 查询条件;
#查询符合条件的字段值的和
Select sum(字段名) from 数据表名 where 查询条件;
<-----------分组查询--------------->
Select 字段名,聚合函数 from 数据表名 where 查询条件 group by 字段名;
Select 字段名,聚合函数 form 数据表名 where 查询条件 group by 字段名 having 过滤条件;
<------------联合查询-------------->
#将两个查询语句的查询结果链接,查询结果中的字段名与第一个select的字段标题相符 若省略all 则相同结果指保留一个;
select 查询语句 union [all] select 查询语句.....
<------------连接查询-------------->
交叉连接(cross join):返回数据表中多有数据行的笛卡尔积;
内连接(inner join):使用比较运算符对表中某些字段数据进行比较,据此返回特定的数据;
外连接(left join||right join||outer join):不只列出与连接相匹配的行,还列出左表||右表||两表中所有符合搜索的数据;
select table1.value2,table2.value2 from table1 inner join table2 on table1.value1=table2.value1;
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值