Basic SQL. Basic SQL. Basic SQL

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

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

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Slides by: Ms. Shree Jaswal

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

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

CSIE30600 Database Systems Basic SQL 2. Outline

Basic SQL II. 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)

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

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

SQL: Data Definition Language

ECE 650 Systems Programming & Engineering. Spring 2018

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

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

SQL Introduction. CS 377: Database Systems

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

SQL Data Definition Language

SQL STRUCTURED QUERY LANGUAGE

Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries

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

The Relational Model of Data (ii)

SQL-99: Schema Definition, Basic Constraints, and Queries. Create, drop, alter Features Added in SQL2 and SQL-99

Database design process

CS2300: File Structures and Introduction to Database Systems

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

SQL DATA DEFINITION LANGUAGE

Overview Relational data model

Data and Tables. Bok, Jong Soon

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

user specifies what is wanted, not how to find it

SQL. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

Database Technology. Topic 3: SQL. Olaf Hartig.

Programming and Database Fundamentals for Data Scientists

SQL DATA DEFINITION LANGUAGE

Lecture 07. Spring 2018 Borough of Manhattan Community College

CSC 261/461 Database Systems Lecture 6. Fall 2017

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

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

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

SQL DATA DEFINITION LANGUAGE

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

Database Technology. Topic 2: Relational Databases and SQL. Olaf Hartig.

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

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

Structured Query Language (SQL)

Lab # 4. Data Definition Language (DDL)

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

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

Database Management Systems,

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

CSCC43H: Introduction to Databases. Lecture 4

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

Unit 1 - Chapter 4,5

DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

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

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

SQL Structured Query Language Introduction

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

Data Definition Language (DDL)

TINYINT[(M)] [UNSIGNED] [ZEROFILL] A very small integer. The signed range is -128 to 127. The unsigned range is 0 to 255.

Lab # 2 Hands-On. DDL Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

MySQL Creating a Database Lecture 3

3ISY402 DATABASE SYSTEMS

Relational Database Systems Part 01. Karine Reis Ferreira

SQL: Advanced Queries, Assertions, Triggers, and Views. Copyright 2012 Ramez Elmasri and Shamkant B. Navathe

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

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

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

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

MTAT Introduction to Databases

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

1) Introduction to SQL

Exact Numeric Data Types

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

Transaction Processing Dr Fawaz Alarfaj Al Imam Mohammed Ibn Saud Islamic University

Part II Relational Databases Data as Tables

Chapter 8. Joined Relations. Joined Relations. SQL-99: Schema Definition, Basic Constraints, and Queries

Department of Computer Science University of Cyprus. EPL342 Databases. Lab 2

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

MTAT Introduction to Databases

THE UNIVERSITY OF AUCKLAND

CS 338 Basic SQL Part II

Chapter 4. The Relational Model

More SQL: Complex Queries, Triggers, Views, and Schema Modification

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

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

Ref1 for STUDENT RECORD DB: Ref2 for COMPANY DB:

Principles of Data Management

DATABASE DESIGN - 1DL400

DATABASE DESIGN - 1DL400

CS W Introduction to Databases Spring Computer Science Department Columbia University

ECE 650 Systems Programming & Engineering. Spring 2018

Information Systems for Engineers Fall Data Definition with SQL

Transcription:

Basic SQL Dr Fawaz Alarfaj Al Imam Mohammed Ibn Saud Islamic University ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation Basic SQL Structured Query Language (SQL): Considered one of the major reasons for the commercial success of relational databases 2 Basic SQL Terminology: Table, row, and column used for relational model terms relation, tuple, and attribute 3

The CREATE TABLE Command in SQL Specifying a new relation Provide name of table Specify attributes, their types and initial constraints Can optionally specify schema: CREATE TABLE COMPANY.EMPLOYEE... or CREATE TABLE EMPLOYEE... 4 The CREATE TABLE Command in SQL CREATE TABLE "table_name" ("column1" "data type", "column2" "data type", "column3" "data type"); 5 The CREATE TABLE Command in SQL CREATE TABLE employee (first varchar(15), last varchar(20), age int(3), address varchar(30)); 6

The CREATE TABLE Command in SQL Base tables (base relations) Relation and its tuples are actually created and stored as a file by the DBMS Virtual relations (views) Created through the CREATE VIEW statement. Do not correspond to any physical file. 7 The CREATE VIEW Command in SQL CREATE VIEW view_name AS SELECT column1,column2,... FROM table_name WHERE condition; 8 Basic data types Numeric data types Integer numbers: INTEGER, INT, and SMALLINT Floating-point (real) numbers: FLOAT or REAL, and DOUBLE PRECISION Character-string data types Fixed length: CHAR(n), CHARACTER(n) Varying length: VARCHAR(n), CHAR VARYING(n), CHARACTER VARYING(n) 9

Numeric data types Integer numbers: INTEGER(p): Precision p TINYINT: Precision 3 SMALLINT: Precision 5 MEDIUMINT: Precision 7 INTEGER, INT: Precision 10 BIGINT: Precision 19 10 Numeric data types Type Storage Minimum Value Maximum Value (Bytes) (Signed/Unsigned) (Signed/Unsigned) TINYINT 1-128 127 0 255 SMALLINT 2-32768 32767 0 65535 MEDIUMINT 3-8388608 8388607 0 16777215 INT 4-2147483648 2147483647 0 4294967295 BIGINT 8-9223372036854775808 9223372036854775807 0 18446744073709551615 11 Numeric data types Floating-point (real) numbers: FLOAT, mantissa precision 16 -(Approximate Value) DOUBLE, mantissa precision 16 -(Approximate Value) REAL, mantissa precision 7 -(Approximate Value) DECIMAL, mantissa precision 16 -(Exact Value) 12

