Você está na página 1de 18

INS125 Lenguajes de Programacin

Ingeniera en Computacin e Informtica


Semestre 2016/1

Structured Query Language


Profesor Rodrigo Caballero V.

Orgenes de SQL
En 1970 E. F. Codd propone el modelo relacional.
Laboratorios de IBM definen el lenguaje SEQUEL
(Structured English Query Language).
Ampliamente implementado por el sistema de
gestin de bases de datos (SGBD).

System R: SGBD experimental desarrollado en 1977


por IBM.
Oracle lo introdujo por primera vez en 1979 en un
producto comercial.

Ao

Nombre

1986

SQL-86

1989
1992

SQL-89
SQL-92

1999

SQL:1999

2003

SQL:2003

2005

SQL:2005

2008

SQL:2008

Alias

Comentarios
Primera publicacin hecha por ANSI (American National
SQL-87
Standards Institute). Confirmada por ISO en 1987.
Revisin menor.
SQL2 Revisin mayor.
Se agregaron expresiones regulares, consultas recursivas
SQL2000 (para relaciones jerrquicas), triggers y algunas
caractersticas orientadas a objetos.
Introduce algunas caractersticas de XML, cambios en las
funciones, estandarizacin del objeto sequence y de las
columnas autonumricas.
ISO/IEC 9075-14:2005 Define las maneras en las cuales SQL
se puede utilizar conjuntamente con XML. Define maneras
de importar y guardar datos XML en una base de datos SQL,
manipulndolos dentro de la base de datos y publicando el
XML y los datos SQL convencionales en forma XML. Adems,
proporciona facilidades que permiten a las aplicaciones
integrar dentro de su cdigo SQL el uso de XQuery, lenguaje
de consulta XML publicado por el W3C (World Wide Web
Consortium) para acceso concurrente a datos ordinarios
SQL y documentos XML.
Permite el uso de la clusula ORDER BY fuera de las
definiciones de los cursores. Incluye los disparadores del tipo
INSTEAD OF. Aade la sentencia TRUNCATE.

Basado en el modelo relacional

Carme Martn Escofet, FUOC P06/M2109/02149 "El lenguaje SQL", pgina 6.

Creando una base de datos


Para crear:

Para eliminar:

Carme Martn Escofet, FUOC P06/M2109/02149 "El lenguaje SQL", pgina 59.

Creando una tabla

Carme Martn Escofet, FUOC P06/M2109/02149 "El lenguaje SQL", pgina 7.

Insertando nuevos valores

Carme Martn Escofet, FUOC P06/M2109/02149 "El lenguaje SQL", pgina 7.

Realizar consultas

Carme Martn Escofet, FUOC P06/M2109/02149 "El lenguaje SQL", pgina 8.

Operadores SQL

Carme Martn Escofet, FUOC P06/M2109/02149 "El lenguaje SQL", pgina 30.

Modificando y borrando filas


Para modificar:

Para eliminar:

Carme Martn Escofet, FUOC P06/M2109/02149 "El lenguaje SQL", pgina 62.

Sistema de Gestin de Bases de Datos


Conjunto de programas que permiten el
almacenamiento, modificacin y extraccin de la
informacin en una base de datos.

Aplicaciones
Practiquemos con algunos conceptos bsicos:
show databases;
use BASE_DE_DATOS;
show tables;
describe TABLA;

select * from TABLA;

Actividades propuestas:
CREATE DATABASE IF NOT EXISTS test;
USE test;
CREATE TABLE IF NOT EXISTS books (
BookID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Title VARCHAR(100) NOT NULL,
SeriesID INT, AuthorID INT);
CREATE TABLE IF NOT EXISTS authors (id INT NOT NULL PRIMARY KEY
AUTO_INCREMENT);
CREATE TABLE IF NOT EXISTS series (id INT NOT NULL PRIMARY KEY
AUTO_INCREMENT);
INSERT INTO books (Title,SeriesID,AuthorID) VALUES('The Fellowship of the
Ring',1,1),
('The Two Towers',1,1), ('The Return of the King',1,1), ('The Sum of
All Men',2,2),
('Brotherhood of the Wolf',2,2), ('Wizardborn',2,2), ('The
Hobbbit',0,1);
Fuente: https://mariadb.com/kb/es/a-mariadb-primer-01-intro/

Actividades propuestas:
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| authors
|
| books
|
| series
|
+----------------+
3 rows in set (0.00 sec)

MariaDB [test]> describe books;


+---------+--------------+------+-----+---------+----------------+
| Field
| Type
| Null | Key | Default | Extra
|
+---------+--------------+------+-----+---------+----------------+
| BookID
| int(11)
| NO
| PRI | NULL
| auto_increment |
| Title
| varchar(100) | NO
|
| NULL
|
|
| SeriesID | int(11)
| YES |
| NULL
|
|
| AuthorID | int(11)
| YES |
| NULL
|
|
+---------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

Actividades propuestas:
MariaDB [test]> select * from books;
+--------+----------------------------+----------+----------+
| BookID | Title
| SeriesID | AuthorID |
+--------+----------------------------+----------+----------+
|
1 | The Fellowship of the Ring |
1 |
1 |
|
2 | The Two Towers
|
1 |
1 |
|
3 | The Return of the King
|
1 |
1 |
|
4 | The Sum of All Men
|
2 |
2 |
|
5 | Brotherhood of the Wolf
|
2 |
2 |
|
6 | Wizardborn
|
2 |
2 |
|
7 | The Hobbbit
|
0 |
1 |
+--------+----------------------------+----------+----------+
7 rows in set (0.00 sec)

Actividades propuestas:
MariaDB [test]> insert into books (Title, SeriesID, AuthorID)
-> values ("Lair of Bones", 2, 2);
Query OK, 1 row affected (0.00 sec)

MariaDB [test]> select * from books;


+--------+----------------------------+----------+----------+
| BookID | Title
| SeriesID | AuthorID |
+--------+----------------------------+----------+----------+
|
1 | The Fellowship of the Ring |
1 |
1 |
|
2 | The Two Towers
|
1 |
1 |
|
3 | The Return of the King
|
1 |
1 |
|
4 | The Sum of All Men
|
2 |
2 |
|
5 | Brotherhood of the Wolf
|
2 |
2 |
|
6 | Wizardborn
|
2 |
2 |
|
7 | The Hobbbit
|
0 |
1 |
|
8 | Lair of Bones
|
2 |
2 |
+--------+----------------------------+----------+----------+
8 rows in set (0.00 sec)

Actividades propuestas:
MariaDB [test]> update books set Title = "The Hobbit" where BookID =
7;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [test]> select * from books;
+--------+----------------------------+----------+----------+
| BookID | Title
| SeriesID | AuthorID |
+--------+----------------------------+----------+----------+
|
1 | The Fellowship of the Ring |
1 |
1 |
|
2 | The Two Towers
|
1 |
1 |
|
3 | The Return of the King
|
1 |
1 |
|
4 | The Sum of All Men
|
2 |
2 |
|
5 | Brotherhood of the Wolf
|
2 |
2 |
|
6 | Wizardborn
|
2 |
2 |
|
7 | The Hobbit
|
0 |
1 |
|
8 | Lair of Bones
|
2 |
2 |
+--------+----------------------------+----------+----------+
8 rows in set (0.00 sec)

Você também pode gostar