IE 423 – Design of Decision Support Systems Database development – Relationships and Queries Introduction to SQL.

Slides:



Advertisements
Similar presentations
Basic SQL Introduction Presented by: Madhuri Bhogadi.
Advertisements

Virtual training week 4 structured query language (SQL)
Introduction to Structured Query Language (SQL)
CSE 190: Internet E-Commerce Lecture 10: Data Tier.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
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)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
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.
Introduction to SQL  SQL or sequel  It is a standardised language with thousands of pages in the standard  It can be in database system through GUI,
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Database Lecture # 1 By Ubaid Ullah.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
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.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
Deanery of Business & Computer Sciences SQL Structured Query Language Implementation Lecture – 8 Database Technology Level I.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
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.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
Chapter 5 MYSQL Database. Introduction to MYSQL MySQL is the world's most popular open-source database. Open source means that the source code, the programming.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
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.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Visual Programing SQL Overview Section 1.
SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
 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.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Introduction to Database SEM I, AY Department of Information Technology Salalah College of Technology Chapter No.3 SQL.
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.
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Introduction to Database Programming with Python Gary Stewart
SQL, the Structured Query Language
Web Systems & Technologies
Chapter 5 Introduction to SQL.
Insert, Update and the rest…
Structured Query Language (SQL) William Klingelsmith
DATABASE MANAGEMENT SYSTEM
Introduction To Structured Query Language (SQL)
Database systems Lecture 3 – SQL + CRUD
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Introduction To Structured Query Language (SQL)
Contents Preface I Introduction Lesson Objectives I-2
Structured Query Language Path from Unorganized to Organized….
SQL (Structured Query Language)
Presentation transcript:

IE 423 – Design of Decision Support Systems Database development – Relationships and Queries Introduction to SQL

Important Events Back from Spring Break (hopefully) Welcome Back

Keepin Up Read Pol and Ahuja – Chapter 8 (should have already done this) Pol and Ahuja – Chapter 9 – SQL Pol and Ahuja – Chapter 10 – Visual Studio

SQL SQL – Structured Query Language Standard language for managing and manipulating Relational Databases and Relational DataBase Management Systems RDBMS? Standard – because it is widely used in RDBMSs  …but not in all

SQL SQL – used in MS Access (behind the “curtain”) MS SQL Server Oracle DB2 MySQL PostGres …

SQL SQL – standard, but… Some variations in SQL across platforms Usually syntactic differences Function differences  Subsets  Arguments

SQL SQL – Important Point! SQL is a database management language SQL is not a programming language What does this mean? Why does this matter? What can we do about this?

SQL SQL – four components to the language DDL – Data Definition Language  Define/delete db objects DML – Data Manipulation Language  Language for using db DCL – Data Control Language  Defines/sets control features of db DSPL – Data Stored Procedure Language  Tools for creating SQL modules

SQL SQL – four components to the language DCL and DSPL – outside the scope of this class (mostly) DDL and DML – we will take a closer look at these two languages

SQL SQL – four components to the language DCL and DSPL – outside the scope of this class (mostly) DDL and DML – we will take a closer look at these two languages

DDL DDL – Data Definition Language Create  TABLES  PRIMARY KEYS  FOREIGN KEYS  INDEXES

