Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
CLASE 4
SP_HELPDB PRUEBITA
SP_HELPDB logistica
SP_HELPDB marketPERU
SP_HELPDB farmaciaBd
SP_HELPDB mrketPERU
Creando tablas
CREATE TABLE
-------CREAR LAS SIGUIENTES TABLAS: "PRODUCTO","ALUMNO", EN LA BASE DE DATOS
"PRUEBITA", CON LAS SIGUIENTE ESTRUCTURA--
USE pruebita
SP_HELP PRODUCTO
-------CREANDO LA TABLA ALUMNO---
----PRUEBITA--
----DIAGRAMAS DE BASE DE DATOS--
----NUEVO DIAGRAMA DE BASE DE DATOS--
---SELECCIONAMOS LAS TABLAS--
---ACEPTAMOS...
---EN LAS TABLAS CLICK DERECHO---
---VISTA DE TABLAS---
---PERSONALIZADA---
INSERT
SELECT*FROM CURSO
SELECT*FROM CURSO
UPDATE CURSO
SET PROF=NULL
------ASIGNAR UN DOCENTE MARLENY FLORES EN EL CURSO DE WINDOWS 10 DE LA TABLA
CURSO-----
UPDATE CURSO
SET PROF='Marleny Flores'
WHERE NOMCURSO='WINDOWS 10'
UPDATE CURSO
SET PROF='Jhonny Tabraj'
WHERE IDCURSO='C002'
UPDATE CURSO
SET PROF='liam Rojas'
WHERE IDCURSO='C003'
-------ACTUALIZAR EN $150.00 EL PRECIO DEL CURSO DE WINDOWS 10 Y EL NUMERODE
VACANTES 35, DE LA TABLA CURSO-----
UPDATE CURSO
SET NUMVACANT=35,
NUMMATRIC=150
WHERE IDCURSO='C001'
DELETE
SELECT*FROM CURSO
------ELIMINAR LOS DATOS DE LA FILA DEL CURSO POWER POINT DE LA TABLA CURSO--
---
SELECT*FROM CURSO
Integridad de Dominio
La integridad dominio establece que condiciones deben cumplir los valores a insertar una
columna.
La integridad de dominio se define mediante:
• Reglas de validacion
• Valores predeterminados
• Valores unicos
Integridad de Entidad
La integridad entidad garantiza la unicidad de las filas mediante un identificador unico.
La integridad de dominio se define mediante:
• Clave primaria
Integridad Referencial
La integridad refeerencial grantiza que la relacion entre la llave primaria (en la tabla
referenciada) y la llave foranea (en la tabla de referencia) siempre se mantenga.
La integridad de dominio se define mediante:
• Clave foranea
RESTRICCIONES (Constraints)
• Las restricciones proporcionan una forma de hacer que SQL Server exija la
integridad de la base de datos.
• Son el metodo preferido para dar fuerza a la integridad de datos y garantizar que
los datos ingresados en las columnas sean valores validos y se mantengan las
relaciones entre las tablas.
• Se definen al momento de crear la tabla o al momento de modificar la definicion
de la tabla.
TIPOS DE RESTRICCIONES:
Sintaxis:
Columna X, Columna Y: Son las columnas que se definen como clave primaria . Las
columnas no deben permitir valores nulos, y ademas no deben tener valores
duplicados.
EJEMPLO 01
-----CREANDO LA CLAVE PRIMARIA A LA COLUMNA IDCURSO DE LA TABLA CURSO-----
EJEMPLO 02
------ELIMINANDO LA CLAVE PRIMARIA PK_CURSO DE LA TABLA CURSO----------
EJEMPLO 03
------COMPROBANDO EL COMPORTAMIENTO DE LA PK DE LA TABLA CURSO----
EJEMPLO 04
CREANDO LA CLAVE PRIMARIA A LA COLUMNA IDCURSO E IDALUMNO DE LA TABLA
MATRICULA
EJEMPLO 05
---CREAR LA CLAVE PRIMARIA DE LA TABLA ALUMNO----
EJEMPLO 06
---CREAR LA CLAVE PRIMARIA DE LA TABLA PAGO----
Sintaxis:
Columna X, Columna Y: Son las columnas que se definen como clave foranea.
EJEMPLO 01
--CREANDO LA CLAVE FORANEA DE LA TABLA MATRICULA QUE HACE REFERENCIA A LA
TABLA CURSO (relacion entre matricula y curso)--
EJEMPLO 02
---CREANDO LA CLAVE FORANEA QUE HACE REFERENCIA A LA TABLA ALUMNO (relacion
entre matricula y alumno)
EJEMPLO 03
--CREANDO LA CLAVE FORANEA QUE HACE REFERENCIA A LA TABLA MATRICULA (relacion
entre pago y matricula)
Sintaxis:
EJEMPLO 01
---CREANDO LA RESTRICCION DE VALOR NO DUPLICADO A LA COLUMNA NOMBRE DEL CURSO
EN LA TABLA CURSO---
EJEMPLO 02
----COMPROBANDO LA RESTRICCION INSERTANDO UN CURSO INGRESANDO UN NOMBRE DE
CURSO DUPLICADO EN LA TABLA CURSO---
Sintaxis:
EJEMPLO 01
--------CREANDO UNA RESTRICCION QUE DEFINA EL NUMERO DE VACANTES EN 20 POR
DEFECTO EN LA TABLA CURSO-------
Sintaxis:
Condicion: Es la expresion que determina como debe ser el valor a ingresar en la columna
afectada por la restriccion.
EJEMPLO 01
------CREANDO UNA RESTRICCION QUE DEFINA UNA REGLA DE VALIDACION A LA COLUMNA
PRECIO DEL CURSO (SOLO DEBE ACEPTAR VALORES POSITIVOS) DE LA TABLA CURSO----
EJEMPLO 02
-----COPROBANDO LA RESTRICCION CHECK INSERTANDO UN CURSO NUEVO (SOLO DEBE
ACEPTAR VALORES POSITIVOS)
EJEMPLO 01
---REGISTRAR UNA MATRICULA DE UN ALUMNO (QUE SE ENCUENTRE EN LA TABLA ALUMNO
ELIGIENDO EL CODIGO DE CURSO no ESTIPULADO EN LA TABLA CURSO----
EJEMPLO 02
--REGISTRAR UNA MATRICULA DE UN ALUMNO ( QUE NO ENCUENTRE EN LA TABLA ALUMNO)
ELIGIENDO EL CODIGO DE CURSO ESTIPULADO EN LA TABLA CURSO.
EJEMPLO 03
----REGISTRAR UNA MATRICULA CON DATOS CORRECTOS-----
EJEMPLO 04
----REGISTRAR LOS PAGOS DE UN ALUMNO QUE SI SE ENCUENTRE MATRICULADO----
La integridad referencial en cascada permite controlar las acciones que lleva a cabo SQL
Server cuando se intenta actualizar o eliminar una fila que contenga clave primaria a las
que apuntan claves foraneas exixtentes.
EJEMPLO 01
---ELIMINANDO A UN ALUMNO MATRICULADO Y QUE TAMBIEN ELIMINE SUS PAGOS
REALIZADOS-------
EJEMPLO 02
-----ELIMINAR UN ALUMNO QUE TAMBIEN SE ELIMINE SU REGISTRO DE MATRICULA Y SUS
PAGOS REALIZADOS-------
-----ELIMINAR LA CLAVE FORANEA DE LA TABLA MATRICULA---