Introducción a las bases de datos SQL 1 Libro de referencia Java How To Program 3ed Edition Deitel&Deitel Prentice Hall, 1999 2
Introduction Relational-Database Model Relational Database Overview: The books Database SQL 3 Database Collection of data Introduction DBMS Database management system Stores and organizes data RDBMS Relational database management system Tables 4
Introduction (Cont.) SQL Relational database Structured Query Language 5 DB Major Tasks Defining Databases - involves specifying the data types, structures, and constraints for the data to be stored in the database. Constructing Databases - storing the data itself (populating) on some storage medium that is controlled by the DBMS. Manipulating Databases - querying the database to retrieve specific data, updating the databases, and generating reports from the data. 6
Database Languages DDL Data Definition Language Provides facilities for definining database objects. DML Data manipulation Language Provides features for specifying the processing to be performed on database objects. SQL provides both 7 Application Architectures 8
Relational database Table Rows, columns Relational Model Primary key Unique data SQL statement Query 9 Tables Authors Author ID FirstName LastName YearBorn 1 Harvey Deitel 1946 2 Paul Deitel 1968 3 Tem Nieto 1969 10
Redundant Table AuthorID FirstName LastName YearBorn ISBN 1 Harvey Deitel 1946 0-13-010671-2 2 Paul Deitel 1968 0-13-010671-2 1 Harvey Deitel 1946 0-13-020522-2 2 Paul Deitel 1968 0-13-020522-2 3 Tem Nieto 1969 0-13-020522-2 11 Related Tables Author ID FirstName LastName YearBorn Authors 1 Harvey Deitel 1946 2 Paul Deitel 1968 3 Tem Nieto 1969 AuthorISBN ISBN Author ID 0-13-010671-2 1 0-13-010671-2 2 0-13-020522-2 1 0-13-020522-2 2 0-13-020522-2 3 12
Relational Database A primary key is a column or set of columns that uniquely identifies the rest of the data in any given row. A foreign key is a column in a table where that column is a primary key of another table. 13 Relational Operations Projection sname rating yuppy 9 lubber 8 guppy 5 rusty 10 14
Projection 15 Relational Operations Selection sname rating yuppy 9 rusty 10 Rating > 8 16
Relational Operations Join sid sname rating age bid day 22 dustin 7 45.0 101 10/10/96 58 rusty 10 35.0 103 11/12/96 17 Join Types Inner Join Only the rows that match the search conditions are returned Left Outer Join Returns all matched rows, plus all unmatched rows from the table on the left of the join clause (use nulls in fields of non-matching tuples) Right Outer Join Returns all matched rows, plus all unmatched rows from the table on the right of the join clause Full Outer Join Returns all (matched or unmatched) rows from the tables on both sides of the join clause 18
Books Database Sample database Four tables,, and Relationships among the tables 19 Tabla Authors de la base de datos Books 20
Datos de la tabla Authors 21 Tabla Publishers 22
Datos de la tabla Publishers 23 Tabla AuthorISBN 24
Datos de la tabla AuthorISBN (parcial) 25 Tabla Titles 26
Datos de la tabla Titles (parcial) 27 Table Relationships authors authorid firstname lastname authorisbn 1 1 authorid isbn publishers publisherid publishername 1 titles isbn title editionnumber copyright publisherid imagefile price 28
Comandos SQL SQL keyword Description CREATE TABLE SELECT FROM WHERE GROUP BY ORDER BY INNER JOIN INSERT UPDATE DELETE Creates a table. Retrieves data from one or more tables. Tables involved in the query. Required in every SELECT. Criteria for selection that determine the rows to be retrieved, deleted or updated. Criteria for grouping rows Criteria for ordering rows. Merge rows from multiple tables. Insert rows into a specified table. Update rows in a specified table. Delete rows from a specified table. 29 Query SELECT Simplest form of a SELECT query SELECT * FROM tablename SELECT * FROM authors Select specific fields from a table (projection) SELECT authorid, lastname FROM authors 30
WHERE Clause specify the selection criteria SELECT columnname1, columnname2, FROM tablename WHERE criteria SELECT title, editionnumber, YearPublished FROM titles WHERE YearPublished > '1998' WHERE clause condition operators <, >, <=, >=, =, <> LIKE wildcard characters % and _ 31 AuthorID y apellido SELECT authorid, lastname FROM Authors 32
Autores nacidos despues de 1960 SELECT * FROM Authors WHERE YearBorn > '1960' 33 Autores cuyos apellidos empiezan con d SELECT * FROM Authors WHERE LastName LIKE 'd%' 34
Autores cuyo nombre tiene i como segunda letra SELECT * FROM Authors WHERE LastName LIKE '_i%' 35 Tabla Authors ordenada por apellido en orden ascendente SELECT * FROM Authors ORDER BY LastName ASC 36
Tabla Authors ordenada por apellido en orden descendente SELECT * FROM Authors ORDER BY LastName DESC 37 Tabla Authors ordenada por apellido y luego por nombre en orden ascendente SELECT * FROM Authors ORDER BY LastName, FirstName ASC 38
Libros de Titles cuyos títulos terminan en How to program ordenados en forma ascendente con respecto al título SELECT * FROM Titles WHERE Title LIKE '%How to Program' ORDER BY Title ASC 39 Combinar los datos de multiples tablas: JOIN Join the tables Merge data from multiple tables into a single view INNER JOIN SELECT columnname1, columnname2, FROM (table1 INNER JOIN table2) ON table1.columnname = table2.column2name 40
Author ID Combinar los datos de multiples tablas: JOIN Authors FirstName LastName YearBorn 1 Harvey Deitel 1946 2 Paul Deitel 1968 3 Tem Nieto 1969 AuthorISBN ISBN Author ID 0-13-010671-2 1 0-13-010671-2 2 0-13-020522-2 1 0-13-020522-2 2 0-13-020522-2 3 SELECT Authors.AuthorID, FirstName, LastName, YearBorn, ISBN FROM Authors INNER JOIN AuthorISBN ON Authors.AuthorID=AuthorISBN.AuthorID 41 Combinar los datos de multiples tablas: JOIN AuthorID FirstName LastName YearBorn ISBN 1 Harvey Deitel 1946 0-13-010671-2 1 Harvey Deitel 1946 0-13-010671-2 2 Paul Deitel 1968 0-13-020522-2 2 Paul Nieto 1968 0-13-020522-2 3 Tem Nieto 1969 0-13-020522-2 42
Autores y los ISBN de los libros que escribieron en orden ascendente por apellido y por nombre SELECT FirstName, LastName, ISBN FROM Authors INNER JOIN AuthorISBN ON Authors.AuthorID=AuthorISBN.AuthorID ORDER BY LastName, FirstName 43 Query TitleAuthor incluida en la base de datos SELECT Titles.Title, Titles.ISBN, Authors.FirstName, Authors.LastName, Titles.YearPublished, Publishers.PublisherName FROM (Publishers INNER JOIN Titles ON Publishers.PublisherID = Titles.PublisherID) INNER JOIN (Authors INNER JOIN AuthorISBN ON Authors.AuthorID = AuthorISBN.AuthorID) ON Titles.ISBN = AuthorISBN.ISBN ORDER BY Titles.Title 44
Ejecución del Query TitleAuthor 45 UPDATE UPDATE table_name SET column_name = new_value WHERE column_name = some_value Ejemplo UPDATE Authors SET FirstName = 'Juan' WHERE FirstName = 'Tem' 46
INSERT INTO INSERT INTO table_name VALUES (value1, value2, ) Ejemplo INSERT INTO Authors VALUES (4, 'bart', 'simpson', '1990') 47 DELETE DELETE FROM table_name WHERE column_name = some_value Ejemplo DELETE FROM Authors WHERE FirstName= 'bart' 48