Posts

Showing posts from 2019

JavaScript Lab 3

// Print prime Numbers... let range = 1; while (range < 10) { let count = 0; for (let i = 1; i <= range ; i++) { if (range % i == 0) { count++; } } if (count == 2) { console.log(range); } range++; } // Printing Star patten for (let i = 1; i <= 5; i++) { console.log(new Array(i + 1).join("*")); }

Javascript Lab 2

/* -------------------------------- JUST BY ADDING CONDITIONS, make the program print: "All the world's a stage, And all the men and women merely players; They have their exits and their entrances," NOTE: You must refernce x, y, and z ------------------------------------ */ let x = true let y = "5" let z = 5 if(x) { console.log('All the world\'s a stage, \nAnd all the men and women merely players;') } if (y == z) { console.log('They have their exits and their entrances,') } else { console.log('And one man in his time plays many parts') } // Exercise #3b /* -------------------------------- Change the values of a, and b so that the program prints: "Thus with a kiss, I say goodbye" ------------------------------------ */ let a = 3; let b = "Rom"; if(b.length <= 3) { console.log("Thus with a kiss, ") } if(b.length % a != 1) { console.log("I say goodbye.") } else

JavaScript Lab 1

// How to print //console.log("Hello"); // There are both string "String 1"; 'string 2'; // This is a number. 42; // Variables // var x = "One"; // console.log(x); // x = "Funny"; // console.log(x); let m = "Mottel"; const h = "goodbye"; console.log(m,h); // let , var, and const are not the same. // For now use let. // Boolean true; false; // How to make null let j; j = null; console.log(j); // // Declare the Variale. // let x = 0; // // We can Add // x = x + 5; // // Multiply // x = x * 5; // // Subtract // x = x - 5; // // divide // x = x / 4; // // Another way to add // x += 5; // // Increment and decrement // x++; // x--; // Adding boolenas // let x = true; // x += 5; // Parsing Strings // let x = "42.2"; // // x = parseInt(x); // x = parseFloat(x); // x *= 2; // x = String(x); // Number to Sting // let x = 42; // x = x.toString(); // x += "hahahahhaha"; // Casting

SQL Lab 11

/* 1. STORE PROCEDURE: count_backwards IN: Number to start from (x) IN: Number to go to (Y) Display all the numbers (inclusive) from x to y as one comma separated value Ex: x = 10, y = 5, Display: 10,9,8,7,6,5 */     drop procedure count_backwards| call count_backwards(10, 5)| CREATE PROCEDURE count_backwards(IN x INT, in y int) BEGIN DECLARE i INT DEFAULT 0;     DECLARE output TEXT;         set i = x;     if x < y then     WHILE i < y DO SET output = CONCAT_WS(", ", output, i);         SET i = i + 1;     END WHILE;     else     WHILE i > y DO SET output = CONCAT_WS(", ", output, i);         SET i = i - 1;     END WHILE;     end if;         SELECT output AS Result; END | /* 2. STORE PROCEDURE: count_funny IN: Number to start from (x) IN: Number to go to (Y) Display all the numbers (inclusive) from x to y and a special message for any number divisible by 5 Ex: x = 10, y = 0 Display: 10 <- funny

SQL Lab 10

/* 1. STORE PROCEDURE: fetch_animal_parents IN: animal id Display if both parents exist -> show parent's name (both mom and dad) Else if only one of the parent exist -> show their name Else show -> No parents */         CREATE PROCEDURE fetch_animal_parents(IN a_id INT) BEGIN DECLARE ac_name VARCHAR(50);     DECLARE am_name VARCHAR(50);     DECLARE ad_name VARCHAR(50);     DECLARE a_year INT DEFAULT 0;         #get the animal info         select child.name as "Child's name" ,             mom.name as "Mother's name" ,             dad.name as "Father's name" into ac_name, am_name, ad_name            from animal child join animal mom on child.mother_id = mom.id join animal dad on child.father_id = dad.id left join race child_race on child_race.id = child.race_id join species child_species on child_species.id = child.species_id join race mom_race on mom_race.id = mom.race_id join species mom_species on mo

SQL Lab 9

# 1. STORE PROCEDURE: list_animals # Display the list of all the animals with a name and a race CREATE PROCEDURE list_animals() BEGIN SELECT animal.name, race.name as race FROM animal join race on race.id = animal.race_id where animal.name is not null and race_id is not null; END| call list_animals| # 2. STORE PROCEDURE: fetch_animal_age # IN: Animal id # OUT: Animal age in numbers CREATE PROCEDURE count_race_by_species (IN p_species_id INT, OUT p_num_races INT)  BEGIN     SELECT COUNT(*) INTO p_num_races     FROM race     WHERE species_id= p_species_id ;                              END | SELECT id, name INTO @var1, @var2 FROM animal WHERE id = 7; SELECT @var1, @var2| CALL count_race_by_species(2, @num_cat_race)| SELECT @num_cat_race| DROP PROCEDURE animal_age| CREATE PROCEDURE animal_age (IN p_animal_id INT, OUT p_num_age INT)  BEGIN     SELECT TIMESTAMPDIFF(YEAR, dob, NOW()) INTO p_num_age     FROM animal     where animal.id = p_animal_id;   

SQL Lab 8

