mySQL(II) 任务1&2

任务一: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  | a@b.com |
| 2  | c@d.com |
| 3  | a@b.com |
+----+---------+

编写一个 SQL 查询,查找 email 表中所有重复的电子邮箱。

查询应返回以下结果: 

+---------+
| Email   |
+---------+
| a@b.com |
+---------+

 

(a)创建表

create table email(
Id int(10) not null,
Email varchar(20) not null,
PRIMARY KEY (Id)
);

insert into email VALUES(1,'a@b.com');
insert into email VALUES(2,'c@d.com');
insert into email VALUES(3,'a@b.com');

 (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/

 

posted on 2019-04-02 10:11  女士品茶  阅读(231)  评论(0编辑  收藏  举报