Databases Intro (from Deitel)

Slides:



Advertisements
Similar presentations
Chapter 15 A Table with a View: Database Queries.
Advertisements

 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.
Introduction to Structured Query Language (SQL)
1 Introduction to Web Application Introduction to Data Base.
Structured Query Language Part I Chapter Three CIS 218.
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
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
 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.
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.
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
CHAPTER 8 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
Database: SQL and MySQL
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
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
Database, SQL, and ADO.NET- Part 1 Session 11 Mata kuliah: M0874 – Programming II Tahun: 2010.
Databases MIS 21. Some database terminology  Database: integrated collection of data  Database Management System (DBMS): environment that provides mechanisms.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Ch Determine the output displayed from procedures Lab sheet 10.4.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Introduction to Databases Queries CS 146. Sample Database: CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT.
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.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
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.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
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.
 A database is an organized collection of data  A database management system (DBMS) provides mechanisms for storing, organizing, retrieving and modifying.
VOCAB REVIEW. A field that can be computed from other fields Calculated field Click for the answer Next Question.
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.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Based on Murach (ch 17) and Deitel Slides 1. o Knowledge o Describe the hardware components of a typical multi-user system. o Describe the software components.
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.
Writing Basic SQL SELECT Statements
 2012 Pearson Education, Inc. All rights reserved.
Basic select statement
ATS Application Programming: Java Programming
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
A Table with a View: Database Queries
JDBC.
The University of Texas – Pan American
ISC440: Web Programming 2 Server-side Scripting PHP 3
Databases - בסיסי נתונים
Chapter 22 - SQL, MySQL, DBI and ADO
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Introduction To Structured Query Language (SQL)
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Chapter 7 Introduction to Structured Query Language (SQL)
Introduction To Structured Query Language (SQL)
A Table with a View: Database Queries
An introduction to database programming
A Table with a View: Database Queries
Java Chapter 6 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Databases Intro (from Deitel)

Databases A database is an organized collection of data A database management system (DBMS) provides mechanisms for storing, organizing, retrieving and modifying data for many users SQL is the international standard language used with relational databases to perform queries and to manipulate data Popular relational database management systems (RDBMSs) Microsoft SQL Server Oracle Sybase IBM DB2 Informix PostgreSQL MySQL 2

A relational database stores data in tables A relational database is a logical representation of data that allows the data to be accessed without consideration of its physical structure A relational database stores data in tables Tables are composed of rows, and rows are composed of columns in which values are stored Primary key —a column (or group of columns) with a unique value that cannot be duplicated in other rows 3

Relational Databases

Books Database The database consists of three tables: Authors AuthorISBN Titles The Authors table consists of three columns that maintain each author’s unique ID number, first name and last name The AuthorISBN table consists of two columns that maintain each ISBN and the corresponding author’s ID number The Titles table consists of four columns that stand for the ISBN, the title, the edition number and the copyright year Foreign key —a column in a table that matches the primary key column in another table. Helps maintain the Rule of Referential Integrity—every foreign-key value must appear as another table’s primary-key value There is a one-to-many relationship between a primary key and a corresponding foreign key 5

A Books Database

Books Database (cont.) Entity-relationship (ER) diagram for the books database Shows the database tables and the relationships among them The names in italic are primary keys A table’s primary key uniquely identifies each row in the table 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 8

SQL (basics)

SQL SELECT A SQL query “selects” rows and columns from one or more tables in a database. The basic form of a SELECT query is SELECT * FROM tableName in which the asterisk (*) wildcard character indicates that all columns from the tableName table should be retrieved. To retrieve all the data in the Authors table, use SELECT * FROM Authors To retrieve only specific columns, replace the asterisk (*) with a comma-separated list of the column names, e.g., SELECT AuthorID, LastName FROM Authors 12

