S511 Session 10, IU-SLIS 1 Structured Query Language.

Slides:



Advertisements
Similar presentations
SQL Rohit Khokher.
Advertisements

Virtual training week 4 structured query language (SQL)
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)
5 Chapter 5 Structured Query Language (SQL2) Revision.
Introduction to Structured Query Language (SQL)
Structured Query Language (SQL)
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi Information Systems Spring 2011.
Introduction to SQL J.-S. Chou Assistant Professor.
ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
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.
6 1 Chapter 6 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Relational Databases Database Driven Applications Retrieving Data Changing Data Analysing Data What is a DBMS An application that holds the data manages.
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.
Advanced SQL Advanced SQL Complex Queries, Joining Tables.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
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.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
3 3 Chapter 3 Structured Query Language (SQL) Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL – Structured Query Language
SQL Structured Query Language. Aims  To introduce the implementation of a Physical design using SQL.  To introduce SQL Data Definition Language (DDL).
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.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
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
SQL.. AN OVERVIEW lecture3 1. Overview of SQL 2  Query: allow questions to be asked of the data and display only the information required. It can include.
Week 4 Lecture Part 2 of 3 Structured Query Language (SQL) Samuel ConnSamuel Conn, Faculty Suggestions for using the Lecture Slides.
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 6 CGI/Perl and databases.
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.
Chapter 3: Relational Databases
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.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
SQL Basics Review Reviewing what we’ve learned so far…….
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Query Getting to the data ……..
Chapter 5 Introduction to SQL.
Chapter 8 Working with Databases and MySQL
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Database systems Lecture 3 – SQL + CRUD
Chapter 7 Introduction to Structured Query Language (SQL)
SQL .. An overview lecture3.
Contents Preface I Introduction Lesson Objectives I-2
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Presentation transcript:

S511 Session 10, IU-SLIS 1 Structured Query Language

S511 Session 10, IU-SLIS 2 S tructured Q uery L anguage : Intro What is SQL? ► Relational model’s standard language all RDBMS software support SQL many software vendors have developed extensions to basic SQL  there are small differences across RDBMS implementations of SQL ► Non-procedural language user specifies what must be done, not how it is to be done  SQL programmers do not need to know the step-by-step task execution details. What can SQL do? ► Create database & table structures ► Perform data manipulation & data administration add, modify, delete, retrieve data create tables, indexes, views ► Query the database to extract useful information Why SQL? ► de facto standard for RDB ANSI SQL (1986, 1992, 1999) ► Relatively easy to learn (vocabulary of less than 100 words)

S511 Session 10, IU-SLIS 3 The SQL Environment ODBC (Open Database Connectivity) ► A standard database access method API for using SQL queries to access data ► Enables data access from any application regardless of DBMS independent of programming languages, DBMS, and OS both application and DBMS have to be ODBC-compliant ► Translates applications data queries into DBMS commands a middle layer (database driver) between application and DBMS ApplicationDBMS Database SQL queries ODBC driver DBMS commands

S511 Session 10, IU-SLIS 4 SQL: Command Type Data Definition Language (DDL) commands ► Create, alter and drop tables ► Typically restricted to DB administrators Data Manipulation Language (DML) commands ► Update, insert, modify, and query the data ► Core commands of SQL Data Control Language (DCL) commands ► Grant/revoke access privileges, store/remove transactions ► Help the DB administrators to control the database

S511 Session 10, IU-SLIS 5 SQL: Creating/Deleting Tables ► CREATE TABLE table_name ( column_name column_characteristics, …, CONSTRAINT constraint_name PRIMARY KEY (column_name), FOREIGN KEY (column_name) REFERENCES table_name); define a new table and its columns “1” side table in 1:M relationship (i.e. EMPLOYEE) should be created first data types vary from RDBMS to RDBMS CREATE TABLE tblPilot (Emp_Num INTEGER NOT NULL, Pil_Hrs NUMBER, Pil_License CHAR(25), Pil_Date DATE, CONSTRAINT index1 PRIMARY KEY(Emp_Num), FOREIGN KEY(Emp_Num) REFERENCES tblEmployee); ► DROP TABLE table_name; DROP TABLE tblPilot;

S511 Session 10, IU-SLIS 6 SQL: Creating Indexes ► CREATE INDEX index_name ON table_name(column_names) Create SQL indexes based on selected columns  CREATE INDEX P_CODEX ON PRODUCT(P_CODE);  CREATE UNIQUE INDEX P_CODEX ON PRODUCT(P_CODE);  CREATE INDEX VENPRODX ON PRODUCT(V_CODE, P_CODE); ► Indexes Improve the efficiency of data search Create indexes on fields used in search DBMS automatically creates a unique index for primary key

