Structured Query Language

Slides:



Advertisements
Similar presentations
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
Advertisements

Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Chapter 3 The Relational Model Transparencies © Pearson Education Limited 1995, 2005.
Introduction to Structured Query Language (SQL)
Chapter 3. 2 Chapter 3 - Objectives Terminology of relational model. Terminology of relational model. How tables are used to represent data. How tables.
1 Minggu 2, Pertemuan 3 The Relational Model Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
Chapter 6 SQL: Data Manipulation. 2 Objectives of SQL u Database language should allow user to: –create database and relation structures –perform insertion,
Introduction to Structured Query Language (SQL)
1 Pertemuan 04 MODEL RELASIONAL Matakuliah: >/ > Tahun: > Versi: >
1 Minggu 4, Pertemuan 7 SQL: Data Manipulation Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
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.
Chapter 3 The Relational Model Transparencies Last Updated: Pebruari 2011 By M. Arief
CSC271 Database Systems Lecture # 10.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Chapter 3 The Relational Model. 2 Chapter 3 - Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between.
Chapter 7 Introduction to SQL(Wk11_12) © Pearson Education Limited 1995, 2005.
Chapter 5: Part 1: DDL STRUCTURED QUERY LANGUAGE (SQL)
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Lecture2: Database Environment Prepared by L. Nouf Almujally 1 Ref. Chapter2 Lecture2.
SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Oracle & SQL Introduction. Database Concepts Revision DB? DBMS? DB Application? Application Programs? DBS? Examples of DBS? Examples of DBMS? 2Oracle.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Chapter 7 SQL HUANG XUEHUA. AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 15: Find the maximum salary, the minimum salary, and the.
Slide 8- 1 THE HAVING-CLAUSE Provides a condition on the summary information Sometimes we want to retrieve the values of these functions for only those.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
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.
The Relational Model. 2 Relational Model Terminology u A relation is a table with columns and rows. –Only applies to logical structure of the database,
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
FEN Introduction to the database field:  SQL: Structured Query Language Seminar: Introduction to relational databases.
The Relational Model © Pearson Education Limited 1995, 2005 Bayu Adhi Tama, M.T.I.
SQL Introduction to database and SQL. Chapter 1: Databases and Database Users 6 Introduction to Databases Databases touch all aspects of our lives. Examples:
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)
Chapter 4 The Relational Model Pearson Education © 2009.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
SQL- Updates, Assertions and Views. Data Definition, Constraints, and Schema Changes Used to CREATE, DROP, and ALTER the descriptions of the tables (relations)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Lecture 11 SQL. Agenda SQL Select statement WHERE clause BindingSource filtering.
SQL Introduction SQL stands for “Structured Query Language” and can be pronounced as “SQL” or “sequel – (Structured English.
Chapter 10 SQL DDL.
REV 00 Chapter 4 SQL and QBE DDC 2483 – Database Systems.
SQL: SchemaDefinition, Constraints, and Queries and Views
Oracle & SQL Introduction
CS580 Advanced Database Topics
STRUCTURED QUERY LANGUAGE
Chapter 4 The Relational Model Pearson Education © 2009.
Chapter 4 The Relational Model Pearson Education © 2009.
CS4222 Principles of Database System
Chapter 4 The Relational Model Pearson Education © 2009.
The Relational Model Transparencies
Data Model.
Chapter 4 The Relational Model Pearson Education © 2009.
SQL-1 Week 8-9.
Chapter 4 The Relational Model Pearson Education © 2009.
Structured Query Language (3)
Chapter Name SQL: Data Manipulation
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
Chapter 4 The Relational Model Pearson Education © 2009.
SQL Updating Database Contents Presented by: Dr. Samir Tartir
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Views Base Relation View
Chapter Name SQL: Data Manipulation
Presentation transcript:

Structured Query Language Chapter 8 Structured Query Language SQL

Chapter Outline INTRODUCTION SQL COMMANDS VIEWS Objective of SQL. History of SQL. Importance of SQL. SQL COMMANDS Tables Creation and Deletion Retrieval Operations. Update Operations VIEWS

INTRODUCTION Objectives of SQL Ideally, database language should allow user to: create the database and relation structures; perform insertion, modification, deletion of data from relations; perform simple and complex queries. Must perform these tasks with minimal user effort and command structure and syntax must be easy to learn. It must be portable. SQL is a transform-oriented language with 2 major components: A DDL for defining the database structure. A DML for retrieving and updating data. SQL does not contain flow control commands. These must be implemented using a programming or job-control language, or interactively by the decisions of the user.

INTRODUCTION Objectives of SQL SQL is relatively easy to learn: It is a non-procedural language - you specify what information you require, rather than how to get it. It is essentially free-format. Consists of standard English words: CREATE TABLE staff(sno VARCHAR(5), lname VARCHAR(15), salary DECIMAL(7,2)); INSERT INTO staff VALUES ('SG16', 'Brown', 8300); SELECT sno, lname, salary FROM staff WHERE salary > 10000; Can be used by a range of users including DBAs, management, application Progr. and other types of end users. An ISO standard now exists for SQL, making it both the formal and de facto standard language for relational databases.

INTRODUCTION History of SQL In 1974, D. Chamberlin (IBM San Jose Laboratory) defined language called 'Structured English Query Language' or SEQUEL. A revised version SEQUEL/2 was defined in 1976 but name was subsequently changed to SQL for legal reasons. Still pronounced 'see-quel', though official pronunciation is 's-q-l'. IBM subsequently produced a prototype DBMS called System R, based on SEQUEL/2. Roots of SQL, however, are in SQUARE (Specifying Queries as Relational Expressions), which predates System R project. In late 70s, ORACLE appeared and was probably first commercial RDBMS based on SQL. In 1987, ANSI and ISO published an initial standard for SQL. In 1989, ISO published an addendum that defined an 'Integrity Enhancement Feature'. In 1992, first major revision to ISO standard occurred, referred to as SQL2 or SQL/92.

INTRODUCTION Importance of SQL SQL has become part of application architectures such as IBM's Systems Application Architecture (SAA). It is strategic choice of many large and influential organizations (e.g. X/OPEN). SQL is Federal Information Processing Standard (FIPS) to which conformance is required for all sales of databases to American Government. SQL Access Group trying to define enhancements that will support interoperability across disparate systems. SQL is used in other standards and even influences development of other standards as a definitional tool. Examples include: ISO's Information Resource Directory System (IRDS) Standard Remote Data Access (RDA) Standard.

SQL consists of the following parts: SQL COMMANDS SQL consists of the following parts: Data Definition Language (DDL) Interactive Data Manipulation Language (Interactive DML) Embedded Data Manipulation Language (Embedded DML) Views Integrity Transaction Control Authorization Catalog and Dictionary Facilities

Writing SQL Commands SQL statement consists of reserved words and user-defined words. Reserved words are a fixed part of SQL and must be spelt exactly as required and cannot be split across lines. User-defined words are made up by user and represent names of various database objects such as relations, columns, views. Most components of an SQL statement are case insensitive, except for literal character data. More readable with indentation and lineation: Each clause should begin on a new line. Start of a clause should line up with start of other clauses. If clause has several parts, should each appear on a separate line and be indented under start of clause. Use extended form of BNF notation: Upper case letters represent reserved words. Lower case letters represent user-defined words. | indicates a choice among alternatives. Curly braces indicate a required element. Square brackets indicate an optional element. … indicates optional repetition (0 or more).

Table Creation & Deletion

Table Creation & Deletion

Table Creation & Deletion Deleting Tables from the Database DROP TABLE Table-name CASCADE/[RESTRICT] DROP TABLE DEPENDENT CASCADE Modifying Table definition in the Database ALTER TABLE Table-name ADD Col-name Type ALTER TABLE Table-name DROP Col-name ALTER TABLE Table-name DROP CONSTRAINT constraint-name ALTER TABLE COMPANY.EMPLOYEE ADD JOB VARCHAR(12) ALTER TABLE COMPANY.EMPLOYEE DROP ADDRESS CASCADE ALTER TABLE COMPANY.DEPARTMENT ALTER MGRSSN VARCHAR(12) ALTER TABLE COMPANY.EMPLOYEE DROP CONSTRAINT EMPSUPERFK CASCADE

Update Operations DELETE Statement UPDATE Statement INSERT Statement INSERT INTO [table_name | table_name(Attribute List)] VLAUES (Attribute List Values) INSERT INTO EMPLOYEE VLAUES (‘Richard’, ‘K’, ‘Marini’, ‘653298653’, ‘98 Oak Forest,Katy, TX’, ‘M’, 37000, ‘987654321’, 4) INSERT INTO EMPLOYEE (FNAME, LNAME,SSN, DNO) VLAUES (‘Richard’, ‘Marini’, 4, ‘653298653’) DELETE Statement DELETE FROM table_name WHERE (Condition) DELETE FROM EMPLOYEE WHERE SSN = ‘123456789’ UPDATE Statement UPDATE table_name SET (Attribute = value, [, ...]) UPDATE PROJECT SET PLOCATION =‘Bellaire’, DNUM = 5 WHERE PNUMBER = 10

Retrieval Operations Examples: SELECT [DISTINCT | ALL] SELECT Statement SELECT [DISTINCT | ALL] {* | [column_expression [AS new_name]] [,...] } FROM table_name [alias] [, ...] [WHERE condition] [GROUP BY column_list] [HAVING condition] [ORDER BY column_list] FROM Specifies table(s) to be used. WHERE Filters rows. GROUP BY Forms groups of rows with same column value. HAVING Filters groups subject to some condition. SELECT Specifies which columns are to appear in the output. ORDER BY Specifies the order of the output. Order of the clauses cannot be changed. Only SELECT and FROM are mandatory. Examples: Queries 1 to 28 in the text book , pp.252-274

Views Base Relation View Purpose of Views A named relation, corresponding to an entity in conceptual schema, whose tuples are physically stored in database. View Dynamic result of one or more relational operations operating on the base relations to produce another relation. A view is a virtual relation that does not actually exist in the database but is produced upon request, at time of request. Contents of a view are defined as a query on one or more base relations. Views are dynamic, meaning that changes made to base relations that affect view attributes are immediately reflected in the view. Purpose of Views Provides a powerful and flexible security mechanism by hiding parts of database from certain users. Permits users to access data in a customized way, so that same data can be seen by different users in different ways, at same time. It can simplify complex operations on base relations.

Views Updating Views All updates to a base relation should be immediately reflected in all views that reference that base relation. If view is updated, underlying base relation should reflect change. However, there are restrictions on types of modifications that can be made through views: Updates are allowed if query involves a single base relation and contains a candidate key of base relation. Updates are not allowed involving multiple base relations. Updates are not allowed involving aggregation or grouping operations. Classes of views are defined as theoretically not updateable, theoretically updateable and partially updateable.

View Operations Creating VIEWS in the Database CREATE VIEW View-name {Attribute List} AS SELECT Expression CREATE VIEW WORKS_ON1 AS SELECT FNAME, LNAME, PNAME, HOURS FROM EMPLOYEE, PROJECT, WORKS_ON WHERE SSN = ESSN AND PNO = PNUMBER CREATE VIEW DEPT_INFO(DEPT_NAME, NO_OF_EMPS, TOTAL_SAL) AS SELECT DNAME, COUNT (*), SUM (SALARY) FROM DEPARTMENT, EMPLOYEE, WHERE DNUMBER = DNO GROUP BY DNAME