Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Unidad II
Creación de una base de datos y de los elementos
que la conforman
TALLER DE BASE DE DATOS
COMANDOS DESCRIPCIÓN
USE USE db_name indica a MySQL que use la base de datos
db_name como la base de datos por defecto (actual) en
sentencias subsiguientes
DROP DROP DATABASE /TABLE/ INDEX, ETC elimina todas
las tablas de la base de datos y borra la base de datos
CREATE CREATE DATABASE/TABLE/ INDEX, ETC crea una base
de datos con el nombre dado
DESCRIBE Proporciona información sobre las columnas de una tabla.
col_name puede ser un nombre de columna o una
cadena que contenga los caracteres comodín SQL '%' and
'_' para obtener salida sólo para las columnas cuyos
nombres coincidan con la cadena. No es necesario
escribir las cadenas entre comillas.
TALLER DE BASE DE DATOS
Object Description
Table Basic unit of storage; composed of
rows
View Logically represents subsets of data
from one or more tables
Index Improves the performance of some
queries
TALLER DE BASE DE DATOS
Table Syntax
Table Syntax
Modify an existing column ALTER TABLE `empleados`.`autos`
CHANGE COLUMN `ano` `anio` VARCHAR(4) NOT NULL ;
Rename a column
ALTER TABLE `empleados`.`emp`
CHANGE COLUMN `nombre` `nombre_emp` VARCHAR(45) NULL
DEFAULT NULL ;
TALLER DE BASE DE DATOS
Foreign Key
A FOREIGN KEY is a key used to link two tables together.
A FOREIGN KEY is a field (or collection of fields) in one table that
refers to the PRIMARY KEY in another table.
TALLER DE BASE DE DATOS
Syntax
Constraint
The constraints are used to specify rules for data in a table.
Constraints are used to limit the type of data that can go into a table. This ensures
the accuracy and reliability of the data in the table. If there is any violation
between the constraint and the data action, the action is aborted.
Constraints can be column level or table level. Column level constraints apply to a
column, and table level constraints apply to the whole table.
TALLER DE BASE DE DATOS
Index
Indexes are used to retrieve data from the database more quickly than
otherwise. The users cannot see the indexes, they are just used to speed up
searches/queries
Rename
To rename a table
Syntax
RENAME TABLE ‘table_name’ TO ‘newTable’;
ALTER TABLE ‘table_name’ RENAME ‘newTable’
TALLER DE BASE DE DATOS
Drop
This is a hard word, because it could drop several objects like schema,
tables, columns, index, etc.
DROP SCHEMA ‘EMPLEADOS;
DROP TABLE ‘TABLE_EMP’
Syntax
ALTER TABLE ‘TABLE_EMP’
DROP INDEX ‘INDEX_NAME’;
View
A view is a virtual table based on the result-set of an SQL statement.
A view contains rows and columns, just like a real table. The fields in a view are
fields from one or more real tables in the database.
Syntax
create view
empleados_view as
select idempleado, nombre , apaterno , amaterno
from empleados
GRACIAS
A.CASTILLO.RUELAS@UADEC.EDU.MX