Database Systems CSE 303. Lecture 02

Similar documents
Database Systems CSE 303. Lecture 02

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

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

Lecture 2: Introduction to SQL

COMP 430 Intro. to Database Systems

Big Data Processing Technologies. Chentao Wu Associate Professor Dept. of Computer Science and Engineering

INF 212 ANALYSIS OF PROG. LANGS SQL AND SPREADSHEETS. Instructors: Crista Lopes Copyright Instructors.

Lecture 3: SQL Part II

DS Introduction to SQL Part 2 Multi-table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

SQL. Assit.Prof Dr. Anantakul Intarapadung

MTAT Introduction to Databases

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Fall 2006 Lecture 3 - Relational Model

Introduction to Database Systems CSE 444

Principles of Database Systems CSE 544. Lecture #2 SQL The Complete Story

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

Polls on Piazza. Open for 2 days Outline today: Next time: "witnesses" (traditionally students find this topic the most difficult)

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

SQL. Structured Query Language

CSE 344 JANUARY 5 TH INTRO TO THE RELATIONAL DATABASE

CS143: Relational Model

COMP 430 Intro. to Database Systems

Introduction to Database Systems CSE 414. Lecture 3: SQL Basics

Introduction to Databases CSE 414. Lecture 2: Data Models

CMPT 354: Database System I. Lecture 3. SQL Basics

Lectures 2&3: Introduction to SQL

Data Informatics. Seon Ho Kim, Ph.D.

Lab # 4. Data Definition Language (DDL)

The Relational Data Model

Introduction to Data Management CSE 344. Lecture 2: Data Models

SQL DATA DEFINITION LANGUAGE

Lecture 04: SQL. Monday, April 2, 2007

Introduction to Database Systems CSE 414. Lecture 3: SQL Basics

CSE 344 JANUARY 8 TH SQLITE AND JOINS

CSE 544 Principles of Database Management Systems

Agenda. Discussion. Database/Relation/Tuple. Schema. Instance. CSE 444: Database Internals. Review Relational Model

Lecture 04: SQL. Wednesday, October 4, 2006

SQL Simple Queries. Chapter 3.1 V3.01. Napier University

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

Full file at

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

L07: SQL: Advanced & Practice. CS3200 Database design (sp18 s2) 1/11/2018

Programming and Database Fundamentals for Data Scientists

CSE 544 Principles of Database Management Systems

Lecture 3 Additional Slides. CSE 344, Winter 2014 Sudeepa Roy

SQL DATA DEFINITION LANGUAGE

Introduction to Data Management CSE 414

Introduction to Relational Databases

SQL Structured Query Language Introduction

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

Introduction to Database Systems. The Relational Data Model

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Introduction to Database Systems. The Relational Data Model. Werner Nutt

CS 564 Midterm Review

Relational Database Languages

Relational Algebra. Spring 2012 Instructor: Hassan Khosravi

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

Principles of Database Systems CSE 544. Lecture #1 Introduction and SQL

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

UNIT III INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL)

CSC 453 Database Technologies. Tanu Malik DePaul University

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

Lecturers & Practical Work. Schedule. Contact. Course site(s): CS 252: Fundamentals of Relational Databases: SQL1. Slides, acknowledgements and thanks

Principles of Database Systems CSE 544. Lecture #1 Introduction and SQL

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

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

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

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

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

SQL DATA DEFINITION LANGUAGE

Database Applications (15-415)

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

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

INTRODUCTION TO DATABASE

SQL: Data Definition Language

The Relational Model of Data (ii)

پوهنتون کابل پوهنځی كمپيوترساینس

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

Before we talk about Big Data. Lets talk about not-so-big data

Introduction to Data Management CSE 344


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

Announcements. Agenda. Database/Relation/Tuple. Schema. Discussion. CSE 444: Database Internals

Relational Model, Key Constraints

Review. The Relational Model. Glossary. Review. Data Models. Why Study the Relational Model? Why use a DBMS? OS provides RAM and disk

Conversion Functions

CMPT 354: Database System I. Lecture 2. Relational Model

Database Management Systems,

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

Announcements. Agenda. Database/Relation/Tuple. Discussion. Schema. CSE 444: Database Internals. Room change: Lab 1 part 1 is due on Monday

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

