Practical Session 13 Structured Data Bases Structured Query Language Exam Questions Factory Method Pattern Abstract Factory Pattern.

Slides:



Advertisements
Similar presentations
Relational Database Systems Higher Information Systems Advanced Implementation in MySQL/PHP.
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Database Design -- Basic SQL
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)MySQL Recap.
Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Introduction to Structured Query Language (SQL)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 SQL: Data Definition, Constraints, and Basic Queries and Updates.
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
Chapter 7: SQL, the Structured Query Language Soid Quintero & Ervi Bongso CS157B.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
Introduction to SQL J.-S. Chou Assistant Professor.
ASP.NET Programming with C# and SQL Server First Edition
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 3 Single-Table Queries
CSE314 Database Systems Lecture 4 Basic SQL Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL LANGUAGE and Relational Data Model TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
CMPT 258 Database Systems The Relationship Model (Chapter 3)
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Practical Session 13 Factory Method Pattern Abstract Factory Pattern Structured Data Bases Structured Query Language Exam Questions.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SQL, the Structured Query Language
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Fundamentals of DBMS Notes-1.
SQL Query Getting to the data ……..
More SQL: Complex Queries,
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
SQL LANGUAGE and Relational Data Model TUTORIAL
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Database systems Lecture 3 – SQL + CRUD
Access: SQL Participation Project
Chapter 7 Introduction to Structured Query Language (SQL)
SQL-1 Week 8-9.
SQL Basics BCHB697.
Presentation transcript:

Practical Session 13 Structured Data Bases Structured Query Language Exam Questions Factory Method Pattern Abstract Factory Pattern

Relational Databases A relational database is a collection of data items organized as a set of formally described tables from which data can be accessed easily A relational database is created using the relational model: – Data Definition Language Used to build and destroy databases, create, and drop tables – Data Manipulation Language Used to manipulate data in databases, insert, delete, and retrieve We will use SQL (Structured Query Language) data definition and query language. Each database consists of a number of tables and each table has its own primary key. Relational: – Because you may have relations between the different tables. Very good tutorial at:

Table -Table name: TEACHING_ASSISTANTS -Column name: Id, Name, Office Hours -Column type: INT, VARCHAR(20), VARCHAR(20) -Each table has primary key, must be unique and non-null: example: id -Each line in table is called a record -You may have foreign key columns, which is a primary key in other table, to denote relationship between two different tables IDNameOffice hours 1AchiyaTue 10:00-12:00 2BoazSun 16:00-18:00 3JumanaSun 16:00-18:00 5MajeedSun 16:00-18:00 4RamziThu 12:00-14:00 6YehonatanTue 12:00-14:00

ANSI SQL Data Types Character strings – CHARACTER(n) or CHAR(n) — fixed-width n-character string, padded with spaces as needed – CHARACTER VARYING(n) or VARCHAR(n) — variable-width string with a maximum size of n characters – NATIONAL CHARACTER(n) or NCHAR(n) — fixed width string supporting an international character set – NATIONAL CHARACTER VARYING(n) or NVARCHAR(n) — variable-width NCHAR string Bit arrays – BIT(n) — an array of n bits – BIT VARYING(n) — an array of up to n bits Numbers – INTEGER and SMALLINT – FLOAT, REAL and DOUBLE PRECISION – NUMERIC(precision, scale) or DECIMAL(precision, scale) Date and time – DATE — for date values (e.g., ) – TIME — for time values (e.g., 15:51:36). – TIME WITH TIME ZONE or TIMETZ — the same as TIME, but including details about the time zone in question. – TIMESTAMP — This is a DATE and a TIME put together in one variable (e.g., :51:36). – TIMESTAMP WITH TIME ZONE or TIMESTAMPTZ — the same as TIMESTAMP, but including details about the time zone in question.

Creating/Deleting a table CREATE TABLE STORE( Id integer PRIMARY KEY, Name varchar(30) NOT NULL, Type varchar(30)); - SQL statements need to end with a semicolon to separate between different statements.

Primary Key A primary key is used to uniquely identify each row in a table. A primary key can consist of one or more fields on a table. When multiple fields are used as a primary key, they are called a composite key. Primary key is inheritably unique.

Foreign Key A foreign key is a field(s) that point to the primary key of another table. The purpose of the foreign key is to ensure referential integrity of the data. – Only values that are in the database are permitted.

Code

Example ColumnTypeConstraints IDintPrimary Key NameVARCHAR(50) Office hoursVARCHAR(9) ColumnTypeConstraints TAintForeign Key GroupintPrimary Key LocationVARCHAR(50) TimeVARCHAR(9) TEACHING_ASSISTANTS PRACTICAL_SESSIONS Effect: PRACTICAL_SESSIONS table cannot contain information on a TA that is not in the Teaching Assistant table.

SQL Commands

PRACTICAL_SESSIONS TAGroupLocationTime Sun Sun Thu Thu Thu Thu Thu Thu Thu Thu Thu IDNameOfficeHours 1AchiyaTue 10:00-12:00 2BoazSun 16:00-18:00 3JumanaSun 16:00-18:00 5MajeedSun 16:00-18:00 4RamziThu 12:00-14:00 6YehonatanTue 12:00-14:00 TEACHING_ASSISTANTS