# 1. Show all the animals (id, name, sex) prepare query1 from 'select id, name, sex from animal'; execute query1; # 2. Show all the animals by ? race_id (id, name, sex, dob, race_name) prepare query2 from 'select animal.id, animal.name, animal.sex, animal.dob, race.name from animal join race on race.id = animal.race_id                     where race_id = ?'; set @r_id = '1'; execute query2 using @r_id; # Show all the animals by ? race_id and ? species_id (id, name, sex, race_name, species_name) prepare query3 from 'select animal.id, animal.name, animal.sex, animal.dob, race.name, species.current_name from animal join race on race.id = animal.race_id join species on species.id = animal.species_id                     where animal.race_id = ? and animal.species_id = ?'; set @r_id = 1, @s_id = 1; execute query3 using @r_id, @s_id; # 4. Which specie has less than ? males and ? females (id, name, sex, race_name, species_name) wher

SQL Lab 7

select dob from animals; SELECT name, dob FROM animals WHERE MONTH(dob) = 6;  # 1. Select all the animals born in June (assuming you don't now the month number of June)      SELECT * FROM animals WHERE MONTHNAME(dob) = 'june'; # 2. Select all the animals born in the first 8 weeks of the year SELECT name, dob, WEEK(dob) AS week, WEEKOFYEAR(dob) AS week2, YEARWEEK(dob) AS week_year FROM animals where WEEKOFYEAR(dob) <= 8; # 3. Display the day (in numbers) and month of birth (in words) of all the turtles and cats born before 2007 (two columns). SELECT name, dob, DATE_FORMAT(dob, '%d')AS day_in_no, DATE_FORMAT(dob, '%M')AS month_in_words from animals where (species_id = 2 or species_id = 3) and (dob < "2007"); # 4. Display the day (in numbers) and month of birth (in words) of all the turtles and cats born before 2007 (one column). SELECT name, dob, DATE_FORMAT(dob, '%d of %M')AS format_date from animals where (speci

SQL Lab 6

#1 Set the default value "George to the names in the animal table" ALTER TABLE animal ALTER name SET DEFAULT 'George'; #2 How many CATS are born the same year? I want to see the total per sex and overall total. Display the name, dob, race_name, species_name, sex) (todo) SELECT animal.name, animal.dob, race.name as "race_name", species.current_name as "species_name",  animal.sex, COALESCE(animal.sex, 'Total') Sex, COUNT(*) AS num_animal FROM animal join race on race.id = animal.race_id join species on species.id = animal.species_id WHERE YEAR(animal.dob) = (SELECT YEAR(animal.dob) FROM species WHERE species.current_name = 'cat') GROUP BY sex WITH ROLLUP; #3 List all the animal(name, race_name) race name is longer then their name SELECT animal.name as "Aname", race.name as "Rname" FROM animal JOIN race ON race.id = animal.race_id where length(race.name) > length(animal

SQL Lab 5

use block_db_w3d3; -- How many races exist in the animals table? (Display all of their name) select count(race_id) as races_number, GROUP_CONCAT(name) from animals; -- How many species exist in the races table? (Display all of their name) select count(distinct species_id) as species, GROUP_CONCAT(distinct species.current_name) from races inner join species on races.species_id = species.id; -- How many Dogs have a father? (Display the children's name and the father's name) select * from animals where species_id = 1 and father_id is not null; select * from animals as t1 join animals as t2 join animals as t3 on (t2.father_id = t1.id) = t3.id where t2.father_id is not null and t2.species_id = 1; -- What is the average race price of each specie? SELECT AVG(price) AS "Average Price",        id AS "Race ID"     FROM races GROUP BY id; -- How many males and females exist in the animals table? select count(*) as male_number from animals where

SQL Lab 4

use block3; -- List all the American Bully and all the American Curl (Race Name, Animal Name) select (select name from races where id = animals.race_id) as "Race Name", animals.name as "Animal Name" from animals where race_id in (select id from races where name = 'American Bully' or name = 'American Curl'); -- List of animals (Name, Sex, DOB, Race_Id, Species Name) without a race . select animals.name as "Animal Name", animals.sex as "SEX", animals.dob as "DOB", (select id from races where id = animals.race_id) as Race_id,(select name from species where id = animals.species_id) as species_name from animals where (select id from races where races.id = animals.race_id) is null; -- List of animals (Name, Sex, DOB, Race_Id) whose race has the word look* in it. select animals.name as "Animal Name", animals.sex as "SEX", animals.dob as "DOB", (select id from races where id = animals

SQL Lab 3

use block3; select * from animals; select * from species; select * from races; -- List all the Affenpinscher (Race Name, Animal Name) select races.name as "Race Name", animals.name as "Animal Name" from races inner join animals on races.id = animals.race_id where races.name = 'Affenpinscher'; -- List of animals (Name, DOB, Race Name, Race Description) who does not have the word pound in their race description. select animals.name as "Animal Name", animals.dob as "DOB", races.name as "Race Name",  races.description as "Race Description" from races inner join animals on races.id = animals.race_id WHERE races.description not like "%pound%"; -- List of animals (Name, DOB, Race Name, Race Description) who does not have a race description. select animals.name as "Animal Name", animals.dob as "DOB", races.name as "Race Name",  races.description as "Race Descript

SQL Lab 2

drop table animals; drop table races; drop table species; use block3; create table species (id INT unsigned primary key not null auto_increment, current_name varchar(50),latin_name varchar(50),description text)engine=InnoDB; Insert into species values(Default,'Dog','Canis familiaris','A domesticated carnivorous mammal that typically has a long snout, an acute sense of smell.'),   (Default,'Cat','Felis catus','A small domesticated carnivorous mammal with soft fur, a short snout, and retractile claws.'),                           (Default,'Turtle','Testudo hermanni','A slow-moving reptile, enclosed in a scaly or leathery domed shell into which it can retract its head and thick legs.'),                           (default,'Parrot','Alipiopstitta xanthops','A bird, often vividly colored, with a short down-curved hooked bill, grasping feet, and a raucous voice, found esp. in the tr