COMANDOS SQL
COMANDOS SQL
(ESTRUCTURA)
Data Definition Language o Lenguaje de
descripción de datos (DDL)
Create Table (Crear Tabla)
Create table NOMBRE_TABLA (
NOMBRE_CAMPO tipo(longitud) opciones,
Primary key (NOMBRE_CAMPO_ID)
)
Ejemplo:
Create table profesores (
ID_PROFESOR int(10) not null,
NOMBRE varchar(30),
APELLIDOS varchar(60),
Primary key (ID_PROFESOR)
)
Rename Table (Renombrar Tabla)
Rename table NOMBRE_TABLA to NUEVO_NOMBRE
Ejemplo:
Rename table profesores to teachers
Drop Table (Eliminar Tabla)
Drop table NOMBRE_TABLA
Ejemplo:
drop table profesores
Alter Table
Agregar Columna
alter table NOMBRE_TABLA
Add NOMBRE_CAMPO tipo(longitud) opciones
Ejemplo:
alter table profesores
add CEDULA varchar(20) not null
Alter Table
Eliminar Columna
alter table NOMBRE_TABLA
Drop column NOMBRE_CAMPO
Ejemplo:
alter table profesores
drop column CEDULA
Alter Table
Modificar Columna
alter table NOMBRE_TABLA
modify column NOMBRE_CAMPO tipo(longitud) opciones
Ejemplo:
alter table profesores
modify column CEDULA int(9) not null
Alter Table
Renombrar Columna
alter table NOMBRE_TABLA
rename column NOMBRE_CAMPO to NUEVO_NOMBRE tipo(longitud)
opciones
alter table NOMBRE_TABLA
change NOMBRE_CAMPO NUEVO_NOMBRE tipo(longitud) opciones
Ejemplo:
alter table profesores
rename column APELLIDOS to APELLIDO varchar(60)
Alter Table
Agregar Llave Primaria (PK)
alter table NOMBRE_TABLA
add primary key (NOMBRE_CAMPO)
Ejemplo:
alter table profesores
add primary key (ID_PROFESOR)
Alter Table
Eliminar Llave Primaria (PK)
alter table NOMBRE_TABLA
drop primary key
Ejemplo:
alter table profesores
drop primary key
Alter Table
Agregar Llave Foránea (FK)
alter table NOMBRE_TABLA
add constraint NOMBRE_RESTRICCION
add foreign key (CAMPO_FK) references
TABLA_REFERENCIA(CAMPO_ID_REFERENCIA)
Ejemplo:
alter table personas
add constraint personas_fk1
add foreign key (ID_PROFESOR) references profesores(ID_PROFESOR)
Alter Table
Eliminar Llave Foránea (FK)
alter table NOMBRE_TABLA
drop foreign key NOMBRE_RESTRICCION
Ejemplo:
alter table personas
drop foreign key personas_fk1
COMANDOS SQL
(MANEJO DE DATOS)
Lenguaje de manipulación de datos o Data
Manipulation Language (DML)
INSERT
Insertar Datos
insert into NOMBRE_TABLA (COLUMNA1, COLUMNA2…) values
(VALOR1, VALOR2…)
Ejemplo:
insert into personas (ID_PERSONA, NOMBRE, APELLIDOS, CEDULA,
EMAIL) values (1, ‘Martha’, ‘Vallecillo Moreno’, ‘2223334455’,
‘[email protected]’);
insert into personas (ID_PERSONA, NOMBRE, APELLIDOS, CEDULA,
EMAIL) values (2, ‘Isaac’, ‘Guzman Zuñiga’, ‘707650987’,
‘[email protected]’);
UPDATE
Modificar Datos
update NOMBRE_TABLA set
COLUMNA1 = VALOR1,
COLUMNA2 = VALOR2
where CAMPO_CLAVE = VALOR_IDENTIFICADOR
Ejemplo:
update personas set
APELLIDOS = ‘Zuñiga Guzman’,
CEDULA = ‘207650987’,
EMAIL = ‘[email protected]’
where ID_PERSONA = 2
DELETE
Eliminar Datos
delete from NOMBRE_TABLA where CAMPO_CLAVE =
VALOR_IDENTIFICADOR
Ejemplo:
delete from personas where ID_PERSONA = 1
SELECT
Consultar Datos
select NOMBRE_COLUMNA(S) from NOMBRE_TABLA where
CONDICIONES OPCIONES
Ejemplos:
select * from personas order by NOMBRE
(selecciona todas las columnas de la tabla personas ordenadas por la
columna NOMBRE)
select NOMBRE,APELLIDOS from presonas where NOMBRE = ‘Carlos’
(selecciona las columnas especificadas de la tabla personas donde el
nombre sea ‘Carlos’)
SELECT
Consultar datos de varias tablas
select NOMBRE_COLUMNA(S) from NOMBRE_TABLA 1, NOMBRE_TABLA 2
where CONDICIONES OPCIONES
Ejemplos:
select B.NOMBRE,B.APELLIDOS,C.ASIGNATURA from profesores A
join personas B on B.ID_PERSONA = A.ID_PERSONA
join asignaturas C on C.ID_ASIGNATURA = A.ID_ASIGNATURA
order by C.ASIGNATURA,B.NOMBRE
(selecciona las columnas Nombre y Apellidos de la tabla personas ordenadas
por la columna NOMBRE)