Database: SQL and MySQL

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. Chapter 22 – Database: SQL, MySQL, DBI and ADO.NET Outline 22.1 Introduction 22.2 Relational Database Model.
Advertisements

Introduction to Structured Query Language (SQL)
 Pearson Education, Inc. All rights reserved Accessing Databases with JDBC.
Introduction to Structured Query Language (SQL)
1 Introduction to Web Application Introduction to Data Base.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Introduction to Structured Query Language (SQL)
CSC 2720 Building Web Applications Database and SQL.
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Exercise SELECT authorID, lastName FROM authors AuthorID FirstName
Java Database Connectivity (JDBC). Introduction Database –Collection of data DBMS –Database management system –Storing and organizing data SQL –Relational.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
ASP.NET Programming with C# and SQL Server First Edition
Intro to JDBC To effectively use Java Data Base Connectivity we must understand: 1.Relational Database Management Systems (RDBMS) 2.JDBC Drivers 3.SQL.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
 2008 Pearson Education, Inc. All rights reserved Database: SQL, MySQL, ADO.NET 2.0 and Java DB.
CHAPTER 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
 2003 Prentice Hall, Inc. All rights reserved. 1 Java Database Connectivity with JDBC TM.
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
Chapter 1 Overview of Database Concepts Oracle 10g: SQL
Database Technical Session By: Prof. Adarsh Patel.
Lecture 2 An Overview of Relational Database IST 318 – DB Admin.
Introduction to databases and SQL. What is a database?  A database is an organized way of holding together pieces of information  A database refers.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CHAPTER 8 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 MySQL and JDBC.
1 Databases November 15, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel, and Goldberg. Published.
 2004 Prentice Hall, Inc. All rights reserved. 1 Segment – 6 Web Server & database.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Selecting Data.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved. 2 Revised by Dr. T. Tran for CSI3140.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
 2005 Pearson Education, Inc. All rights reserved Accessing Databases with JDBC.
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
 2008 Pearson Education, Inc. All rights reserved Database: SQL, MySQL, ADO.NET 2.0 and Java DB.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 22 - SQL, MySQL, DBI and ADO Outline 22.1 Introduction 22.2 Relational Database Model 22.3 Relational.
 2009 Pearson Education, Inc. All rights reserved Databases and LINQ to SQL.
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
ORDER BY Clause The result of a query can be sorted in ascending or descending order using the optional ORDER BY clause. The simplest form of.
Fundamentals of DBMS Notes-1.
Database, SQL and ADO.NET
Web Systems & Technologies
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Chapter 5 Introduction to SQL.
 2012 Pearson Education, Inc. All rights reserved.
JDBC.
Databases Intro (from Deitel)
Chapter 7 Working with Databases and MySQL
Chapter 8 Working with Databases and MySQL
Chapter 22 - SQL, MySQL, DBI and ADO
Presentation transcript:

Database: SQL and MySQL Chapter 5 Database: SQL and MySQL

2 Introduction A database is an integrated collection of data. A database management system (DBMS) provides mechanisms for storing, organizing, retrieving and modifying data. Today’s most popular database management systems are relational database systems. SQL is the international standard language used almost universally with relational database systems to perform queries and manipulate data. 2

3 Relational Data A relational database stores data in tables. Tables are composed of rows, and rows are composed of columns in which values are stored. A primary key is provides unique values that cannot be duplicated in other rows of the same table. Each column of a table represents a different attribute in a row of data. The primary key can be composed of more than one column. SQL provides a rich set of language constructs that enable you to define complex queries to retrieve data from a database. 3

Relational Data (Cont.) 4 Relational Data (Cont.) Every column in a primary key must have a value, and the value of the primary key must be unique. This is known as the Rule of Entity Integrity. A one-to-many relationship between tables indicates that a row in one table can have many related rows in a separate table. A foreign key is a column in a table that matches the primary-key column in another table. The foreign key helps maintain the Rule of Referential Integrity: Every foreign-key value must appear as another table’s primary-key value. Foreign keys can be used to combine information from multiple tables. There is a one-to-many relationship between a primary key and its corresponding foreign key. 4

