温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mysql操作命令有哪些

发布时间:2021-12-30 16:52:45 来源:亿速云 阅读:116 作者:小新 栏目:大数据

这篇文章主要介绍mysql操作命令有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

//创建数据库create database school;

//创建表
create table info (id int not null primary key auto_increment,name char(10) not

null,score decimal(5,2),hobby int(2));
#primary key 主键 auto_increment 自增列 
//查看表结构
desc info;
//多表查询(关联表)
select * from info inner join hob where info.hobby=hob.id;

select info.name,score,hob.hobname from info inner join hob where

info.hobby=hob.id;
//别名查询
select i.name,score,h.hobby from info as i inner join hob as h where i.hobby=h.id;

//聚合函数
统计count(): 可以改为1
select count(
) from info;
平均值avg ()
select avg(score) from info;
//查看数据库
show databases;
//进入数据库
use myschool;
//查看myschool中的表
show tables;
//查看info中的数据
select from info;
//在info中插入数据
insert into info (id,name,score) values ('tianqi',55); #前后匹配,如果为空:null
//筛选信息
mysql> select 
from 表名 where id=2[and name=?] [or name=?]
//更新信息
update info set score=75 where id=6;
//删除信息
delete from info where name='test'; #整行删除
//删除表、数据库
drop table info; drop database school;
//排序
select from info where 1=1 order by score ; asc--升序,可不写 #默认升序
select 
from info where 1=1 order by score desc ; desc--降序

数据库索引、事务、视图

索引:快速查询数据  条件:数据数目大于两千条 
相当于一本书前的目录页

create index 索引名称 on tablename 列;
id name score address hobby

create index id_index on info(id); 创建普通索引

show index from info \G; 查看索引折行显示

drop index id_index on info; 删除索引

create unique index id_index on info(id); 创建唯一索引

alter table info add primary key(id); 主键索引

alter table info add column age int(3); 添加列

alter table info drop column age; 删除列

create table infos (descript TEXT,FULLTEXT(descript));全文索引,descript列名描述

create index multi_index on info(name,address); 多页索引,讲两个条件联合起来进行

查询

事务:一组操作共同执行或者都不执行,结果保持一致

举个栗子:银行转账
条件:转账条件余额大于0
姓名 余额
张三 100
李四 200

张三转账100 to 李四

begin 开始

updata bank set money=money-100 where name='zhangsan'

updata bank set money=money+100 where name='lisi'

commit 提交

savepoint s1; 设定回滚点

rollback to savepoint s1; 回到s1回滚点

set autocommit=0 禁止自动提交
set autocommit=1 开启自动提交
rollback 回滚

原子性 不可分割

一致性 前后结果保持一致

隔离性 事务之间隔离,互不影响

持久性 一旦执行成功,不可更改

视图      数据库中的虚拟表

作用:一张表或者多张表中的数据给不同的权限用户提供访问

create view 视图名称 AS

select 语句

select * from info where score > 80; 查看大于80分的人

create view score_view as select * from info where score >80; 形成视图进行查看

select * from score_view; 查看视图

以上是“mysql操作命令有哪些”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI