STRUCTURED QUERY LANGUAGE Chandra S. Amaravadi.

Slides:



Advertisements
Similar presentations
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
Advertisements

SQL Rohit Khokher.
BACS 485—Database Management Advanced SQL Overview Advanced DDL, DML, and DCL Commands.
Basic SQL Introduction Presented by: Madhuri Bhogadi.
Virtual training week 4 structured query language (SQL)
Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved.
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to 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.
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.
o At the end of this lesson, you will be able to:  Describe the life-cycle development phases  Discuss the theoretical and physical aspects of a relational.
Database A collection of related data. Database Applications Banking: all transactions Airlines: reservations, schedules Universities: registration, grades.
RDB/1 An introduction to RDBMS Objectives –To learn about the history and future direction of the SQL standard –To get an overall appreciation of a modern.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
SQL (DDL & DML Commands)
ACTION QUERIES (SQL COMMANDS ) STRUCTURED QUERY LANGUAGE.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
STRUCTURED QUERY LANGUAGE Chandra S. Amaravadi 1.
SQL FUNDAMENTALS SQL ( Structured Query Language )
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.
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.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
D R. E.F.C ODD ’ S R ULES FOR RDBMS Dr. E.F.Codd is an IBM researcher who first developed the relational data model in 1970.Dr. Codd published a list.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
1 SQL SQL (Structured Query Language) : is a database language that is used to create, modify and update database design and data. Good Example of DBMS’s.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
9-1 © Prentice Hall, 2007 Topic 9: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
SQL. Originally developed by IBM Standardized in 80’s by ANSI and ISO Language to access relational database and English-like non-procedural Predominant.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
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.
 CONACT UC:  Magnific training   
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Fundamentals of DBMS Notes-1.
SQL Query Getting to the data ……..
The SQL Database Grammar
Introduction to SQL Karolina muszyńska
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Chapter 4 Basic SQL.
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
“Introduction To Database and SQL”
DATABASE MANAGEMENT SYSTEM
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Database solutions Database environment Marzena Nowakowska Faculty of Management and Computer Modelling Kielce University of Technology rooms: 3.21 C,
Database systems Lecture 3 – SQL + CRUD
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Index Note: A bolded number or letter refers to an entire lesson or appendix. A Adding Data Through a View ADD_MONTHS Function 03-22, 03-23,
Contents Preface I Introduction Lesson Objectives I-2
Database Design: Relational Model
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Structured Query Language Path from Unorganized to Organized….
Presentation transcript:

STRUCTURED QUERY LANGUAGE Chandra S. Amaravadi

IN THIS PRESENTATION Codd’s rules for relational systems Types of SQL DDL and DML Examples

CODD’S RULES AND TYPES OF SQL

CODD’S RULES FOR RDBMSs Codd has written a paper in which he outlined the rules for relational systems. These are as follows: Information representation Guaranteed access Dynamic on-line catalog Comprehensive data sub-language View updating Note: Codd was a research fellow at IBM in the ’70s

CODD’S RULES FOR RDBMSs.. High-level insert, update and delete Physical data independence Logical data independence Integrity independence Distribution independence Non-subversion

CODD’S RULES FOR RDBMSs Information representation -- all information should be represented as atomic values in tables. Guaranteed access -- given a row, column and table name we should be able to access values in the table. Online catalog -- the system catalog (data dictionary) should be online and accessible by the system. Comp. data lang. -- there should be a language for data definition and data manipulation. View updating -- the system must be able to automatically update views based on a base table.

CODD’S RULES FOR RDBMSs High level insertion, update.. -- insertion, deletion and update should operate on a table. Physical data independence -- should be able to change internal storage structures without affecting application programs. Logical data independence -- should be able to change logical scheama Integrity independence -- integrity controls must be independent of appln. prog. Distribution independence -- the users/appln. programs should not be affected by where the data is physically stored. Non subversion -- should not be able to bypass integrity rules by using the data sub language.

STRUCTURED QUERY LANGUAGE SQL: command language used to define/retrieve data from a database Originated from SEQEL (system R) based on relational calculus (uses select, project, join etc.) standardized in ’82,’95, ‘05 Embedded SQL a standard in ‘89 Two major types and three other types DDL, DML SQL/T, SQL/I, DCL Embedded SQL

STRUCTURED QUERY LANGUAGE There are five types of SQL as follows: DDL - Creating / modifying schema objects DML - Retrieving / inserting / updating information SQL/T - Transaction boundaries SQL/I - Integrity constraints DCL. - To authorize access to the database objects

DATA DEFINITION LANGUAGE

DATA DEFINITION LANGUAGE DDL is the language used to define/modify the database Schema. Create/Open Database (not discussed) Create Schema Employee; Create/Alter/Drop Table Create/Drop View Create/Drop Index

DATA DEFINITION LANGUAGE.. TYPICAL FORMAT Action DatabaseComponent ComponentName… Create Drop Table View Index Cust….. Emp….. ………… Alter Table Emp…..

