Documentos de Académico
Documentos de Profesional
Documentos de Cultura
MySQL permite crear base de datos y tablas, insertar datos, modificarlos, eliminarlos, ordenarlos, hacer consultas y realizar muchas operaciones, etc.
Ingresando instrucciones en la lnea de comandos o embebidas en un lenguaje como PHP nos comunicamos con el servidor.
Cada sentencia debe acabar con punto y coma (;).
La sensibilidad a maysculas y minsculas, es decir, si hace diferencia entre ellas, depende del sistema operativo, Windows no es sensible, pero Linux si. Por ejemplo Windows interpreta igualmente las siguientes sentencias:
create database administracion; Create DataBase administracion; Pero Linux interpretar como un error la segunda. Se recomienda usar siempre minsculas. Es ms el sitio mysqlya.com.ar est instalado sobre un servidor Linux por lo que todos los ejercicios debern respetarse maysculas y minsculas.
Tipo de Campo TINYINT SMALLINT MEDIUMINT INT INTEGER BIGINT FLOAT(X) FLOAT DOUBLE
Tamao de Almacenamiento 1 byte 2 bytes 3 bytes 4 bytes 4 bytes 8 bytes 4 8 bytes 4 bytes 8 bytes
DOUBLE PRECISION
REAL DECIMAL(M,D) NUMERIC(M,D)
8 bytes
8 bytes
CREATE TABLE alumnos ( codigo int(11) NOT NULL auto_increment, nombre varchar(40) default NULL,
<html> <head> <title>Problema</title> </head> <body> <h1>Alta de Alumnos</h1> <form action="insertar2.php" method="post"> Ingrese nombre: <input type="text" name="nombre"><br> Ingrese mail: <input type="text" name="mail"><br> Seleccione el curso: <select name="codigocurso"> <option value="1">PHP</option> <option value="2">ASP</option> <option value="3">JSP</option> </select> <br> <input type="submit" value="Registrar"> </form> </body> </html>
Insertar.php
<html> <head> <title>Problema</title> </head> <body> <h1>Alta de Alumnos</h1> <form action="insertar2.php" method="post"> Ingrese nombre: <input type="text" name="nombre"><br> Ingrese mail: <input type="text" name="mail"><br>
Seleccione el curso: <select name="codigocurso"> <option value="1">PHP</option> <option value="2">ASP</option> <option value="3">JSP</option> </select> <br> <input type="submit" value="Registrar"> </form> </body> </html>
<html> <head> <title>insertar base de datos</title> </head> <body> <?php $conexion=mysql_connect("localhost","root","root") or die("Problemas en la conexion"); mysql_select_db("phpfacil",$conexion) or die("Problemas en la seleccion de la base de datos"); mysql_query("insert into alumnos(nombre,mail,codigocurso) values ('$_REQUEST[nombre]','$_REQUEST[mail]','$_REQUEST[codigocurso]')", $conexion) or die("Problemas en el select".mysql_error()); mysql_close($conexion); echo "El alumno fue dado de alta."; ?> </body> </html>
insertar2.php
Insetar2.php
<html> <head> <title>insertar base de datos</title> </head> <body> <?php $conexion=mysql_connect("localhost","root","root") or die("Problemas en la conexion"); mysql_select_db("phpfacil",$conexion) or die("Problemas en la seleccion de la base de datos");
CONSULTA
Seleccion1.php
<html> <head> <title> Prueba de registros</title> </head> <body> <?php $conexion=mysql_connect("localhost","root","root") or die("Problemas en la conexion"); mysql_select_db("phpfacil",$conexion) or die("Problemas en la conexion de la base de datos"); $registros=mysql_query("select codigo,nombre, mail, codigocurso from alumnos",$conexion) or die("problemas en select:".mysql_error()); while($reg=mysql_fetch_array($registros)) {
echo "Codigo: ".$reg['codigo']."<br>"; echo "Nombre: ".$reg['nombre']."<br>"; echo "Mail: ".$reg['mail']."<br>"; echo "Curso: "; switch ($reg['codigocurso']){ case 1:echo "PHP"; break; case 2:echo "ASP"; break; case 3:echo "JSP"; break; } echo "<br>"; echo "<br>"; } mysql_close($conexion); ?> </body> </html>
Consulta.php <html> <head> <title>Problema</title> </head> <body> <form action="consulta2.php" method="post"> Ingrese el mail del alumno a consultar: <input type="text" name="mail"> <br> <input type="submit" value="buscar"> </form> </body> </html>
consulta2.php 1
Consulta2.php
echo "Nombre:".$reg['nombre']."<br>"; echo "Curso:"; switch ($reg['codigocurso']) { case 1:echo "PHP"; break; case 2:echo "ASP"; break; case 3:echo "JSP"; break; }
Consulta2.php
} else { echo "No existe un alumno con ese mail."; } mysql_close($conexion); ?> </body> </html>
borrar.php <html> <head> <title>Problema</title> </head> <body> <form action="borrar2.php" method="post"> Ingrese el mail del alumno a borrar: <input type="text" name="mail"> <br> <input type="submit" value="buscar"> </form> </body> </html>
borrar2.php <html> <head> <title>Problema</title> </head> <body> <?php $conexion=mysql_connect("localhost","root","123") or die("Problemas en la conexion"); mysql_select_db("phpfacil",$conexion) or die("Problemas en la seleccin de la base de datos"); $registros=mysql_query("select codigo from alumnos where mail='$_REQUEST[mail]'",$conexion) or die("Problemas en el select:".mysql_error());
if ($reg=mysql_fetch_array($registros)) { mysql_query("delete from alumnos where mail='$_REQUEST[mail]'",$conexion) or die("Problemas en el select:".mysql_error()); echo "Se efectu el borrado del alumno con dicho mail."; } else { echo "No existe un alumno con ese mail."; } mysql_close($conexion); ?> </body> </html>
borrar2.php
<html> Borrartodo.php <head> <title>Problema</title> </head> <body> <?php $conexion=mysql_connect("localhost","root","123") or die("Problemas en la conexion"); mysql_select_db("phpfacil",$conexion) or die("Problemas en la seleccin de la base de datos"); mysql_query("delete from alumnos",$conexion) or die("Problemas en el select:".mysql_error()); echo "Se efectu el borrado de todos los alumnos."; mysql_close($conexion); ?> </body> </html>
Contador de registros
contador.php
<html> <head> <title> Borra todos los registros</title> </head> <body> <?php $conexion=mysql_connect("localhost","root","root") or die("Problemas de la conexion"); mysql_select_db("phpfacil",$conexion) or die("Problemas en la seleccion de la base de datos"); $registros=mysql_query("sELECT COUNT(*) as cantidad FROM alumnos",$conexion) or die("Problemas en el SELECT:".mysql_error()); $reg=mysql_fetch_array($registros); echo "La cantidad de alumnos inscritos son: ".$reg['cantidad']; ?> </body> </html>