1.索引
1.BTREE
2.优点
加快数据的检索速度
3.缺点
1.当对表中数据进行增加 修改 删除时,索引需要动态低了数据的维护速度
2.索引需要占用物理存
4.索引示例
1.开启运行时间检测:set profiling = 1 ;
备注: show variables like "profiling" ;
2.执行查询语句
select name from t1 where name = "lucy99999";
3.查看执行时间
show profiles;
4.给name字段创建索引
create index name on t1(name);
5.执行查询语句
select name from t1 where name = "lucy88888";
6.查看执行时间
show profiles;
5.索引类型
1.普通索引(index)
1.使用规则
1.可设置对个字段
2.约束 : 无
3.KEY标志: MUL
2.创建
1.创建表时
index(字段名),
index(字段名)
2.已有表
create index 索引名 on 表名(字段名);
3.查看
1.desc 表名;
2.show index from 表名\G;
4.删除
drop index 索引名 on 表名
2.唯一索引(unique)
1.使用规则
1.可设置多个字段
2.约束:字段值不允许重复,允许为NULL
3.KEY标志:UNI
2.创建
1.创建表
unique(字段名),
unique(字段名)
2.已有表
create unique index 索引名 on 表名(字段名);
3.查看 删除同普通索引
Non_unique: 0 唯一索引
Non_unique: 1 普通索引
3.主键(primary key)&&自增长属性(auto_increment)
1.使用规则
1.只能有一个主键字段
2.约束:不允许重复,不能为NULL
3.KEY标志: PRI
4.通常设置记录编号字段 id 为主键,唯一锁定一条记录
2.创建
1.创建表时
id int primary key auto_increment
2.已有表
alter table 表名 add primary key(字段名);
3.删除
1.删除自增长属性
alter table 表名 modify id int;
2.删除主键
alter table 表名 drop primary key;
4.指定自增长属性起始值
1.创建表
create table 表名(
id int primary key auto_increment,
... ...
)auto_increment=10000;
2.已有表
alter table 表名 auto_increment=10000;
4.外键(foreign key)
2.数据导入
1.把文件系统内容导入到数据的表中
2.命令格式
load data infile "文件名"
into table 表名
fileds terminated by "分隔符"
lines terminated by "\n"
3.将scoretable.csv文件导入到db3库下的score表中
1.先将scoretable.csv文件拷贝到数据库的默认搜索路径中
1.查看搜索路径方法
show variables like "secure_file_priv";
2.执行复制命令
sudo cp /home/tarena/scoretable.csv /var/lib/mysql-files/
2.创建库 表(utf8字符集)
1.create database db3 character set utf8;
2.use db3;
3.
create table score(
id int,
name varchar(20),
score decimal(5,2),
phone char(11),
class char(7)
)character set utf8;
3.执行导入语句
load data infile "/var/lib/mysql-files/scoretable.csv"
into table score
fields terminated by ","
lines terminated by "\n";
注意:
1.库 表必须都为utf8编码
2.路径必须写绝对路径,"/var/lib/mysql-files/..."
4.文件权限问题
rwx 所有者对此文件权限
r-- 所属组其他用户对此文件权限
r-- 其他组的用户对此文件权限
root 所有者
root 所属组
r : 读
w : 写
x : 执行
1.修改文件权限
chmod + rw scoretable.csv
chmod 666 scoretable.csv
r : 4
w : 2
x : 1
最高777
4.excel文件转为csv文件
打开excel表格 -> 文件 -> 另存为 -> *.csv(逗号分隔)
5.更改一个文件的字符编码
用记事本/编辑器 打开,文件 -> 另存为 -> 选择编码
3.数据导出
1.将数据库中表记录保存到系统文件里
2.语法格式
select ... from 表名
into outfile "文件名"
fields terminated by "分隔符"
lines terminated by "\n"
3.示例
1.把sanguo表中的姓名 攻击值 国家三个字段导出到文件sanguo.csv中
select name,gongji,country from MOSHOU.sanguo
into outfile "/var/lib/mysql-files/sanguo.csv"
fields terminated by ","
lines terminated by "\n"
2.把mysql库下的user表中 user host 两个字段的值导出到user.txt中,字段之间用 三个空格 去分隔
select user,host from user
into outfile "/var/lib/mysql-files/user.txt"
fields terminated by " "
lines terminated by "\n"
4.注意
1.导出的内容完全由SQL查询语句决定
2.路径必须指定为数据库搜索的绝对路径
4.表的复制
1.语法
create table 表名 select ... from 表名;
2.示例
1.复制sanguo表,sanguo2
create table MOSHOU.sanguo2 select * from MOSHOU.sanguo;
2.复制sanguo表的前3条记录,sanguo3
create table MOSHOU.sanguo3
select * from MOSHOU.sanguo limit 3;
3.复制sanguo表的id name country 三个字段的前5条记录,sanguo4
create table MOSHOU.sanguo4 select id,name,country from MOSHOU.sanguo limit 5;
3.只复制表结构
create table 表名 select * from 表名 where false;
create table MOSHOU.sanguo5 select * from MOSHOU.sanguo where false; -------------------------------------
5.嵌套查询
1.定义
把内层的查询结果作为外层的查询条件
2.select ... from 表名 where 字段名 运算符(select ...);
3.示例
1.把攻击值小于平均攻击值 的名字和攻击值显示出来
select name,gongji from MOSHOU.sanguo
where gongji <(select avg(gongji) from MOSHOU.sanguo);
2.找出每个国家攻击力最高的英雄名字和攻击值
select country,name,gongji from sanguo
where gongji in(
select max(gongji) from sanguo
group by country);
6.多表查询
1.两种方式
1.select 字段名列表 from 表1,表2;笛卡尔积(不加where)
2.select 字段名列表 from 表1,表2 where 条件;
1.显示省 市 详细信息
select sheng.s_name,city.c_name,xian.x_name from sheng,city,xian
where sheng.s_id = city.cfather_id and city.c_id = xian.xfather_id;
7.连接查询
1.内连接
1.语法格式
select ... from 表1 inner join 表2 on 条件;
2.示例
1.显示 省 市 详细信息
select sheng.s_name,city.c_name from sheng
inner join city on sheng.s_id = city.cfather_id;
2.显示省 市 县详细信息
select sheng.s_name,city.c_name,xian.x_name from
sheng inner join city on sheng.s_id = city.cfather_id
inner join xian on city.c_id = xian.xfather_id;
2.外连接
1.左连接
1.以左表为主显示查询结果
2.语法格式
select ... from 表1 left join 表2 on 条件 ;
3.示例
1.显示 省 市 详细信息,要求省全部显示
select sheng.s_name,city.c_name from sheng
left join city
on sheng.s_id=city.cfather_id;
2.显示 省 市 县 详细信息,要求省全部信息
select sheng.s_name,city.c_name,xian.x_name from sheng
left join city
on sheng.s_id = city.cfather_id
left join xian
on city.c_id = xian.xfather_id;
3.显示 省 市 县 详细信息,要求市全部信息
select sheng.s_name,city.c_name,xian.x_name from sheng
right join city
on sheng.s_id = city.cfather_id
left join xian
on city.c_id = xian.xfather_id;
2.右连接
作业:
1.把 /etc/passwd 文件中内容导入到 db3 库下的 userinfo表
tarena:x:1000:1000:
Day03作业:
综述:两张表,一张顾客信息表customers,一张订单表orders
1、创建一张顾客信息表customers,字段要求如下:
c_id 类型为整型,设置为主键,并设置为自增长属性
c_name 字符类型,变长,宽度为20
c_age 微小整型,取值范围为0~255(无符号)
c_sex 枚举类型,要求只能在('M','F')中选择一个值
c_city 字符类型,变长,宽度为20
c_salary 浮点类型,要求整数部分最大为10位,小数部分为2位
在表中任意插入3条记录,c_name为"Zhangsan","Lisi","Wangwu", c_city尽量 写"Beijing","Shanghai" ......
create table customers(
c_id int primary key auto_increment,
c_name varchar(20),
c_age tinyint unsigned,
c_sex enum('M','F'),
c_city varchar(20),
c_salary float(12,2)
);
insert into customers values(1,"Zhangsan",18,"M","Beijing",2000.55),
(2,"Lisi",20,"F","Shanghai",3900),
(3,"Wangwu",30,"M","Guangzou",8000);
2、创建一张订单表orders,字段要求如下:
o_id 整型
o_name 字符类型,变长,宽度为30
o_price 浮点类型,整数最大为10位,小数部分为2位
设置此表中的o_id字段为customers表中c_id字段的外键,更新删除同步
在表中任意插入5条记录(注意外键限制)
o_name分别为"iphone","ipad","iwatch","mate9","r11",其他信息自己定
create table orders(o_id int
o_name varchar(30),
o_price float(12,2),
foreign key(o_id) references customers(c_id)
on delete cascade
on update cascade);
insert into orders values(1,'iphone',49.5),
(2,'ipad',2000),
(3,'iwatch',300),
(4,'mate9',4000),
(5,'r11',4020.5);
3、返回customers表中,工资大于4000元,或者年龄小于29岁,满足这样条件的前2条记录
select * from customers where salary > 4000 or age < 29;
4、把customers表中,年龄大于等于25岁,并且地址是北京或者上海,这样的人的工资上调15%
5、把customers表中,城市为北京的顾客,按照工资降序排列,并且只返回结果中的第一条记录
6、选择工资salary最少的顾客的信息
7、找到工资大于5000的顾客都买过哪些产品的记录明细
8、删除外键限制
9、删除customers主键限制