DATA DEFINITION LANGUAGE.. Command used to create tables CREATE TABLE Table name (attribute attr. type, attribute attr.type..) [CONSTRAINT Constr name TYPE attr]; Command used to change attributes in tables ALTER TABLE Table name ADD attr. attr. type, attr. attr. type; Command used to delete table definitions DROP TABLE Table name ;

CREATE TABLE Creates a table (schema) Dept. d_no d_name d_mgr_ssn d_phone CREATE TABLE DEPT. ( d_no Integer, d_name VarChar(15), d_mgr_SSN Char(9), d_phone Char(12));

ALTER, DROP TABLE Alter table adds/drops attributes; Drop table drops the entire table. Dept. d_no d_name d_mgr_ssn d_phone no_of_emp ALTER TABLE DEPT. ADD no_of_emp Integer; ALTER TABLE DEPT. DROP no_of_emp; DROP TABLE Dept.;

CREATE VIEW CREATE VIEW Viewname AS SELECT... Command used to create VIEWS CREATE VIEW Viewname AS SELECT... A view is the way a user looks at the data. Views are subsets of the data in the database. A view could include data from more than one table. All application programs access data via views. Views provide logical data independence. Reports can be created from views (as well as from tables)

CREATE VIEW.. Creates a query; AS is an “alias” or name Emp. e_ssn e_name e_title Create a view of employees who are analysts CREATE VIEW Analysts AS SELECT e_name, e_title FROM Emp. WHERE e_title = “analyst”; DROP VIEW ANALYSTS;

CREATE/DROP INDEX CREATE INDEX TI_INDEX ON EMP(e_title); An index is an example of file organization used to facilitate retrieval CREATE INDEX TI_INDEX ON EMP(e_title); DROP INDEX TI_INDEX;

DATA MANIPULATION LANGUAGE

DATA MANIPULATION LANGUAGE DML is the language used to create/modify and delete data in the database. INSERT insert a record UPDATE change values DELETE delete records SELECT choose record

THE INSERT COMMAND.. Inserts a record into a table with name TABLENAME. FORMAT INSERT INTO TABLENAME VALUES (attr1 value, attr2 value….);

THE INSERT COMMAND.. Insert employee record EMP. e_ssn e_name e_title INSERT INTO EMP VALUES (‘978-98-9878’, ‘Sullivan’, ‘developer’); e_ssn e_name e_title 978-98-9878 Sullivan developer

THE UPDATE COMMAND Update employee title to ‘analyst’ EMP. e_ssn e_name e_title UPDATE EMP SET e_title = ‘analyst’ WHERE e_name = ‘Sullivan’; /* table name */ /* new values */ /* condition */ EMP. e_ssn e_name e_title 978-98-9878 Sullivan analyst

THE DELETE COMMAND Delete Employee record EMP. DELETE FROM EMP. e_ssn e_name e_title 978-98-9878 Sullivan analyst DELETE FROM EMP. WHERE e_name = ‘Sullivan’; EMP. e_ssn e_name e_title What if you were to issue, DELETE FROM EMP?

THE SELECT STATEMENT

THE SELECT STATEMENT SELECT <Attribute list> FROM <table list> [WHERE <Conditions> AND/OR <Conditions>] [GROUP BY <Attribute list>] [HAVING <Conditions>] [ORDER BY <Attribute list> DESC/ASC]; Group by is used to organize data into groups and provide summary information. Having is used for the group condition.

ADDITIONAL NOTES ON SELECT Notes on the select statement The Select part can include literals (“The number of..”) Some functions can be included in SELECT part or WHERE part More than one table and one condition can be specified Conditions are connected by logical operators -- and/or etc. When GROUP BY is used, the WHERE clause is not used. Instead the group condition is specified by HAVING. ORDER BY is optional and used if sorting is required.

DISCUSSION The output of a SELECT statement is: a) an attribute ? b) a single record ? c) table ?

FUNCTIONS IN SQL Operators to carry out different types of calculations Logical Arithmetic String Date IN WHICH PART OF THE QUERY ARE FUNCTIONS USED?

LOGICAL OPERATORS Logical operators are generally used to carry out comparison “=“, “>“, “<“ “>=“, “<=“ “<>“ OR “!=“ (NOT) BETWEEN X1 AND X2 (inclusive) LIKE “_” or “%” IN (NOT) NULL Dept. d_name d_phone Finance 845-9878 Sales 989-0087 Marketing 787-9934 Fin. records 884-5768 (Select….Where D_Name like “%fin%”) Finance Financial Records

LOGICAL OPERATORS.. To select employees between certain income range Select emp#, emp name From emp Where income between 70000 and 90000; To select customers in Macomb, Chicago or Bloomington Cust. Select cust.cust# From cust Where cust.zip in (61455, 60601….) cust# zip 1156 61455 1157 61706 1158 54555 1160 60601

BUILT-IN ARITHMETIC FUNCTIONS Arithmetic functions are used to carry out math operations ABS ROUND TRUNC COUNT SUM AVG MAX MIN Select count(emp_name)… Select max(emp_salary)…..

STRING FUNCTIONS String functions are used to carry out string manipulation LENGTH(string) SUBSTR(string, start, no of ch.) LOWER UPPER Select substr(prob_descr, 0, 10) ……… Prob_descr = “I am unable to log in…”

