Conferencia MySQL5
-
Upload
milton-labanda -
Category
Documents
-
view
416 -
download
2
description
Transcript of Conferencia MySQL5
![Page 1: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/1.jpg)
MySQL 5.0
Milton Labanda
UNIVERSIDAD NACIONAL DE LOJA
Carrera de Ingeniería en Sistemas
Enero 2006
![Page 2: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/2.jpg)
Objetivos
Actualizar conocimientos relacionados al currículum.
Revisar algunas novedades en la v 5.0 Explotar el uso de herramientas relacionadas
![Page 3: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/3.jpg)
¿Ventajas de MySQL?
Es un Sistema Gestor de Bases de Datos disponible en la mayoría de plataformas.
El servidor de Bases de Datos Open Source más popular.
Liviano y rápido: adecuado para aplicaciones Web.
Cuenta con APIs disponibles para C, C++, Perl, PHP, Python, Java, .NET
![Page 4: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/4.jpg)
Estado de MySQL
Anterior versión estable: 4.0 y 4.1
Actual versión estable: 5.0
Futura versión estable: 5.1
Se sigue utilizando las versiones 4.0 y 4.1
![Page 5: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/5.jpg)
Novedades de la Versión 5.0
Múltiples Instancias del Servidor Stored Procedures (Procedimientos
Almacenados) Triggers (Disparadores) Views (Vistas) Acceso al Metadata de las BD
![Page 6: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/6.jpg)
Configuración de Múltiples Instancias Archivo ‘my.ini’
######################################## Servidor 1 #######################################[mysqld1]datadir = "C:/mydata1/"port = 3307enable-named-pipesocket = pipe1
######################################## Servidor 2 ########################################[mysqld2]datadir = "c:/mydata2/"port = 3308enable-named-pipesocket = pipe2
![Page 7: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/7.jpg)
Configuración de Múltiples Instancias Instalación de las instancias como servicios de
windows: mysqld-nt - -install mysqld1 mysqld-nt - -install mysqld2
Arranque de las Instancias del servidor: NET START mysqld1 NET START mysqld2
![Page 8: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/8.jpg)
Stored Procedures
Un procedimiento almacenado es una rutina (procedimiento o función) que es almacenada dentro de la base de datos.
Sentencias:CREATE PROCEDURE/FUNCTIONALTER PROCEDURE/FUNCTIONDROP PROCEDURE/FUNCTION
![Page 9: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/9.jpg)
Stored Procedures: Sintaxis
Sintaxis de Creación:
CREATE PROCEDURE nombre_proc([parametro …])[característica …]cuerpo_proc
CREATE FUNCTION nombre_func([parametro …])RETURNS tipo_dato[característica …]cuerpo_func
![Page 10: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/10.jpg)
Stored Procedures
Cuerpo de la Rutina Especifica que es lo que hará la rutina Puede contener una o más sentencias SQL Puede incluir sentencias DECLARE
Sintaxis del cuerpoBEGIN
[DECLARE variables, cursores, condiciones][Sentencias SQL]
END
![Page 11: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/11.jpg)
Stored Procedures: Ejemplos
CREATE PROCEDURE p1 ()
BEGIN
DECLARE v1 INT DEFAULT 10;
SET v1 = 5;
INSERT INTO t VALUES (v1);
END;
![Page 12: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/12.jpg)
Stored Procedures: Ejemplos
CREATE PROCEDURE copiar () BEGIN DECLARE var1 VARCHAR(34);
DECLARE c1 CURSOR FOR SELECT name FROM test.t; OPEN c1; REPEAT FETH c1 INTO var1;
….END;
![Page 13: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/13.jpg)
Stored Procedures
CREATE FUNCTION saludar (nombre CHAR(20)) RETURNS CHAR (40)BEGIN DECLARE saludo CHAR(40); SET saludo = CONCAT('Hola, ‘ ,UPPER(nombre), ',
como estas?'); RETURN saludo;END
![Page 14: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/14.jpg)
Stored Procedures
Invocar a un Procedimiento almacenado: call nombre_proc([params, …]) Ejemplo:
CALL proc1( );
Invocar a una función almacenada: Generalmente: select nombre_func([params, …]) Ejemplo:
SELECT saludar(‘Enrique’);
![Page 15: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/15.jpg)
Stored Procedures: Sentencias de Control IF – si <condicion> hacer <acción> CASE – selección alternativa, dependiente LOOP – hacer <acción> repetidamente LEAVE – terminación de un bucle o bloque ITERATE – reinicia un bucle REPEAT – repetir <acción> hasta
<condición> sea verdadera WHILE – repetir <action> mientras
<condición> sea verdadera
![Page 16: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/16.jpg)
Stored Procedures
Ventajas:Mejorar el DesempeñoAhorrar tiempo, dinero y recursos
![Page 17: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/17.jpg)
Triggers
Un Trigger es una acción en cadena que empieza cuando un evento específico ocurre sobre una tabla específica.
![Page 18: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/18.jpg)
Triggers: Sintaxis
CREATE TRIGGER nombre_trigger
{BEFORE | AFTER}
{INSERT | UPDATE | DELETE} ON nombre_table
FOR EACH ROW acción_trigger | sentencias SQL
![Page 19: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/19.jpg)
Triggers: Usos
Registrar, auditar y monitorear la actividad de cambio de datos
Validar datos, cambiando o negando acciones como INSERT, UPDATE, DELETE en una tabla
Preservar la consistencia y claridad de los datos ejecutando acciones relacionadas en tablas relacionadas
![Page 20: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/20.jpg)
Triggers: Ejemplos
CREATE TRIGGER info
AFTER INSERT ON test.t
FOR EACH ROW
SET @ultimo = NEW.id;
![Page 21: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/21.jpg)
Triggers: Ejemplos
CREATE TRIGGER update_check BEFORE UPDATE ON test.t
FOR EACH ROW BEGIN IF LENGTH(NEW.name) = 0 THEN SET NEW.name = 'Nombres'; END IF;
END;
![Page 22: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/22.jpg)
Vistas
Una Vista es una tabla virtual Provee otra manera de buscar en los
datos existentes No usan espacio extra en disco La sintaxis de las Vistas MySQL son
100% compatibles con el SQL standard.
![Page 23: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/23.jpg)
Vistas: Sintaxis
CREATE [OR REPLACE]
VIEW nombre_vista
[(lista_columnas)]
AS sentencia_select
[WITH [CASCADED | LOCAL] CHECK OPTION]
![Page 24: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/24.jpg)
Vistas: Ejemplos
![Page 25: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/25.jpg)
Vistas: Ejemplos
WITH CHECK OPTION
CREATE VIEW v1 ASSELECT col1 FROM t1 WHERE col1 <> 'A'WITH CHECK OPTION;
mysql> UPDATE v1 SET col1 = 'A';ERROR 1369 (HY000): CHECK OPTION failed ‘test.v1'
![Page 26: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/26.jpg)
Vistas: Ejemplos
WITH LOCAL CHECK OPTION
CREATE VIEW v2 ASSELECT * FROM v1 WHERE col1 <> 'B'WITH LOCAL CHECK OPTION;
mysql> INSERT INTO v2 VALUES ('B');ERROR 1369 (HY000): CHECK OPTION failed ‘test.v2'
mysql> INSERT INTO v2 VALUES ('A');Query OK, 1 row affected (0.01 sec)
![Page 27: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/27.jpg)
Vistas: Ejemplos
WITH CASCADED CHECK OPTION
CREATE VIEW v3 ASSELECT * FROM v2 WHERE col1 <> 'C'WITH CASCADED CHECK OPTION;
mysql> INSERT INTO v3 VALUES ('B');ERROR 1369 (HY000): CHECK OPTION failed ‘test.v3'-- porque las condiciones de v2 no permiten 'B' y v3 depende de v2
mysql> INSERT INTO v3 VALUES ('A');ERROR 1369 (HY000): CHECK OPTION failed ‘test.v3'-- porque las condiciones de v1 no permiten 'A' and v3 depende de
v1 y v2
![Page 28: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/28.jpg)
Vistas: Actualización
Una Vista actualizable puede aceptar sentencias UPDATE, DELETE
Algunas vistas pueden permitir INSERT Los cambios de los datos se propagan
automáticamente a la tabla base.
![Page 29: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/29.jpg)
Vistas: Usos
Las vistas pueden ser usadas en lugar de definir restricciones utilizando por ejemplo NOT NULL, BETWEEN, etc.
Pueden usarse en la reestructuración de tablas, ocultando o moviendo columnas.
Permiten implementar seguridad en los datos al ocultar registros.
![Page 30: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/30.jpg)
INFORMATION_SCHEMA
El Esquema de Información es la forma SQL standard para acceder el metadata de la base de datos. Contiene una serie de tablas que describen
los objetos de la base de datosEs una base de datos virtual visible a través
de sentencias SELECT
![Page 31: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/31.jpg)
INFORMATION_SCHEMA
Ventajas:Standarización del manejo del metadata,
quedando atrás el uso de comandos SHOW específicos de MySQL
No se necesita aprender un nuevo conjunto de comandos para acceder el metadata.
![Page 32: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/32.jpg)
INFORMATION_SCHEMA
Visualización:
![Page 33: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/33.jpg)
INFORMATION_SCHEMA
Objetos mas importantes:SCHEMATATABLESVIEWSROUTINESCOLUMNS
![Page 34: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/34.jpg)
INFORMATION_SCHEMA
Objetos mas importantes:
![Page 35: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/35.jpg)
MySQL Tips Mostrar resultados verticalmente: \G
![Page 36: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/36.jpg)
MySQL Tips
Prevenir resultados inesperados al usar sentencias de modificación:Usar la opción -U al conectarse
C:>mysql –u usuario –p –U
Obliga a usar la cláusula WHERE al realizar un UPDATE O DELETE.
![Page 37: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/37.jpg)
MySQL Tips
Cambiar la contraseña del superusuario ‘root’ y renombrar el usuario
c:> mysqladmin -u root password ‘nueva_clave’
mysql> update mysql.user -> set user = ‘admin’-> where user = ‘root’
![Page 38: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/38.jpg)
MySQL Tips
Proteger el directorio de instalación del acceso de otros usuarios.
Apegarse al ANSI SQL si se requiere portabilidad
No mezclar el código de la interfaz del usuario con código de bases de datos.
Usar pool de conexiones en Servidores Web y Servidores a Aplicaciones
![Page 39: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/39.jpg)
MySQL Tips
Ejecutar sentencias sin entrar al cliente mysqlC:> mysql –u root –p –e “select * from test.t”
Exportar a XMLC:> mysql –u user –p –e “sentencias” –X > archivo.xml
Exportar a HTMLC:> mysql –u user –p –e “sentencias” –H > archivo.html
![Page 40: Conferencia MySQL5](https://reader033.fdocuments.mx/reader033/viewer/2022052601/558fcfc31a28ab3f1b8b4609/html5/thumbnails/40.jpg)
GRACIAS!