create table student
(
id int(4) not null,
name char(20) not null,
age tinyint(2) not null,
dept varchar(16) not null
);
=====================联表查询====================
create table student(
Sno int(10) not null comment ‘学号’,
Sname varchar(16) not null comment ‘姓名’,
Ssex char(6) not null comment ‘性别’,
Sage tinyint(2) not null default ‘0’ comment ‘学生年龄’,
Sdept varchar(16) default null comment ‘学生所在系别’,
primary key (Sno),
key index_Sname(Sname)
) engine=innodb auto_increment=1 default charset=utf8;
create table course(
Cno int(10) not null comment ‘课程号’,
Cname varchar(64) not null comment ‘课程名’,
Ccredit tinyint(2) not null comment ‘学分’,
primary key (Cno)
) engine=innodb auto_increment=1 default charset=utf8;
create table SC(
SCid int(12) not null auto_increment comment ‘主键’,
Cno int(10) not null comment ‘课程号’,
Sno int(10) not null comment ‘学号’,
Grade tinyint(2) not null comment ‘学生成绩’,
primary key (SCid)
) engine=innodb default charset=utf8;
随便插入学生数据
insert into student values(0001,’hognzhi’,’male’,30,’jisuanji’),(0002,’wammg’,’male’,30,’jisuanji’) ,(0003,’oldboy’,’male’,28,’wuliu’) ,(0004,’ts’,’male’,29,’jisuanji’) ,(0005,’oldgirl’,’female’,30,’jisuanji’) ,(0006,’yuany’,’female’,22,’nurse’);
insert into course values(1001,’Linuxyunwei’,3),(1002,’Linuxyunwei’,5),(1003,’Linuxyunwei’,4),(1004,’Linuxyunwei’,4),(1001,’Linuxyunwei’,3);
insert into SC(Sno,Cno,Grade) values(0001,1001,4);
insert into SC(Sno,Cno,Grade) values(0001,1002,3);
insert into SC(Sno,Cno,Grade) values(0001,1003,1);
insert into SC(Sno,Cno,Grade) values(0001,1003,6);
insert into SC(Sno,Cno,Grade) values(0002,1001,3);
insert into SC(Sno,Cno,Grade) values(0002,1002,2);
insert into SC(Sno,Cno,Grade) values(0002,1003,2);
insert into SC(Sno,Cno,Grade) values(0002,1003,8);
insert into SC(Sno,Cno,Grade) values(0003,1001,4);
insert into SC(Sno,Cno,Grade) values(0003,1002,4);
insert into SC(Sno,Cno,Grade) values(0003,1003,2);
insert into SC(Sno,Cno,Grade) values(0003,1003,8);
insert into SC(Sno,Cno,Grade) values(0004,1001,1);
insert into SC(Sno,Cno,Grade) values(0004,1002,1);
insert into SC(Sno,Cno,Grade) values(0004,1003,2);
insert into SC(Sno,Cno,Grade) values(0004,1003,3);
insert into SC(Sno,Cno,Grade) values(0005,1001,5);
insert into SC(Sno,Cno,Grade) values(0005,1002,3);
insert into SC(Sno,Cno,Grade) values(0005,1003,2);
insert into SC(Sno,Cno,Grade) values(0005,1003,9);
联表查询
select student.Sno,student.Sname,course.Cname,SC.Grade from student, course, SC where student.Sno=SC.Sno and course.Cno=SC.Cno;
select student.Sno,student.Sname,course.Cname,SC.Grade from student, course, SC where student.Sno=SC.Sno and course.Cno=SC.Cno order by Sno;
在where后面建索引
explain 查询select 查询语句执行计划
就是查询这个语句,能不能走索引?
use oldboy;
drop table test;
create table test(
id int(4) not null auto_increment,
name char(20) not null,
primary key (id)
);
insert into test values(1,’oldboy’),(2,’oldgir’),(3,’inca’),(4,’zuma’),(5,’kaka’);
select * from test where name=’oldboy’;
explain select * from test where name=’oldboy’;
explain select * from test where name=’oldboy’\G;
create index index_name on test(name);
建索引和没有建索引的区别
mysql> explain select * from test where name=’oldboy’\G
* 1. row *
id: 1
select_type: SIMPLE
table: test
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 5
Extra: Using where
1 row in set (0.00 sec)
mysql> create index index_name on test(name);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain select * from test where name=’oldboy’\G
* 1. row *
id: 1
select_type: SIMPLE
table: test
type: ref
possible_keys: index_name
key: index_name
key_len: 60
ref: const
rows: 1
Extra: Using where; Using index
1 row in set (0.00 sec)