Populating and Querying tables Insert and mostly View (DML)

Slides:



Advertisements
Similar presentations
What is a Database By: Cristian Dubon.
Advertisements

The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
Accounting System Design
Relational Databases Chapter 4.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Using SQL to create tables Ways of using Databases.
Northwind Sample database (also supplied with MS Access)
Fundamentals, Design, and Implementation, 9/e Chapter 7 Using SQL in Applications.
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
Introduction to Structured Query Language (SQL)
Chapter 4 Relational Databases Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-1.
Database Features Lecture 2. Desirable features in an information system Integrity Referential integrity Data independence Controlled redundancy Security.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Week 2 Lecture 2 Structure of a database. External Schema Conceptual Schema Internal Schema Physical Schema.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
Information systems and databases Database information systems Read the textbook: Chapter 2: Information systems and databases FOR MORE INFO...
Chapter 4 Relational Databases Copyright © 2012 Pearson Education 4-1.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Oracle Data Definition Language (DDL)
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Chapter 4 The Relational Model Pearson Education © 2014.
Chapter 4 The Relational Model.
Copyright © 2003 by Prentice Hall Module 4 Database Management Systems 1.What is a database? Data hierarchy and data organization Field, record, file,
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
XML in SQL Server Overview XML is a key part of any modern data environment It can be used to transmit data in a platform, application neutral form.
DAY 14: ACCESS CHAPTER 1 Tazin Afrin October 03,
DATABASES Pindaro Demertzoglou – Lally School of Management and Technology.
CHAPTER 8: MANAGING DATA RESOURCES. File Organization Terms Field: group of characters that represent something Record: group of related fields File:
Relational Databases Database Driven Applications Retrieving Data Changing Data Analysing Data What is a DBMS An application that holds the data manages.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Relational Database. Database Management System (DBMS)
Lecture # 3 & 4 Chapter # 2 Database System Concepts and Architecture Muhammad Emran Database Systems 1.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Databases Shortfalls of file management systems Structure of a database Database administration Database Management system Hierarchical Databases Network.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Information Building and Retrieval Using MySQL Track 3 : Basic Course in Database.
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Concepts 2440: 180 Database Concepts Instructor:
Visual Programing SQL Overview Section 1.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Database Management Supplement 1. 2 I. The Hierarchy of Data Database File (Entity, Table) Record (info for a specific entity, Row) Field (Attribute,
More queries Outer joins and summary queries. Inner and outer joins An Inner join only returns matching rows from two tables –E.g. if I join the customer.
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
Enterprise manager Using the Enterprise manager. Purpose of the Enterprise Manager To design tables To populate / update tables To draw diagrams of tables.
Populating and Querying tables Insert, Update, Delete and View (DML)
3 1 Database Systems The Relational Database Model.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
IMS 4212: Intro to Multi-Table SELECT Statements 1 Dr. Lawrence West, MIS Dept., University of Central Florida Multi-Table SELECT Statements—Topics.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
The Relational Model © Pearson Education Limited 1995, 2005 Bayu Adhi Tama, M.T.I.
Databases Flat Files & Relational Databases. Learning Objectives Describe flat files and databases. Explain the advantages that using a relational database.
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 Database Fundamentals Introduction to SQL. 2 SQL Overview Structured Query Language The standard for relational database management systems (RDBMS)
Data Resource Management Application Layer TPS A RCHITECTURE Data Layer Sales/MarketingHR Finance/Accounting Operations Spreadsheet Data MS Access Accounts.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
Fundamentals of DBMS Notes-1.
Databases and Information Management
Teaching slides Chapter 8.
Web Services שפת SQL כתבה: זהבה יעקובסון ליווי מקצועי : ארז קלר
Databases and Information Management
Flat Files & Relational Databases
Chapter 7 Using SQL in Applications
Relational Database Design
Advanced Joins IN ( ) Expression Subqueries with IN ( ) Expression
Presentation transcript:

Populating and Querying tables Insert and mostly View (DML)

Contents of this lecture Inserting data into a single table. Queries –On two tables. –On two joined tables. –On more than two joined tables. –Lop-sided queries (outer joins). –Tables joined to themselves. –Group data using the GROUP BY clause –Include or exclude grouped rows by using the HAVING clause

Inserting data into a table To add a row of data to a table INSERT INTO VALUES (value1, value2, …valueN) If the value list matches the column list exactly, there is no need to list the column names. If the value list does not match the column list exactly, there is a need to list the column names.

Example insert into Expert values ( ,'Dr. O''Meara','Orthopaedic'); Where the expert table was set up as:- CREATE TABLE Expert ( Expert_Id numeric(7,0), Expert_Name varchar(50), Expertise_area varchar(15), PRIMARY KEY (Expert_Id)) Note also, if you want to put an apostrophe in a string, put in two apostrophes.

Referential integrity The above tables have no foreign keys and a straightforward insert does not compromise referential integrity. If a table is constrained by foreign key values, then there is a need to see what is in the other table. Retrieving data from a database –Data can be retrieved from a table or set of tables using the SELECT statement

Common errors on insert Server: Msg 2627, Level 14, State 1, Line 1 Violation of PRIMARY KEY constraint 'PK__Contract__05D8E0BE'. Cannot insert duplicate key in object 'Contract'. –This happens when you put a value in the primary key field that already appears in the file – i.e. record already exists. INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK__Contract__Custom__06CD04F7'. The conflict occurred in database 'pobyrne', table 'customer', column 'Customer_number'. –In this statement, I tried to add a contract to the contract table, referencing a customer that was not in the customer table. The contract table has customer_number as a foreign key referencing the customer table.

Enquiry Access Paths Traversing a data model Start with a data model or ERD.

ERD

Which customers have bought stock supplied by ‘supplierId’?

What are the sales that this staff member made?

Multi-table queries

Revisiting database structure External Schema Conceptual Schema Internal Schema Physical Schema

The external schema –Level visible to user –Multiple views of the system e.g. View an order - see limited product and customer information –Only the database Administrator may access the whole database at this level Each external view is defined by means of an external schema –Provides definitions of each external view. –Written in a Data Definition Language. –individual to the user or user group. –accessed through a 3GL, a query language or a special purpose forms or menu- based language

Queries User creates a query, to provide a specific view of the data Database application converts this into a statement in SQL DBMS executes the SQL statement BASE TABLE - a table storing data DERIVED TABLE - virtual result table from a query

Query essentials Selection –This is where rows from a table are extracted on the basis of the value of one or more of the fields Projection –This is where columns from a table are extracted on the basis of the value of one or more of the fields Join –This is where rows and columns from more than one table are extracted

Selection Selects only those ROWS which satisfy the selection criteria Example: –Set up a query to display claims where the state is trivial (T): Select * from Claim where Claim_state = 'T'

Projection A projection of a table onto a subset of its attributes (i.e. throw away unnecessary attributes) e.g. project Expert Name and Expertise from Expert: –Select Expert_Name, Expertise_area from Expert

Joins Tables can be joined together on fields which have the same attributes: SELECT * FROM products, suppliers WHERE products.supplierId = suppliers.supplierId; –Note: Tables can be joined in this way even if they were set up without a foreign key.

More usually, the query uses ‘join’ Select * from products join suppliers on products.supplierId = suppliers.supplierId

3-table join If you have a query that involves more than one relationship, you must join over relationships For example (Northwind), –“Show me the sales for each product, ordered by category and product, giving the categoryname and product name.

NorthWind ERD

Enquiry Access Path

Joins required Categories needs to join with products Products needs to join with Order details Joins can be nested A join B on A.x = B.x join C on B.y = C.y Join D on C.z = D.z etc.

In this example, there are 2 joins Categories join products on Categories.categoryId = Products.CategoryId Products join [order details] on products.productId = [order details].productId –There is also a derived column, from the [order details] table. Income = (unitPrice * quantity) - discount

Query use Northwind; Select categoryName, ProductName, (([order details].unitPrice*Quantity)- Discount) as income from categories join products on categories.categoryid = products.categoryid join [order details] on products.productid = [order details].productid;

Result categoryName ProductName income Beverages Chai Beverages Chai Beverages Chai Beverages Chai Beverages Chai Beverages Chai Beverages Chai Beverages Chai Beverages Chai Beverages Chai Beverages Chai Beverages Chai Beverages Chai Beverages Chai 54.0

Improve layout of output Select categoryName, cast(ProductName as char(12))as Product, cast(([order details].unitPrice*Quantity)- Discount as decimal(10,2)) as income from categories join products on categories.categoryid = products.categoryid join [order details] on products.productid = [order details].productid

Improved output categoryName Product income Beverages Chai Beverages Chai Beverages Chai Beverages Chai Beverages Chai Beverages Chai Beverages Chai Beverages Chai Beverages Chai Beverages Chai