Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 582179a

Browse files
committed
answers for q4,p2
1 parent 4545d61 commit 582179a

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

Week2/homework/part2.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,23 @@ let query = connection.query("CREATE TRIGGER language AFTER INSERT ON countrylan
1717
console.log(results);
1818
});
1919

20+
//correct answer:
21+
delimiter;;
22+
create procedure new_world.language_add (in_name varchar(52), in lang varchar(30), inoffical enum(‘T’, ‘F’), in _pct float(4,1))
23+
begin
24+
declare v_country_code char(3);
25+
declare v_new_lang_count int;
26+
select code into v_country_code from country where name = in_name;
27+
insert into countrylanguage( countrycode, language, isofficial, percentage)
28+
values
29+
(v_country_code, in_lang, in_official, in_pct);
30+
select(language) into v_new_lang_count
31+
from countrylanguage where countrycode = v_country_code;
32+
if v_new_lang_count >= 10
33+
then
34+
select concat(‘Warning: country’, in_name, ‘now has’, v_new_lang_count, ‘languages’);
35+
else
36+
select concat(‘Succeded: country’, in_name, ‘now has’, v_new_lang_count, ‘languages’);
37+
end if;
38+
end;;
2039

Week2/homework/queries_mysql.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ DEALLOCATE PREPARE capital;
2222

2323
//2. List all the languages spoken in the region Y (Accept Y from user)
2424
Prepared Statement:
25-
PREPARE language FROM 'SELECT countrylanguage.language FROM countrylanguage INNER JOIN country ON country.code = countrylanguage.countrycode WHERE country.Region = ?';
25+
PREPARE language FROM 'SELECT DISTINCT countrylanguage.language FROM countrylanguage INNER JOIN country ON country.code = countrylanguage.countrycode WHERE country.Region = ?';
2626

2727
Execution:
2828
SET @a = 'Middle East';
2929
EXECUTE language USING @a:
3030

3131
Result:
3232
mysql> EXECUTE language USING @a;
33+
(this was without DISTINCT, therefore there are repeats of the languages)
3334
+-------------+
3435
| language |
3536
+-------------+

Week2/homework/question4.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ let connection = mysql.createConnection({
88
password: "hyfuser",
99
database: "new_world",
1010
});
11+
let select = 'select co.continent, co.name, l.language from country co inner join countrylanguage l on co.code = l.countrycode where l.language in (select l.language from country c inner join countrylanguage l on c.code = l.countrycode where c.name = "France" and l.isofficial = "T") and co.continent = (select continent from country where name = "Barbados")';
12+
1113

1214

1315
let userInput = process.argv[2] + ' ' + process.argv[3];
1416
let input2 = process.argv[4];
15-
let params = userInput + ' ' + input2;
16-
let query = connection.query('SELECT name FROM country WHERE region IN (SELECT region FROM country WHERE name = ? AND code IN (SELECT countrycode FROM countrylanguage WHERE language = "French" AND isOfficial = "T"))', params, ((err, results) =>{
17+
let params =userInput + ' ' + input2;
18+
let query = connection.query(select, params, ((err, results) =>{
1719
if (err) throw err;
1820
console.log(results);
1921
}));
20-
22+
23+
connection.end();
24+
25+
26+

0 commit comments

Comments
 (0)