Alexa Iulia 7 1

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

1. Make the sum of two variables and display the result.

�( 5 mins)

SET @a=1;
SET @b=1;
SELECT (@a+@b) AS Suma

2.Display the number of entries in the entire database. Count the rows from each
table, put the result in a variable and sum all the variables at the end. ( 10
mins)
select @var1 := count(course.IDCourse) from course;
select @var2 := count(faculty.idfac) from faculty;
select @var3 := count(grade.idgrade) from grade;
select @var4 := count(speciality.idspec) from speciality;
select @var5 := count(studdata.idstud) from studdata;
select @var6 := count(student.idstud) from student;
select @var1, @var2, @var3, @var4, @var5, @var6;
select @var7 := @var1 + @var2 + @var3 + @var4 + @var5 + @var6 as suma_mare;

3.Store the number of courses into a variable and display it. (5 mins)
select @var1 := count(course.IDcourse) from course;
select @var1 as suma_cursuri;

4.Create relevant examples on your own where you use variables into the following
places: SELECT, WHERE, INSERT, UPDATE, DELETE ( 15 mins)
SET @nume= (SELECT LastName FROM studdata WHERE Mother="Ana");
SET @prenume= (SELECT FirstName FROM studdata WHERE LastName=@nume);
SELECT @nume, @prenume;

UPDATE studdata SET Father=@prenume WHERE LastName=@nume;


SELECT * FROM studdata;

SET @nume="Nume";
SET @adresa="Sibiu";
SET @an=1986;
INSERT INTO faculty ( Name, Address, YearFounded) VALUES ( @nume, @address,
@yearfounded);
SELECT * FROM faculty;

DELETE FROM faculty WHERE Name=@nume;


SELECT * FROM faculty;

5.EXTRA: Find ways to perform loops. Think simple or complex, your choice :)
Moduri de a face bucle in mysql:LOOP Statement, WHILE Statement (WHILE LOOP),
REPEAT Statement (REPEAT UNTIL LOOP), ITERATE Statement, LEAVE Statement, RETURN
Statement

CREATE TABLE `tabelnou` (


`coloana1` VARCHAR(50) NULL DEFAULT NULL
);
DELIMITER $$$$$
CREATE PROCEDURE ABCDEFGHIJKL()

BEGIN
DECLARE a INT Default 1 ;
simple_loop: LOOP
insert into tabelnou values(a);
SET a=a+1;
IF a=51 THEN
LEAVE simple_loop;
END IF;
END LOOP simple_loop;
END $$$$$

delimiter ;;
drop procedure if exists test2;;
create procedure test2()
BEGIN
SELECT MONTH(CURDATE()) INTO @curmonth;
SELECT MONTHNAME(CURDATE()) INTO @curmonthname;
SELECT DAY(LAST_DAY(CURDATE())) INTO @totaldays;
SELECT DAY(@checkweekday) INTO @checkday;
SET @daycount = 0;
SET @workdays = 0;

WHILE(@daycount < @totaldays) DO


IF (WEEKDAY(@checkweekday) < 5) THEN
SET @workdays = @workdays+1;
END IF;
SET @daycount = @daycount+1;
END WHILE;
SELECT
@curmonth,@curmonthname,@totaldays,@daycount,@workdays,@checkweekday,@checkday;
END
;;
call test2

CALL `ABCDEFGHIJKL`()

6. EXTRA : How would you store and work with variables with comma separated
values ?
like this:

CREATE TABLE sometable


(`something` varchar(1), `someId` int)
;

INSERT INTO sometable


(`something`, `someId`)
VALUES
('a', 1),
('b', 2),
('c', 3),
('d', 4),
('e', 5)
;
select something from sometable where someId in (1,2,3)

///////////rectificare////////////
/*CREATE TABLE sometable
(`something` varchar(1), `someId` int)
;

INSERT INTO sometable


(`something`, `someId`)
VALUES
('a', 1),
('b', 2),
('c', 3),
('d', 4),
('e', 5)
;*/
select something from sometable where someId in (1,2,3);

SET @myIds = '1,2,3';


SET @sql = CONCAT('select something from sometable where someId in (',@myIds,')');

/* SET @myIds = '1,2,3';


SET @sql = CONCAT('select something from sometable where someId in (',@myIds,')');

PREPARE stmt FROM @sql;


EXECUTE stmt;
DEALLOCATE PREPARE stmt; */

////////////////////////////////////////////////

SET @myIds = '1,2,3'


SET @sql = CONCAT('select someId (',@myIds,')')
SELECT @sql

You might also like