Numeric data types Floating-point (real) numbers: MySQL permits a nonstandard syntax: FLOAT(M,D) or REAL(M,D) or DOUBLE(M,D). Here, (M,D) means than values can be stored with up to M digits in total, of which D digits may be after the decimal point. 13 FLOAT(4,2) (a) (b) (c) 7689.23 76.89 76.8923 14 Numeric data types Floating-point (real) numbers: For example, a column defined as FLOAT(7,4) will look like -999.9999 when displayed. MySQL performs rounding when storing values, so if you insert 999.00009 into a FLOAT(7,4) column, the approximate result is 999.0001. 15

Boolean data type Values of TRUE or FALSE or NULL Valid examples Invalid examples TRUE 1 true yes FALSE 0 false no 16 Basic data types Character-string data types Fixed length: CHAR(n), CHARACTER(n) Varying length: VARCHAR(n), CHAR VARYING(n), CHARACTER VARYING(n) 17 Basic data types Character-string data types Value CHAR(4) VARCHAR(4) '' ' ' '' 'ab' 'ab ' 'ab' 'abcd' 'abcd' 'abcd' 'abcdefgh' 'abcd' 'abcd' 18

Date and Time Types The date and time types for representing temporal values are DATE, TIME, DATETIME, TIMESTAMP, and YEAR. Each temporal type has a range of valid values, as well as a zero value that may be used when you specify an invalid value that SQL cannot represent. 19 Date and Time Types Date parts must always be given in year-monthday order (for example, '98-09-04'), rather than in the month-day-year or day-month-year orders commonly used elsewhere (for example, '09-04-98', '04-09-98'). 20 Date and Time Types Dates containing two-digit year values are ambiguous. MySQL interprets two-digit year values using these rules: Year values in the range 70-99 are converted to 1970-1999. Year values in the range 00-69 are converted to 2000-2069. 21

Date and Time Types MySQL permits you to store a zero value of '0000-00-00' as a dummy date. This is in some cases more convenient than using NULL values, and uses less data and index space. To disallow '0000-00-00', enable the NO_ZERO_DATE mode. 22 Date and Time Types Data Type Zero Value DATE '0000-00-00' TIME '00:00:00' DATETIME '0000-00-00 00:00:00' TIMESTAMP '0000-00-00 00:00:00' YEAR 0000 23 Quiz 24

Specifying Constraints in SQL Basic constraints: Relational Model has 3 basic constraint types that are supported in SQL: Key constraint: A primary key value cannot be duplicated Entity Integrity Constraint: A primary key value cannot be null Referential integrity constraints: The foreign key must have a value that is already present as a primary key. 25 Specifying Attribute Constraints Other Restrictions on attribute domains: Default value of an attribute DEFAULT <value> NULL is not permitted for a particular attribute NOT NULL CHECK clause Dnumber INT NOT NULL CHECK (Dnumber > 0 AND Dnumber < 21); 26 Specifying Key and Referential Integrity Constraints PRIMARY KEY clause Specifies one or more attributes that make up the primary key of a relation D_number INT PRIMARY KEY; UNIQUE clause Specifies alternate (secondary) keys (called CANDIDATE keys in the relational model). D_name VARCHAR(15) UNIQUE; 27

Specifying Key and Referential Integrity Constraints FOREIGN KEY clause Default operation: reject update on violation Attach referential triggered action clause Options include SET NULL, CASCADE, and SET DEFAULT Action taken by the DBMS for SET NULL or SET DEFAULT is the same for both ON DELETE and ON UPDATE CASCADE option suitable for relationship relations 28 Basic Retrieval Queries in SQL SELECT statement One basic statement for retrieving information from a database 29 The SELECT-FROM-WHERE Structure of Basic SQL Queries Basic form of the SELECT statement: 30

The SELECT-FROM-WHERE Structure of Basic SQL Queries Logical comparison operators =, <, <=, >, >=, <> Projection attributes Attributes whose values are to be retrieved Selection condition Boolean condition that must be true for any retrieved tuple. 31 Basic Retrieval Queries 32 Basic Retrieval Queries 33

Basic Retrieval Queries Retrieve employee with salary more than 3000 34 Basic Retrieval Queries (Q0) 35 Basic Retrieval Queries (Q0) 36

Basic Retrieval Queries (Q1) 37 Basic Retrieval Queries (Q1) 38 Basic Retrieval Queries (Q2) 39

Basic Retrieval Queries (Q2) 40 Ambiguous Attribute Names Same name can be used for two (or more) attributes in different relations As long as the attributes are in different relations Must qualify the attribute name with the relation name to prevent ambiguity 41 Aliasing, and Renaming Aliases or tuple variables Declare alternative relation names E and S to refer to the EMPLOYEE relation twice in a query: Query 8. For each employee, retrieve the employee s first and last name and the first and last name of his or her immediate supervisor. SELECT E.Fname, E.Lname, S.Fname, S.Lname FROM EMPLOYEE AS E, EMPLOYEE AS S WHERE E.Super_ssn = S.Ssn; 42

Unspecified WHERE Clause Missing WHERE clause Indicates no condition on tuple selection Effect is a CROSS PRODUCT Result is all possible tuple combinations result 43 Use of the Asterisk Specify an asterisk (*) Retrieve all the attribute values of the selected tuples The * can be prefixed by the relation name; e.g., EMPLOYEE * 44 Tables as Sets in SQL SQL does not automatically eliminate duplicate tuples in query results For aggregate operations duplicates must be accounted for Use the keyword DISTINCT in the SELECT clause Only distinct tuples should remain in the result 45