ZULIA SIM 6 B LUIS MANUEL SOTO GARCIA BASES DE DATOS

Anuncio
MARACAIBO, 15 DE NOVIEMBRE DEL 2010
UNEFA – ZULIA SIM 6 B
LUIS MANUEL SOTO GARCIA
BASES DE DATOS
TERMINAL
lab1@canaima:~$ xampp start
Starting XAMPP for Linux 1.7.3a...
XAMPP: Starting Apache with SSL (and PHP5)...
XAMPP: Starting MySQL...
XAMPP: Starting ProFTPD...
XAMPP for Linux started.
lab1@canaima:~$ mysql -h localhost -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.41 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database tienda;
ERROR 1007 (HY000): Can't create database 'tienda'; database exists
mysql> create database bicentenario;
Query OK, 1 row affected (0,01 sec)
mysql> use bicentenario;
Database changed
mysql> create table proveedor (ID_pro bigint not null primary key auto_increment,RIF varchar(15),Nombre
varchar(15),Direccion varchar(15), Telefono varchar(15));
Query OK, 0 rows affected (0,00 sec)
mysql> drop table proveedor;
Query OK, 0 rows affected (0,00 sec)
mysql> create table proveedor(ID_pro bigint auto_increment primary
varchar(15),Direccion varchar(15),Telefono varchar(15))type=innodb;
key,Rif varchar(15),Nombre
Query OK, 0 rows affected, 1 warning (0,00 sec)
mysql> create table producto(ID_produ bigint auto_increment primary key, cod_producto varchar(12),
Nombre varchar(20), Precio double, ID_pro bigint, index (ID_pro), foreign key (ID_pro) references proveedor
(ID_pro) on update cascade on delete cascade)type=innodb;
Query OK, 0 rows affected, 1 warning (0,00 sec)
mysql> create table cliente(ID_cli bigint auto_increment primary key, Nombre varchar(20),Cedula
varchar(15),Direccion varchar(15), telefono varchar(15),ID_produ bigint, index(ID_produ), foreign
key(ID_produ) references producto (ID_produ) on update cascade on delete cascade) type=innodb;
Query OK, 0 rows affected, 1 warning (0,01 sec)
mysql> insert into proveedor values(null,"J-12345678-9","Empresas Polar","Km4","0261-7894561");
Query OK, 1 row affected (0,01 sec)
mysql> insert into proveedor values(null,"J-78945612-3","Coca Cola","Av - Goajira","0261-14253");
Query OK, 1 row affected (0,01 sec)
mysql> insert into proveedor values(null,"J-74859612-3","Camprolac","Haticos","0261-1245781");
Query OK, 1 row affected (0,00 sec)
mysql> select*from proveedor;
+--------+--------------+----------------+--------------+--------------+
| ID_pro | Rif
| Nombre
| Direccion | Telefono
|
+--------+--------------+----------------+--------------+--------------+
|
1 | J-12345678-9 | Empresas Polar | Km4
|
2 | J-78945612-3 | Coca Cola
|
3 | J-74859612-3 | Camprolac
| 0261-7894561 |
| Av - Goajira | 0261-14253 |
| Haticos
| 0261-1245781 |
+--------+--------------+----------------+--------------+--------------+
3 rows in set (0,00 sec)
mysql> insert into producto values(null,"789456-1","Cerveza","Bs.F 3,00","1");
Query OK, 1 row affected, 1 warning (0,00 sec)
mysql> insert into producto values(null,"745812-9","Malta","Bs.F 3,50","1");
Query OK, 1 row affected, 1 warning (0,01 sec)
mysql> insert into producto values(null,"752631-4","Arroz mary","Bs.F 3,66","1");
Query OK, 1 row affected, 1 warning (0,00 sec)
mysql> insert into producto values(null,"7142536-8","Leche","Bs.F 15,66","3");
Query OK, 1 row affected, 1 warning (0,01 sec)
mysql>
PRODUCTO CARTESIANO
Consulta con 2 tablas (Relacion binaria)
select, from, where
select campo1,campo2 from tabla1,tabla2
where tabla2.fk=tabla1.fk;
Ej:
select producto.nombre,proveedor.nombre
from producto,proveedor where
producto.id_prov=proveedor.id_prov;
SENTENCIAS JOIN Y INNER JOIN
mysql> use clinicas;
Database changed
mysql> create table clinica(ID_Clinica bigint auto_increment primary key, Nombre_Clinica varchar(40),
Direccion varchar(40), ID_Paciente bigint, index (ID_Paciente), foreign key (ID_Paciente) references PACIENTE
(ID_Paciente) on update cascade on delete cascade,ID_Doctor bigint, index (ID_Doctor), foreign key
(ID_Doctor) references DOCTOR (ID_Doctor) on update cascade on delete restrict)Type=innodb;
Query OK, 0 rows affected, 1 warning (0.15 sec)
mysql> insert into clinica values(null,"Clinica Zulia","Sabaneta","2","3");
Query OK, 1 row affected (0.04 sec)
mysql> insert into clinica values(null,"Clinica Sagrada Familia","Amparo","1","2");
Query OK, 1 row affected (0.02 sec)
mysql> insert into clinica values(null,"Clinica Paraiso","Av. Universidad","3","3");
Query OK, 1 row affected (0.02 sec)
mysql> select*from paciente;
+-------------+-----------------+-------------------+----------+--------------+
| ID_PACIENTE | Nombre_Paciente | Apellido_Paciente | Cedula | Telefono
+-------------+-----------------+-------------------+----------+--------------+
|
1 | Ruben Antonio | Alvarez Moreno | 20429581 | 0424-7645209 |
|
2 | Eduardo Jose | Briceno Gutierrez | 18218749 | 0424-6142319 |
|
3 | Cesar Andres | Estrada Carmona | 19681259 | 0416-4644478 |
+-------------+-----------------+-------------------+----------+--------------+
3 rows in set (0.00 sec)
|
mysql> select*from doctor;
+-----------+------------+---------------------+-----------------+--------------+-------------+
| ID_DOCTOR | Cod_Doctor | Nombre_Doctor
| Apellido_Doctor | Especialidad| ID_PACIENTE |
+-----------+------------+---------------------+-----------------+--------------+-------------+
|
2 | 4885
| Mariara del Mar
| Viloria Nava | Pediatra|
3|
|
3 | 5896
| Leandro Joaquin
| Bracho Garcia | Traumatologo|
|
4 | 7825
| Norlangri del Valle | Toloza Morales | Oftamologia|
1|
2|
+-----------+------------+---------------------+-----------------+--------------+-------------+
3 rows in set (0.00 sec)
mysql> insert into clinica values(null,"Clinica Los Olivos","Av. La Limpia","3","1");
Query OK, 1 row affected (0.04 sec)
mysql> select*from clinica;
+------------+-------------------------+-----------------+-------------+-----------+
| ID_Clinica | Nombre_Clinica
| Direccion
| ID_Paciente | ID_Doctor |
+------------+-------------------------+-----------------+-------------+-----------+
|
1 | Clinica Zulia
| Sabaneta
|
3 | Clinica Sagrada Familia | Amparo
|
5 | Clinica Paraiso
|
6 | Clinica Los Olivos
|
2|3|
|
1|2|
| Av. Universidad |
3|3|
| Av. La Limpia |
3|1|
+------------+-------------------------+-----------------+-------------+-----------+
4 rows in set (0.00 sec)
mysql> select*from clinica where Direccion like"A%";"
+------------+-------------------------+-----------------+-------------+-----------+
| ID_Clinica | Nombre_Clinica
| Direccion
| ID_Paciente | ID_Doctor |
+------------+-------------------------+-----------------+-------------+-----------+
|
3 | Clinica Sagrada Familia | Amparo
|
5 | Clinica Paraiso
|
6 | Clinica Los Olivos
|
1|2|
| Av. Universidad |
3|3|
| Av. La Limpia |
3|1|
+------------+-------------------------+-----------------+-------------+-----------+
3 rows in set (0.00 sec)
mysql> select*from paciente where cedula like "1%";
+-------------+-----------------+-------------------+----------+--------------+
| ID_PACIENTE | Nombre_Paciente | Apellido_Paciente | Cedula | Telefono
|
+-------------+-----------------+-------------------+----------+--------------+
|
2 | Eduardo Jose | Briceno Gutierrez | 18218749 | 0424-6142319 |
|
3 | Cesar Andres | Estrada Carmona | 19681259 | 0416-4644478 |
+-------------+-----------------+-------------------+----------+--------------+
2 rows in set (0.00 sec)
mysql> select*from doctor where nombre_doctor like "m%";
+-----------+------------+-----------------+-----------------+--------------+-------------+
| ID_DOCTOR | Cod_Doctor | Nombre_Doctor | Apellido_Doctor | Especialidad | ID_PACIENTE |
+-----------+------------+-----------------+-----------------+--------------+-------------+
|
2 | 4885
| Mariara del Mar | Viloria Nava | Pediatra
|
3|
+-----------+------------+-----------------+-----------------+--------------+-------------+
1 row in set (0.00 sec)
mysql> select*from doctor where nombre_doctor like "n%";
+-----------+------------+---------------------+-----------------+--------------+-------------+
| ID_DOCTOR | Cod_Doctor | Nombre_Doctor
| Apellido_Doctor | Especialidad| ID_PACIENTE |
+-----------+------------+---------------------+-----------------+--------------+-------------+
|
1 | 7825
| Norlangri del Valle | Toloza Morales | Oftamologia|
2|
+-----------+------------+---------------------+-----------------+--------------+-------------+
1 row in set (0.00 sec)
mysql> select*from paciente;
+-------------+-----------------+-------------------+----------+--------------+
| ID_PACIENTE | Nombre_Paciente | Apellido_Paciente | Cedula | Telefono
|
+-------------+-----------------+-------------------+----------+--------------+
|
1 | Ruben Antonio | Alvarez Moreno | 20429581 | 0424-7645209 |
|
2 | Eduardo Jose | Briceno Gutierrez | 18218749 | 0424-6142319 |
|
3 | Cesar Andres | Estrada Carmona | 19681259 | 0416-4644478 |
+-------------+-----------------+-------------------+----------+--------------+
3 rows in set (0.00 sec)
mysql> select*from doctor;
+-----------+------------+---------------------+-----------------+--------------+-------------+
| ID_DOCTOR | Cod_Doctor | Nombre_Doctor
| Apellido_Doctor | Especialidad| ID_PACIENTE |
+-----------+------------+---------------------+-----------------+--------------+-------------+
|
1 | 7825
| Norlangri del Valle | Toloza Morales | Oftamologia|
2|
|
2 | 4885
| Mariara del Mar
| Viloria Nava | Pediatra|
|
3 | 5896
| Leandro Joaquin
| Bracho Garcia | Traumatologo|
3|
+-----------+------------+---------------------+-----------------+--------------+-------------+
3 rows in set (0.00 sec)
1|
mysql> select Nombre_Paciente as Paciente,Nombre_Doctor,Especialidad as Titulo from paciente join doctor
on paciente.ID_Paciente=doctor.ID_Doctor;
+---------------+---------------------+--------------+
| Paciente
| Nombre_Doctor
| Titulo
|
+---------------+---------------------+--------------+
| Ruben Antonio | Norlangri del Valle | Oftamologia |
| Eduardo Jose | Mariara del Mar
| Pediatra
|
| Cesar Andres | Leandro Joaquin
| Traumatologo |
+---------------+---------------------+--------------+
3 rows in set (0.01 sec)
mysql> select Nombre_Paciente as Paciente,Nombre_Doctor as Doctor,Especialidad as Area,Nombre_Clinica as
Clinica from(paciente inner join doctor on paciente.ID_Paciente=doctor.ID_Paciente) inner join clinica on
doctor.ID_Doctor=clinica.ID_Doctor;
+---------------+---------------------+--------------+-------------------------+
| Paciente
| Doctor
| Area
| Clinica
|
+---------------+---------------------+--------------+-------------------------+
| Eduardo Jose | Norlangri del Valle | Oftamologia | Clinica Los Olivos |
| Cesar Andres | Mariara del Mar | Pediatra | Clinica Sagrada Familia |
| Ruben Antonio | Leandro Joaquin | Traumatologo | Clinica Zulia
|
| Ruben Antonio | Leandro Joaquin | Traumatologo | Clinica Paraiso
|
+---------------+---------------------+--------------+-------------------------+
4 rows in set (0.02 sec)
mysql>
NORMALIZACION
A través del siguiente ejercicio se intenta afirmar los conocimientos de normalización con un ejemplo
simplificado de una base de datos para una pequeña biblioteca.
CodLibro
Titulo
Autor
Editorial
McGraw
Hill
NombreLector
Pérez Gómez,
Juan
FechaDev
1001
Variable
compleja
Murray
Spiegel
1004
Visual Basic 5
E. Petroustsos Anaya
Ríos Terán, Ana 17/04/2005
1005
Estadística
Murray
Spiegel
McGraw
Hill
Roca, René
16/04/2005
1006
Oracle
University
Nancy
Greenberg y
Priya Nathan
Oracle
Corp.
García Roque,
Luis
20/04/2005
1007
Clipper 5.01
Ramalho
McGraw
Hill
Pérez Gómez,
Juan
18/04/2005
15/04/2005
Esta tabla no cumple el requisito de la Primera Forma Normal (1NF) de sólo tener campos atómicos, pues el
nombre del lector es un campo que puede (y conviene) descomponerse en apellido paterno, apellido materno
y nombres. Tal como se muestra en la siguiente tabla.
1NF
CodLibro
Titulo
Autor
Murray
Spiegel
Editorial Paterno Materno Nombres
1001
Variable
compleja
McGraw
Pérez
Hill
1004
Visual Basic E.
Anaya
5
Petroustsos
1005
Estadística
Murray
Spiegel
McGraw
Roca
Hill
1006
Oracle
University
Nancy
Greenberg
Oracle
Corp.
García
1006
Oracle
University
Priya
Nathan
Oracle
Corp.
García
1007
Clipper
5.01
Ramalho
McGraw
Pérez
Hill
Ríos
Como se puede ver, hay cierta redundancia característica de 1NF.
FechaDev
Gómez
Juan
15/04/2005
Terán
Ana
17/04/2005
René
16/04/2005
Roque
Luis
20/04/2005
Roque
Luis
20/04/2005
Gómez
Juan
18/04/2005
La Segunda Forma Normal (2NF) pide que no existan dependencias parciales o dicho de otra manera, todos los
atributos no clave deben depender por completo de la clave primaria. Actualmente en nuestra tabla tenemos
varias dependencias parciales si consideramos como atributo clave el código del libro.
Por ejemplo, el título es completamente identificado por el código del libro, pero el nombre del lector en
realidad no tiene dependencia de este código, por tanto estos datos deben ser trasladados a otra tabla.
2NF
CodLibro
Titulo
Autor
Editorial
1001
Variable compleja
Murray
Spiegel
McGraw
Hill
1004
Visual Basic 5
E.
Petroustsos
Anaya
1005
Estadística
Murray
Spiegel
McGraw
Hill
1006
Oracle University
Nancy
Greenberg
Oracle
Corp.
1006
Oracle University
Priya Nathan
Oracle
Corp.
1007
Clipper 5.01
Ramalho
McGraw
Hill
La nueva tabla sólo contendrá datos del lector.
CodLector Paterno Materno Nombres
501
Pérez
Gómez
Juan
502
Ríos
Terán
Ana
503
Roca
504
García
René
Roque
Luis
Hemos creado una tabla para contener los datos del lector y también tuvimos que crear la columna CodLector
para identificar unívocamente a cada uno. Sin embargo, esta nueva disposición de la base de datos necesita
que exista otra tabla para mantener la información de qué libros están prestados a qué lectores. Esta tabla se
muestra a continuación:
CodLibro CodLector
FechaDev
1001
501 15/04/2005
1004
502 17/04/2005
1005
503 16/04/2005
1006
504 20/04/2005
1007
501 18/04/2005
Para la Tercera Forma Normal (3NF) la relación debe estar en 2NF y además los atributos no clave deben ser
mutuamente independientes y dependientes por completo de la clave primaria. También recordemos que
dijimos que esto significa que las columnas en la tabla deben contener solamente información sobre la
entidad definida por la clave primaria y, por tanto, las columnas en la tabla deben contener datos acerca de
una sola cosa. En nuestro ejemplo en 2NF, la primera tabla conserva información acerca del libro, los autores y
editoriales, por lo que debemos crear nuevas tablas para satisfacer los requisitos de 3NF.
3NF
CodLibro
Titulo
CodAutor
Autor
Variable
1001 compleja
Murray
801 Spiegel
1004 Visual Basic 5
E.
802 Petroustsos
1005 Estadística
Oracle
1006 University
1007 Clipper 5.01
Nancy
803 Greenberg
804 Priya Nathan
806 Ramalho
CodEditorial
Editorial
McGraw
901 Hill
902 Anaya
Oracle
903 Corp.
Aunque hemos creado nuevas tablas para que cada una tenga sólo información acerca de una entidad,
también hemos perdido la información acerca de qué autor ha escrito qué libro y las editoriales
correspondientes, por lo que debemos crear otras tablas que relacionen cada libro con sus autores y
editoriales.
CodLibro
codAutor
1001
801
1004
802
1005
801
1006
803
1006
804
1007
806
CodLibro
codEditorial
1001
901
1004
902
1005
901
1006
903
1007
901
Y el resto de las tablas no necesitan modificación.
CodLector Paterno Materno Nombres
CodLibro
CodLector
FechaDev
MARACAIBO, 17 DE ENERO DEL 2011
BASES DE DATOS.
SENTENCIAS JOIN
mysql> create database prueba;
Query OK, 1 row affected (0,01 sec)
mysql> use prueba
Database changed
mysql> create table turno(id_t bigint auto_increment primary key, turno varchar(20))type=innodb;
Query OK, 0 rows affected, 1 warning (0,03 sec)
mysql> create table curso(id_c bigint auto_increment primary key,Nombre varchar(20),id_t bigint,index
(id_t),foreign key (id_t) references turno (id_t) on update cascade on delete cascade)type=innodb;
Query OK, 0 rows affected, 1 warning (0,02 sec)
mysql> create table estudiante(id_e bigint auto_increment primary key,Nombre varchar(20),apellido
varchar(20),Direccion varchar(20),Telefono varchar(20),id_c bigint,index (id_c),foreign key (id_c) references
curso (id_c) on update cascade on delete cascade)type=innodb;
Query OK, 0 rows affected, 1 warning (0,01 sec)
mysql> insert into turno values(null,"Matutino");
Query OK, 1 row affected (0,00 sec)
mysql> insert into turno values(null,"Vespertino");
Query OK, 1 row affected (0,01 sec)
mysql> insert into turno values(null,"Nocturno");
Query OK, 1 row affected (0,01 sec)
mysql> insert into curso values(null,"Datos","1");
Query OK, 1 row affected (0,01 sec)
mysql> insert into curso values(null,"Datos","2");
Query OK, 1 row affected (0,00 sec)
mysql> insert into curso values(null,"null","1");
Query OK, 1 row affected (0,00 sec)
mysql> insert into curso values(null,"null","2");
Query OK, 1 row affected (0,01 sec)
mysql> insert into estudiante values(null,"mariara","viloria","mara norte","0424-6694512","1");
Query OK, 1 row affected (0,01 sec)
mysql> insert into estudiante values(null,"norlangri","toloza","sierra maestra","0424-6678411","2");
Query OK, 1 row affected (0,00 sec)
mysql> insert into estudiante values(null,"ronald","contreras","la limpia","0424-78451411","1");
Query OK, 1 row affected (0,01 sec)
mysql> insert into estudiante values(null,"null","null","null","null","1");
Query OK, 1 row affected (0,00 sec)
mysql> insert into estudiante values(null,"null","null","null","null","2");
Query OK, 1 row affected (0,00 sec)
mysql> select*from estudiante;
+------+-----------+-----------+----------------+---------------+------+
| id_e | Nombre | apellido | Direccion
| Telefono
| id_c |
+------+-----------+-----------+----------------+---------------+------+
| 1 | mariara | viloria | mara norte
| 0424-6694512 | 1 |
| 2 | norlangri | toloza | sierra maestra | 0424-6678411 | 2 |
| 4 | ronald | contreras | la limpia
| 0424-78451411 | 1 |
| 5 | null
| null
| null
| null
| 1|
| 6 | null
| null
| null
| null
| 2|
+------+-----------+-----------+----------------+---------------+------+
5 rows in set (0,00 sec)
mysql> select*from curso;
+------+--------+------+
| id_c | Nombre | id_t |
+------+--------+------+
| 1 | Datos | 1 |
| 2 | Datos | 2 |
| 4 | null | 1 |
| 5 | null | 2 |
+------+--------+------+
4 rows in set (0,00 sec)
mysql> select*from turno;
+------+------------+
| id_t | turno
|
+------+------------+
| 1 | Matutino |
| 2 | Vespertino |
| 3 | Nocturno |
+------+------------+
3 rows in set (0,00 sec)
mysql> select estudiante.nombre,curso.nombre from estudiante right outer join curso on
estudiante.id_c=curso.id_c;
+-----------+--------+
| nombre | nombre |
+-----------+--------+
| mariara | Datos |
| ronald | Datos |
| null
| Datos |
| norlangri | Datos |
| null
| Datos |
| NULL
| null |
| NULL
| null |
+-----------+--------+
7 rows in set (0,00 sec)
mysql> select estudiante.nombre as estudiante,curso.nombre as curso from estudiante left outer join curso on
estudiante.id_c=curso.id_c;
+------------+-------+
| estudiante | curso |
+------------+-------+
| mariara | Datos |
| norlangri | Datos |
| ronald
| Datos |
| null
| Datos |
| null
| Datos |
+------------+-------+
5 rows in set (0,00 sec)
mysql> select curso.nombre, turno,estudiante.nombre,apellido from (curso left outer join turno on curso
.id_t=turno.id_t) left outer join estudiante on estudiante.id_c=curso.id_c;
+--------+------------+-----------+-----------+
| nombre | turno
| nombre | apellido |
+--------+------------+-----------+-----------+
| Datos | Matutino | mariara | viloria |
| Datos | Matutino | ronald | contreras |
| Datos | Matutino | null
| null
|
| Datos | Vespertino | norlangri | toloza |
| Datos | Vespertino | null
| null
| null | Matutino | NULL
mysql> create database prueba;
Query OK, 1 row affected (0,01 sec)
mysql> use prueba
Database changed
|
mysql> create table turno(id_t bigint auto_increment primary key, turno varchar(20))type=innodb;
Query OK, 0 rows affected, 1 warning (0,03 sec)
mysql> create table curso(id_c bigint auto_increment primary key,Nombre varchar(20),id_t bigint,index
(id_t),foreign key (id_t) references turno (id_t) on update cascade on delete cascade)type=innodb;
Query OK, 0 rows affected, 1 warning (0,02 sec)
mysql> create table estudiante(id_e bigint auto_increment primary key,Nombre varchar(20),apellido
varchar(20),Direccion varchar(20),Telefono varchar(20),id_c bigint,index (id_c),foreign key (id_c) references
curso (id_c) on update cascade on delete cascade)type=innodb;
Query OK, 0 rows affected, 1 warning (0,01 sec)
mysql> insert into turno values(null,"Matutino");
Query OK, 1 row affected (0,00 sec)
mysql> insert into turno values(null,"Vespertino");
Query OK, 1 row affected (0,01 sec)
mysql> insert into turno values(null,"Nocturno");
Query OK, 1 row affected (0,01 sec)
mysql> insert into curso values(null,"Datos","1");
Query OK, 1 row affected (0,01 sec)
mysql> insert into curso values(null,"Datos","2");
Query OK, 1 row affected (0,00 sec)
mysql> insert into curso values(null,"null","1");
Query OK, 1 row affected (0,00 sec)
mysql> insert into curso values(null,"null","2");
Query OK, 1 row affected (0,01 sec)
mysql> insert into estudiante values(null,"mariara","viloria","mara norte","0424-6694512","1");
Query OK, 1 row affected (0,01 sec)
mysql> insert into estudiante values(null,"norlangri","toloza","sierra maestra","0424-6678411","2");
Query OK, 1 row affected (0,00 sec)
mysql> insert into estudiante values(null,"ronald","contreras","la limpia","0424-78451411","1");
Query OK, 1 row affected (0,01 sec)
mysql> insert into estudiante values(null,"null","null","null","null","1");
Query OK, 1 row affected (0,00 sec)
mysql> insert into estudiante values(null,"null","null","null","null","2");
Query OK, 1 row affected (0,00 sec)
mysql> select*from estudiante;
+------+-----------+-----------+----------------+---------------+------+
| id_e | Nombre | apellido | Direccion
| Telefono
| id_c |
+------+-----------+-----------+----------------+---------------+------+
| 1 | mariara | viloria | mara norte
| 0424-6694512 | 1 |
| 2 | norlangri | toloza | sierra maestra | 0424-6678411 | 2 |
| 4 | ronald | contreras | la limpia
| 0424-78451411 | 1 |
| 5 | null
| null
| null
| null
| 1|
| 6 | null
| null
| null
| null
| 2|
+------+-----------+-----------+----------------+---------------+------+
5 rows in set (0,00 sec)
mysql> select*from curso;
+------+--------+------+
| id_c | Nombre | id_t |
+------+--------+------+
| 1 | Datos | 1 |
| 2 | Datos | 2 |
| 4 | null | 1 |
| 5 | null | 2 |
+------+--------+------+
4 rows in set (0,00 sec)
mysql> select*from turno;
+------+------------+
| id_t | turno
|
+------+------------+
| 1 | Matutino |
| 2 | Vespertino |
| 3 | Nocturno |
+------+------------+
3 rows in set (0,00 sec)
mysql> select estudiante.nombre,curso.nombre from estudiante right outer join curso on
estudiante.id_c=curso.id_c;
+-----------+--------+
| nombre | nombre |
+-----------+--------+
| mariara | Datos |
| ronald | Datos |
| null
| Datos |
| norlangri | Datos |
| null
| Datos |
| NULL
| null |
| NULL
| null |
+-----------+--------+
7 rows in set (0,00 sec)
mysql> select estudiante.nombre as estudiante,curso.nombre as curso from estudiante left outer join curso on
estudiante.id_c=curso.id_c;
+------------+-------+
| estudiante | curso |
+------------+-------+
| mariara | Datos |
| norlangri | Datos |
| ronald
| Datos |
| null
| Datos |
| null
| Datos |
+------------+-------+
5 rows in set (0,00 sec)
mysql> select curso.nombre, turno,estudiante.nombre,apellido from (curso left outer join turno on curso
.id_t=turno.id_t) left outer join estudiante on estudiante.id_c=curso.id_c;
+--------+------------+-----------+-----------+
| nombre | turno
| nombre | apellido |
+--------+------------+-----------+-----------+
| Datos | Matutino | mariara | viloria |
| Datos | Matutino | ronald | contreras |
| Datos | Matutino | null
| null
|
| Datos | Vespertino | norlangri | toloza |
| Datos | Vespertino | null
| null
|
| null | Matutino | NULL
| NULL
|
| null | Vespertino | NULL
| NULL
|
+--------+------------+-----------+-----------+
7 rows in set (0,00 sec)
mysql> select curso.nombre as curso, turno,estudiante.nombre as estudiante,apellido from (curso left outer
join turno on curso .id_t=turno.id_t) left outer join estudiante on estudiante.id_c=curso.id_c;
+-------+------------+------------+-----------+
| curso | turno
| estudiante | apellido |
+-------+------------+------------+-----------+
| Datos | Matutino | mariara | viloria |
| Datos | Matutino | ronald
| Datos | Matutino | null
| contreras |
| null
|
| Datos | Vespertino | norlangri | toloza |
| Datos | Vespertino | null
| null
| null | Matutino | NULL
| NULL
|
| null | Vespertino | NULL
| NULL
|
+-------+------------+------------+-----------+
7 rows in set (0,00 sec)
|
mysql> select curso.nombre as curso, turno,estudiante.nombre as estudiante,apellido from (curso left outer
join turno on curso .id_t=turno.id_t) left outer join estudiante on estudiante.id_c=curso.id_c;
+-------+------------+------------+-----------+
| curso | turno
| estudiante | apellido |
+-------+------------+------------+-----------+
| Datos | Matutino | mariara | viloria |
| Datos | Matutino | ronald
| Datos | Matutino | null
| contreras |
| null
|
| Datos | Vespertino | norlangri | toloza |
| Datos | Vespertino | null
| null
| null | Matutino | NULL
| NULL
|
| null | Vespertino | NULL
| NULL
|
+-------+------------+------------+-----------+
7 rows in set (0,00 sec)
mysql>
|
COMANDOS
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database clinica_caracas;
Query OK, 1 row affected (0.00 sec)
mysql> use clinica_caracas;
Database changed
mysql> create table sala(id_sala bigint auto_increment primary key,codigo varchar(10),nombre_sala
varchar(15), cantida_cama varchar(15))type=innodb;
Query OK, 0 rows affected, 1 warning (0.15 sec)
mysql> create table hospital(id_cod_hosp bigint auto_increment primary key,cod_hosp
varchar(12),nombre_hosp varchar(12),direccion_hosp varchar(12),telefono_hosp varchar(12))type=innodb;
Query OK, 0 rows affected, 1 warning (0.07 sec)
mysql> create table laboratorio(id_lab bigint auto_increment primary key,cod_lab varchar(12),nombre_lab
varchar(12),direccion_lab varchar(12),telefono_lab varchar(12),id_cod_hosp bigint,index
(id_cod_hosp),foreign key (id_cod_hosp) references hospital (id_cod_hosp) on update cascade on delete
cascade)type=innodb;
Query OK, 0 rows affected, 1 warning (0.09 sec)
mysql> create table doctor(id_doc bigint auto_increment primary key,cod_doc varchar(12),nombre_doctor
varchar(12),apellido varchar(12), direccion varchar(12),telefono varchar(12),especialidad varchar(12),
id_cod_hosp bigint,index (id_cod_hosp),foreign key (id_cod_hosp) references hospital (id_cod_hosp) on
update cascade on delete cascade)type=innodb;
Query OK, 0 rows affected, 1 warning (0.13 sec)
mysql> create table pacientes(id_p bigint auto_increment primary key, cedula varchar(12),nombre_p
varchar(12), apellido_p varchar(12), direccion_p varchar(12),fecha_na varchar(12),telefono_p varchar(12),
sexo_p varchar(12),id_doc bigint,index(id_doc),foreign key(id_doc) references doctor(id_doc) on update
cascade on delete cascade,id_sala bigint, index (id_sala),foreign key (id_sala) references sala (id_sala) on
update cascade on delete cascade)type=innodb;
Query OK, 0 rows affected, 1 warning (0.13 sec)
mysql> select*from sala;
Empty set (0.06 sec)
mysql> describe sala;
+--------------+-------------+------+-----+---------+----------------+
| Field
| Type
| Null | Key | Default | Extra
|
+--------------+-------------+------+-----+---------+----------------+
| id_sala | bigint(20) | NO | PRI | NULL | auto_increment |
| codigo
| varchar(10) | YES | | NULL |
|
| nombre_sala | varchar(15) | YES | | NULL |
|
| cantida_cama | varchar(15) | YES | | NULL |
|
+--------------+-------------+------+-----+---------+----------------+
4 rows in set (0.05 sec)
mysql> insert into sala values(null,"45","Maternida","20");
Query OK, 1 row affected (0.08 sec)
mysql> select*from sala;
+---------+--------+-------------+--------------+
| id_sala | codigo | nombre_sala | cantida_cama |
+---------+--------+-------------+--------------+
|
1 | 45 | Maternida | 20
|
+---------+--------+-------------+--------------+
1 row in set (0.00 sec)
mysql> insert into sala values(null,"49","pedriatria","15");
Query OK, 1 row affected (0.03 sec)
mysql> select*from sala;
+---------+--------+-------------+--------------+
| id_sala | codigo | nombre_sala | cantida_cama |
+---------+--------+-------------+--------------+
|
1 | 45 | Maternida | 20
|
|
2 | 49 | pedriatria | 15
|
+---------+--------+-------------+--------------+
2 rows in set (0.00 sec)
mysql> insert into sala values(null,"null","null","null");
Query OK, 1 row affected (0.03 sec)
mysql> select*from sala;
+---------+--------+-------------+--------------+
| id_sala | codigo | nombre_sala | cantida_cama |
+---------+--------+-------------+--------------+
|
1 | 45 | Maternida | 20
|
|
2 | 49 | pedriatria | 15
|
|
3 | null | null
| null
|
+---------+--------+-------------+--------------+
3 rows in set (0.00 sec)
mysql> select*from hospital;
Empty set (0.00 sec)
mysql> describe hospital;
+----------------+-------------+------+-----+---------+----------------+
| Field
| Type
| Null | Key | Default | Extra
|
+----------------+-------------+------+-----+---------+----------------+
| id_cod_hosp | bigint(20) | NO | PRI | NULL | auto_increment |
| cod_hosp
| varchar(12) | YES | | NULL |
|
| nombre_hosp | varchar(12) | YES | | NULL |
|
| direccion_hosp | varchar(12) | YES | | NULL |
|
| telefono_hosp | varchar(12) | YES | | NULL |
|
+----------------+-------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)
mysql> insert into hospital values(null,"83","chiquinquira","centro","1234562");
Query OK, 1 row affected (0.02 sec)
mysql> insert into hospital values(null,"41","pons","zona norte","12554562");
Query OK, 1 row affected (0.02 sec)
mysql> select*from hospital;
+-------------+----------+--------------+----------------+---------------+
| id_cod_hosp | cod_hosp | nombre_hosp | direccion_hosp | telefono_hosp |
+-------------+----------+--------------+----------------+---------------+
|
1 | 83
| chiquinquira | centro
| 1234562
|
|
2 | 41
| pons
| zona norte | 12554562 |
+-------------+----------+--------------+----------------+---------------+
2 rows in set (0.00 sec)
mysql> describe laboratorio;
+---------------+-------------+------+-----+---------+----------------+
| Field
| Type
| Null | Key | Default | Extra
|
+---------------+-------------+------+-----+---------+----------------+
| id_lab
| bigint(20) | NO | PRI | NULL | auto_increment |
| cod_lab
| varchar(12) | YES | | NULL |
|
| nombre_lab | varchar(12) | YES | | NULL |
|
| direccion_lab | varchar(12) | YES | | NULL |
|
| telefono_lab | varchar(12) | YES | | NULL |
|
| id_cod_hosp | bigint(20) | YES | MUL | NULL |
|
+---------------+-------------+------+-----+---------+----------------+
6 rows in set (0.02 sec)
mysql> insert into laboratorio values(null,"486","banco de sangre","zona sur","124562","2");
Query OK, 1 row affected, 1 warning (0.05 sec)
mysql> insert into laboratorio values(null,"486","banco de sangre","zona sur","124562","1");
Query OK, 1 row affected, 1 warning (0.02 sec)
mysql> select*from laboratorio;
+--------+---------+--------------+---------------+--------------+-------------+
| id_lab | cod_lab | nombre_lab | direccion_lab | telefono_lab | id_cod_hosp |
+--------+---------+--------------+---------------+--------------+-------------+
| 1 | 486 | banco de san | zona sur | 124562
|
2|
| 2 | 486 | banco de san | zona sur | 124562
|
1|
+--------+---------+--------------+---------------+--------------+-------------+
2 rows in set (0.00 sec)
mysql> describe doctor;
+---------------+-------------+------+-----+---------+----------------+
| Field
| Type
| Null | Key | Default | Extra
|
+---------------+-------------+------+-----+---------+----------------+
| id_doc
| bigint(20) | NO | PRI | NULL | auto_increment |
| cod_doc
| varchar(12) | YES | | NULL |
|
| nombre_doctor | varchar(12) | YES | | NULL |
|
| apellido | varchar(12) | YES | | NULL |
|
| direccion | varchar(12) | YES | | NULL |
|
| telefono | varchar(12) | YES | | NULL |
|
| especialidad | varchar(12) | YES | | NULL |
|
| id_cod_hosp | bigint(20) | YES | MUL | NULL |
|
+---------------+-------------+------+-----+---------+----------------+
8 rows in set (0.04 sec)
mysql> insert into doctor values(null,"481","Luis","soto","los olivos","485555","ginecologo","2");
Query OK, 1 row affected (0.03 sec)
mysql> insert into doctor values(null,"478411","yingfa","sang","la curva","4851616165","cardiologo","1");
Query OK, 1 row affected (0.03 sec)
mysql> select*from doctor;
+--------+---------+---------------+----------+------------+------------+--------------+-------------+
| id_doc | cod_doc | nombre_doctor | apellido | direccion | telefono | especialidad | id_cod_hosp |
+--------+---------+---------------+----------+------------+------------+--------------+-------------+
| 1 | 481 | Luis
| soto | los olivos | 485555 | ginecologo |
2|
| 2 | 478411 | yingfa
| sang | la curva | 4851616165 | cardiologo |
1|
+--------+---------+---------------+----------+------------+------------+--------------+-------------+
2 rows in set (0.00 sec)
mysql> describe pacientes;
+-------------+-------------+------+-----+---------+----------------+
| Field
| Type
| Null | Key | Default | Extra
|
+-------------+-------------+------+-----+---------+----------------+
| id_p
| bigint(20) | NO | PRI | NULL | auto_increment |
| cedula | varchar(12) | YES | | NULL |
|
| nombre_p | varchar(12) | YES | | NULL |
|
| apellido_p | varchar(12) | YES | | NULL |
|
| direccion_p | varchar(12) | YES | | NULL |
|
| fecha_na | varchar(12) | YES | | NULL |
|
| telefono_p | varchar(12) | YES | | NULL |
|
| sexo_p | varchar(12) | YES | | NULL |
|
| id_doc | bigint(20) | YES | MUL | NULL |
|
| id_sala | bigint(20) | YES | MUL | NULL |
|
+-------------+-------------+------+-----+---------+----------------+
10 rows in set (0.01 sec)
mysql> show tables;
+---------------------------+
| Tables_in_clinica_caracas |
+---------------------------+
| doctor
|
| hospital
|
| laboratorio
|
| pacientes
|
| sala
|
+---------------------------+
5 rows in set (0.02 sec)
mysql> insert into pacientes values(null,"12345645","yin","garcias","la
muerte","4_enero","125874","F","1","1");
Query OK, 1 row affected (0.03 sec)
mysql> insert into pacientes values(null,"12346125","yink","perez","la
nueva","9_enero","12552474","M","2","2");
Query OK, 1 row affected (0.03 sec)
mysql> select*from pacientes;
+------+----------+----------+------------+-------------+----------+------------+--------+--------+---------+
| id_p | cedula | nombre_p | apellido_p | direccion_p | fecha_na | telefono_p | sexo_p | id_doc | id_sala |
+------+----------+----------+------------+-------------+----------+------------+--------+--------+---------+
| 1 | 12345645 | yin | garcias | la muerte | 4_enero | 125874 | F | 1 |
1|
| 2 | 12346125 | yink | perez | la nueva | 9_enero | 12552474 | M | 2 |
2|
+------+----------+----------+------------+-------------+----------+------------+--------+--------+---------+
2 rows in set (0.00 sec)
mysql> select nombre_hosp as hospital,direccion_hosp,nombre_lab as laboratorio from hospital join
laboratorio on hospital.id_cod_hosp=hospital.id_cod_hosp;
+--------------+----------------+--------------+
| hospital | direccion_hosp | laboratorio |
+--------------+----------------+--------------+
| chiquinquira | centro
| banco de san |
| pons
| zona norte | banco de san |
| chiquinquira | centro
| banco de san |
| pons
| zona norte | banco de san |
+--------------+----------------+--------------+
4 rows in set (0.02 sec)
mysql> select nombre_hosp as hospital,direccion_hosp,nombre_lab as laboratorio from hospital right outer
join laboratorio on hospital.id_cod_hosp=hospital.id_cod_hosp;
+--------------+----------------+--------------+
| hospital | direccion_hosp | laboratorio |
+--------------+----------------+--------------+
| chiquinquira | centro
| banco de san |
| pons
| zona norte | banco de san |
| chiquinquira | centro
| banco de san |
| pons
| zona norte | banco de san |
+--------------+----------------+--------------+
4 rows in set (0.04 sec)
mysql> select nombre_hosp as hospital,direccion_hosp,nombre_lab as laboratorio from hospital left outer join
laboratorio on hospital.id_cod_hosp=hospital.id_cod_hosp;
+--------------+----------------+--------------+
| hospital | direccion_hosp | laboratorio |
+--------------+----------------+--------------+
| chiquinquira | centro
| banco de san |
| chiquinquira | centro
| banco de san |
| pons
| zona norte | banco de san |
| pons
| zona norte | banco de san |
+--------------+----------------+--------------+
4 rows in set (0.00 sec)
mysql>
MARACAIBO, 22 DE NOVIEMBRE DEL 2010
UNEFA NUCLEO ZULIA
LUIS MANUEL SOTO
SIM – 6B
BASES DE DATOS
lab1@canaima:~$ xampp start
Starting XAMPP for Linux 1.7.3a...
XAMPP: Starting Apache with SSL (and PHP5)...
XAMPP: Starting MySQL...
XAMPP: Starting ProFTPD...
XAMPP for Linux started.
lab1@canaima:~$ mysql -h localhost -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.41 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database unefa;
Query OK, 1 row affected (0,00 sec)
mysql> use unefa;
Database changed
mysql> create table Trabajo_Grado(ID_tra bigint auto_increment primary key, Nombre varchar(20),
Fecha_Inicio varchar(20), Presupuesto varchar (20));
Query OK, 0 rows affected (0,02 sec)
mysql> drop table Trabajo_Grado;
Query OK, 0 rows affected (0,00 sec)
mysql> create table Trabajo_Grado(ID_tra bigint auto_increment primary key, Nombre varchar(20),
Fecha_Inicio varchar(20), Presupuesto varchar(20))type=innodb;
Query OK, 0 rows affected, 1 warning (0,00 sec)
mysql> alter table Trabajo_Grado modify Presupuesto double;
Query OK, 0 rows affected (0,01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create table Estudiante(ID_Est bigint auto_increment primary key, Nombres varchar(15),Apellido
varchar(15),Cedula varchar(15), Direccion varchar(20),Telefono varchar(15),ID_tra bigint,index (ID_tra),
foreign key (ID_tra) references Trabajo_Grado (ID_tra) on update cascade on delete cascade)type=innodb;
Query OK, 0 rows affected, 1 warning (0,02 sec)
mysql> create table Asesor(ID_As bigint auto_increment primary key, Nombre varchar(15),Apellido
varchar(15), Cedula varchar(15),Profesion varchar(20),Direccion varchar(20),ID_Est bigint, index(ID_Est),
foreign key (ID_Est) references Estudiante(ID_Est) on update cascade on delete cascade)type=innodb;
Query OK, 0 rows affected, 1 warning (0,00 sec)
mysql> create table Conferencia(ID_Conf bigint auto_increment primary key, Nombre_Conf varchar(20), Fecha
date, Hora datetime, Lugar Varchar(20),ID_Est bigint, index(ID_Est), foreign key(ID_Est) references Estudiante
(ID_Est) on update cascade on delete cascade)type=innodb;
Query OK, 0 rows affected, 1 warning (0,01 sec)
mysql> create table Carrera(ID_Car bigint auto_increment primary key, Nombre_Car varchar(20),ID_Est bigint,
index(ID_Est), foreign key(ID_Est) references Estudiante (ID_Est) on update cascade on delete cascade,ID_As
bigint,index(ID_As),foreign key(ID_As) references Asesor (ID_As) on update cascade on delete
cascade)type=innodb;
Query OK, 0 rows affected, 1 warning (0,01 sec)
mysql>
Descargar