Practicas SQL
Practicas SQL
Practicas SQL
INSTITO TECNOLÓGICO DE
QUERÉTARO
ALUMNO:CRUZ CRISTALES
JOSÉ MANUEL
INGENIERÍA EN SISTEMAS
COMPUTACIONALES
PRACTICAS SQL
Practica 2
Note: Save your subprograms as .sql files, using the Save Script button.
Remember to set the SERVEROUTPUT on if you set it off previously.
1. Create and invoke the ADD_JOB procedure and consider the results.
a. Create a procedure called ADD_JOB to insert a new job into the
JOBS table. Provide the ID and title of the job, using two parameters.
CREATE OR REPLACE PROCEDURE add_job
(p_jobid IN
jobs.job_id%TYPE,p_jobtitle IN
jobs.job_title%TYPE)
IS
BEGIN
INSERT INTO jobs (job_id, job_title)
VALUES (p_jobid, p_jobtitle);
COMMIT;
END add_job;
b. Compile the code, and invoke the procedure with IT_DBA
as job ID and Database Administrator as job title.
Query the JOBS table to view the results.
In iSQL*Plus, load and run the script file created in question 1a above.
Procedure created.
EXECUTE add_job ('IT_DBA', 'Database Administrator')
SELECT * FROM jobs WHERE job_id = 'IT_DBA';
b. Compile the code; invoke the procedure to change the job title of the job ID IT_DBA to Data
Administrator. Query the JOBS table to view the results. Also check the exception
handling by trying to update a job that does not exist (you can use job ID IT_WEB and job title
Web Master).
In iSQL*Plus, load and run the script file created in the above question.
Procedure created.
EXECUTE upd_job ('IT_DBA', 'Data Administrator')
SELECT * FROM jobs WHERE job_id = 'IT_DBA';
3. Create a procedure called DEL_JOB to delete a job from the JOBS table.
a. Create a procedure called DEL_JOB to delete a job from the JOBS table. Include the
necessary exception handling if no job is deleted.
Also, check the exception handling by trying to delete a job that does not exist (use job ID
IT_WEB). You should get the message you used in the exception-handling section of the procedure
as output.
EXECUTE del_job ('IT_WEB')
4. Create a procedure called QUERY_EMP to query the EMPLOYEES table, retrieving the salary and job
ID for an employee when provided with the employee ID.
a. Create a procedure that returns a value from the SALARY and JOB_ID columns for a
specified employee ID.
Use host variables for the two OUT parameters salary and job ID.
CREATE OR REPLACE PROCEDURE query_emp
(p_empid IN employees.employee_id%TYPE,
p_sal OUT employees.salary%TYPE,
p_job OUT employees.job_id%TYPE)
IS
BEGIN
SELECT salary, job_id
INTO p_sal, p_job
FROM employees
WHERE employee_id = p_empid;
END query_emp;
b. Compile the code, invoke the procedure to display the salary and job ID for employee ID 120.
In iSQL*Plus, load and run the script file created in the above question.
Procedure created.
There is no employee in the EMPLOYEES table with an EMPLOYEE_ID of 300. The SELECT
statement retrieved no data from the database, resulting in a fatal PL/SQL error,
NO_DATA_FOUND.
Practica 3
a. Compile the code; create a host variable G_TITLE and invoke the function with job ID
SA_REP. Query the host variable to view the result.
In iSQL*Plus, load and run the script file created in the above question.
Function created.
1. Create a function called ANNUAL_COMP to return the annual salary by accepting two parameters: an
employee’s monthly salary and commission. The function should address NULL values.
a. Create and invoke the function ANNUAL_COMP, passing in values for monthly salary and
commission. Either or both values passed can be NULL, but the function should still return an
annual salary, which is not NULL. The annual salary is defined by the basic formula:
(sal*12) + (commission_pct*salary*12)
1. Create a procedure, NEW_EMP, to insert a new employee into the EMPLOYEES table. The procedure
should contain a call to the VALID_DEPTID function to check whether the department ID specified
for the new employee exists in the DEPARTMENTS table.
a. Create a function VALID_DEPTID to validate a specified department ID. The function should
return a BOOLEAN value.
a. Create the procedure NEW_EMP to add an employee to the EMPLOYEES table. A new row should
be added to EMPLOYEES if the function returns TRUE. If the function returns FALSE, the
procedure should alert the user with an appropriate message.
Define DEFAULT values for most parameters. The default commission is 0, the default salary is
1000, the default department ID is 30, the default job is SA_REP and the default manager
number is 145. For the employee’s ID, use the sequence EMPLOYEES _SEQ. Provide the last
name, first name and e-mail for the employee.
Test your NEW_EMP procedure by adding a new employee named Jane Harris to department 15. Allow all other
parameters to default. What was the result?
a. Test your NEW_EMP procedure by adding a new employee named Joe Harris to department 80.
Allow all other parameters to default. What was the result?
EXECUTE new_emp(p_lname=>'Harris',p_fname=>'Joe',
p_email=>'JOHARRIS',p_deptno => 80)
PL/SQL procedure successfully completed.
Practica 5
1. Create a package specification and body called JOB_PACK. (You can save the package body and
specification in two separate files.) This package contains your ADD_JOB, UPD_JOB, and
DEL_JOB procedures, as well as your Q_JOB function.
Note: Use the code in your previously saved script files when creating the package.
a. Make all the constructs public.
Note: Consider whether you still need the stand-alone procedures and functions you just
packaged.
2. Create and invoke a package that contains private and public constructs.
a. Create a package specification and package body called EMP_PACK that contains your
NEW_EMP procedure as a public construct, and your VALID_DEPTID function as a private
construct. (You can save the specification and body into separate files.)
EXCEPTION
WHEN NO_DATA_FOUND
THEN RAISE_APPLICATION_ERROR (-20000,
'Not a valid manager for this department');
END;
END chk_dept_mgr;
END chk_pack;
/
Package Body Created.
b. Test the CHK_HIREDATE procedure with the following command:
EXECUTE chk_pack.chk_hiredate('01-JAN-47')
What happens, and why?
b. Test the second version of PRINT_IT with the following set of commands:
VARIABLE g_emp_sal number
EXECUTE :g_emp_sal := over_load.print_it('33,600')
PRINT g_emp_sal
Conceda a otro usuario acceso a la tabla DEPT. Pida al usuario que le conceda acceso de consulta a
su tabla DEPT.
GRANT select
ON dept
To < user1>
GRANT select
ON dept
To < user2>
Agregue una nueva fila a la tabla DEP. El equipo 1 debe agregar Educación como departamento
número 50. El equipo 2 debe agregar Administración como departamento número 50. Haz
que los cambios sean permanentes.
CRUZ CRISTALES JOSÉ MANUEL
Team2executesthisINSERTstatement.
SQL>INSERTINTOdept(deptno,dname)
2 VALUES (50 , 'Administration ' ) ;
SQL> COMMIT;
Consulte todas las filas de la tabla DEPT del otro equipo con su sinónimo.
SELECT
FROM dept;
Consulte el diccionario de datos USER_TABLES para ver información sobre las tablas de su
propiedad.
SQL> SELECT table_name
2FROM usertables;
TABLE_NAME
-----------------------
BONUS
CUSTOMER
DEPARTMENT
DEPT
CRUZ CRISTALES JOSÉ MANUEL
DUMMY
EMP
EMPLOYEE
ITEM
MY_EMPLOYEE
ORD
PRICE
PRODUCT
SALGRADE
Consulte la vista de diccionario de datos ALL_TABLES para ver información sobre todas las tablas
a las que puede acceder. Excluya las tablas que son de su propiedad.
TABLE_NAME OWNER
---------------------- ---------------
DEPT <user2>
S Q L > S E L E C T t a b l e_ n a m e , o w n e r
2FROM *
3 WHERE owner != <your account>----------------------------------------------------
SQL> REVOKE select
2 ON dept
3 FROM u s e r 2 ;------------------------------------------------------------------------------
Team2revokestheprivilege.
SQL> REVOKE select
2 ON dept
3FROMuserl;