DDL CREATE tables CREATE TABLE [tblName] ({ }, {},…) CONSTRAINT { PRIMARY KEY (fieldname1, fieldname2,…) {}… ;

DDL CREATE tables CREATE TABLE tblDepartment (DeptIDVARCHAR2 (10) NOT NULL, NameVARCHAR2 (20), Address VARCHAR2 (200), PhoneNUMBER (30), CollegeIDVARCHAR2(3) ) CONSTRAINT deptPK PRIMARY KEY (DeptID), deptFK FOREIGN KEY (CollegeID) REFERENCES tblCollege (CollegeID) ;

DDL CREATE tables Standard SQL datatypes  VARCHAR(maxbytes) -- TEXT same in MySQL Max maxbytes – Oracle = 4000; SQL Server=8000; MySQL=65,532 (see docs for specific DB platform  DECIMAL(precision, scale) (optional) Numeric base10 to a specific precision and scale (number of fractional decimal positions) DECIMAL(9,2) could store 9,999,  DATE (some variation across DB platforms) DATE TIME TIMESTAMP

DDL CREATE tables Standard SQL datatypes  SMALLINT 2 bytes -32,768 to 32,767  INTEGER 4 bytes -2,147,483,648 to a 2,147,483,647  BIGINT 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

DDL CREATE Indexes CREATE INDEX tblDeptAddressIndex ON tblDepartment (Address) ;

DDL DDL – Data Definition Language DROP – deletes db objects DROP TABLE – deletes a table (and its data) from a db …not used to drop a field from a table or a record from a table

DDL DDL – Data Definition Language DROP TABLE [tblName];  DROP TABLE tblInstructors; DROP INDEX [tblNameindex]  DROP INDEX tblStudentIndex;

DDL DDL – Data Definition Language ALTER – modifies db objects Add a field to a table ALTER tblDepartment ADD COLUMN Chair Number (10) ADD CONSTRAINT deptFK2 FOREIGN KEY (Chair) REFERENCES tblFaculty(FacultyID) ;

DDL DDL – Data Definition Language ALTER – modifies db objects Delete a field from a table ALTER tblDepartment DROP Chair DROP CONSTRAINT DeptFK2 ;

DML DML – Data Manipulation Language Language to use DB Language to do queries and other DB functions Why do you need to know this?

DML Select Queries SELECT [fieldName, fieldName2,…] FROM [tblName]; SELECT Name, Phone FROM tblDept; SELECT * FROM tblStudent;

DML Select Queries - JOIN SELECT [fieldName, fieldName2,…] FROM [tblName1, tblName2, tblName3,…]; SELECT Name, , DeptName FROM tblFaculty tblDept; Simple Query with Join But what does this do?

DML Select Queries – JOIN SELECT Name, , DeptName FROM tblFaculty tblDept; Simple Query with Join But what does this do? Cartesian Product What’s that?

DML Select Queries – JOIN and the WHERE clause SELECT Name, , DeptName FROM tblFaculty tblDept WHERE tblFaculty.DeptID=tblDept.DeptID;

DML Select Queries – JOIN and the WHERE clause How about this – Name in tblFaculty, Name in tblDept SELECT Name, , Phone, Name FROM tblFaculty tblDept WHERE tblFaculty.DeptID=tblDept.DeptID; Two fields in query with same identifier? Oh my!

DML Select Queries – JOIN and the WHERE clause Use a qualifier – fully specify field name using its table name SELECT tblFaculty.Name, , Phone, tblDept.Name FROM tblFaculty tblDept WHERE tblFaculty.DeptID=tblDept.DeptID;

DML Select Queries – JOIN and the WHERE clause WHERE clause operators –  = >= !=  BETWEEN, LIKE, IN  AND, OR, NOT

DML Select Queries – JOIN and the WHERE clause SELECT Name, , Salary, StartDate, tblDept.Name FROM tblFaculty, tblDept WHERE tblFaculty.DeptID=tblDept.DeptID AND (Salary > 80000) AND (StartDate > #1/1/1995#);

DML Select Queries – JOIN and the WHERE clause BETWEEN – matching a range SELECT Name, , Salary, StartDate, tblDept.Name FROM tblFaculty, tblDept WHERE tblFaculty.DeptID=tblDept.DeptID AND (Salary BETWEEN AND 80000) AND (StartDate > #1/1/1995#);

DML Select Queries – JOIN and the WHERE clause BETWEEN – matching a range SELECT Name, , Salary, StartDate, tblDept.Name FROM tblFaculty, tblDept WHERE tblFaculty.DeptID=tblDept.DeptID AND (Salary BETWEEN AND 80000) AND (StartDate > #1/1/1995#);

DML Select Queries – JOIN and the WHERE clause LIKE – matching a string or part of one SELECT Name, , Rank FROM tblStudent, tblRegister, tblCourses WHERE (tblStudent.SID=tblRegister.StudentID) AND (tblRegister.CourseID=tblCourses.CourseID) AND (tblCourses.Cname LIKE “Cooking*” OR tblCourses.Cname LIKE “Food*” );

DML Select Queries – JOIN and the WHERE clause LIKE – matching a string or part of one LIKE match operators * - zero or more characters ? – zero or one character % - any number of characters ( %science% ) _ - any one character

DML Select Queries – JOIN and the WHERE clause Using aliases AS -- lets your rename objects  Within the query  Alias does not persist SELECT Name, , Rank FROM tblStudent AS tS, tblRegister AS tR, tblCourses AS tC WHERE (tS.SID=tR.StudentID) AND (tR.CourseID=tC.CourseID) AND (tC.Cname LIKE “Cooking*” OR tC.Cname LIKE “Food*” );

DML Select Queries – JOIN and the WHERE clause Using aliases AS -- lets your rename objects  Within the query  Alias does not persist SELECT CONCAT(FirstName,” “,LastName) AS FullName, , Rank FROM tblStudent;

DML Select Queries – JOIN and the WHERE clause Sometimes we only want one result per entity

DML Select Queries – JOIN and the WHERE clause Remember this? What would we get as a result?

DML Select Queries – JOIN and the WHERE clause Remember this? What would we get as a result? SELECT Name, , Rank FROM tblStudent AS tS, tblRegister AS tR, tblCourses AS tC WHERE (tS.SID IN SELECT DISTINCT StudentID FROM tR WHERE tR.CourseID IN SELECT CourseID FROM tC WHERE (tC.Cname LIKE “Cooking*” OR tC.Cname LIKE “Food*” );

DML Select Queries – JOIN and the WHERE clause The keywork DISTINCT causes only one record per criteria match to be returned SELECT Name, , Rank FROM tblStudent AS tS, tblRegister AS tR, tblCourses AS tC WHERE (tS.SID IN SELECT DISTINCT StudentID FROM tR WHERE tR.CourseID IN SELECT CourseID FROM tC WHERE (tC.Cname LIKE “Cooking*” OR tC.Cname LIKE “Food*” );

DML Select Queries – JOIN and the WHERE clause Did you notice something else new? The keyword IN SELECT Name, , Rank FROM tblStudent AS tS, tblRegister AS tR, tblCourses AS tC WHERE (tS.SID IN SELECT DISTINCT StudentID FROM tR WHERE tR.CourseID IN SELECT CourseID FROM tC WHERE (tC.Cname LIKE “Cooking*” OR tC.Cname LIKE “Food*” );

DML Select Queries – JOIN and the WHERE clause Did you notice something else new? The keyword IN  Allows you to nest queries in queries

DML Select Queries – Ordering the results Use the ORDER BY clause SELECT [fields] FROM [tables] WHERE [select_criteria] ORDER BY [field1 ;

DML Select Queries – Ordering the results Use the ORDER BY clause SELECT Name, , Phone, StartDate FROM tblFaculty ORDER BY StartDate ASC; SELECT Name, , Phone, StartDate FROM tblFaculty ORDER BY StartDate DESC, Name ASC;

DML Select Queries – Summarizing data – getting some stats The GROUP BY clause SELECT tblDept.Name, COUNT(tblStudent.SID) AS StudentCount), AVG(tblStudent.GPA) AS MeanGPA FROM tblStudent, tblDept WHERE tblStudent.DeptID = tblDept.DeptID AND (tblDept.College=“CEMR”) GROUP BY tblDept.Name;

DML Select Queries – Summarizing data – getting some stats The HAVING clause, like WHERE but for GROUP BY SELECT tblDept.Name, COUNT(tblStudent.SID) AS StudentCount), AVG(tblStudent.GPA) AS MeanGPA FROM tblStudent, tblDept WHERE tblStudent.DeptID = tblDept.DeptID AND (tblDept.College=“CEMR”) GROUP BY tblDept.Name HAVING COUNT(tblStudent.SID >25);

DML Action Queries INSERT Queries DELETE Queries UPDATE Queries

DML Action Queries INSERT Queries INSERT INTO [table] (field1, field2, …) Values(“value1”, value2,…);

DML Action Queries INSERT Queries INSERT INTO tblStudent (Name, Age, ) Values(“Bob Smith”, 21, Order is important Datatypes must match

DML Action Queries DELETE Queries Deletes records from a table DELETE FROM [table] WHERE [delete_criteria];

DML Action Queries DELETE Queries Deletes records from a table DELETE FROM tblStudent WHERE Rank=“Senior”;

DML Action Queries Update Queries UPDATE [table] SET [field=value, field=value, …] WHERE [update_criteria];

DML Action Queries Update Queries UPDATE tblFaculty SET Salary = Salary *1.01 WHERE StartDate < 1/1/1920;