Employee table sample data. 5 Employee table sample data. 5

6 MySQL MySQL (pronounced “my sequel”) is a robust and scalable relational database management system (RDBMS) that was created by the Swedish consulting firm TcX in 1994. MySQL is a multiuser, multithreaded RDBMS server that uses SQL to interact with and manipulate data. Multithreading capabilities enable MySQL database to perform multiple tasks concurrently, allowing the server to process client requests efficiently. Implementations of MySQL are available for Windows, Mac OS X, Linux and UNIX. 6

Introduction to MySQL MySQL is the database program most commonly used to develop database-driven PHP Web sites. MySQL is a fast, efficient program that is available for use on Unix and Windows computers. MySQL is suitable for small to medium-sized projects and requires very few system resources to run. MySQL is also an Open Source product, which means you can use it free of charge. Although MySQL is very powerful, it is relatively easy to install and manage. This makes it a good program for new developers to use when learning to work with databases using PHP.

Relationship PHP and MySQL One of the most useful features of PHP is the ability to access a database. Databases store and manage large collections of information. PHP pages can be used to make this information available to the users who visit your Web site. A PHP page can also allow users to manipulate the data in a database. Using databases to store information and then using PHP pages to access that information is an efficient method of displaying up-to-date information in a Web site.

Structured Query Language (SQL) SQL is the language used in a PHP script to communicate with a database. Language for creating database queries and manipulating data. Originally developed by IBM as a database query language for use with mainframe computers, SQL was soon adopted by many vendors for use with their relational database management systems.

10 SQL The next several sections will discuss most of the keywords listed in the following slide in the context of SQL queries and statements. 10

11 SQL query keywords. 11

Relational Database Overview: A books Database 12 Relational Database Overview: A books Database An entity-relationship (ER) diagram shows the database tables and the relationships among them. Every row must have a primary-key value, and that value must be unique in the table. This is known as the Rule of Entity Integrity. An infinity symbol (∞) indicates a one-to-many relationship, in which an entry from a table can have an arbitrary number of entries in another table. A many-to-many relationship indicates that multiple entries can be related between tables. 12

Table relationships in the books database. 13 One-to-many relationship One-to-many relationship Foreign keys Primary keys Many-to-many relationship Table relationships in the books database. 13

Relational Database Overview: A books Database 14 Relational Database Overview: A books Database authors table from the books database. 14

Sample data from the authors table. 15 Sample data from the authors table. 15

authorISBN table from the books database. 16 authorISBN table from the books database. 16

Sample data from the authorISBN table of books. 17 Sample data from the authorISBN table of books. 17

titles table from the books database. 18 titles table from the books database. 18

Sample data from the titles table of the books database. 19 Sample data from the titles table of the books database. 19

Common Programming Error 20 Common Programming Error Not providing a value for every column in a primary key breaks the Rule of Entity Integrity and causes the DBMS to report an error. Providing the same value for the primary key in multiple rows causes the DBMS to report an error. Providing a foreign-key value that does not appear as a primary-key value in another table breaks the Rule of Referential Integrity and causes the DBMS to report an error. Foreign keys also allow related data in multiple tables to be selected from those tables for analytic purposes—this is known as joining the data. 20

Create Database CREATE DATABASE databaseName; Create a database (make a directory) whose name is books CREATE DATABASE books;

Basic SELECT Query The basic form of a query is 22 Basic SELECT Query The basic form of a query is SELECT * FROM tableName where the asterisk (*) indicates that all columns from tableName should be selected, and tableName specifies the table in the database from which rows will be retrieved. To retrieve specific columns from a table, replace the asterisk (*) with a comma-separated list of column names. 22

Sample authorID and lastName data from the authors table. 23 SELECT authorID, lastName FROM authors Sample authorID and lastName data from the authors table. 23