S511 Session 10, IU-SLIS 7 SQL: Modifying Table Data ► INSERT INTO table_name VALUES (col1_value, col2_value, …); Insert a single row into table  INSERT INTO tblEmployee VALUES (1, ‘Doe’, ‘John’, ‘M’); INSERT INTO tblEmployee VALUES (2, ‘Dew’, ‘Jane’, NULL); INSERT INTO tblEmployee(Emp_Num, Emp_Fname) VALUES (3, ‘Jim’); ► INSERT INTO table_name SELECT column_names FROM table_name WHERE condition; Insert multiple rows into table INSERT INTO tblEmployee (Emp_Num, Emp_Fname, Emp_Lname, Emp_Init) SELECT App_Hire, App_Fname, App_Lname, App_Init FROM tblApplicant WHERE App_Hire=4; ► UPDATE table_names SET column_name=value WHERE condition; Change column values  UPDATE tblEMPLOYEE SET Emp_Lname=‘Dew’, Emp_Status=‘M’ WHERE Emp_Num=3; ► DELETE FROM table_name WHERE condition; Delete table rows  DELETE FROM tblEmployee WHERE Emp_Num=3;

S511 Session 10, IU-SLIS 8 SQL: Altering Tables ► ALTER TABLE table_name ADD column_name column_characteristics; Add column to a table  ALTER TABLE tblProduct ADD P_Salescode CHAR(1); ► ALTER TABLE table_name ADD CONSTRAINT constraint_name FOREIGN KEY(column_name) REFERENCES table_name; Designate a foreign key to a table  ALTER TABLE tblProduct ADD CONSTRAINT pk1 FOREIGN KEY(SUP_Num) REFERENCES tblSupplier; ► ALTER TABLE table_name ALTER column_name column_characteristics; Change column characteristics  ALTER TABLE tblProduct ALTER P_Salescode CHAR(10); ► ALTER TABLE table_name DROP column_name; Drop column from a table  ALTER TABLE tblProduct DROP P_Salescode;

S511 Session 10, IU-SLIS 9 SQL: Copying Table Data 1. Create the new table whose column characteristics match the original table 2. Copy desired table columns and rows into the new table 3. Designate primary and foreign keys if needed CREATE TABLE tbRetiree ( Emp_Num INTEGER NOT NULL, Retire_Date DATE, CONSTRAINT pk1 PRIMARY KEY(Emp_Num)); INSERT INTO tblRetiree(Emp_Num, Retire_Date) SELECT (Emp_Num, Hire_Date+3650) FROM tblEmployee WHERE (Hire_Date ) – Date() < 30; ALTER TABLE tblRetiree ADD CONSTRAINT fk1 FOREIGN KEY (Emp_Num) REFERENCES tblEmployee;

S511 Session 10, IU-SLIS 10 SQL: Queries ► SELECT column_names FROM table_name WHERE condition; Retrieve table rows that match the target criteria Examples SELECT Emp_Num, Emp_Lname FROM tblEmployee; SELECT * FROM tblEmployee WHERE Emp_Num=1; SELECT P_Onhand*P_Price AS TotalValue FROM tblProduct WHERE P_Code=1;

S511 Session 10, IU-SLIS 11 SQL: Where Clause Mathematical Operators ► + - * / ^ Comparison Operators ► = >= <> (!=) Logical Operators ► AND, OR, NOT Special Operators ► BETWEEN, IS NULL, LIKE, IN, EXISTS Wild Cards ► % (*) _ (?) Precedence & Case Sensitivity ► differs across DBMS/platform WHERE P_ID NOT 1234; WHERE P_Price BETWEEN AND ; WHERE Emp_Status IS NULL; WHERE (Emp_Lname LIKE ‘Smith%’) AND (Emp_Status IN (‘M’,‘D’)); WHERE Emp_Status EXISTS;

S511 Session 10, IU-SLIS 12 SQL: Aggregate Functions ► COUNT Number of rows containing not null values in the given column  SELECT DISTINCT (V_code) FROM tblProduct WHERE P_Price < 10.00; ► MIN/MAX Minimum/maximum value in the given column  SELECT * FROM tblProduct WHERE P_Price = (SELECT MAX(P_Price) FROM tblProduct); ► SUM Sum of all values for the given column  SELECT SUM(P_Onhand*P_Price) FROM tblProduct; ► AVG Mean value of the given column  SELECT * FROM tblProduct WHERE P_Price > (SELECT AVG(P_Price) FROM tblProduct);

S511 Session 10, IU-SLIS 13 SQL: Sorting & Grouping ► ORDER BY column_names DESC Sort the list (result of SELECT) by column values Must be the last clause in the command sequence  SELECT Emp_Lname, Emp_Fname, Emp_Init FROM tblEmployee ORDER BY Emp_Lname, Emp_Fname, Emp_Init; ► GROUP BY column_names HAVING condition; Aggregate column values by group column values Must be used with aggregate function HAVING condition is applied to the output of GROUP BY  SELECT SUM(P_Onhand*P_Price) FROM tblProduct GROUP BY V_Code HAVING AVG(P_Price) > 1; ► SELECT DISTINCT column_name FROM table_name; List only the unique column values  SELECT DISTINCT V_code FROM tblProduct;

S511 Session 10, IU-SLIS 14 SQL: Joining Tables Natural Join ► SELECT column_names FROM table1_name INNER JOIN table2_name ON table1.column = table2.column; SELECT tblProduct.P_Code, tblProduct.P_Price, tblVendor.V_Name FROM tblProduct INNER JOIN tblVendor ON tblProduct.V_Code = tblVendor.V_Code; Left Outer Join ► SELECT column_names FROM table1_name LEFT JOIN table2_name ON table1.column = table2.column; Right Outer Join ► SELECT column_names FROM table1_name RIGHT JOIN table2_name ON table1.column = table2.column;