Introducción a las bases de datos SQL 1 Libro de referencia Java How To Program 3ed Edition Deitel&Deitel Prentice Hall, 1999 2 %DVHVGHGDWRV Ɣ Introduction Ɣ Relational-Database Model Ɣ Relational Database Overview: The books Database Ɣ SQL 3 Introduction • Database – Collection of data • 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 Model • Relational database – Table • Rows, columns – 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 Authors Author ID FirstName LastName YearBorn 1 Harvey Deitel 1946 2 Paul Deitel 1968 3 Tem Nieto 1969 ISBN AuthorISBN 0-13-010671-2 0-13-010671-2 0-13-020522-2 0-13-020522-2 0-13-020522-2 Author ID 1 2 1 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 VLG VQDPH UDWLQJ DJH \XSS\ OXEEHU JXSS\ UXVW\ sname rating yuppy lubber guppy rusty 9 8 5 10 14 Projection 15 Relational Operations Selection VLG VQDPH UDWLQJ DJH \XSS\ OXEEHU JXSS\ UXVW\ sname rating yuppy 9 rusty 10 Rating > 8 16 Relational Operations Join VLG VLG ELG GD\ VQDPH UDWLQJ DJH GXVWLQ OXEEHU UXVW\ sid 22 58 sname rating age dustin 7 45.0 rusty 10 35.0 bid 101 103 day 10/10/96 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 ERRNV database – Four tables $XWKRUV, 3XEOLVKHUV, $XWKRU,6%1 and 7LWOHV – 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 1 authorISBN authorID isbn 1 titles isbn title lastName editionNumber publishers publisherID publisherName 1 copyright publisherID imageFile price 28 Comandos SQL SQL keyword Description CREATE TABLE Creates a table. SELECT Retrieves data from one or more tables. FROM Tables involved in the query. Required in every SELECT. WHERE Criteria for selection that determine the rows to be retrieved, deleted or updated. GROUP BY Criteria for grouping rows ORDER BY INNER JOIN Criteria for ordering rows. Merge rows from multiple tables. INSERT Insert rows into a specified table. UPDATE Update rows in a specified table. DELETE 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 Combinar los datos de multiples tablas: JOIN Authors AuthorISBN Author ID FirstName LastName YearBorn ISBN 1 Harvey Deitel 1946 2 Paul Deitel 1968 3 Tem Nieto 1969 0-13-010671-2 0-13-010671-2 0-13-020522-2 0-13-020522-2 0-13-020522-2 Author ID 1 2 1 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