24 WHERE Clause The optional WHERE clause in a query specifies the selection criteria for the query. The basic form of a query with selection criteria is SELECT columnName1, columnName2, … FROM tableName WHERE criteria The WHERE clause can contain operators <, >, <=, >=, =, <> and LIKE. Operator LIKE is used for string pattern matching with wildcard characters percent (%) and underscore (_). A percent character (%) in a pattern indicates that a string matching the pattern can have zero or more characters at the percent character’s location in the pattern. An underscore (_) in the pattern string indicates a single character at that position in the pattern. 24

Sampling of titles with copyrights after 2005 from table titles. 25 SELECT title, editionNumber, copyright FROM titles WHERE copyright > '2005' Sampling of titles with copyrights after 2005 from table titles. 25

Authors whose last name starts with D from the authors table. 26 SELECT authorID, firstName, lastName FROM authors WHERE lastName LIKE 'D%' Authors whose last name starts with D from the authors table. 26

SELECT authorID, firstName, lastName FROM authors 27 SELECT authorID, firstName, lastName FROM authors WHERE lastName LIKE '_o%' The only author from the authors table whose last name contains o as the second letter. 27

28 ORDER BY Clause The result of a query can be sorted in ascending or descending order using the optional ORDER BY clause. The simplest form of an ORDER BY clause is SELECT columnName1, columnName2, … FROM tableName ORDER BY column ASC SELECT columnName1, columnName2, … FROM tableName ORDER BY column DESC where ASC specifies ascending order, DESC specifies descending order and column specifies the column on which the sort is based. The default sorting order is ascending, so ASC is optional. Multiple columns can be used for ordering purposes with an ORDER BY clause of the form ORDER BY column1 sortingOrder, column2 sortingOrder, … The WHERE and ORDER BY clauses can be combined in one query. If used, ORDER BY must be the last clause in the query. 28

authors sample data in ascending order by lastName. 29 SELECT authorID, firstName, lastName FROM authors ORDER BY lastName ASC authors sample data in ascending order by lastName. 29

authors sample data in descending order by lastName. 30 SELECT authorID, firstName, lastName FROM authors ORDER BY lastName DESC authors sample data in descending order by lastName. 30

authors sample data in ascending order by lastName and firstName. 31 SELECT authorID, firstName, lastName FROM authors ORDER BY lastName, firstName authors sample data in ascending order by lastName and firstName. 31

SELECT isbn, title, editionNumber, copyright FROM titles 32 SELECT isbn, title, editionNumber, copyright FROM titles WHERE title LIKE '%How to Program' ORDER BY title ASC Sampling of books from table titles whose titles end with How to Program in ascending order by title. 32

Combining Data from Multiple Tables: INNER JOIN 33 Combining Data from Multiple Tables: INNER JOIN An INNER JOIN combines rows from two tables by matching values in columns that are common to the tables. The basic form for the INNER JOIN operator is: SELECT columnName1, columnName2, … FROM table1 INNER JOIN table2 ON table1.columnName = table2.columnName The ON clause specifies a condition that determines which rows are joined. This condition often compares columns from each table If a SQL statement uses columns with the same name from multiple tables, the column names must be fully qualified by prefixing them with their table names and a dot (.). 33

SELECT firstName, lastName, isbn FROM authors INNER JOIN authorISBN ON authors.authorID = authorISBN.authorID ORDER BY lastName, firstName 34 Sampling of authors and ISBNs for the books they have written in ascending order by lastName and firstName. 34