Insert/Delete/Update [record]

Select The most common operation in SQL is the query, which is performed with the declarative SELECT statement. SELECT retrieves data from one or more tables, or expressions. Standard SELECT statements have no persistent effects on the database. Some non-standard implementations of SELECT can have persistent effects, such as the SELECT INTO syntax that exists in some databases.

Select A query includes a list of columns to be included in the final result immediately following the SELECT keyword. An asterisk ("*") can be used to specify that the query should return all columns of the queried tables. SELECT is the most complex statement in SQL, with optional keywords and clauses that include: – FROM : The FROM clause which indicates the table(s) from which data is to be retrieved. – WHERE : The WHERE clause includes a comparison predicate, which restricts the rows returned by the query. The WHERE clause eliminates all rows from the result set for which the comparison predicate does not evaluate to True. – GROUP BY: The GROUP BY clause is used to project rows having common values into a smaller set of rows. GROUP BY is often used in conjunction with SQL aggregation functions or to eliminate duplicate rows from a result set. The WHERE clause is applied before the GROUP BY clause. – HAVING: The HAVING clause includes a predicate used to filter rows resulting from the GROUP BY clause. Because it acts on the results of the GROUP BY clause, aggregation functions can be used in the HAVING clause predicate. – ORDER BY: The ORDER BY clause identifies which columns are used to sort the resulting data, and in which direction they should be sorted (options are: ASC (default) ascending or DESC descending).ORDER BY Without an ORDER BY clause, the order of rows returned by an SQL query is undefined.

SQL Aggregation Functions SQL aggregate functions return a single value, calculated from values in a column. – AVG() - Returns the average value – COUNT() - Returns the number of rows – FIRST() - Returns the first value – LAST() - Returns the last value – MAX() - Returns the largest value – MIN() - Returns the smallest value – SUM() - Returns the sum

FROM * returns all columns. You may specifically choose columns you want, and their order

WHERE AND OR IS (IS NOT NULL) IN BETWEEN LIKE

HAVING/GROUP BY

ORDER BY If not specified, default order is ascending.

JOIN The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables. Tables in a database are often related to each other with keys. Different SQL JOINs: – INNER JOIN/JOIN: Return rows when there is at least one match in both tables. – LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table. – RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table. – OUTER JOIN/FULL JOIN: Return rows when there is a match in one of the tables. Examples:

Exam Questions Write a model that holds the following information: – Movie: Name, publish year, origin country, director name – Director: Name, list of movies – Actor: Name, list of roles in movies

Notes 1:N relations are encoded by a single foreign key in the table that has the N end: – Movies Director (In general a movie has only one director) N:N relations are encoded by a cross-table; two foreign keys to the related tables with additional information that characterizes the relation if needed – Movies Actors (In general, actors play in many movies, and movies have many actors) – In our case, the role played by the actor in the movie is data that belongs to the cross table. If you assume that a movie can have several directors, then you need a cross table movies_directors as well. If you assume that additional information on directors may not be added, or that names of directors are not modified – then you may use the field directorName directly in the movies table.

Solution

Query on table Write an SQL query that returns: – Movie name, Director name, Actor, Role

Multiple Roles for Actor in Movie We wish to add support for multiple roles for each actor in a movie. Current implementation defines the primary key of actors_movies as (actorId, movieId). This means that we cannot have the same actor in the same movie more than once. Solution? – Add the field role to the primary key. – Primary key (actorId, movieId, role);

Query Write an SQL query that returns the roles of “Christoph Waltz” in movies that where directed by “Quentin Tarantino”

Answer? Inglorious Bastards Django Unchained

Factory Method Pattern How it is done? – Making Constructors private/protected – Implementing a function which its sole purpose is creating desired objects and returning them create() open() Can be done by using static creation functions.

Example

Abstract Factory Pattern Done by creating a class which its sole purpose is creating requested objects. The rest of the classes are not public, not part of the interface. Any creation of objects need to be done by making an instance of the Factory object and using its methods.

Example

When and why to use Factory over Constructors Allows developers of the framework to change the constructors when needed, without worrying about backward compatibility. – Frameworks which uses Constructors cannot change any constructor at all once the product is released. – Releasing new versions of the product requires them to keep these constructors to allow applications which already use their framework to continue working. – Solution? Factory Pattern. Allows creation of objects when it is unknown which type to create. – When deciding on object type relies on the state of the framework, using “new” is not possible. – Users of framework cannot know internal state of the framework itself. – Using new operator already decides which object type to create. When the user does not really care about many of the constructor parameters. – Framework: Hiring Agency [agency] – User: Company wishes to hire a Java developer, with 3 years experience. Factory method: agency.hireDeveloper(“Java”, 3); Constructor: new Worker(name, age, experience, skills, address, id); – The “user” only cares about 2 things, however creating the object requires many more items. If having a named method is the only way to make the creation self-explanatory, consider factory pattern over regular constructors. – Constructor names must