Crear una Base de Datos en MYSQL

Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 4.1.8-nt-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

Ejemplo de Sintaxis para crear una base de datos.

mysql> create database embarques;
Query OK, 1 row affected (0.80 sec)

Ejemplo  de Sintaxis para conectarse ala base de datos, es importante señalar que antes de crear la tables es necesario conectarse

mysql> use  embarques;
Database changed
mysql> create table clientes (numclie  integer not null,
    ->  empresa  char (20) not null,
    ->  repclie   integer   not null,
    ->  limitecredito  decimal(5,2),
    -> PRIMARY KEY (numclie));
Query OK, 0 rows affected (0.77 sec)

mysql> create table repventas (
    -> numempl integer not null,
    -> nombre char(15) not null,
    -> edad integer,
    -> oficinarep integer not null,
    -> titulo char (15),
    -> contrato date not null,
    -> director integer not null,
    -> cuota numeric,
    -> ventas numeric not null,
    -> PRIMARY KEY (numempl));
Query OK, 0 rows affected (0.64 sec)

mysql> create table oficinas (
    -> oficina integer not null,
    -> ciudad char (15) not null,
    -> region char (10) not null,
    -> dir integer not null,
    -> objetivo numeric,
    -> ventas numeric not null,
    -> PRIMARY KEY (oficina),
    -> FOREIGN KEY (dir) REFERENCES repventas (numempl));
Query OK, 0 rows affected (0.63 sec)

mysql> create table productos (
    -> idfab  char(3) not null,
    -> idproducto  char(5) not null,
    -> descripcion  char(20) not null,
    -> precio      numeric not null,
    -> existencias  integer not null,
    -> PRIMARY KEY (idfab, idproducto));
Query OK, 0 rows affected (0.33 sec)

mysql> create table pedidos (
    -> numpedido  integer  not null,
    -> fechapedido  date not null,
    -> clie integer not null,
    -> rep  integer  not  null,
    -> fab   char(3) not null,
    -> producto  char(5) not null,
    -> cant  integer  not null,
    -> importe  numeric not null,
    -> PRIMARY KEY (numpedido),
    -> FOREIGN KEY(clie) REFERENCES clientes (numclie),
    -> FOREIGN KEY(rep) REFERENCES repventas (numempl),
    -> FOREIGN KEY(fab,producto) REFERENCES productos (idfab,idproducto));
Query OK, 0 rows affected (2.22 sec)


Ejemplo de Sintaxis para insertar registros a una base de datos

mysql> insert into clientes values(2101, 'jones Mfg.', 106, 65000);
Query OK, 1 row affected, 1 warning (2.44 sec)