35 INSERT Statement An INSERT statement inserts a new row into a table. The basic form of this statement is INSERT INTO tableName ( columnName1, columnName2, …, columnNameN ) VALUES ( value1, value2, …, valueN ) where tableName is the table in which to insert the row. The tableName is followed by a comma-separated list of column names in parentheses. The list of column names is followed by the SQL keyword VALUES and a comma-separated list of values in parentheses. SQL uses single quotes (') as the delimiter for strings. To specify a string containing a single quote in SQL, the single quote must be escaped with another single quote. 35

Sample data from table Authors after an INSERT operation. 36 INSERT INTO authors ( firstName, lastName ) VALUES ( 'Sue', 'Smith' ) Sample data from table Authors after an INSERT operation. 36

37 UPDATE Statement An UPDATE statement modifies data in a table. The basic form of an UPDATE statement is UPDATE tableName SET columnName1 = value1, columnName2 = value2, …, columnNameN = valueN WHERE criteria where tableName is the table in which to update data. The tableName is followed by keyword SET and a comma-separated list of column name/value pairs in the format columnName = value. The optional WHERE clause criteria determines which rows to update. 37

Sample data from table authors after an UPDATE operation. 38 UPDATE authors SET lastName = 'Jones' WHERE lastName = 'Smith' AND firstName = 'Sue' Sample data from table authors after an UPDATE operation. 38

39 DELETE Statement A DELETE statement removes rows from a table. The simplest form for a DELETE statement is DELETE FROM tableName WHERE criteria where tableName is the table from which to delete a row (or rows). The optional WHERE criteria determines which rows to delete. If this clause is omitted, all the table’s rows are deleted. 39

Sample data from table authors after a DELETE operation. 40 DELETE FROM authors WHERE lastName = 'Jones' AND firstName = 'Sue' Sample data from table authors after a DELETE operation. 40

41 ALTER Statement The structure of a table can be changed using ALTER command. Using this command we can add, delete, modify and rename the table. ALTER TABLE tableName ADD newfieldName ALTER TABLE tableName RENAME newtableName ALTER TABLE tableName DROP fieldName 41

DROP DATABASE databasename; DROP Statement A DROP TABLE command is used to remove or delete tables from a database. DROP TABLE tableName; A DROP DATABASE command is used to delete entire database. DROP DATABASE databasename;

QA

Write SQL queries for the books database that perform each of the following tasks: a) Select all authors from the Authors table with the columns in the order lastName, firstName and authorID. b) Select a specific author and list all books for that author. Include the title, year and ISBN number. Order the information alphabetically by title. c) Add a new author to the Authors table. d) Add a new title for an author (remember that the book must have an entry in the AuthorISBN table).

Correct each of the following SQL statements that refer to the books database. a) SELECT FROM author FIELD firstName WHERE authorID = 3 ANS: SELECT firstName FROM Authors WHERE authorID = 3. b) SELECT isbn, title FROM Titles ORDER WITH title DESC ANS: SELECT isbn, title FROM Titles ORDER BY title DESC. c) INSERT Authors ( authorID, firstName, lastName ) VALUES ( "2", "Jane", "Doe" ) ANS: INSERT INTO Authors ( firstName, lastName ) VALUES ( 'Jane', 'Doe' ).

Answers a) Select all authors from the Authors table with the columns in the order lastName, firstName and authorID. ANS: SELECT lastName, firstName, authorID FROM Authors. b) Select a specific author and list all books for that author. Include the title, year and ISBN number. Order the information alphabetically by title. ANS: SELECT Authors.authorID, Titles.title, Titles.copyright, Titles.isbn, Authors.lastName, Authors.firstName FROM Titles, Authors, AuthorISBN WHERE Authors.authorID = AuthorISBN.authorID AND AuthorISBN.isbn = Titles.isbn AND ( Authors.authorID = 1 ) ORDER BY Titles.title c) Add a new author to the Authors table. INSERT INTO Authors ( firstName, lastName ) VALUES ( 'Betsy', 'BLUE' )

Answers d) Add a new title for an author (remember that the book must have an entry in the AuthorISBN table). ANS: First, insert a row into the Titles table by using the following: INSERT INTO Titles ( isbn, title, editionNumber, copyright ) VALUES ( '5-55-555555-5', 'Technical Editing', '1', 2008 ) ANS: Then, insert a row into the AuthorISBN table by using the following: INSERT INTO AuthorISBN ( isbn, authorID ) VALUES ( '5-55-555555-5', 5 )