createtable user6( id int, name varchar(20), unique(name) ); createtable user7( id int, name varchar(20) unique ); createtable user8( id int, name varchar(20), unique(id,name) );
–两个键在一起不重复就OK
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Query OK, 0rows affected (0.08 sec) mysql>desc user8; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ | id |int(11) | YES | MUL |NULL|| | name |varchar(20) | YES ||NULL|| +-------+-------------+------+-----+---------+-------+ 2rowsinset (0.00 sec) mysql>insertinto user8 values(1,'zhangsan'); Query OK, 1row affected (0.02 sec) mysql>insertinto user8 values(2,'zhangsan'); Query OK, 1row affected (0.02 sec)
–如何删除唯一约束
1 2 3 4 5 6 7 8 9 10 11 12
altertable user7 drop index name; Query OK, 0rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql>desc user7; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ | id |int(11) | YES ||NULL|| | name |varchar(20) | YES ||NULL|| +-------+-------------+------+-----+---------+-------+ 2rowsinset (0.00 sec)
–modify添加
1 2 3 4 5 6 7 8 9 10 11 12
altertable user7 modify name varchar(20) unique; Query OK, 0rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql>desc user7; +-------+-------------+------+-----+---------+-------+ | Field | Type |Null| Key |Default| Extra | +-------+-------------+------+-----+---------+-------+ | id |int(11) | YES ||NULL|| | name |varchar(20) | YES | UNI |NULL|| +-------+-------------+------+-----+---------+-------+ 2rowsinset (0.00 sec)
createtable myorder( order_id primary key, product_id int, customer_id int ); createtable product( id int, name varchar(20) ); createtable customer( id intprimary key, name varchar(20) );
–分成三个表之后完全依赖 满足第二范式
–第三范式
–3NF
–必须先满足第二范式,除开主键列的其他列之间不能有传递关系
1 2 3 4 5 6 7 8 9 10 11
createtable myorder( order_id primary key, product_id int, customer_id int ); createtable customer( id intprimary key, name varchar(20) phone varchar(15) );
1.查询student表中所有记录 select*from student; 2.查询student表中所有记录的sname 、ssex、class、 select sname,ssex,class from student; 3.查询教师所有的单位即不重复的depart列 distinct 排除重复 selectdistinct depart from teacher; 4.查询score表中成绩在60到80之间的记录 查询区间 between…and… select*from score where degree between60and80; 或者 selet *from score where degree >60and degree <80; 5.查询score表中成绩为85,86或88 的记录 表示或者关系的查询 in select*from score where degree in (85,86,88); 6.查询student表中“95031”班或性别为“女”的同学的记录 or 表示或者 select*from student where class='95031'or ssex='女'; 7.以class降序查询student表的所有记录 升序asc,降序desc select*from studnt orderby class desc; 默认升序 select*from studnt orderby class (asc); 8.以cno升序、degree降序查询score表中的所有记录 select*from student score oder by cno asc,degree desc; 9.查询“95031”班 的学生人数 统计 count selectcount(*) from student where class='95031'; 10.查询score表中的最高分的学生学号和课程号(子查询或排序) select sno,cno from score where degree=(selectmax(degree) from score); 1、找到最高分 selectmax(degree) from score; 2、找最高分的sno 和cno select sno,cno from score where degree=(selectmax(degree) from score); 排序的做法 select sno,cno from score oder by degree; select sno,cno from score oder by degree desc limit 0,1; limit a,b a=从第几条开始查 b=查多少条 11.查询每门课的平均成绩 分开去找 select*from course; avg() selectavg(degree) from score where cno-'3-105'; select degree from score where cno ='3-105'; 在一个sql中写 groupby 分组 select cno,avg(degree) from score groupby cno; 12.查询score表中至少有两名学生选修的并以3开头的课程的平均数 select cno,avg(degree) from score groupby cno havingcount(cno)>=2 and cno like'3%'; 13.查询分数大于70,小于90的sno列 select sno ,dgree from score where degree >70and degree <90; 或者 select sno ,dgree from score where degree between70and90; 14.查询所有学生的sname cno 和degree列 select sname from student; select cno,degree from score; 多表查询 select sname,cno,degree from student,score where student.sno=score.sno; 15.查询所有学生的sno cname 和degree 列 select sno,cname,degree from course,score where course.cno=score.cno; 16.查询所有学生的sname cname和degree 列 sname->student cname->course degree->score select sname,cname,degree from student,course,score where student.sno=score.sno and course.cno=score.cno; 17.查询“95031”班学生每门课的平均成绩 分步思路 select*from student where class ='95031'; select sno from student where class='95031'; select*from score where sno in(select*from student where class ='95031'); 合并 select cno,avg(degree) from score where sno in (select sno from student where class='95031') groupby cno; 18.查询选修“3-105”课程的成绩高于“109”号同学“3-105”成绩的所有同学的记录 select degree from score where sno='109'and cno='3-105'; select*from score where cno='3-105'and degree>(select degree from score where sno='109'and cno='3-105'); 19.查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录 select*from score whereand degree>(select degree from score where sno='109'and cno='3-105'); 20.查询和学号为108、101的同学同年出生的所有学生的sno、sname和sbirthday列 selectyear(sbirthday) from student where sno in (108,101); select*from student whereyear(sbirthday) in (selectyear(sbirthday) from student where sno in (108,101)); 21.查询“张旭”教师任课的学生成绩 select*from teacher where tname='张旭'; select tno from teacher where tname='张旭'; select*from course where tno=(select tno from teacher where tname='张旭'); select cno from course where tno=(select tno from teacher where tname='张旭'); select*from score where cno=(select cno from course where tno=(select tno from teacher where tname='张旭')); 22.查询选修某课程的同学人数多于5人的教师姓名 select cno from score groupby cno havingcount(*)>5; select*from teacher; select tno from course where cno=(select cno from score groupby cno havingcount(*)>5); select tname from teacher where tno=(select tno from course where cno=(select cno from score groupby cno havingcount(*)>5)); 23.查询95033班和95031班全体学生的记录 select*from student; select*from student where calss in ('95031','95033'); 24.查询存在有85分以上成绩的课程Cno select cno from score where degree >85; 25.查询出“计算机系”教师所教课程的成绩表 select*from teacher where depart='计算机系'; select*from course where tno in(select tno from teacher where depart='计算机系'); select*from score where cno in(select cno from course where tno in(select tno from teacher where depart='计算机系')); 26.查询“计算机系”与“电子工程系”不同职称的教师的tname和prof union 求并集 select*from teacher ; select prof from teacher where depart ='电子工程系'; select*from teacher where depart ='计算机系'and prof notin(select prof from teacher where depart ='电子工程系') ; select*from teacher where depart ='电子工程系'and prof notin(select prof from teacher where depart ='计算机系') ; select*from teacher where depart ='计算机系'and prof notin(select prof from teacher where depart ='电子工程系') unionselect*from teacher where depart ='电子工程系'and prof notin(select prof from teacher where depart ='计算机系') ; 27.查询选修编号为“3-105”课程且成绩至少高于选修编号为“3-245”的同学的cno 、sno和degree,并按照degree从高到低的次序排序 select*from score where cno='3-245'; select*from score where cno='3-105';
至少:大于其中至少一个,**any
1 2 3 4 5 6 7 8 9
select*from score where cno='3-105' and degree>any(select degree from score where cno='3-245') orderby degree desc; 28.查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的cno、sno和degree select*from score where cno='3-105' and degree>all(select degree from score where cno='3-245');
29.查询所有教师和同学的name、sex和birthday 别名 as select tname as name,tsex as sex,tbirthday as birthday from teacher union select sname,ssex,sbirthday from student; 30.查询所有“女”教师和“女”同学的name、sex、birthday select tname as name,tsex as sex,tbirthday as birthday from teacher where tsex='女' union select sname,ssex,sbirthday from student where ssex='女'; 31.查询成绩比该课程平均成绩低的同学的成绩表 select cno,avg(degree) from score groupby cno; select*from score; select*from score a where degree < (selectavg(degree) from score b where a.cno=b.cno); 32.查询所有任课教师的tname和depart select*from teacher;
select*from course; select*from teacher where tno in(select tno from course); select tname,depart from teacher where tno in(select tno from course); 33.查询至少有两名男生的班号 select*from student; select class from student where ssex ='男'groupby class havingcount(*)>1; 34.查询student表中不姓“王”的同学的记录 select*from student; select*from student where sname notlike'王%'; 35.查询student表中每个学生的姓名和年龄 年龄:当前年份-出生年龄 当前年龄: selectyear (now()); selectyear(sbirthday) from student; select sname,year(now())-year(sbirthday) as'年龄'from student; 36.查询student表中最大和最小的sbirthday日期值 select sbirthday from student; select sbirthday from student orderby sbirthday;
selectmax(sbirthday) as'最大',min(sbirthday) as'最小'from student; 37.以班号和年龄从大到小的顺序查询student表中的全部记录 select*from student orderby class sedc,sbirthday; 38.查询“男”教师及其所上的课程 select*from teacher where tsex='男'; select*from course where tno in(select tno from teacher where tsex='男'); 39.查询最高分同学的sno、cno和degree列 selectmax(degree) from score; select*from score where degree=(selectmax(degree) from score); 40.查询和“李军”同性别的所有同学的sname; select ssex from student where sname='李军'; select sname from student where ssex=(select ssex from student where sname='李军'); 41.查询和李军同性别并且同班的同学sname select sname from student where ssex=(select ssex from score where degree=(selectmax(degree) from score)) and class=(select class from student where sname='李军'); 42.查询所有选修“计算机导论”课程的“男”同学的成绩表 select*from student where ssex='男'; select*from course where cname='计算机导论'; select*from score where cno=(select cno from course where cname='计算机导论') andwhere sno in(select sno from student where ssex='男'); 43.略
mysql>select*fromuser; +----+------+-------+ | id | name | money | +----+------+-------+ |1| a |900| |2| b |1100| +----+------+-------+ 2rowsinset (0.00 sec)