SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Similar documents
user specifies what is wanted, not how to find it

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

SQL: Data Definition Language

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

The data structures of the relational model Attributes and domains Relation schemas and database schemas

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

Basic SQL. Basic SQL. Basic SQL

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

What is a Database? Peter Wood

Databases 1. Defining Tables, Constraints

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

MTAT Introduction to Databases

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Chapter 2 The relational Model of data. Relational model introduction

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

SQL Data Definition and Data Manipulation Languages (DDL and DML)

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Running Example Tables name location

Principles of Data Management

COMP 430 Intro. to Database Systems

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Chapter 9: Working with MySQL

CSCI3030U Database Models

SQL Introduction. CS 377: Database Systems

CS145 Introduction. About CS145 Relational Model, Schemas, SQL Semistructured Model, XML

Database Management Systems,

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

The Relational Model of Data (ii)

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

SQL Structured Query Language Introduction

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

SQL Data Definition Language

SQL functions fit into two broad categories: Data definition language Data manipulation language

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Full file at

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

2.9 Table Creation. CREATE TABLE TableName ( AttrName AttrType, AttrName AttrType,... )

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

CSIE30600 Database Systems Basic SQL 2. Outline

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

Chapter 3: Introduction to SQL

SQL. The Basics Advanced Manipulation Constraints Authorization 1. 1

Slides by: Ms. Shree Jaswal

QQ Group

Chapter 3: Introduction to SQL

Data Definition and Data Manipulation. Lecture 5: SQL s Data Definition Language CS1106/CS5021/CS6503 Introduction to Relational Databases

Lecture 5: SQL s Data Definition Language

Programming and Database Fundamentals for Data Scientists

The SQL data-definition language (DDL) allows defining :

In This Lecture. SQL Data Definition SQL SQL. Non-Procedural Programming. Notes. Database Systems Lecture 5 Natasha Alechina

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

Lab # 4. Data Definition Language (DDL)

Constraints. Local and Global Constraints Triggers

Creating Tables, Defining Constraints. Rose-Hulman Institute of Technology Curt Clifton

CS2300: File Structures and Introduction to Database Systems

INFORMATION TECHNOLOGY NOTES

Databases-1 Lecture-01. Introduction, Relational Algebra

Full file at

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

CS143: Relational Model

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

Introduction to Databases. MySQL Syntax Guide: Day 1

Part II Relational Databases Data as Tables

1) Introduction to SQL

Data Types in MySQL CSCU9Q5. MySQL. Data Types. Consequences of Data Types. Common Data Types. Storage size Character String Date and Time.

SQL Overview. CSCE 315, Fall 2017 Project 1, Part 3. Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

After completing this unit, you should be able to: Define the terms

Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH

Advanced Constraints SQL. by Joe Celko copyright 2007

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

Chapter 6 The database Language SQL as a tutorial

Introduction to SQL SELECT-FROM-WHERE STATEMENTS SUBQUERIES DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

Basis Data Terapan. Yoannita

Simple Quesries in SQL & Table Creation and Data Manipulation

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries

DATABASE PART 2. Components and Functions

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Database Systems SQL SL03

Basic SQL. Dr Paolo Guagliardo. University of Edinburgh. Fall 2016

Draft. Students Table. FName LName StudentID College Year. Justin Ennen Science Senior. Dan Bass Management Junior

Constraints and Triggers

Database Systems SQL SL03

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Relational Databases Fall 2017 Lecture 7

The M in LAMP: MySQL CSCI 470: Web Science Keith Vertanen Copyright 2014

CSCC43H: Introduction to Databases. Lecture 4

Transcription:

SQL SQL Functionality stands for Structured Query Language sometimes pronounced sequel a very-high-level (declarative) language user specifies what is wanted, not how to find it number of standards original ANSI SQL updated in 1992 to SQL-92 or SQL2 most recent is SQL:1999 (also called SQL3) also extensions to SQL:1999, called SQL:2003 DBMS vendors all support ANSI SQL and largely SQL-92 some of SQL:1999 and SQL:2003 with variations and own extensions data definition language data manipulation language integrity constraints view definition transaction control embedded and dynamic SQL authorisation Creating Relation Schemas Creating Relation Schemas Pubs table create table Pubs ( varchar(50) primary key, varchar(50) ); Sells table beer price Bad Habit 1.50 Rampant Ram 2.00 Shining Wit 2.75 Rampant Ram 2.50......... create table Sells ( varchar(50), beer varchar(50), price numeric(4,2), primary key (,beer) foreign key () references Pubs );

