mysql> select * from teachers;
| tno | tname | tsex | tbirthday | prof | depart |
| 804 | 李诚 | 男 | 1958-12-02 00:00:00 | 副教授 | 计算机系 |
| 856 | 张旭 | 男 | 1969-03-12 00:00:00 | 讲师 | 电子工程系 |
| 825 | 王萍 | 女 | 1972-05-05 00:00:00 | 助教 | 计算机系 |
| 831 | 刘冰 | 女 | 1977-08-14 00:00:00 | 助教 | 电子工程系 |
4 rows in set (0.00 sec)
mysql> select * from students;
| sno | sname | ssex | sbirthday | class |
| 108 | 曾华 | 男 | 1977-09-01 00:00:00 | 95033 |
| 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 107 | 王丽 | 女 | 1976-01-23 00:00:00 | 95033 |
| 101 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 |
| 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
| 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
6 rows in set (0.00 sec)
mysql> select * from scores;
| sno | cno | degree |
| 103 | 3-245 | 86.0 |
| 105 | 3-245 | 75.0 |
| 109 | 3-245 | 68.0 |
| 103 | 3-105 | 92.0 |
| 105 | 3-105 | 88.0 |
| 109 | 3-105 | 76.0 |
| 101 | 3-105 | 64.0 |
| 107 | 3-105 | 91.0 |
| 108 | 3-105 | 78.0 |
| 101 | 6-166 | 85.0 |
| 107 | 6-106 | 79.0 |
| 108 | 6-166 | 81.0 |
12 rows in set (0.00 sec)
mysql> select * from courses;
| cno | cname | tno |
| 3-105 | 计算机导论 | 825 |
| 3-245 | 操作系统 | 804 |
| 6-166 | 数据电路 | 856 |
| 9-888 | 高等数学 | 100 |
4 rows in set (0.00 sec)
-- -- Table structure for table `courses` -- DROP TABLE IF EXISTS `courses`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `courses` ( `cno` varchar(5) NOT NULL, `cname` varchar(10) NOT NULL, `tno` varchar(10) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `courses` -- LOCK TABLES `courses` WRITE; /*!40000 ALTER TABLE `courses` DISABLE KEYS */; INSERT INTO `courses` VALUES ('3-105','计算机导论','825'),('3-245','操作系统','804'),('6-166','数据电路','856'),('9-888','高等数学','100'); /*!40000 ALTER TABLE `courses` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `scores` -- DROP TABLE IF EXISTS `scores`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `scores` ( `sno` varchar(3) NOT NULL, `cno` varchar(5) NOT NULL, `degree` decimal(10,1) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `scores` -- LOCK TABLES `scores` WRITE; /*!40000 ALTER TABLE `scores` DISABLE KEYS */; INSERT INTO `scores` VALUES ('103','3-245',86.0),('105','3-245',75.0),('109','3-245',68.0),('103','3-105',92.0),('105','3-105',88.0),('109','3-105',76.0),('101','3-105',64.0),('107','3-105',91.0),('108','3-105',78.0),('101','6-166',85.0),('107','6-106',79.0),('108','6-166',81.0); /*!40000 ALTER TABLE `scores` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `students` -- DROP TABLE IF EXISTS `students`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `students` ( `sno` varchar(3) NOT NULL, `sname` varchar(4) NOT NULL, `ssex` varchar(2) NOT NULL, `sbirthday` datetime DEFAULT NULL, `class` varchar(5) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `students` -- LOCK TABLES `students` WRITE; /*!40000 ALTER TABLE `students` DISABLE KEYS */; INSERT INTO `students` VALUES ('108','曾华','男','1977-09-01 00:00:00','95033'),('105','匡明','男','1975-10-02 00:00:00','95031'),('107','王丽','女','1976-01-23 00:00:00','95033'),('101','李军','男','1976-02-20 00:00:00','95033'),('109','王芳','女','1975-02-10 00:00:00','95031'),('103','陆君','男','1974-06-03 00:00:00','95031'); /*!40000 ALTER TABLE `students` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `teachers` -- DROP TABLE IF EXISTS `teachers`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `teachers` ( `tno` varchar(3) NOT NULL, `tname` varchar(4) NOT NULL, `tsex` varchar(2) NOT NULL, `tbirthday` datetime NOT NULL, `prof` varchar(6) DEFAULT NULL, `depart` varchar(10) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `teachers` -- LOCK TABLES `teachers` WRITE; /*!40000 ALTER TABLE `teachers` DISABLE KEYS */; INSERT INTO `teachers` VALUES ('804','李诚','男','1958-12-02 00:00:00','副教授','计算机系'),('856','张旭','男','1969-03-12 00:00:00','讲师','电子工程系'),('825','王萍','女','1972-05-05 00:00:00','助教','计算机系'),('831','刘冰','女','1977-08-14 00:00:00','助教','电子工程系'); /*!40000 ALTER TABLE `teachers` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
整形:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
x | tinyint(4) | YES | NULL | ||
y | smallint(6) | YES | NULL | ||
z | mediumint(9) | YES | NULL | ||
m | int(11) | YES | NULL | ||
n | bigint(20) | YES | NULL |
浮点形与定点形:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
x | float(5,1) | YES | NULL | ||
y | double(5,1) | YES | NULL | ||
z | decimal(5,1) | YES | NULL |
日期与时间类型:
1998-08-08 08:08:08 | 1998-08-08 | 时间 | 10:05:05 | 2018 |
---|---|---|---|---|
datetime | date | timestamp | time | year |
字符串类型:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
x | char(4) | YES | NULL | ||
y | varchar(4) | YES | NULL | ||
z | binary(4) | YES | NULL | ||
m | varbinary(4) | YES | NULL | ||
n | blob | YES | NULL | ||
n1 | text | YES | NULL | ||
n2 | enum(‘first’,’second’) | YES | NULL | ||
n3 | set(‘a’,’b’,’c’) | YES | NULL |
新建用户用作远程登录
grant all on . to username@’%’ identified by 123;
flush privileges;//刷新权限
mysql -h 172.16.72.195 -u username -p passwdcreate database mydata; //创建数据库
drop database mydata; //删除数据库
mysqldump -u root -p phpmyadmin>php.sql//备份数据库
或者如下:
show database;//查看数据库
use databasename;//选择数据库
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)//新建表
create table tab_new like tab_old//旧表新用
show tables;//查看数据库的表
desc tablename;//查看具体的表
drop table tabname;//删除表
alter table courses add n6 varchar(10);//添加列
alter table courses drop n6;//删除列
alter table courses modify n6 int(10);//修改列
alter table students add primary key(sno);//添加主键
alter table students drop primary key;// 删除主键
alter table scores add foreign key(sno) references students(sno);//添加外键(子父的字段类型相同,父字段为主键)
show create table scores;//查看构建的代码
alter table scores drop foreign key scores_ibfk_1;//删除外键
alter table scores drop KEY sno ;//也不知为啥遗留了KEY,删了
4.index
create index myindex on students(sname,sno);//创建索引
drop index myindex on students;//删除索引
ALTER TABLE用来创建普通索引、UNIQUE索引或PRIMARY KEY索引。//创建
ALTER TABLE table_name ADD INDEX index_name (column_list)
ALTER TABLE table_name ADD UNIQUE (column_list)
ALTER TABLE table_name ADD PRIMARY KEY (column_list)
CREATE INDEX index_name ON table_name (column_list)
CREATE UNIQUE INDEX index_name ON table_name (column_list)
DROP INDEX index_name ON talbe_name//删除
ALTER TABLE table_name DROP INDEX index_name
ALTER TABLE table_name DROP PRIMARY KEY
show index from tblname;//查看
show keys from tblname;
create view myview as select * from students;
select * from myview;
alter view myview sa select * from courses;
drop view myview;
Insert into 表名(x1,x2,x3) values(x1,x2,x3)
Delete from 表名 Where
Update 表名 Set x=xx Where
Where 里面的用词 (比较运算)>、=、<、<=、>=、<>(!=) (逻辑+is null)and/or/not/is null (设范围+in)between and/in (匹配)like 聚合查询 函数有sum count avg只有在having里面使用 Where是对元组进行过滤,having对分组进行过滤 集合运算 Union except intersect(SQLserver不支持) 存在量词 Exists
选择:select * from table1 where 范围
插入:insert into table1(field1,field2) values(value1,value2)
删除:delete from table1 where 范围
更新:update table1 set field1=value1 where 范围
查找:select * from table1 where field1 like ’%value1%’ ―like的语法很精妙,查资料!
排序:select * from table1 order by field1,field2 [desc]
总数:select count as totalcount from table1
求和:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1
A: UNION 运算符(合并select ,select的结果和顺序必须相同)
UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2),不加all会消去重复。
例子:
select * from myview where sno in (105,103) union select * from myview where sno in (109,101,103);
| sno | sname | ssex | sbirthday | class |
| 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
| 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
| 101 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 |
select * from myview where sno in (105,103) union all select * from myview where sno in (109,101,103);
| sno | sname | ssex | sbirthday | class |
| 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
| 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 103 | 陆君 | 男 | 1974-06-03 00:00:00 | 95031 |
| 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
| 101 | 李军 | 男 | 1976-02-20 00:00:00 | 95033 |
EXCEPT (相减,select11中不出现select2中有的东西)
INTERSECT 运算符(相交,都有)
注:使用运算词的几个查询结果行必须是一致的。
left (outer) join:
左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。
mysql> select * from view1;
| sname | cname | degree |
| 匡明 | 操作系统 | 75.0 |
| 匡明 | 计算机导论 | 88.0 |
| 曾华 | 计算机导论 | 78.0 |
| 曾华 | 数据电路 | 81.0 |
| 李军 | 计算机导论 | 64.0 |
| 李军 | 数据电路 | 85.0 |
| 王丽 | 计算机导论 | 91.0 |
| 王芳 | 操作系统 | 68.0 |
| 王芳 | 计算机导论 | 76.0 |
| 陆君 | 操作系统 | 86.0 |
| 陆君 | 计算机导论 | 92.0 |
11 rows in set (0.00 sec)
mysql> select * from view2;
| sname | class |
| 陆君 | 95031 |
| 陆君 | 95031 |
| 匡明 | 95031 |
| 匡明 | 95031 |
| 王芳 | 95031 |
| 王芳 | 95031 |
| 李军 | 95033 |
| 李军 | 95033 |
| 王丽 | 95033 |
| 曾华 | 95033 |
| 曾华 | 95033 |
11 rows in set (0.00 sec)
select view1.* ,view2.class from view1 left join view2 on view1.sname=view2.sname;
| sname | cname | degree | class |
| 匡明 | 操作系统 | 75.0 | 95031 |
| 匡明 | 操作系统 | 75.0 | 95031 |
| 匡明 | 计算机导论 | 88.0 | 95031 |
| 匡明 | 计算机导论 | 88.0 | 95031 |
| 曾华 | 计算机导论 | 78.0 | 95033 |
| 曾华 | 计算机导论 | 78.0 | 95033 |
| 曾华 | 数据电路 | 81.0 | 95033 |
| 曾华 | 数据电路 | 81.0 | 95033 |
| 李军 | 计算机导论 | 64.0 | 95033 |
| 李军 | 计算机导论 | 64.0 | 95033 |
| 李军 | 数据电路 | 85.0 | 95033 |
| 李军 | 数据电路 | 85.0 | 95033 |
| 王丽 | 计算机导论 | 91.0 | 95033 |
| 王芳 | 操作系统 | 68.0 | 95031 |
| 王芳 | 操作系统 | 68.0 | 95031 |
| 王芳 | 计算机导论 | 76.0 | 95031 |
| 王芳 | 计算机导论 | 76.0 | 95031 |
| 陆君 | 操作系统 | 86.0 | 95031 |
| 陆君 | 操作系统 | 86.0 | 95031 |
| 陆君 | 计算机导论 | 92.0 | 95031 |
| 陆君 | 计算机导论 | 92.0 | 95031 |
右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。
select count(*) from view1 right join view2 on view1.sname=view2.sname;
full/cross (outer) join:
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。
select count(*) from view1 full join view2;
group by having:(分组,然后进行计算,但是分组后返回的值只相当于一个字段组,如果select a,b …group by xx将会报错。a,b必须为聚合函数avg,sum,count等,或者为xx,也就是能操作一个组的东西。
组相关的信息:(统计信息) count,sum,max,min,avg 分组的标准
例子:平均成绩大于八十的学生名字,学科,分数
select sname ,cname,degree from view1 where sname in (select sname from view1 group by sname having avg(degree)
sname | cname | degree |
---|---|---|
匡明 | 操作系统 | 75.0 |
匡明 | 计算机导论 | 88.0 |
王丽 | 计算机导论 | 91.0 |
陆君 | 操作系统 | 86.0 |
陆君 | 计算机导论 | 92.0 |
1、 查询Student表中的所有记录的Sname、Ssex和Class列。
select sname, ssex,class from students;
2、 查询教师所有的单位即不重复的Depart列。
select distinct depart from teachers;
3、 查询Student表的所有记录。
select * from students;
4、 查询Score表中成绩在60到80之间的所有记录。
select * from scores where degree between 60 and 80;
5、 查询Score表中成绩为85,86或88的记录。
select * from scores where degree in (85,86,88);
6、 查询Student表中“95031”班或性别为“女”的同学记录。
select * from students where class='95031' or ssex='Ů';
7、 以Class降序查询Student表的所有记录。
select * from students where class='95031' or ssex='Ů';
8、 以Cno升序、Degree降序查询Score表的所有记录。
select * from scores order by cno ,degree desc;
9、 查询“95031”班的学生人数。
select count(*) as studentnum from students where class='95031';
10、查询Score表中的最高分的学生学号和课程号。
select sno,cno from scores order by degree desc limit 1; or select sno,cno from scores where degree = (select max(s.degree) from scores s);
11、查询‘3-105’号课程的平均分。
select avg(degree) from scores where cno='3-105';
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select cno, avg(degree) from scores where cno like '3%' group by cno having count(sno)>=5;
13、查询最低分大于70,最高分小于90的Sno列。
select sno from scores group by sno having min(degree)>70 and max(degree)<90;
14、查询所有学生的Sname、Cno和Degree列。
select a.sname,c.cno,b.degree from students a,scores b,courses c where a.sno=b.sno and b.cno=c.cno;
17、查询“95033”班所选课程的平均分。
select avg(degree) from students a,scores b,courses c where a.sno=b.sno and b.cno=c.cno and class='95033' group by a.class;
18、假设使用如下命令建立了一个grade表:
create table grade(low int(3),upp int(3),rank char(1));
insert into grade values(90,100,’A’);
insert into grade values(80,89,’B’);
insert into grade values(70,79,’C’);
insert into grade values(60,69,’D’);
insert into grade values(0,59,’E’);
commit;
现查询所有同学的Sno、Cno和rank列。
select sno,cno,rank from scores left join grade on (scores.degree>=grade.low and scores.degree<=grade.upp) order by sno desc ,cno desc ; or select sno,cno,rank from scores inner join grade on (scores.degree>=grade.low and scores.degree<=grade.upp) order by sno;
19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
select a.* from scores a left join scores b on(a.cno=b.cno and a.degree>b.degree) where a.cno='3-105' and b.sno='109'; or select a.* from scores a where a.cno='3-105' and a.degree > (select degree from scores where sno='109' and cno='3-105');
20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
select b.* from scores b ,(select cno ,max(degree) de from scores group by cno) a where b.sno in (select sno from scores group by sno having count(degree)>1) and b.cno=a.cno and a.de!=b.degree; or select b.* from scores b left join (select cno ,max(degree) de from scores group by cno) a on (a.cno=b.cno and a.de!=b.degree) where b.sno in (select sno from scores group by sno having count(degree)>1) and a.cno=b.cno;
| sno | cno | degree |
| 105 | 3-245 | 75.0 |
| 109 | 3-245 | 68.0 |
| 105 | 3-105 | 88.0 |
| 109 | 3-105 | 76.0 |
| 101 | 3-105 | 64.0 |
| 107 | 3-105 | 91.0 |
| 108 | 3-105 | 78.0 |
| 108 | 6-166 | 81.0 |
21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select a.* from scores a left join scores b on (a.cno=b.cno and a.degree>b.degree) where a.cno='3-105' and b.sno='109' order by a.sno
22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
select a.sno,a.sname,a.sbirthday from students a left join students b on (year(a.sbirthday)=year(b.sbirthday)) where a.sno='108';
23、查询“张旭“教师任课的学生成绩。
select sno,degree from scores left join courses on(scores.cno=courses.cno) left join teachers on(teachers.tno=courses.tno) where teachers.tname='张旭'; or select a.sno,a.degree from scores a,teachers b,courses c where a.cno=c.cno and c.tno=b.tno and b.tname='张旭';
24、查询选修某课程的同学人数多于5人的教师姓名。
select teachers.* from teachers ,courses where courses.cno=(select courses.cno from scores left join courses on(scores.cno=courses.cno) left join teachers on(teachers.tno=courses.tno) group by courses.cno having count(courses.cno)>5) and courses.tno=teachers.tno;
25、查询95033班和95031班全体学生的记录。
select * from students where class in ('95033','95031')
26、查询存在有85分以上成绩的课程Cno.
select distinct cno from scores where degree>85
27、查询出“计算机系“教师所教课程的成绩表。
select d.tname,c.cname,a.sname,b.degree from students a,scores b,courses c,teachers d where a.sno=b.sno and b.cno=c.cno and c.tno=d.tno and d.depart='计算机系'; or select d.tname,c.cname,a.sname,b.degree from students a left join scores b on(a.sno=b.sno) left join courses c on (b.cno=c.cno) left join teachers d on ( c.tno=d.tno) where d.depart='计算机系';
28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
select distinct a.tname,a.prof from teachers a where depart='计算机系' and depart not in (select distinct b.depart from teachers b where b.depart='电子工程系');
29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
select a.cno,a.sno,a.degree from scores a left join scores b on (a.sno=b.sno) where a.cno='3-105' and b.cno='3-245' and a.degree>b.degree order by a.degree desc;
30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
select a.cno,a.sno,a.degree from scores a left join scores b on (a.sno=b.sno) where a.cno='3-105' and b.cno='3-245' and a.degree>b.degree order by a.degree desc;
31、查询所有教师和同学的name、sex和birthday
select sname ,ssex,sbirthday from students union select tname,tsex,tbirthday from teachers;
32、查询所有“女”教师和“女”同学的name、sex和birthday.
select sname ,ssex,sbirthday from students where ssex='Ů' union select tname,tsex,tbirthday from teachers where tsex='Ů' ;
33、查询成绩比该课程平均成绩低的同学的成绩表。
select a.* from scores a left join (select cno ,avg(degree) varage from scores group by cno) b on(a.cno=b.cno and a.degree > b.varage) ;
34、查询所有任课教师的Tname和Depart.
select tname,depart from teachers where tno in (select tno from courses );
35 查询所有未讲课的教师的Tname和Depart.
select tname,depart from teachers where tno not in (select tno from courses );
36、查询至少有2名男生的班号。
select class ,count(1) boycount from students where ssex='男' group by class ;
37、查询Student表中不姓“王”的同学记录。
select * from students where sname not like '王%'
38、查询Student表中每个学生的姓名和年龄。
select sname,year(now())-year(sbirthday) age from students;
39、查询Student表中最大和最小的Sbirthday日期值。
select max(sbirthday),min(sbirthday) from students
40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
select * from students order by class desc, sbirthday ;
41、查询“男”教师及其所上的课程。
select tname,cname from teachers,courses where tsex='男' and teachers.tno=courses.tno;
42、查询最高分同学的Sno、Cno和Degree列。(单科成绩最高分)
select d.* from scores d left join (select a.cno,max(a.degree) ma from scores a group by a.cno) b on (b.cno=d.cno ) where b.ma=d.degree;
43、查询和“李军”同性别的所有同学的Sname.
select * from students where ssex=(select ssex from students where sname='李军'); or select a.* from students a left join (select ssex from students where sname='李军') b on(b.ssex=a.ssex) where a.ssex=b.ssex;
44、查询和“李军”同性别并同班的同学Sname.
select a.* from students a left join (select class, ssex from students where sname='李军') b on(b.ssex=a.ssex) where a.ssex=b.ssex and b.class=a.class; or select * from students where ssex=(select ssex from students where sname='李军' ) and class=(select class from students where sname='李军' ) ;
45、查询所有选修“计算机导论”课程的“男”同学的成绩表
select a.sname,c.cname,b.degree from students a left join scores b on (a.sno=b.sno) left join courses c on(c.cno=b.cno) where c.cname='计算机导论';