美文网首页
创建mysql数据库索引;

创建mysql数据库索引;

作者: ALuckyLive | 来源:发表于2017-09-12 18:12 被阅读0次
mysql
    
创建数据库
CREATE DATABASE mydb;

使用数据库
use mydb;


创建表格
CREATE TABLE students (stuid INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,name VARCHAR(200),age TINYINT UNSIGNED,gender ENUM('F','M'),major VARCHAR(200));


添加键值
 INSERT INTO students (name,age,gender,major) VALUES('jia baoyu',17,'M','pixie jianfa'),('linchong',37,'M','xianglong shiba zhang'),('XImen',31,'M','kuihua baodian'),('linchong',27,'F','wuxiang shengong');


列出表格
 SELECT * FROM students;


添加键值
 for i in {5..1000};do mysql -e "INSERT INTO mydb.students VALUES ('$i','stu$i',$[$RANDOM%100+1],'F','major$i')";done


for i in {1001..2000};do mysql -e "INSERT INTO mydb.students VALUES ('$i','stu$i',$[$RANDOM%100+1],'M','major$i')";done

for i in {2001..3000};do mysql -e "INSERT INTO mydb.students VALUES ('$i','stu$i',$[$RANDOM%100+1],'M','major$i')";done



查看大于等于15年龄的段落

SELECT name,age FROM students WHERE age>= 15;


查询到数据的来源(如在那个表,标的类型)
 EXPLAIN SELECT name,age FROM students WHERE age>= 15;  

 
EXPLAIN SELECT *FROM students  WHERE name = 'stu1002';

 
查询数量   计算学号count(stuid)  
SELECT count(stuid) FROM students;


创建缩影,
CREATE INDEX name ON students(name);
查看缩影
SHOW INDEXES FROM students---生成stuid主键缩影和name缩影

再次查询name=stu1002的内容;

EXPLAIN SELECT *FROM students WHERE name= 'stu1002';



查询包含100的行数有多少;
 EXPLAIN SELECT * FROM students WHERE name LIKE 'stu100%';


创建name和age的缩影
CREATE INDEX name_and_age ON students(name,age);
查看生成的缩影类型;
 EXPLAIN SELECT * FROM students WHERE name LIKE 'STU100%'

单个条件搜索时;
MariaDB [mydb]> EXPLAIN SELECT * FROM students WHERE age >= 50;
+------+-------------+----------+------+---------------+------+---------+------+------+-------------+
| id   | select_type | table    | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+------+-------------+----------+------+---------------+------+---------+------+------+-------------+
|    1 | SIMPLE      | students | ALL  | NULL          | NULL | NULL    | NULL | 3714 | Using where |
+------+-------------+----------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

两个条件搜索时;
MariaDB [mydb]> EXPLAIN SELECT * FROM students WHERE name LIKE 'stu%' AND age >=50;
+------+-------------+----------+------+-------------------+------+---------+------+------+-------------+
| id   | select_type | table    | type | possible_keys     | key  | key_len | ref  | rows | Extra       |
+------+-------------+----------+------+-------------------+------+---------+------+------+-------------+
|    1 | SIMPLE      | students | ALL  | name,name_and_age | NULL | NULL    | NULL | 3714 | Using where |
+------+-------------+----------+------+-------------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
CREATE INDEX name_and_age ON students(name,age);


联合查询;判断年龄与序号
 SELECT * FROM students WHERE age >=99 UNION SELECT * FROM  students WHERE  name LIKE ;stu;NION SELECT * FROM  students WHERE  name LIKE 'stu100%';

相关文章

  • face17 mysql创建高性能索引

    mysql创建高性能索引 简单描述 mysql中 索引 主键 唯一索引 联合索引的区别对数据库性能有什么影响 创建...

  • MySQL--索引

    MySQL索引 查看索引 创建索引 创建唯一索引 创建主键索引 删除索引 删除主键 MySQL视图 创建视图 删除...

  • java高频面试总结(mysql篇)

    1.mysql常用的索引有哪些种类? 答:1)普通索引:针对数据库表创建索引2)唯一索引:索引列值必须唯一,允许有...

  • MySQL 索引及B+树

    索引 是帮助MySQL数据库高效获取数据的数据结构 索引的优点: 增加数据检索速度 时间: 创建和维护索引需要耗费...

  • mysql笔记

    mysql笔记 索引创建索引创建唯一索引CREATE UNIQUE INDEX indexName ON tabl...

  • Solr 7.4自学笔记一(Solr 7.4创建core和通过M

    Solr 7.4创建core和通过Mysql数据库建立索引步骤 1. 下载Solr solr-7.4.xxxx...

  • MySQL索引简述--BTree索引

    MySQL数据库有如下几种常见的索引类型: BTree索引 哈希索引 全文索引 索引的本质 MySQL官方对索引的...

  • 创建mysql数据库索引;

  • 五、索引

    MySQL 索引 创建索引创建表时创建索引CREATE 在已存在的表上创建索引ALTER TABLE 在已存在的表...

  • mysql 查询优化

    参考文章:mysql 如何优化left joinmysql 创建索引和删除索引mysql 查看索引 查看字符编码

网友评论

      本文标题:创建mysql数据库索引;

      本文链接:https://www.haomeiwen.com/subject/clhcsxtx.html