Some Types Some Integrity Constraints char(n) varchar(n) int smallint decimal(p, d) real boolean date time... character string of fixed length n character string of varying length up to n integer (usually 4 bytes) small integer (usually 2 bytes) fixed-point number comprising p digits, d of them to the right of the decimal point floating-point number true, false or unknown (in MySQL, true is non-zero, while false is zero) year, month and day values (e.g., YYYY-MM-DD) hour, minute and second values (e.g., HH:MM:SS) primary key(a 1,..., A n ): attributes A 1,..., A n form a primary key of the relation foreign key(a 1,..., A n ) references R: values for attributes A 1,..., A n must correspond to values of primary key attributes in R not null: null value not allowed for attribute unique: attributes values must be unique in relation (like a simple candidate key) check: check constraint on attribute value, e.g., check (price > 0)... Inserting Data in a Relation Changing a Relation Schema Sells: beer price Bad Habit 1.50 Rampant Ram 2.00 Shining Wit 2.75 Rampant Ram 2.50 Add an attribute to a relation: ALTER TABLE Sells ADD( measure varchar(10)); Change the data type of an attribute: INSERT INTO Sells values (, Shining Wit, 2.50); INSERT INTO Sells (, beer) values (, Bad Habit ); INSERT INTO Sells values (, Bad Habit, 1.75); error INSERT INTO Sells () values ( ); ALTER TABLE Sells MODIFY( measure varchar(15)); Remove an attribute from a relation: ALTER TABLE Sells DROP(measure); Delete a relation schema (relation must be empty): DROP TABLE Sells; error

Some Notes on SQL Syntax SQL: Select-From-Where Keywords (e.g., ADD, DROP) are not case sensitive Identifiers (used to tables, columns, etc.) are not case sensitive (although in MySQL table s are case sensitive) Tables are defined within a (default) schema; for a non-default schema, use schema:table Comments can be enclosed in /* and */ (for multi-line comments) or started with -- (for a comment ended by the end of line) Overall form of an SQL query is SELECT desired attributes FROM one or more tables WHERE conditions on rows of the tables are satisfied Running Example Pubs (, ) Drinkers (, ) Sells (, beer, price) Visits (drinker, ) each has a and each drinker has a and where they live s sell beers at various prices drinkers visit various s Running Example Tables Pubs: Sells: beer price Bad Habit 1.50 Rampant Ram 2.00 Shining Wit 2.75 Rampant Ram 2.50 Bad Habit 1.75 Rampant Ram 2.50 Bad Habit 2.50 Shining Wit 2.25 Rampant Ram 2.50 Rampant Ram 2.75

Running Example Tables Retrieving an entire table Drinkers: drinker Visits: Stratford SELECT * FROM Drinkers; Stratford Stratford Projection Projection Find where s are located: SELECT FROM Pubs; Find unique s of s: SELECT DISTINCT FROM Pubs; SQL does not implement set semantics

Projection Projection Find what beers are sold by which s: SELECT beer, FROM Sells; beer Bad Habit Rampant Ram Shining Wit Rampant Ram Bad Habit Rampant Ram Bad Habit Shining Wit Rampant Ram Rampant Ram Find the prices of beers in euros: SELECT FROM Ale beer AS Ale, price*1.12 AS PriceInEuros Sells; Bad Habit 1.68 Rampant Ram 2.24 Shining Wit 3.08 Rampant Ram 2.80 Bad Habit 1.96 Rampant Ram 2.80 Bad Habit 2.80 Shining Wit 2.52 Rampant Ram 2.80 Rampant Ram 3.08 PriceInEuros Selection Selection Who visits the : SELECT drinker FROM Visits WHERE = ; drinker drinker Which s sell some beer for less than 2.50?: (See Sells table) SELECT DISTINCT FROM Sells WHERE price < 2.50;

Selection Selection Condition Which s sell Bad Habit for less than 2.50?: SELECT FROM Sells WHERE beer= Bad Habit AND price < 2.50; A boolean expression in the WHERE clause may contain the following operators: AND: beer= Bad Habit AND price < 2.50 NOT: NOT beer= Bad Habit Equivalently: beer <> Bad Habit OR: beer= Bad Habit OR beer= Rampant Ram (See Sells table) or any combination: beer= Bad Habit OR beer= Rampant Ram AND price < 2.50 which is equivalent to beer= Bad Habit OR (beer= Rampant Ram AND price < 2.50)