SQL: Structured Query Language

Slides:



Advertisements
Similar presentations
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
1 Lecture 11: Basic SQL, Integrity constraints
The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)
SQL Sangeeta Devadiga CS157A, Fall Outline Background Data Definition Basic Structure Set Operation.
Dec 4, 2003Murali Mani SQL B term 2004: lecture 14.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
Structured query language This is a presentation by JOSEPH ESTRada on the beauty of Structured Query Language.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
CS3431 SQL : Query Language. CS3431 SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”)
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
Murali Mani SQL. Murali Mani SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”) (Student)
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.
Database Systems Lecture 5 Natasha Alechina
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
The Relational Model These slides are based on the slides of your text book.
Relational Algebra Instructor: Mohamed Eltabakh 1.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
CS 370 Database Systems Lecture 12 Introduction to SQL.
1 The Relational Model Instructor: Mohamed Eltabakh
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
CS3431-B111 The Relational Model Instructor: Mohamed Eltabakh
Advanced SQL: Triggers & Assertions
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
CMPT 258 Database Systems The Relationship Model (Chapter 3)
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1 Part II.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
Copyright © Curt Hill SQL The Data Manipulation Language.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1.
Fundamentals of DBMS Notes-1.
COP Introduction to Database Structures
SQL: Schema Definition and Constraints Chapter 6 week 6
CS 405G: Introduction to Database Systems
SQL Views CS542.
SQL LANGUAGE and Relational Data Model TUTORIAL
Instructor: Mohamed Eltabakh
The Relational Model Content based on Chapter 3
The Relational Model Relational Data Model
CS 405G: Introduction to Database Systems
SQL : Query Language CS3431.
SQL OVERVIEW DEFINING A SCHEMA
SQL Views and Updates cs3431.
Advanced SQL: Views & Triggers
Instructor: Mohamed Eltabakh
SQL: The Query Language Part 1
Instructor: Mohamed Eltabakh
Instructor: Mohamed Eltabakh
SQL: Structured Query Language
CMPT 354: Database System I
CMPT 354: Database System I
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
SQL: Structured Query Language
Instructor: Mohamed Eltabakh
SQL: Structured Query Language
Structured Query Language Path from Unorganized to Organized….
The Relational Model Content based on Chapter 3
The Relational Model Content based on Chapter 3
SQL (Structured Query Language)
SQL: Structured Query Language
Presentation transcript:

SQL: Structured Query Language Instructor: Mohamed Eltabakh meltabakh@cs.wpi.edu

SQL Language Data Definition Language (DDL) Create tables, specifying the columns and their types of columns, the primary keys, etc. Drop tables, add/drop columns, add/drop constraints – primary key, unique, etc. Data Manipulation Language (DML) Update, Insert, Delete tuples Query the data These are already covered Our focus today

Reminder About DDL Create “Students” relation Create “Courses” relation CREATE TABLE Students (sid: CHAR(20) Primary Key, name: CHAR(20) Not NULL, login: CHAR(10), age: INTEGER, gpa: REAL); CREATE TABLE Courses (cid: Varchar(20) Primary Key, name: string, maxCredits : integer, graduateFlag: boolean); Create “Enrolled” relation CREATE TABLE Enrolled (sid: CHAR(20) Foreign Key References (Students.sid), cid: Varchar(20), enrollDate: date, grade: CHAR(2), Constraints fk_cid Foreign Key cid References (Courses.cid)); Alter Table Enrolled Add Constraints fk_cid Foreign Key cid References Courses(cid));

Reminder About: Insert, Update, Delete This is performed using Data Manipulation Language of SQL (DML) Insertion Insert into Students values (‘1111’, …); Deletion Delete from Students; Delete from Students Where sid = ‘1111’; Update Update Students Set GPA = GPA + 0.4; Update Students Set GPA = GPA + 0.4 Where sid = ‘1111’;

SQL Query Language SELECT Statement