SQLite, Firefox, and our small IMDB movie database. CS3200 Database design (sp18 s2) Version 1/17/2018

Chapter 3: Introduction to SQL

The Structured Query Language Get Started

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

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

CSIE30600 Database Systems Basic SQL 2. Outline

Relational Algebra. Lecture 4A Kathleen Durant Northeastern University

Lecture 1: Relational Databases

Chapter 3: Introduction to SQL

Chapter-14 SQL COMMANDS

Transcription:

Database Systems CSE 303 Lecture 02 2016 Structure Query Language (SQL) Today s Outline (mainly from chapter 2) SQL introduction & Brief History Relational Model Data in SQL Basic Schema definition Keys and Constraints 1 2 SQL Introduction Standard language for querying and manipulating data Structured Query Language Original name was SEQUEL: Structured English QUEry Language Correct modern pronunciation is S-Q-L. Chronology 1970s IBM - first relational database System R, then DB2. Others include: Ingres Database - query language QUEL Digital - Relational Database Operator ISBL - relational algebra DML dbase family of products for PCs 1979 Oracle 1980 First standardisation efforts. 1984 ISO SQL standard - many flaws but universally adopted. 1992 Update to standard called SQL92 - The basic standard for any modern database 1999 Update to standard called SQL99 - Oracle database conforms to SQL99 2003 Current standard SQL2003 - Not many databases fully support this standard yet. 3 4 1

SQL Table name Tables in SQL Attribute names Data Definition Language (DDL) Create/alter/delete tables and their attributes Today s lecture... Data Manipulation Language (DML) Insert/delete/modify tuples in tables Query one or more tables Following lectures Product PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 Gadgets GizmoWorks SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi Tuples or rows 5 6 Tables Explained A relation is a table. (Envisioned by E.F. Codd 1970, IBM) The schema of a table is the table name and its attributes: Product(PName, Price, Category, Manfacturer) Database = collection of relations. Database schema = set of all relation schemas in the database. Tables Explained A tuple = a record/ a row in the table Example: (SingleTouch, 149.99, Photography, Canon) A table = a set of tuples Like a list but it is unordered: no first(), no next(), no last(). 7 8 2

