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

--Rezolvari lab 2

--vizualizari

select * from students order by id;

delete from students where id > 1997;

--ex 1

create view viz_students as

select * from students where scholarship > 1350;

select * from viz_students;

--ex 2

update students set scholarship = 1400 where id = 1;

update students set scholarship = 1200 where id = 1;

--ex 3

update viz_students set scholarship = 1400 where id = 1;

update viz_students set scholarship = 1200 where id = 1;

update viz_students set scholarship = 9999 where id = 1;

--ex 4

insert into viz_students values(1998,'123AB1','f1','l1',3,'B1',200,sysdate, 'aaa@gmail.com', sysdate,


sysdate);

insert into viz_students values(1999,'123AB2','f2','l2',2,'B2',1400,sysdate, 'abc@gmail.com', sysdate,


sysdate);

select * from students order by id desc;

select * from viz_students order by id desc;


--ex 5

delete from viz_students where id>1997;

delete from students where id>1997;

create or replace view viz_students as

select * from students where scholarship > 1350 with check option;

insert into viz_students values(1998,'123AB1','f1','l1',3,'B1',200,sysdate, 'aaa@gmail.com', sysdate,


sysdate);

insert into viz_students values(1999,'123AB2','f2','l2',2,'B2',1400,sysdate, 'abc@gmail.com', sysdate,


sysdate);

--ex 6

create or replace view viz_students_lim as

select lname, fname, scholarship

from students

where scholarship > 1350;

insert into viz_students_lim values('x', 'y', 1500);

create or replace view viz_students_lim as

select id, registration_number, lname, fname, scholarship

from students

where scholarship > 1350;

insert into viz_students_lim values(2000, 'reg1', 'x', 'y', 1500);

select * from students order by id desc;

--var de substitutie

SELECT employee_id, last_name, salary, department_id

FROM employees WHERE employee_id = &p_cod;


DEFINE p_cod;

SELECT employee_id, last_name, salary, department_id

FROM employees WHERE employee_id = &p_cod;

UNDEFINE p_cod;

DEFINE p_cod=100;

SELECT employee_id, last_name, salary, department_id

FROM employees WHERE employee_id = &&p_cod;

UNDEFINE p_cod;

ACCEPT p_cod PROMPT 'cod= ';

SELECT employee_id, last_name, salary, department_id

FROM employees WHERE employee_id = &&p_cod;

--temporary table

CREATE GLOBAL TEMPORARY TABLE my_temp_table_334 (

id NUMBER,

description VARCHAR2(20)

ON COMMIT preserve ROWS;

INSERT INTO my_temp_table_334 VALUES (1, 'ONE');

--Ce ar trebui sa se afiseze?

SELECT * FROM my_temp_table_334;

commit;

SELECT COUNT(*) FROM my_temp_table;


DROP TABLE my_temp_table_334 PURGE;

-----------------------------------------

--Session id

select

sys_context('USERENV','SID')

from dual;

select

sid

from

v$mystat

where

rownum <=1;

select

to_number(substr(dbms_session.unique_session_id,1,4),'XXXX') mysid

from dual;

select distinct

sid

from v$mystat;

---index

SELECT

index_name,
index_type,

visibility,

status

FROM

all_indexes

WHERE

table_name = 'EMPLOYEES';

drop index employees_last_name_i ;

CREATE INDEX employees_last_name_i

ON employees(last_name);

EXPLAIN PLAN FOR

select employee_id, last_name

from employees

where upper(last_name)='KING';

SELECT

PLAN_TABLE_OUTPUT

FROM

TABLE(DBMS_XPLAN.DISPLAY());

drop index employees_idx;

declare
y number

begin

case when y<>0 then dbms_output.put_line(1) ;

else dbms_output.put_line(1); end case;

end;

/
1)

ACCEPT emp_id PROMPT 'Inserati id';--VARIANTA CU PROMPT

--DEFINE emp_id; VARIANTA CU VARIABILA

SELECT * FROM employees e WHERE e.employee_id = &emp_id;

UNDEFINE emp_id;

------------------------------------------------------------------------

create table ex_tabel ( id NUMBER(6,2));

insert into ex_tabel

values (secventa.nextval);

select * from ex_tabel

--------------------------------------------------------

-- DDL for Sequence SECVENTA

--------------------------------------------------------

CREATE SEQUENCE "VALENTINSAVOIU"."SECVENTA" MINVALUE 1 MAXVALUE 10000 INCREMENT


BY 10 START WITH 200 NOCACHE NOORDER NOCYCLE ;

select * from user_sequences

--Exercitii PL/SQL

SET SERVEROUTPUT ON;

BEGIN

dbms_output.put_line ('Un mesaj random');

END;

<<suma>>
BEGIN

dbms_output.put_line (&var1 + &var2);

END suma;

<<ex3>>

DECLARE

x number;

y number;

rezultat number;

BEGIN

x := &x;

y := &y;

CASE

WHEN y<>0 THEN rezultat := x/y + y;

WHEN y = 0 THEN rezultat := x**2;

END CASE;

dbms_output.put_line (rezultat);

END ex3;

<<ex4>>

DECLARE

jobID VARCHAR(10);

suma number;

BEGIN

jobID := &jobID;

SELECT sum(e.salary)INTO suma from employees e where e.job_id = UPPER(jobID);

dbms_output.put_line(suma);

END ex4;

You might also like