Mysql使用方法有哪些

62次阅读
没有评论

共计 5346 个字符,预计需要花费 14 分钟才能阅读完成。

这篇文章主要为大家展示了“Mysql 使用方法有哪些”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让丸趣 TV 小编带领大家一起研究并学习一下“Mysql 使用方法有哪些”这篇文章吧。

 
# 登录
-hlocalhost -uroot -p;
# 修改密码
mysqladmin -uroot -pold password new;
 
  www.2cto.com  
# 显示数据库
show databases;
# 显示数据表
show tables;
# 选择数据库
use examples;
# 创建数据库并设置编码 utf-8 多语言
create database `examples` default character set utf8 collate utf8_general_ci;
# 删除数据库
drop database examples;
# 创建表
create table test(
    id int(10) unsigned zerofill not null auto_increment,
    email varchar(40) not null,
    ip varchar(15) not null,
    state int(10) not null default -1 ,
    primary key (id)
)engine=InnoDB;
# 显示表结构
describe 
# 删除表
drop table test;
# 重命名表
alter table test_old rename test_new;
# 添加列
alter table test add cn int(4) not null;
# 修改列
alter table test change id id1 varchar(10) not null;
# 删除列  
alter table test drop cn;
# 创建索引
alter table test add index (cn,id);
# 删除索引
alter table test drop index cn
# 插入数据
insert into test (id,email,ip,state) values(2, qq@qq.com , 127.0.0.1 , 0
# 删除数据  
delete from test where id = 1;
# 修改数据
update test set id= 1 ,email= q@qq.com where id=1;
# 查数据
select * from test;  # 取所有数据
select * from test limit 0,2;  # 取前两条数据  
select * from test email like %qq% #查含有 qq 字符 _表示一个 % 表示多个
select * from test order by id asc;# 降序 desc
select * from test id not in(2 , 3 #id 不含 2,3 或者去掉 not 表示含有
select * from test timer between 1 and 10;# 数据在 1,10 之间
  www.2cto.com  
#————————— 表连接知识 ——————————
# 等值连接又叫内链接 inner join 只返回两个表中连接字段相等的行
select * from A inner join B on A.id = B.id; #写法 1
select * from A,B where A.id = B.id; #写法 2
select a.id,a.title from A a inner join B b on a.id=b.id and a.id=1;# 写法 3 表的临时名称
select a.id as ID,a.title as 标题 from A inner join B on A.id=B.id;# 添加 as 字句
 
# 左连接又叫外连接 left join 返回左表中所有记录和右表中连接字段相等的记录
select * from A left join B on A.id = B.id;
 
select * from A left join (B,C,D) on (B.i1=A.i1 and C.i2=A.i2 and D.i3 = A.i3);# 复杂连接
 
# 右连接又叫外连接 right join 返回右表中所有记录和左表中连接字段相等的记录
select * from A right join B on A.id = B.id;
 
# 完整外部链接 full join 返回左右表中所有数据
select * from A full join B on A.id = B.id;
 
# 交叉连接 没有 where 字句 返回卡迪尔积
select * from A cross join B;
————————- 表连接结束 ————————————————————
—————– 索引创建 ————————————————
show index from A #查看索引
alter table A add primary key(id) #主键索引
alter table A add unique(name) #唯一索引
alter table A add index name(name) #普通索引
alter table A add fulltext(name) #全文索引
alter table A add index name(id,name) #多列索引
 
# 常用函数
abs(-1)# 绝对值
pi()#pi 值
sqrt(2)# 平方根
mod(-5,3)# 取余 -2
ceil(10.6)# 进位 +1 结果 11 ceil(10.0) 结果 10
floor(10.6)# 取整 10
round(2.5)# 四舍五入到整数 结果 3
round(2.5,2)# 保留两位小数 结果 2.50
truncate(2.5234,3)# 取小数后 3 位不四舍五入 2.523
sign(-2);# 符号函数 返回 -1 0 还是 0 正数返回 1
pow(2,3),exp(2);#2 的 3 次幂 或 e 的 2 次幂
log(2),log10(2);# 求对数
radians(180),degrees(0.618);# 角度弧度转换
sin(0.5),asin(0.5)# 正弦和反正弦 类似 cos acos tan atan
length(hi)# 计算字符长度
concat(1 ,1, hi)# 合并字符串
insert(12345 ,1,0, 7890 #从开头第 1 个字符开始到 0 个结束,替换成后边字符串,0 表示在最前边插入
ucase(a),lcase(A)# 转成大写和小写
left(abcd ,2),right(abcd ,2);# 返回前两个字符和后两个字符
ltrim( 0  ),rtrim(0),trim( 0  )# 删除空格
replace(1234567890 , 345678 , 0 #替换输出 12090
substring(12345 ,1,2)# 取字符 输出 12 1 是位置 2 是长度
instr(1234 , 234 #取得 234 位置是 2
reverse(1234 #反序输出 4321
current()# 返回日期  www.2cto.com  
curtime()# 返回时间
now()# 返回日期时间
month(now())# 当前月份 monthname 英文月份
dayname(now())# 星期英文 dayofweek()1 是星期天 weekday()1 是星期二
week(now())# 本年第多少周
dayofyear(now()),dayofmonth(now())# 今天是本年第多少天 今天是本月第多少天
year(now()),month(now()),day(now()),hour(now()),minute(now()),second(now())# 返回年月日 时分秒
time_to_sec(now()),sec_to_time(3600*8);# 转换时间为秒和还原
version()#mysql 版本
database()# 当前连接的数据库 没有为 null
user()# 获取用户名
md5(a)# 加密字符串
ascii(a)#ascii 值 97
bin(100),hex(100),oct(100)# 返回二进制 十六进制 八进制
conv(10001,2,8);# 各种进制相互转换
rand()# 生成 0 到 1 之间随机数
sleep(0.02)# 暂停秒数
 
数据库优化
1. 开启缓存,尽量使用 php 函数而不是 mysql
2. explain select 语句可以知道性能
3. 一行数据使用 limit 1;
4. 为搜索字段重建索引 比如关键字 标签
5. 表连接 join 保证字段类型相同并且有其索引
6. 随机查询使用 php $r = mysql_query(SELECT count(*) FROM user
                    $d = mysql_fetch_row($r);  www.2cto.com  
                    $rand = mt_rand(0,$d[0] – 1);
                    $r = mysql_query(SELECT username FROM user LIMIT $rand, 1
7. 避免使用 select * 应该使用具体字段
8. 每张表都是用 id 主键,并且是 unsigned int
9. 对于取值有限而固定使用 enum 类型,如性别 国家 名族 部门 状态
10. 尽可能使用 not null ip 存储使用 int(4), 使用 ip 转化函数 ip2long() 相互 long2ip()
11.delete 和 insert 语句会锁表,所以可以采用分拆语句操作
    while(1){操作语句;usleep(2000);}
12. 选择正确的存储引擎;MyISAM 适合大量查询 写操作多用 InnoDB 支持事务
 
# 存储过程
# 存储程序
delimiter #定义存储程序
create procedure getversion(out params varchar(20)) #params 是传出参数 in 传进 out 传出 inout 传回
begin
select version() into params; #版本信息赋值 params
end
call getversion(@a); #调用存储过程
select @a;
delimiter #定义存储函数
create function display(w varchar(20)) returns varchar(20)
begin
return concat(hello ,w);
end
select display(world
 
drop procedure if exists spName; #删除一个存储过程
alter function spName [];# 修改一个存储过程
show create procedure spName;# 显示存储过程信息
declare varName type default value;# 声明局部变量
#if 语句
if 条件 then 语句
elseif 条件 then 语句
else 语句  www.2cto.com  
end if
#case 语句
case 条件
when 条件 then 语句
when 条件 then 语句
else 语句
end case
#loop 语句
fn:loop
语句
end loop fn;
leave fn #退出循环
#while 语句
fn:while 条件 do
语句
end while fn
 
#mysql 使用帮助资料
? contents; #列出帮助类型
? data types;# 列出数据类型
? int;# 列出具体类型
? show;#show 语句
? create table;#
# 常见表的比较
                    Myisam   BDB    Memory    InnoDB    Archive
存储限制        no           no      yes                64T        no
事物安全                       支持                         支持                        
锁机制         表锁           页锁     表锁             行锁           行锁
全文索引       支持
外键支持                                                         支持
myisam  frm 存储表定义 MYD 存储数据 MYI 存储索引
InnoDB 用于事务处理
char 和 varchar 保存和索引都不相同
浮点数 float(10,2) 定点数 decimal(10,2)
长度一定下,浮点数表示更大数据范围,缺点是引起精度丢失,货币等使用定点数存储
        索引适合于 where 字句或者连接字句列
        对于唯一值使用唯一索引  www.2cto.com  
 
添加新用户 grant select,insert,update,delete on *.* to Yoby@localhost identified by mysql  
#           *.* 数据库名. 表名,限制登录某一个数据库 test.*                           localhost 是本地主机 网络可以使用 % 代替所有主机         mysql 是密码 Yoby 是用户名   所有权限可以用 all 代替
查看用户权限 show grants for root @ localhost
移除权限  revoke all on *.* from root@localhost;
group by id 分组
having 限制字句
select1 union select2 联合查询有重复去掉保留一行
select2 union all select2 所有行合并到结果集中去

以上是“Mysql 使用方法有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注丸趣 TV 行业资讯频道!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-27发表,共计5346字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)