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%';
创建mysql数据库索引;
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1、创建新数据库create schema [数据库名称] default character set utf8 ...