WHERE Close In most cases, only rows that satisfy selection criteria are selected SQL uses the optional WHERE clause in a query to specify the selection criteria for the query The basic form of a query with selection criteria is SELECT columnName1, columnName2, … FROM tableName WHERE criteria To select the Title, EditionNumber and Copyright columns from table Titles for which the Copyright date is greater than 2005, use the query SELECT Title, EditionNumber, Copyright FROM Titles WHERE Copyright > '2005' Strings in SQL are delimited by single (') rather than double (") quotes The WHERE clause criteria can contain the operators <, >, <=, >=, =, <> and LIKE 13

Operator LIKE Operator LIKE is used for pattern matching with wildcard characters percent (%) and underscore (_) A pattern that contains a percent character (%) searches for strings that have zero or more characters at the percent character’s position in the pattern For example, the next query locates the rows of all the authors whose last name starts with the letter D: SELECT AuthorID, FirstName, LastName FROM Authors WHERE LastName LIKE 'D%' 14

Other Patterns Wildcard character Description Example % Any string of zero or more characters. WHERE title LIKE '%computer%' finds all book titles with the word 'computer' anywhere in the book title. _ (underscore) Any single character. WHERE au_fname LIKE '_ean' finds all four-letter first names that end with ean (Dean, Sean, and so on). [ ] Any single character within the specified range ([a-f]) or set ([abcdef]). WHERE au_lname LIKE '[C-P]arsen' finds author last names ending with arsen and starting with any single character between C and P, for example Carsen, Larsen, Karsen, and so on. In range searches, the characters included in the range may vary depending on the sorting rules of the collation. [^] Any single character not within the specified range ([^a-f]) or set ([^abcdef]). WHERE au_lname LIKE 'de[^l]%' all author last names starting with de and where the following letter is not l.

ORDER BY Clause The rows in the result of a query can be sorted into ascending or descending order by using the optional ORDER BY clause. The basic form of a query with an ORDER BY clause is SELECT columnName1, columnName2, … FROM tableName ORDER BY column ASC SELECT columnName1, columnName2, … FROM tableName ORDER BY column DESC ASC specifies ascending order (lowest to highest) DESC specifies descending order (highest to lowest) column specifies the column on which the sort is based. To obtain the list of authors in ascending order by last name (), use the query SELECT AuthorID, FirstName, LastName FROM Authors ORDER BY LastName ASC To obtain the same list of authors in descending order by last name (), use the query SELECT AuthorID, FirstName, LastName FROM Authors ORDER BY LastName DESC Multiple columns can be used for sorting with an ORDER BY clause of the form ORDER BY column1 sortingOrder, column2 sortingOrder, … sortingOrder is either ASC or DESC. Sort all the rows in ascending order by last name, then by first name. SELECT AuthorID, FirstName, LastName FROM Authors ORDER BY LastName, FirstName 16

ORDER BY Clause cont The WHERE and ORDER BY clauses can be combined in one query, as in SELECT ISBN, Title, EditionNumber, Copyright FROM Titles WHERE Title LIKE '%How to Program' ORDER BY Title ASC which returns the ISBN, Title, EditionNumber and Copyright of each book in the Titles table that has a Title ending with "How to Program" and sorts them in ascending order by Title 17

ActiveX Data Objects (ADO) SqlConnection con = new SqlConnection(...); con.Open(); SqlCommand cmd = new SqlCommand( @"SELECT * FROM Customers WHERE c.Region = @Region", con ); cmd.Parameters.AddWithValue("@Region", "US"); DataReader dr = cmd.ExecuteReader(); while (dr.Read()) { string name = dr.GetString(dr.GetOrdinal("Name")); string phone = dr.GetString(dr.GetOrdinal("Phone")); DateTime date = dr.GetDateTime(3); } dr.Close(); con.Close(); Hard to write database-neutral SQL because of subtle variances in the parameters, escaping, types etc.

AirlineReservation Db connection private SqlConnection connectToDatabase() { SqlConnection con = new SqlConnection( @"DataSource=(LocalDB)\v11.0;AttachDbFilename=C:\Projects\MIS2 18\AirlineReservation3\AirlineReservation\AirlineReservationDb .mdf;Integrated Security=True"); return con; }

Save seats SqlConnection con = null; try { con = connectToDatabase(); if (con != null) { con.Open(); } SqlCommand deleteCommand = new SqlCommand("DELETE FROM SeatsTable", con); //always //delete first int rowsDeleted = deleteCommand.ExecuteNonQuery(); for (int i = 0; i < seats.Length; i++) { SqlCommand insertCommand = new SqlCommand("INSERT INTO SeatsTable (seatNumber, isReserved) VALUES(@param1, @param2)", con); insertCommand.Parameters.Add("@param1", SqlDbType.Int).Value = i; insertCommand.Parameters.Add("@param2", SqlDbType.Bit).Value = seats[i]; int rowsInserted = insertCommand.ExecuteNonQuery(); Console.WriteLine("Saved seat {0} reserved {1} ", i, seats[i]); return true; catch (SqlException ex) { Console.WriteLine(ex.StackTrace); Console.WriteLine(ex.Message); return false; finally { con.Close();