加入收藏 | 设为首页 | 会员中心 | 我要投稿 驾考网 (https://www.jiakaowang.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

mysql数据库基础运营大全

发布时间:2023-06-05 13:22:54 所属栏目:MySql教程 来源:
导读:一、MysqL---增删改查
增:

//创建数据库
create database school;
//创建表
create table info (id int not null auto_increment primary key,name e char(10) not null,score decimal(5,2),hobby int(2));
一、MysqL---增删改查
增:

//创建数据库
create database school;
//创建表
create table info (id int not null auto_increment primary key,name e char(10) not null,score decimal(5,2),hobby int(2));
注释: primary key 主键 auto_increment 自增列

//新增表中内容
insert into info (id,name,score,hobby) values (1,'zhangsan',89,1); #注意前后匹配
//增加列
alter table info add column age int(3);

查:

//查看数据库
MysqL> show databases;
//查看表结构
MysqL> desc info;
//查看数据库中表
MysqL> show tables;
//查看表中内容
MysqL> select from info;
//查看表中具体条目
select from 表名 where id=2[and name=?] [or name=?];

上述查看条目请参考第一部分MysqL的创建部分操作,这里不再赘述,咱们重点看多表查询。

//多表查询 关联表(附表的主键是主表的附键)
select * from info inner join hobby where info.id=hobby.id;
select info.name,score,hobby.hobname from info inner join hobby where info.id=hobo.id=hobby;
select i.name,score,h.hobname from info as i inner join hobby as h where i.id=h.id;

改:

//更改表中数据
update info set score=75 where id=6;

删:

//整行删除
delete from info where name='test';
//删除列
alter table info drop column age;
//删除表
drop table info;
//数据库
drop database school;

以下是简单对删除行、列作的演示,其他同理

二、MysqL---排序、聚合函数
排序:
select from info where 1=1 order by score ; asc--升序,可不写 #默认升序
select from info where 1=1 order by score desc ; desc--降序

函数:

统计count() ; 可以改为1
select count(*) from info;
平均值avg ()
select avg(score) from info;

三、MysqL---索引
简介
索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的引用指针。更通俗的说,数据库索引好比是一本书前面的目录,能加快数据库的查询速度。
类型
普通索引:最基本的索引,没有任何限制
唯一索引:与"普通索引"类似,不同的就是:索引列的值必须唯一,但允许有空值。
主键索引:它 是一种特殊的唯一索引,不允许有空值。
全文索引:仅可用于 MyISAM 表,针对较大的数据,生成全文索引很 耗时好空间。
组合索引:为了更多的提高MysqL效率可建立组合索引,遵循”最左前缀“原则。创建复合索引时应该将最常用(频 率)作限制条件的列放在最左边,依次递减。

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

//创建主键索引
alter table info add primary key(id)

//创建全文索引
create table infos (descript TEXT,FULLTEXT(descript));

//创建多页索引
create index multi_index on info(name,address);

//查看索引: 上述过程已经显示
show index from info;
show index from info \G; 纵向显示

unique 1 不是唯一
unique 0 唯一
//删除索引 (主键、全文索引删除命令比较特殊)
drop index id_index on info; #普通/唯一索引
alter table info drop primary key; #主键索引
drop table infos; #全文索引

(编辑:驾考网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章