Domains Data types of attributes Restriction: all attributes are of atomic type that is elementary type i.e., integer, string Each attribute has an associated domain i.e. the domain of Pname is string The domain of price is real numbers Creating Tables: SQL command CREATE TABLE Student ( name CHAR(50), id NUMBER(10), gender CHAR(1), address CHAR(100), gpa FLOAT 9 10 Characters: Syntax: CHAR[(maximum_size)] Examples `kamal CHAR(40) name CHAR(40) CHAR gender CHAR Padded with blank at right VARCHAR: Syntax: VARCHAR(maximum_size) Variable length character string Example VARCHAR(40) address VARCHAR(100) VARCHAR not permitted Memory is allocated based on exact length of string. 11 12 3

CHAR vs. VARCHAR: VARCHAR: no wastage of memory CHAR: faster processing Which one to use when? If you want to store password? If you want to store last name/first name etc...? For numbers (like C) INT or INTEGER REAL FLOAT 13 14 Fixed point decimal numbers: DECIMAL[(precision, scale)] in SQL NUMBER[(precision, scale)] in ORACLE precision: how many digits in total scale: how many digits after/before decimal In SQL Decimal(p,s) Example When you insert 7,456,123.89 (In this number 9 digits in total, 2 decimal digits) Data Type Value stored - Number(9) - Number(9,1) - Number(9,2) - Number(7,-2) - Number(6) 7456124 7456123.9 7456123.89 7456100 Rejected Only NUMBER any fraction 15 16 4

Date data type: Standard SQL YYYY-MM-DD Example: Date 1994-10-21 Oracle provides a function to_date for creating an object of Date data type: General syntax TO_DATE(<string>, '<format>') Example: TO_DATE( 1994-10-21, YYYY MM-DD ) TO_DATE( 1994-OCT-21, YYYY MON-DD ) TO_DATE( 1994-OCT-21, YYYY MON-DD ) 17 18 Oracle s default date format is DD-MON-YY If you use this format you don t have to use to_date function. For outputing date you can use to_char function. The general usage of TO_CHAR is: TO_CHAR(<date>, '<format>') where the <format> string can be formed from over 40 options. Some of the more popular ones are on the next slide 19 20 5

MM Numeric month (e.g., 07) MON MONTH Abbreviated month name (e.g., JUL) Full month name (e.g., JULY) DD Day of month (e.g., 24) DY Abbreviated name of day (e.g., FRI) YYYY 4-digit year (e.g., 1998) YY Last 2 digits of the year (e.g., 98) RR Like YY, but the two digits are ``rounded'' to a year in the range 1950 to 2049. Thus, 06 is considered 2006 instead of 1906 Two dates can be compared using = <> > etc. Two dates can only be subtracted NO addition, multiplication or division. 21 22 NULL data NULL data Any value of any type can be NULL. Signals missing value. Students(sid: string, name: string, email: string, gpa: float dob: date) Value doesn t exist. Value exists but unknown. Value not applicable to this record. 23 Any value of any type can be NULL. Signals missing value. Students(sid: string, name: string, email: string, gpa: float dob: date) Value doesn t exist. (for example email of a student) Value exists but unknown. (for example dob needs verification) Value not applicable to this record. (for example, L1 T1 student) 24 6

Creating table Creating table CREATE TABLE <tablename> ( <list of attributes and types> CREATE TABLE <tablename> ( attribute1 type1, attribute2 type2,.. 25 26 Creating table CREATE TABLE Product ( maker VARCHAR(10), model NUMBER(10), type VARCHAR(10), Case doesn t matter Case insensitive: Same: SELECT Select select Same: Product product Different: Seattle seattle Constants: abc - yes abc - no 27 28 7

Convention to follow SQL Commands: all caps Example: CREATE TABLE Table name : Start with upper case, any subsequent word upper case Example: Product, MovieStar Attributes: Start with lower case, any subsequent word upper case or separate by underscore. Example: maker, producer, certno (or cert_no) Data types Example: CHAR, VARCHAR CREATE TABLE Product ( maker VARCHAR(10), model NUMBER(10), type VARCHAR(10), 29 Keys Key = minimal set of attributes acting as a unique tuple identifier. Consider the universe of all potential relation data, not just what the table currently contains. p_name price manufacturer Gizmo 19.99 GizmoWorks Powergizmo 39.99 GizmoWorks Widget 19.99 WidgetsRUs HyperWidget 203.99 Hyper Product 30 What is the key here? A. dept B. dept, number C. dept, number, section D. dept, number, section, instructor Course dept number section instructor MATH 101 A Jones MATH 101 B Smith MATH 102 A Williams PHYS 101 A Baker PHYS 102 B Baker 31 Primary keys Every table should select one key as primary key. Each tuple must have distinct non-null values of primary key attributes. Adding a new record with duplicate or NULL primary key will fail. p_name price manufacturer Gizmo 19.99 GizmoWorks Powergizmo 39.99 GizmoWorks Widget 19.99 WidgetsRUs HyperWidget 203.99 Hyper Product Product (p_name: string, price: float, manufacturer: string) 32 8

Class Exercise Movie relation Movie relation Movie(title: string, year:int, length: int, genre: string) MovieStar relation 33 CREATE TABLE Movie( title VARCHAR(50), year INTEGER, length INTEGER, genre CHAR(10), PRIMARY KEY (title, year) ) 34 MovieStar relation UNIQUE attribute MovieStar(name: string, address:string, gender: char, birthdate: date) CREATE TABLE MovieStar( name VARCHAR(50), address VARCHAR(50), gender CHAR(1), birthdate DATE, PRIMARY KEY (name) 35 Each tuple must have distinct values of UNIQUE attributes except null. More than one tuple may have null values in unique attribute(s) Adding a new record with duplicate in unique attribute will fail. Students(sid: string, name: string, email: string, gpa: float dob: date, username: string) 36 9

UNIQUE attribute (example) Students(sid: string, name: string, email: string, gpa: float dob: date, username: string) CREATE TABLE Students( sid CHAR(10), name VARCHAR(50), email VARCHAR(50), gpa FLOAT, dob DATE, username CHAR(5) UNIQUE, PRIMARY KEY (sid) 37 10