MySQL-day07
CREATE TABLE`test_table`(
`ID`bigint(20) NOT NULL AUTO_INCREMENT COMMENT'主键',
`DOMAIN_CODE`varchar(20) NOT NULL COMMENT'考试单位编号',
`EXAM_NAME`varchar(300) NOT NULL COMMENT'考试名称',
`EXAM_TYPE`int(1) NOT NULL COMMENT'考试类型(正式考试,补考)',
`TARGET_EXAM_ID`bigint(20) DEFAULT NULL COMMENT'关联正式考试的ID(如果是补考,该处是必填)',
`EXAM_PICTURE_PATH`varchar(100) DEFAULT NULL COMMENT'图示路径',
`EXAM_BEGIN_TIME`timestamp NOT NULL DEFAULT NOW() COMMENT'考试开始时间',
`EXAM_END_TIME`timestamp NOT NULL DEFAULT'0000-00-00 00:00:00'COMMENT'考试结束时间',
`EXAM_TIME`int(3) NOT NULL COMMENT'考试时长',
`EXAM_NEED_SCORE`int(5) NOT NULL COMMENT'考试所需积分',
`EXAM_PAPER_TYPE`int(1) DEFAULT NULL COMMENT'考试试卷类型(0固定、1随机)',
`EXAM_SCORE`double(6,2) DEFAULT NULL COMMENT'考试总分(关联试卷后回填)',
`EXAM_PASS_SCORE`double(6,2) NOT NULL COMMENT'考试及格分',
`EXAM_COMMIT_NUM`int(2) NOT NULL COMMENT'参考最大次数',
`EXAM_STATUS`int(1) NOT NULL COMMENT'发布状态0未发布,1已发布',
`EXAM_YEAR`varchar(5) NOT NULL COMMENT'年份',
`EXAM_PAPER_ID`bigint(20) DEFAULT NULL COMMENT'关联试卷ID',
`EXAM_DISCRIPTION`varchar(1000) DEFAULT NULL COMMENT'考试备注',
`OPERATOR_USER_ACCOUNT`varchar(20) NOT NULL COMMENT'修改人',
`OPERATOR_TIME`timestamp NOT NULL DEFAULT'0000-00-00 00:00:00'COMMENT'修改时间',
`TARGET_DOMAIN_CODE`varchar(20) DEFAULT NULL COMMENT'发布目标单位编号(发布时回填)',
`RANK`varchar(100) DEFAULT NULL COMMENT'职务级别(发布时回填)',
`EXAM_DIPLOMA_ID`bigint(20) DEFAULT NULL COMMENT'关联证书',
`DIPLOMA_NAME`varchar(200) DEFAULT NULL COMMENT'证书标题(关联证书后回填',
`DIPLOMA_PICTURE_PATH`varchar(200) DEFAULT NULL COMMENT'证书背景图片保存位置(关联证书后回填)',
`INDUSTRY_CODES`varchar(1000) DEFAULT NULL,
`LANGUAGE`int(2) NOT NULL DEFAULT'1'COMMENT'试言(0:全部,1:汉试,2:维试,3:è’试,4:哈试)',
`EXT1`int(1) NOT NULL DEFAULT'1'COMMENT'成绩计入学分的字段标识(0 是,1否)',
`EXT2`int(3) DEFAULT NULL COMMENT'成绩所占比例',
`EXT3`varchar(1) DEFAULT NULL,
`EXT4`varchar(1) DEFAULT NULL,
`EXT5`varchar(1) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY`DOMAIN_CODE`(`DOMAIN_CODE`),
KEY`EXAM_PAPER_ID`(`EXAM_PAPER_ID`)
)ENGINE=InnoDBAUTO_INCREMENT=365DEFAULTCHARSET=utf8;
3.自连接
#世界上小于100人的人口城市是哪个国家的?
city.countrycode=country.code
#世界上小于100人的城市说的什么语言?
城市名,语言,人口数量
city.name,countrylanguage.language,city.population
NATURAL JOIN
select city.name,countrylanguage.language,city.population
from city natural join countrylanguage
where city.population <100;
#前提条件:两个表中必须有相同的列名字,并且数据一致
4.外连接
#左外连接
select city.name,city.countrycode,country.name
from city left join country
on city.countrycode=country.code
and city.population<100;
mysql> select city.name,city.countrycode,country.name from city left join country on city.countrycode=country.code and city.population<100 limit10;
+----------------+-------------+------+
| name | countrycode | name |
+----------------+-------------+------+
| Kabul | AFG | NULL |
| Qandahar | AFG | NULL |
| Herat | AFG | NULL |
| Mazar-e-Sharif | AFG | NULL |
| Amsterdam | NLD | NULL |
| Rotterdam | NLD | NULL |
| Haag | NLD | NULL |
| Utrecht | NLD | NULL |
| Eindhoven | NLD | NULL |
| Tilburg | NLD | NULL |
+----------------+-------------+------+
mysql> select city.name,city.countrycode,country.name from city right join country on city.countrycode=country.code andd city.population<100 limit10;
+------+-------------+----------------------+
| name | countrycode | name |
+------+-------------+----------------------+
| NULL | NULL | Aruba |
| NULL | NULL | Afghanistan |
| NULL | NULL | Angola |
| NULL | NULL | Anguilla |
| NULL | NULL | Albania |
| NULL | NULL | Andorra |
| NULL | NULL | Netherlands Antilles |
| NULL | NULL | United Arab Emirates |
| NULL | NULL | Argentina |
| NULL | NULL | Armenia |
+------+-------------+----------------------+
mysql> select city.name,country.code,country.name from city right join country on city.countrycode=country.code and city.population<100 limit10;
+------+------+----------------------+
| name | code | name |
+------+------+----------------------+
| NULL | ABW | Aruba |
| NULL | AFG | Afghanistan |
| NULL | AGO | Angola |
| NULL | AIA | Anguilla |
| NULL | ALB | Albania |
| NULL | AND | Andorra |
| NULL | ANT | Netherlands Antilles |
| NULL | ARE | United Arab Emirates |
| NULL | ARG | Argentina |
| NULL | ARM | Armenia |
+------+------+----------------------+
字符集校验规则
1)ci:大小写不敏感2)cs或bin:大小写敏感
统一字符集
工具修改字符集
系统修改字符集
#CentOS6:
[root@db01 ~]# vim /etc/sysconfig/i18n
#CentOS7:
[root@db01 ~]# vim /etc/locale.conf
LANG="en_US.UTF-8"
数据库修改字符集
#库级别的字符集
mysql> create database oldboy charset utf8;
#表级别的字符集
mysql> create table mysql.oldboy(id int)charset utf8;
CREATE TABLE`student5`(
`id`int(11) NOT NULL AUTO_INCREMENT COMMENT'学生学号',
`name`varchar(20) NOT NULL COMMENT'学生姓名',
`age`tinyint(3) unsigned NOT NULL COMMENT'学生年龄',
`gender`enum('f','m') DEFAULT NULL COMMENT'学生性别',
`cometime`datetime DEFAULT CURRENT_TIMESTAMP,
`state`enum('0','1') DEFAULT'1',
PRIMARY KEY (`id`));
#修改表的字符集
mysql> show create table stu3;
索引管理
索引的类型(算法)
BTREE:B树索引(Btree,B+tree,B*tree)
HASH:HASH索引
FULLTEXT:全文索引
RTREE:R树索引
Btree算法
B+tree算法
1.在叶子节点,添加了相邻节点的指针
2.优化了,范围查询
索引分类
主键索引(聚集索引)
普通索引
联合索引
前缀索引
唯一索引(唯一键)
主键:唯一 、 非空 primary key
唯一键:唯一 、 可以为空 unique key
primary key = unique key + not null
索引创建规则
1.一个字段可以创建一个索引
2.多个字段可以创建一个索引
3.多个字段可以创建多个索引,但是不能是主键
索引操作
#创建主键索引 primary key
mysql> alter table student2 add primary key pri(name);
#创建普通索引 index
mysql> alter table student2 add index idx_name(name);
#创建唯一索引 unique key
mysql> alter table student2 add unique key uni_age(age);
#如何判断,某列是否可以创建唯一索引
distinct()
count()
mysql> select count(name) from student4;
+-------------+
| count(name) |
+-------------+
|9|
+-------------+
1rowinset(0.00 sec)
mysql> select count(distinct(name)) from student4;
+-----------------------+
| count(distinct(name)) |
+-----------------------+
|2|
+-----------------------+
mysql> select count(name) from country;
+-------------+
| count(name) |
+-------------+
|239|
+-------------+
1rowinset(0.00 sec)
mysql> select count(distinct(name)) from country;
+-----------------------+
| count(distinct(name)) |
+-----------------------+
|239|
+-----------------------+
1rowinset(0.00 sec)
mysql> alter table country add unique key uni_name(name);
#查看索引
mysql> show index from student2;
mysql> desc student2;
mysql> show create table student2;
#删除索引
mysql> alter table student2 drop index idx_all;
mysql> alter table student2 drop index uni_age;
前缀索引
给表中数据量大的列,创建前缀索引
#创建前缀索引
mysql> alter table student2 add index idx_name(name(3));
1.避免对大列建索引2.如果有,就使用前缀索引
联合索引
性别
长相
身材
收入
年龄
爱好
...
id 名字 性别 长相 身材 身高 QQ 微信 手机 收入
mysql> create table xiangqin(id int,name varchar(10),gender enum('0','1'),face varchar(10),height int,weight int,salary int,hobby varchar(10),QQ varchar(11));
mysql> alter table xiangqin add index idx_all(gender,salary,face,weight);
联合索引,走索引情况
A:gender B:salary C:face D:weight
全部走索引:
select * from xiangqin where A;
select * from xiangqin where A B;
select * from xiangqin where A B C;
select * from xiangqin where A B C D;
部分走索引:
select * from xiangqin where A C D;
select * from xiangqin where A B D;
select * from xiangqin where A D;
全不走索引
select * from xiangqin where B C D;
select * from xiangqin where C D;
select * from xiangqin where D;