目录:
MySQL-Day03笔记
索引(index)
- 定义
对数据库中表的一列或者多列的值进行排序的一种结构(MySQL中索引用BTREE方式) - 优点
可以加快数据的检索速度 - 缺点
1、当对表中的数据进行增加、删除和修改的时候,索引需要动态维护,降低了数据的维护速度
2、索引需要占用物理空间 - 索引示例
- 开启运行时间检测
show variables like “%pro%”;
set profiling=1; - 执行查询语句
select name from t1 where name=”lucy1009999”; - 查看执行时间
show profiles; - 在name字段创建索引
create index index_name on t1(name); - 执行查询语句
select name from t1 where name=”lucy1009998”; - 查看执行时间
show profiles;
- 开启运行时间检测
索引类型
- 普通索引(index)
- 使用规则
- 一张表中可以有多个index字段
- 字段的值可以重复,且可以为NULL
- 经常把做查询条件的字段设置为index字段
- index字段的key标志为 MUL
- 创建index
- 创建表
… index(字段名),index(字段名)); - 已有表
create index 索引名 on 表名(字段名);
- 创建表
- 查看索引
- desc 表名;
- show index from 表名;
- 删除index
drop index 索引名 on 表名;
## 删除index只能一个一个删
- 使用规则
- 唯一索引(unique)
- 使用规则
- 一个表中可以有多个unique字段
- 对应字段的值不允许重复,但可以为NULL
- key标志:UNI
2、创建 - 创建表
… unique(字段名),unique(字段名));
… unique(phnumber),unique(cardnumber)); - 已有表
create unique index 索引名 on 表名(字段名); - 查看/删除(同index)
drop index 索引名 on 表名;
- 使用规则
- 主键索引(primary key)&&自增长属性(auto_increment)配合主键一起使用
- 使用规则
- 一个表中只能有一个主键字段
- 对应字段值不允许重复,且不能为NULL
- key标志:PRI
- 把表中能够唯一标识一条记录的字段设置为主键,通常把记录编号的字段设置为主键
- 创建
- 创建表
… id int primary key auto_increment,
… …)[auto_increment=10000];
… id int auto_increment,
… primary key(id))[auto_increment=1000]; - 在已有表中创建
alter table 表名 add primary key(字段名); - 删除主键
- 删除auto_increment属性
alter table 表名 modify id int; - 删除主键
alter table 表名 drop primary key;
- 删除auto_increment属性
- 在已有表中添加自增长属性并指定起始值
- 添加自增长属性
alter table 表名 modify id int auto_increment; - 指定起始值
alter table 表名 auto_increment=值;
- 添加自增长属性
- 创建表
- 使用规则
外键索引(foreign key)
- 定义
让当前表的字段值在另一个表的范围内选择 - 语法格式
foreign key(参考字段名)
references 被参考表名(被参考字段名)
on delete 级联动作
on update 级联动作 示例
表1:缴费信息表(财务)
学号 姓名 班级 金额
1 唐伯虎 二班 200
2 点秋香 二班 300
3 祝枝山 二班 500表2:班级信息表(班主任)
学号 姓名 金额
1 唐伯虎 200
2 点秋香 300
表1:
create table jftab(
id int primary key,
name varchar(20),
class varchar(7),
money int)character set utf8;
insert into jftab values
(1,”唐伯虎”,”AID04”,200),
(2,”点秋香”,”AID04”,300),
(3,”祝枝山”,”AID04”,500);
表2:
create table bjtab(
stu_id int,
name varchar(20),
money int,
foreign key(stu_id) references jftab(id) on delete cascade on update cascade
)character set utf8;
insert into bjtab values
(1,”唐伯虎”,200),
(2,”点秋香”,300);- 级联动作
- cascade :数据级联更新
当主表删除记录或更改被参考字段值时,从表级联更新 - restrict(默认)
当主表删除记录或更改被参考字段值时,从表中有相关联记录则不允许主表操作 - set null
当主表删除记录或更改被参考字段值时,从表中相关联记录的字段值设置为 NULL - no action
同 restrict,都是立即检查外键限制
- cascade :数据级联更新
- 删除外键限制
alter table 表名 drop foreign key 外键名;
外键名查看方式:show create table 表名; - 在已有表中添加外键限制
alter table 表名 add
foreign key(..) references 表名(字段名)
on delete 级联动作
on update 级联动作 - 使用规则
- 两张表参考字段和被参考字段数据类型要一致
- 被参考字段必须为key的一种,通常primary key
- 定义
- 普通索引(index)
数据导入
- 作用 :把文件系统的内容导入到数据库中
- 语法格式
load data infile “文件名”
into table 表名
fields terminated by “分隔符”
lines terminated by “\n” - 示例
- 把 /ect/passwd 文件中的内容导入到 db3下的userinfo表
tarena : x : 1000 : 1000 :
用户名 密码 uid gid
tarena,,, : /home/tarena : /bin/bash
用户描述 主目录 登录权限
- 把 /ect/passwd 文件中的内容导入到 db3下的userinfo表
- 操作步骤
1、在数据库中创建对应的表
create table userinfo(
username varchar(20),
password char(1),
uid int,
gid int,
comment varchar(50),
homedir varchar(50),
shell varchar(50)
)character set utf8;
- 把要导入的文件拷贝到数据库搜索路径中
- 查看数据库搜索路径
show variables like “secure_file_priv”; - sudo cp /etc/passwd /var/lib/mysql-files/
- 查看数据库搜索路径
- 执行数据导入语句
load data infile “/var/lib/mysql-files/passwd”
into table userinfo
fields terminated by “:”
lines terminated by “\n”;
- 把要导入的文件拷贝到数据库搜索路径中
练习
将AID1709.csv文件导入到 aid1709 表中
创建表
create table aid1709(
id int,
name varchar(20),
score float(5,2),
phone bigint,
class char(7)
)character set utf8;- 拷贝文件
$ : sudo cp ~/AID1709.csv /var/lib/mysql-files/ - 执行数据导入语句
load data infile “/var/lib/mysql-files/AID1709.csv”
into table aid1709
fields terminated by “,”
lines terminated by “\n”;
数据导出
- 作用
将数据库中表记录保存到系统文件里 - 语法格式
select … from 表名
into outfile “文件名”
fields terminated “分隔符”
lines terminated by “\n” - 示例
- 把userinfo表中的用户名、密码和uid号三个字段的值给导出来
-> select username,password,uid from userinfo
-> into outfile “/var/lib/mysql-files/user1.txt”
-> fields terminated by ” ”
-> lines terminated by “\n”; - 把 mysql库下的user表中 user、host的值导出来,user.csv
-> select user,host from mysql.user
-> into outfile “/var/lib/mysql-files/user.csv”
-> fields terminated by “,”
-> lines terminated by “\n”;
- 把userinfo表中的用户名、密码和uid号三个字段的值给导出来
- 注意
1、导出的内容由SQL查询语句决定
2、执行导出命令时路径必须指定对应的数据库目录下
### chmod 777 文件名
### chmod +rw 文件名 - 表的复制
- 表的重命名:alter table 表名 rename 新表名;
- 复制语法格式
create table 表名 select … from 表名 …; - 示例
1、复制 aid1709 表的全部记录和字段,aid17092
create table aid17092 select * from aid1709;
2、复制 aid1709 表中成绩低于65分的学生到表next
create table next select * from aid1709 where socre<65; - 只复制表结构
create table 表名 select * from 表名 where false;
- 嵌套查询(子查询)
- 定义 :把内层的查询结果作为外层查询的条件
- 语法
SQL查询语句 where 条件(SQL查询语句) - 示例
- 把uid的值小于这个平均值的用户名和uid号显示出来
select username,uid from userinfo
where uid<(select avg(uid) from userinfo); - 找出每个国家攻击力最高的英雄的名字和攻击值
-> select name,gongji from sanguo
-> where gongji in(
-> select max(gongji) from sanguo group by country);
- 把uid的值小于这个平均值的用户名和uid号显示出来