Relation between Algebra and SQL-Select Will be mapped to the algebraic operators that we learned SELECT <projection list> FROM <relation names> WHERE <conditions> GROUP BY <grouping columns> HAVING <grouping conditions> ORDER BY <order columns>;

SELECT-FROM-WHERE π SELECT <list of columns> σ relation name SELECT <list of columns> FROM <relation name> WHERE <conditions>;

SELECT-FROM-WHERE SELECT * FROM Student * Means “project all attributes” SELECT * FROM Student WHERE sName= ‘Greg’ AND address=‘320FL’; Must terminate with ; to execute Student sNumber sName address professor 1 Dave 311FL MM 2 Greg 320FL 3 Matt ER sNumber sName address professor 2 Greg 320FL MM  (sName=‘Greg’ AND address=‘320FL’) (Student)

SELECT-FROM-WHERE SELECT sNumber FROM Student WHERE sName=‘Greg’ AND address=‘320FL’; Student sNumber sName address professor 1 Dave 311FL MM 2 Greg 320FL 3 Matt ER sNumber sName address professor 2 Greg 320FL MM πsNumber((sName=‘Greg’ AND address=‘320FL’) (Student))

Select-From Query Only SELECT and FROM clauses are mandatory The WHERE clause is optional If not exist, then all records will be returned (there are no selection predicates) SELECT <list of columns> FROM <relation name>;

Select-From Query SELECT sNumber, sName FROM Student; address professor 1 Dave 320FL MM 2 Greg 3 Matt ER sNumber sName 1 Dave 2 Greg 3 Matt  (sNumber, sName) (Student)

Extended Projection SELECT <list of columns or expressions> The select clause can have expressions and constants SELECT <list of columns or expressions> FROM <relation name> WHERE <conditions>; Can also rename the fields or expressions using “AS” keyword

Extended Projection SELECT ‘Name:’ || sName AS info, 0 AS gpa FROM Student WHERE address=‘320FL’; Student sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER info gpa Name:Dave Name:Greg Name:Matt  (info  ‘Name:’||sName, gpa  0 ) ( (address=‘320FL’) (Student))

Mapping between SQL and Relational Algebra  L ( C (R)) SELECT L FROM R WHERE C

Renaming Relations and Tuple Variables SELECT S1.sNumber AS num FROM Student S1 WHERE S1.sNumber >= 1; Tuple variable Student sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER num 1 2 3  (num  S1.sNumber) ( (S1.sNumber >= 1) (S1(Student)))

Where Clause The comparison operator depends on the data type For Numbers: <, >, <=, >=, =, <> What about Strings?? SELECT S1.sNumber AS num FROM Student S1 WHERE S1.sNumber >= 1;

String Operators Comparison Operators based on lexicographic ordering: =, <, >, <>, >=, <= Concatenation operator: || Pattern match: s LIKE p p denotes a pattern Can use wild characters in p such as _, % _  matches exactly any single character %  matches zero or more characters SELECT ‘Name:’ || sName FROM Student WHERE address=‘320FL’;

String Matching Example SELECT s1.sNumber AS num FROM Student S1 WHERE s1.sName LIKE ‘Da%’ Or S1.professor LIKE ‘M_’ ; sNumber sName address professor 1 Dave 320FL MM 2 Greg 3 Matt ER sNumber 1 2

Set Operators in SQL Set Semantics Bag Semantics Union, Intersect, Except Bag Semantics Union All, Intersect All, Except All The two relations R and S must have the same number of columns and data types (Union Compatible) Oracle allows columns to have different names

Example Operators : UNION, INTERSECT, and EXCEPT (SELECT sName FROM Undergrad_Student) UNION (SELECT sName FROM Grad_Student) If two students have the same name, it will keep only one (SELECT sName FROM Undergrad_Student) UNION All (SELECT sName FROM Grad_Student) Will keep all names even if identical

Set Operations in SQL: Example

Example Queries SELECT * FROM loan WHERE amount > 1200 ; SELECT L.loan_number FROM loan L WHERE L.amount > 1200 ;

Example Queries SELECT customer_name FROM depositor Union FROM borrower;