mysql> insert into repventas values(101, 'Dan Roberts', 45, 12, 'Rep Ventas', "1
986-10-20", 104, 300000, 305673);
Query OK, 1 row affected (0.00 sec)

mysql> insert into oficinas values(12, 'Chicago', 'Este', 104, 800000, 735042);
Query OK, 1 row affected (0.00 sec)

mysql> insert into  productos values ('ACI','41001','Articulo Tipo I',55,277);
Query OK, 1 row affected (0.02 sec)

mysql> insert into pedidos values(112963,"1989-12-17",2103,105,'ACI','41004',28,
 3276);
Query OK, 1 row affected (0.00 sec)


Ejercicio 1.

Definir la tabla empleo con campos NOMBRE, EDAD, ALOJAMIENTO, siendo clave primaria (EDAD, ALOJAMIENTO). Definir también la tabla PROBLEMA con campos CIUDAD, FECHAMUESTRA, MDIODIA y MEDIANOCHE utilizando como clave externa de la clave primaria (MEDIANOCHE; CIUDAD), las columnas (EDAD,ALOJAMIENTO) de la tabla EMPLEO. Elegir los tipos de campos razonables. Usar COSTARINT para nombrar restricciones (reglas de negocio).



mysql> create table empleo
    -> (nombre   char(25) not null,
    -> edad  numeric,
    -> alojamiento   char(15),
    -> PRIMARY KEY (edad, alojamiento));
Query OK, 0 rows affected (0.58 sec)

mysql> create table problema
    -> (ciudad    char(15),
    -> fechamuestra   datetime not null,
    -> mediodia   numeric(3,1),
    -> medianoche  numeric,
    -> CONSTRAINT pk_primaria  PRIMARY KEY (medianoche, ciudad),
    -> CONSTRAINT  fk_externa   FOREIGN KEY (medianoche, ciudad)
    -> REFERENCES empleo (edad, alojamiento));
Query OK, 0 rows affected (0.63 sec)





Ejercicio 2.

Construir la tabla CLIENTES con los campos IDCLIENTE (clave primaria) y APELLIDOCLIENTE. Construir también la tabla pedidos con campos IDPEDIDO (clave primaria), IDCLIENTE y NOTASPEDIDO, de modo que la clave externa de IDCLIENTE sea el campo del mismo nombre en la tabla CLIENTES. Se exige además que cuando se elimine un cliente de la tabla CLIENTES, también se eliminen todos los registros de la tabla PEDIDOS que contienen el mismo valor para el identificador de cliente. ¿cómo sería la sintaxis para que al eliminar un cliente de la tabla CLIENTES, todas las claves externas correspondientes de la tabla PEDIDOS se configuren con el valor NULL? Definir adecuadamente los tipos de datos para los campos de las tablas.




Para  el primer caso, la sintaxis adecuada basada en la clausula ON DELETE CASCADE, seria lo siguiente:

mysql> create table clientes (idcliente   integer primary key,
    -> apellidocliente  char(50));
Query OK, 0 rows affected (0.58 sec)

mysql> create table pedidos (idpedido  integer primary key,
    -> idcliente  integer,
    -> notaspedido  varchar(355),
    -> CONSTRAINT claveextpedidosidcliente  FOREIGN  KEY  (idcliente)
    -> REFERENCES clientes ON DELETE CASCADE);
Query OK, 0 rows affected, 1 warning (0.61 sec)

Para el Segundo caso, la sintaxis adecuada de la tabla   PEDIDOS (basada en la cláusula ON DELETE  SET  NULL), sería la siguiente:

mysql> create table pedidos_2 (idpedido  integer primary key,
    -> idcliente  integer,
    -> notaspedido  varchar(355),
    -> CONSTRAINT claveextpedidosidcliente  FOREIGN  KEY  (idcliente)
    -> REFERENCES clientes ON DELETE CASCADE);
Query OK, 0 rows affected, 1 warning (0.61 sec)

mysql> use empleado
Database changed
mysql> show tables;
+--------------------+
| Tables_in_empleado |
+--------------------+
| obreros            |
+--------------------+
1 row in set (0.00 sec)

mysql> select *
    -> from obreros;
+--------+------------+----------+
| id_obr | nombre_obr | edad_obr |
+--------+------------+----------+
|    456 | manuela    |       21 |
|      5 | veronica   |       21 |
|    334 | miguel     |       22 |
|    654 | rolando    |       34 |
|   2576 | alberto    |       60 |
|    283 | agapito    |       21 |
|    287 | zoila      |       24 |
|   9876 | eloy       |       26 |
|   1287 | mario      |       22 |
|    546 | candido    |       15 |
|    341 | pereira    |       18 |
|    768 | marcos     |       56 |
|    354 | Yesenia    |       29 |
|    232 | gilberto   |       45 |
+--------+------------+----------+
14 rows in set (0.25 sec)

mysql> select avg(edad_obr) as Edad_Media from Obreros;
+------------+
| Edad_Media |
+------------+
|    29.5714 |
+------------+
1 row in set (0.50 sec)

mysql> select id_obr, nombre_obr, edad_obr
    -> from obreros
    -> order by edad_obr;
+--------+------------+----------+
| id_obr | nombre_obr | edad_obr |
+--------+------------+----------+
|    546 | candido    |       15 |
|    341 | pereira    |       18 |
|    456 | manuela    |       21 |
|      5 | veronica   |       21 |
|    283 | agapito    |       21 |
|    334 | miguel     |       22 |
|   1287 | mario      |       22 |
|    287 | zoila      |       24 |
|   9876 | eloy       |       26 |
|    354 | Yesenia    |       29 |
|    654 | rolando    |       34 |
|    232 | gilberto   |       45 |
|    768 | marcos     |       56 |
|   2576 | alberto    |       60 |
+--------+------------+----------+
14 rows in set (0.00 sec)


mysql> select id_obr, nombre_obr, edad_obr
    -> from obreros
    -> order by edad_obr desc;
+--------+------------+----------+
| id_obr | nombre_obr | edad_obr |
+--------+------------+----------+
|   2576 | alberto    |       60 |
|    768 | marcos     |       56 |
|    232 | gilberto   |       45 |
|    654 | rolando    |       34 |
|    354 | Yesenia    |       29 |
|   9876 | eloy       |       26 |
|    287 | zoila      |       24 |
|   1287 | mario      |       22 |
|    334 | miguel     |       22 |
|    283 | agapito    |       21 |
|      5 | veronica   |       21 |
|    456 | manuela    |       21 |
|    341 | pereira    |       18 |
|    546 | candido    |       15 |
+--------+------------+----------+
14 rows in set (0.00 sec)


mysql> select id_obr, nombre_obr, edad_obr
    -> from obreros
    -> group by edad_obr;
+--------+------------+----------+
| id_obr | nombre_obr | edad_obr |
+--------+------------+----------+
|    546 | candido    |       15 |
|    341 | pereira    |       18 |
|    456 | manuela    |       21 |
|    334 | miguel     |       22 |
|    287 | zoila      |       24 |
|   9876 | eloy       |       26 |
|    354 | Yesenia    |       29 |
|    654 | rolando    |       34 |
|    232 | gilberto   |       45 |
|    768 | marcos     |       56 |
|   2576 | alberto    |       60 |
+--------+------------+----------+
11 rows in set (0.50 sec)

mysql> select id_obr, nombre_obr, edad_obr
    -> from obreros
    -> group by edad_obr desc;
+--------+------------+----------+
| id_obr | nombre_obr | edad_obr |
+--------+------------+----------+
|   2576 | alberto    |       60 |
|    768 | marcos     |       56 |
|    232 | gilberto   |       45 |
|    654 | rolando    |       34 |
|    354 | Yesenia    |       29 |
|   9876 | eloy       |       26 |
|    287 | zoila      |       24 |
|    334 | miguel     |       22 |
|    456 | manuela    |       21 |
|    341 | pereira    |       18 |
|    546 | candido    |       15 |
+--------+------------+----------+
11 rows in set (0.00 sec)


No hay comentarios:

Publicar un comentario