Beruflich Dokumente
Kultur Dokumente
db2 "CREATE TABLE employees (empid INTEGER, name CHAR(50), dept INTEGER)"
6. Volvemos a ver la definicin de la tabla para comprobar que se haya hecho el cambio
8. Como nos manda un cdigo de error para saber que significa ese cdigo tecleamos el siguiente comando
CORDERO CASTRO CSAR ARMANDO NOLLA SALTIEL CARLOS 9. Como cambiamos el tipo de dato de una columna la tabla esta en un estado pendiente entonces tenemos que usar el comando db2 reorg table
employees
db2 "INSERT INTO employees (EMPID, NAME, DEPT) VALUES (1, 'Adam', 'A01 '),(2, 'John', 'B01'), (3, 'Peter', 'B01'), (4, 'William', 'A01')"
11. Comprobaremos con un select que se hayan insertado los campos correctamente
CORDERO CASTRO CSAR ARMANDO NOLLA SALTIEL CARLOS podemos ver como implcitamente un esquema es creado cuendo otro objeto es creado
CORDERO CASTRO CSAR ARMANDO NOLLA SALTIEL CARLOS 19. listamos las tablas del schema que se cre cuando creamos la tabla anterior
21. crearemos una nueva tabla llamada employees dentro del schema myschema
CORDERO CASTRO CSAR ARMANDO NOLLA SALTIEL CARLOS y entonces tendremos dos tablas con el mismo nombre en la misma base de datos esto es posible porque una esta dentro de un schema
VISTAS
22. Ahora crearemos una vista, las vistas son una forma diferente de visualizar los datos de una tabla, La vista ser de la tabla employees y queremos que la columna de Dept no se visualice
db2 "CREATE VIEW empview (employee_id, first_name) AS SELECT EMPID, NAME FROM employees"
CORDERO CASTRO CSAR ARMANDO NOLLA SALTIEL CARLOS 24. Describiremos la vista creada
25. Ahora cuando nosotros seleccionamos todos los campos de nuestra vista solo nos muestra employee_id y first_name
26. Si nosotros actualizamos algn campo de nuestra vista entonces los cambios tambin se vern reflejados en la tabla original
db2 "update empview SET FIRST_NAME='Piotr' WHERE employee_id=3"
ALIASES
28. crearemos un alias a la tabla employees ahora podremos hacer referencia a la tabla a travs del nombre empinfo
29. Para ver el alias creado tenemos que usar el comando para listar tablas
CORDERO CASTRO CSAR ARMANDO NOLLA SALTIEL CARLOS 30. Ahora si hacemos un select con el alias creado nos traer el mismo resultado que si hiciramos un select a employees
31. Nosotros podemos crear un alias a uno ya existente Crearemos un alias a empinfo y posteriormente listaremos las tablas y haremos un select al alias creado para que nos de cmo resultado los datos de employees
db2 CREATE ALIAS refempinfo FOR empinfo db2 list tables db2 "SELECT * FROM refempinfo
Indexes
32. intentaremos crear un ndice unico a la tabla employees sobre el campo empid como no hay valores repetidos si nos permite crearlo si hubieran valores duplicados no permitira crear el ndice
db2 "CREATE UNIQUE INDEX unique_id ON employees(empid)" db2 DESCRIBE INDEXES FOR TABLE employees
34. Ahora si intentamos insertar un registro con un empid ya existente no nos permitir hacer la transaccin
35. crearemos un ndice que recolecte automticamente estadsticas para hacer mas eficientes nuestras consultas y posteriormente describiremos este ndice.
Sequences
36. Crearemos una secuencia que empiece en cuatro, tenga incrementos de uno en uno y no este ciclado. Tambin insertaremos un valor usando una secuencia .
db2 "CREATE SEQUENCE emp_id START WITH 4 INCREMENT BY 1 NO CYCLE CACHE 5" db2 "INSERT INTO EMPLOYEES VALUES (NEXT VALUE FOR emp_id, 'Daniel', 'B01')"
37. si insertamos otro registro veremos que el ndice se sigue incrementendo de uno en uno
db2 "INSERT INTO EMPLOYEES VALUES (NEXT VALUE FOR emp_id, 'Stan', 'B01')" db2
"SELECT
* FROM employees"
38. si nosotros cerramos la conexin con la bd y nos volvemos a conectar y volvemos a insertar un registro con una secuencia insertara un 9 porque nosotros le dijimos que guardara 5 valores en cache y solo utilizamos 4,5 los otros tres 6,7,8 se perdieron al cerrar la conexin y poreso vuelve a empezar en el numero 9
Find out all sales information from salesman called LEE in the Ontario- South region from the table SALES. Find out the name of all the departments that have a manager assigned to it from table DEPARTMENT.
CORDERO CASTRO CSAR ARMANDO NOLLA SALTIEL CARLOS SELECT DEPTNAME FROM DEPARTMENT WHERE MGRNO is not NULL
Section 6.1.3.1 Query 1 Using the table STAFF, rank all the people by their years of experience in descending order. For people with same YEARS, rank again by their salary in ascending order. SELECT * FROM STAFF WHERE YEARS is not NULL ORDER BY YEARS DESC, SALARY ASC
Section 6.1.4.1
Find the total amount of sales for each sales person in table SALES. SELECT SALES_PERSON, SUM(SALES) AS total_sales FROM SALES GROUP BY SALES_PERSON
Count the number of male employees for each job position. SELECT JOB, COUNT(*) as TOTAL_NUM FROM EMPLOYEE WHERE SEX = 'M' GROUP BY JOB
CORDERO CASTRO CSAR ARMANDO NOLLA SALTIEL CARLOS Join tables EMPLOYEE and DEPARTMENT, considering WORKDEPT in EMPLOYEE is the FK referring to DEPTNO the PK in table DEPARTMENT. Save the results, and repeat this query, but use LEFT OUTER JOIN, RIGHT OUTER JOIN and FULL OUTER JOIN instead. Compare the results.
Section 6.2.1.1
Our company just started a new department called FOO with department number K47, and 'E01' as ADMRDEPT. Insert this record into table DEP ARTMENT. INSERT INTO DEPARTMENT (DEPTNO, DEPTNAME, ADMRDEPT) VALUES ('K47', 'FOO', 'E01')
Create a new table called D11_PROJ with the same structure of table PROJECT and add to it data about all projects from department D11.
CORDERO CASTRO CSAR ARMANDO NOLLA SALTIEL CARLOS INSERT INTO D11_PROJ SELECT * FROM PROJECT WHERE DEPTNO = 'D11'
Section 6.2.2.1
Try to update Paul's EDLEVEL to 'NULL', see what happens. UPDATE EMPLOYEE SET EDLEVEL = NULL WHERE EMPNO = 300001