mySQL(II) 任务一
Posted statlearning2019
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mySQL(II) 任务一相关的知识,希望对你有一定的参考价值。
任务一:mySQL软件安装及数据库基础
内容:
1. 任务
2. 遇到的问题
3. 代码
4. 参考资料
1.任务
1.1 软件安装及配置
根据教程安装mysql和图形管理工具。windows系统下用cmd安装,IDE选择的是HeidiSQL [5]。
1.2 查询语句
查询语句是sql的基础,应用很多。以下是一些常用的查询语句:
(a) SELECT FROM
SELECT expressions FROM tables [WHERE conditions];
(b) WHERE
WHERE conditions;
(c) GROUP BY
SELECT expression1, expression2, ... expression_n, aggregate_function (expression) FROM tables [WHERE conditions] GROUP BY expression1, expression2, ... expression_n;
(d) ORDER BY
SELECT expressions FROM tables [WHERE conditions] ORDER BY expression [ ASC | DESC ];
(e) INSERT
INSERT INTO table (column1, column2, ... ) VALUES (expression1, expression2, ... ), (expression1, expression2, ... ), ...;
2. 遇到的问题
由于首次下载使用mySQL,设置过程中遇到不少问题,比如一开始无法初始化sql,以及创建数据库等。根据网上的相关博文一一解决了这些问题,成功安装。
3. 代码
3.1 项目一 :查找重复的电子邮箱(难度:简单)
创建email表:
+----+---------+ | Id | Email | +----+---------+ | 1 | [email protected] | | 2 | [email protected] | | 3 | [email protected] | +----+---------+
编写一个 SQL 查询,查找 email 表中所有重复的电子邮箱。
查询应返回以下结果:
+---------+ | Email | +---------+ | [email protected] | +---------+
(a)创建表
create table email( Id int(10) not null, Email varchar(20) not null, PRIMARY KEY (Id) ); insert into email VALUES(1,‘[email protected]‘); insert into email VALUES(2,‘[email protected]‘); insert into email VALUES(3,‘[email protected]‘);
(b) 查询
select Email from email GROUP BY Email HAVING count(*)>1
截图:
3.2 项目二:查找大国(难度:简单)
创建如下 World 表:
+------------+----------+---------+--------------+---------------+ | name | continent| area | population | gdp | +------------+----------+---------+--------------+---------------+ | Afghanistan| Asia | 652230 | 25500100 | 20343000 | | Albania | Europe | 28748 | 2831741 | 12960000 | | Algeria | Africa | 2381741 | 37100000 | 188681000 | | Andorra | Europe | 468 | 78115 | 3712000 | | Angola | Africa | 1246700 | 20609294 | 100990000 | +------------+----------+---------+--------------+---------------+
如果一个国家的面积超过 300 万平方公里,或者(人口超过 2500 万并且 gdp 超过 2000 万),那么这个国家就是大国家。
编写一个 SQL 查询,输出表中所有大国家的名称、人口和面积,预期结果:
+--------------+-------------+--------------+ | name | population | area | +--------------+-------------+--------------+ | Afghanistan | 25500100 | 652230 | | Algeria | 37100000 | 2381741 | +--------------+-------------+--------------+
(a) 创建表
create TABLE World( name VARCHAR(20) NOT NULL, continent VARCHAR(20) NOT NULL, area INT(20) NOT NULL, population INT(30) NOT NULL, gdp INT(30) NOT NULL, PRIMARY KEY (NAME) ) ; INSERT INTO World VALUES(‘Afghanistan‘,‘Asia‘,652230,25500100,20343000); INSERT INTO World VALUES(‘Albania‘,‘Europe‘,28748,2831741,12960000); INSERT INTO World VALUES(‘Algeria‘,‘Africa‘,2381741,37100000,188681000); INSERT INTO World VALUES(‘Andorra‘,‘Europe‘,468,78115,3712000); INSERT INTO World VALUES(‘Angola‘,‘Africa‘,1246700,20609294,100990000);
(b)查询
SELECT NAME, population, AREA FROM World WHERE AREA >3000000 OR (population > 25000000 AND gdp>20000000)
截图:
4. 参考资料
[1] https://www.techonthenet.com/mysql/index.php
[2] http://www.runoob.com/mysql/mysql-install.html
[3] https://www.cnblogs.com/reyinever/p/8551977.html
[4] https://www.cnblogs.com/xiaojian1/p/mysql.html
[5] https://www.heidisql.com/
以上是关于mySQL(II) 任务一的主要内容,如果未能解决你的问题,请参考以下文章