DATE FUNCTIONS Date functions are used to carry out date arithmetic ADD_MONTHS(1/1/17, 5) = 6/1/17 MONTHS_BETWEEN (sysdate, hiredate) NEXT_DAY(hiredate, ‘Friday’) TO_DATE(string, picture) TO_DATE(“12/12/17”, ‘DY th MM, YYYY’) = 12 th DEC, 2017 Next_day(12/12/17, ‘Friday’) = 12/16/17

SIMPLE RETRIEVALS Select employees who are analysts EMP. e_ssn e_name e_title SELECT e_name FROM EMP WHERE e_title = ‘analyst’;

SIMPLE RETRIEVALS.. EMPLOYEE e_ssn e_name e_title e_salary 456-34-8895 Smith developer $35,000 459-66-6785 Johnson analyst $27,000 467-89-8898 Weintraub $60,000 478-64-8005 Dickson manager $64,000 489-12-5575 Ferrel $47,000 492-93-4438 Rao $71,000 McDonald $85,000

SIMPLE RETRIEVALS.. Select employees whose name ends with ‘son’ EMP. e_ssn e_name e_title SELECT e_name FROM EMP WHERE e_name like ‘%son’

SIMPLE RETRIEVALS.. Select employees whose name does not begin with ‘a’ EMP. e_ssn e_name e_title SELECT e_name FROM EMP WHERE e_name not like ‘a%’

RETRIEVALS WITH AGGREGATES.. Count the number of developers EMP. e_ssn e_name e_title SELECT COUNT(e_name) FROM EMP WHERE e_title = ‘developer’

RETRIEVALS WITH AGGREGATES.. EMP. e_ssn e_name e_title e_salary SELECT “Average Salary=“, Avg(e_salary) From EMP Where e_title = ‘developer’ or e_title = ‘analyst’ ; What does this query do?

RETRIEVALS WITH EXPRESSIONS.. List employees and their witholdings (calculated as 8% of salary). EMP. e_ssn e_name e_title e_salary SELECT e_name, e_salary * 0.08 AS withholdings FROM EMP

RETRIEVALS WITH GROUP BY.. List all job titles and the number of emps in each EMP. e_ssn e_name e_title SELECT e_title, COUNT(e_name) FROM EMP GROUP BY e_title

GROUP BY.. EMPLOYEE e_ssn e_name e_title e_salary 456-34-8895 Smith developer $35,000 459-66-6785 Johnson analyst $27,000 467-89-8898 Weintraub $60,000 478-64-8005 Dickson manager $64,000 489-12-5575 Ferrel $47,000 492-93-4438 Rao $71,000 McDonald $85,000

RETRIEVALS WITH HAVING.. List the number of employees in each job category with salary > $50,000 EMP. e_ssn e_name e_title e_salary SELECT “Number of: “, e_title, “=“, COUNT(e_name) FROM EMP GROUP BY e_title HAVING e_salary > 50000

RETRIEVALS WITH HAVING.. EMP. e_ssn e_name e_title e_salary 456-34-8895 Smith developer $35,000 459-66-6785 Johnson analyst $27,000 467-89-8898 Weintraub $60,000 478-64-8005 Dickson manager $64,000 489-12-5575 Ferrel $47,000 492-93-4438 Rao $71,000 McDonald $85,000

RETRIEVAL FROM MULTIPLE TABLES Sharing of information a key concept Normalization process leads to multiple tables When data is retrieved from > 1 table, need to link tables together This is done by equating FK values with PK values for each set of tables that need to be linked Emp. emp#, emp name, dept# Dept. dept#, dept name, mgr

RETRIEVAL FROM MULTIPLE TABLES EMP. e_ssn e_name e_title Dept. d_name d_no d_mgr_ssn d_phone SELECT Emp.e_name, Dept.d_name FROM EMP, dept. WHERE EMP.e_ssn = dept.d_mgr_ssn

RETRIEVAL FROM MULTIPLE TABLES.. EMPLOYEE e_ssn e_name e_title e_salary 456-34-8895 Smith developer $35,000 459-66-6785 Johnson analyst $27,000 477-89-8898 Weintraub $60,000 478-64-8005 Dickson manager $64,000 489-12-5575 Ferrel $47,000 492-93-4438 Rao $71,000 467-89-8898 McDonald $85,000 DEPARTMENT d_name d_no d_mgr_ssn d_phone Manugistics 142 467-89-8898 845-9878 IMS 230 479-99-0045 989-0087 Pilot 345 478-64-8005 787-9934 InfoSec 467 898-98-0967 884-5768

DISCUSSION Write SQL queries for the following: 1. Create Emp table with E_SSN, E_Name, E_title as attr. -- assume data types. 2. Add E_salary to the Employee table. 3. Create an index, “Ti_ndx” on E_title. 4. Insert Bob White with SS# 556455555 as an analyst 5. List employees and job titles in order of title. 6. List employees other than developers. 7. Create a view “hi_flier” listing developers with salary > $100K. 8. Count the #. of employees who are managers.