Introduction to Databases. MySQL Syntax Guide: Day 1

Similar documents
SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

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

user specifies what is wanted, not how to find it

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

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

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

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

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Review. Objec,ves. Example Students Table. Database Overview 3/8/17. PostgreSQL DB Elas,csearch. Databases

tablename ORDER BY column ASC tablename ORDER BY column DESC sortingorder, } The WHERE and ORDER BY clauses can be combined in one

SQL Commands & Mongo DB New Syllabus

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

MySQL by Examples for Beginners

IBM DB2 UDB V7.1 Family Fundamentals.

Introduction to Databases and SQL

Chapter 13 : Informatics Practices. Class XI ( As per CBSE Board) SQL Commands. New Syllabus Visit : python.mykvs.in for regular updates

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

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Slides by: Ms. Shree Jaswal

DATABASE MANAGEMENT SYSTEMS

Lecture 5. Monday, September 15, 2014

Basic SQL. Basic SQL. Basic SQL

CSC Web Programming. Introduction to SQL

Exact Numeric Data Types

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

HKTA TANG HIN MEMORIAL SECONDARY SCHOOL SECONDARY 3 COMPUTER LITERACY. Name: ( ) Class: Date: Databases and Microsoft Access

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

SQL Data Definition Language: Create and Change the Database Ray Lockwood

Chapter 3. Introduction to relational databases and MySQL. 2010, Mike Murach & Associates, Inc. Murach's PHP and MySQL, C3

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

CS 327E Lecture 2. Shirley Cohen. January 27, 2016

Structured Query Language (SQL)

Course Topics. Microsoft SQL Server. Dr. Shohreh Ajoudanian. 01 Installing MSSQL Server Data types

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

How to use SQL to create a database

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

618 Index. BIT data type, 108, 109 BIT_LENGTH, 595f BIT VARYING data type, 108 BLOB data type, 108 Boolean data type, 109

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

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

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

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

Practical 8. SHANKER SINH VAGHELA BAPU INSTITUTE OF TECHNOLGY Subject: - DBMS Branch: - IT/CE Semester: - 3rd. 1. What does SQL stand for?

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

Oracle 1Z MySQL 5.6 Developer.

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

Principles of Data Management

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

SQL. Char (30) can store ram, ramji007 or 80- b

SQL stands for Structured Query Language. SQL is the lingua franca

CST272 SQL Server, SQL and the SqlDataSource Page 1

Database Systems. phpmyadmin Tutorial

1) Introduction to SQL

CSCI-UA: Database Design & Web Implementation. Professor Evan Sandhaus Lecture #23: SQLite

Relational Database Languages

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

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

Relational Database Development

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

Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH

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

sqoop Automatic database import Aaron Kimball Cloudera Inc. June 18, 2009

Introduction to relational databases and MySQL

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

STIDistrict Query (Basic)

IBM DB2 9 Family Fundamentals. Download Full Version :

Chapter 3 Introduction to relational databases and MySQL

30. Structured Query Language (SQL)

DB2 SQL Class Outline

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

CSIE30600 Database Systems Basic SQL 2. Outline

Oracle Exam 1z0-882 Oracle Certified Professional, MySQL 5.6 Developer Version: 7.0 [ Total Questions: 100 ]

CGS 3066: Spring 2017 SQL Reference

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

T-SQL Training: T-SQL for SQL Server for Developers

SQL STRUCTURED QUERY LANGUAGE

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

QQ Group

How to use SQL to work with a MySQL database

Relational databases and SQL

The SELECT-FROM-WHERE Structure

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

The SQL database language Parts of the SQL language

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

Databases (MariaDB/MySQL) CS401, Fall 2015

Based on the following Table(s), Write down the queries as indicated: 1. Write an SQL query to insert a new row in table Dept with values: 4, Prog, MO

MySQL. Prof.Sushila Aghav

Relational Database Management Systems for Epidemiologists: SQL Part I

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

Overview Relational data model

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

SQL Structured Query Language Introduction

The SELECT-FROM-WHERE Structure

Chapter-14 SQL COMMANDS

Database Systems SQL SL03

COMP283-Lecture 6 Applied Database Management

Writing High Performance SQL Statements. Tim Sharp July 14, 2014

Tables From Existing Tables

Transcription:

Introduction to Databases Question What type of database type does Shodor use? Answers A relational database What DBMS does Shodor use? MySQL MySQL Syntax Guide: Day 1 SQL MySQL Syntax Results Building Table and Table Info CREATE TABLE Step1: Write proper SQL syntax to query the People table using Sequel Pro. CREATE TABLE People ( id int(11) unsigned NOT NULL auto_increment, firstname varchar(255) default NULL, lastname varchar(255) default NULL, favcolor varchar(255) default NULL, shoesize decimal (3,1)default NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; What Was the Result Creates a table with the name People, the primary key is the id, and it has a firstname field, lastname field, favcolor field, and shoesize field. SHOW TABLE SHOW TABLES; Shows all the tables in the database DESCRIBE TABLE DESCRIBE People; Describes the ids and the data types of the table DROP TABLE (*Don t Do unless you really want to get rid of a table and all its data. You will have to start over.) DROP TABLE People; DO NOT RUN this query unless you want to destroy a table and data. Once done it

DO NOT RUN QUERY! INSERT INTO Step2: INSERT INTO People (firstname, lastname,favcolor, shoesize) VALUES ('','Edinboro','Orange',11.5), ('Gustavo','Nunez-Vivanco','Blue',9.5), ('Chris','Parker', 'Red', 10), ('Huny','Chung','Light Green', 9), ('Aaron','Weeden', 'Purple', 10), ('No','Hope', 'Black', 4), ('Shawn','Kang', 'Red', 10), ('Divya','Aikat','Purple',9.5), ('Michael','Allen Dacany', 'Blue', 12), ('Naylea ','Jacobo', 'Blue', 7.5), ('Mihir','Khadri', 'Red', 10.5), ('Krista', 'Katzenmeyer', 'Cupcake', 8.5), ('','Aiello', 'Yellow', 3), ('Joel','Coldren', 'Green', 13.5), ('Rohin','Shahi', 'Red', 12.5), ('Ameya','Rao', 'Blue', 9.5), ('Luka','Ashe-Jones', 'Orange', 8), ('','Edinboro', 'Blue', 12), ('','Edinboro', 'Green', 11), ('Leanne','Chan', 'Green',6), ('','Aiello', 'Purple', 3.5); cannot be undone. Adds these entries to the table NOT NULL CREATE TABLE Products ( ID int unsigned NOT NULL auto_increment, price int NOT NULL, productname varchar(225) NOT NULL, PRIMARY KEY (id), )ENGINE=InnoDB DEFAULT CHARSET=utf8; PRIMARY KEY CREATE TABLE Products ( ID int unsigned NOT NULL auto_increment, price int NOT NULL, productname varchar(225) NOT NULL, PRIMARY KEY (id), )ENGINE=InnoDB DEFAULT CHARSET=utf8; AUTO_INCREMENT CREATE TABLE Products ( ID int unsigned NOT NULL auto_increment, price int NOT NULL, Ensures that you must have a value in each of these fields for an entry The primary key here is the id, so it comes first, and the list is ordered based on the id The id auto increments at each entry by 1

productname varchar(225) NOT NULL, PRIMARY KEY (id), )ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER Step3: ALTER TABLE People ADD coinflip VARCHAR(5) NOT NULL; Adds another field called coinflip DELETE Be careful! DO NOT RUN QUERY! DELETE WHERE id=1; Do not run query s FREEBIE! Add Data to the newly created coinflip field. Step4: Useful SQL Syntax UPDATE SELECT WHERE ORDER BY INSERT INTO People (id, coinflip) VALUES ('1','Tails'),('2','Tails'),('3','Tails'), ('4','Heads'),('5','Heads'),('6','Tails'), ('7','Heads'),('8','Tails'),('9','Heads'), ('10','Heads'),('11','Tails'),('12','Heads'), ('13','Heads'),('14','Heads'),('15','Tails'), ('16','Heads'),('17','Heads'),('18','Heads'), ('19','Heads'),('20','Tails'),('21','Heads') ON DUPLICATE KEY UPDATE id=values(id), coinflip=values(coinflip); UPDATE People SET firstname= Ernest WHERE id=1; SELECT favcolor ; SELECT shoesize WHERE favcolor= Blue ; SELECT * ORDER BY favcolor; (default is ascending) To do descending: DESC Adds the data for the coinflip The first row was affected: the firstname changed from to Ernest Displayed the favcolor column of all entries Displayed the shoesize of people whose favorite color was blue Orders entries in alphabetical order by color AND, OR, Not (used in the where clause) AND: the first names of

Count, Avg, Sum LIKE IN BETWEEN ALIAS SQL Operators Arithmetic Operators WHERE favcolor= Blue AND coinflip= Heads ; WHERE favcolor= Blue OR favcolor= Green ; SELECT firstname WHERE NOT coinflip= Tails ; SELECT COUNT(favColor) ; SELECT AVG(shoeSize) ; SELECT SUM(shoeSize) WHERE favcolor= Blue ; SELECT lastname WHERE favcolor LIKE b% ; SELECT * WHERE favcolor IN ( Blue, Green, Red ); SELECT * WHERE shoesize BETWEEN 6 AND 9; Best to use Arithmetic and Comparison Operators on shoesize field those whose favcolor was blue, and the coin flipped as heads showed up OR: first names of those whose favcolor was blue or green NOT: those with a heads flip Returns 21 Returns 9.09524 Returns 50.5 Displayed the lastnames of anyone whose favcolor was blue or black Displayed all entries with favcolor green,red, or blue Displayed all entries with shoe size between 6 and 9 Add SELECT 30 + 10; 40

Subtraction SELECT 30-10; 20 Multiplication SELECT 5*3; 15 Division SELECT 20/5; 4.0000 Modulus SELECT 42%10; 2 Comparison Operators Equal to Greater than > Less than < Greater than equal to >= SELECT lastname WHERE shoesize = 9; WHERE shoesize > 8; WHERE shoesize < 8; WHERE shoesize >= 10; Chung Ernest Gustavo Chris Huny Aaron Shawn Divya Michael Mihir Krista Joel Rohin Ameya Keith No Naylea Leanne Ernest Chris Aaron Shawn Michael Mihir Joel

Less than equal to <= Not equal to <> List 10 SQL Data Types CHARACTER(n) WHERE shoesize <= 10; WHERE shoesize <> 1 What types of Data do they Store? This data type stores a character which has a fixed length of n Rohin Keith Gustavo Chris Huny Aaron No Shawn Divya Naylea Krista Ameya Luka Leanne Ernest Gustavo Huny No Divya Michael Naylea Mihir Krista Joel Rohin Ameya Luka Leanne Keith

BOOLEAN DECIMAL(p,s) TIME SMALLINT This data type stores true or false values This stores a decimal that has p-s values before the decimal and s values after the decimal Stores hour, minute, and second values Stores an integer with max 5 values BINARY(n) Binary string with a fixed length of n (ex BINARY(6) = 011001) DATE ARRAY XML BIGINT Stores month, day, and year values Set length and ordered collection of elements Stores xml data Integer with max of 19 values SQL MINI Challenge Write SQL query to answer the questions Result What is s favorite color? Whose shoe size is 3? Show First and last name Whose favorite color is Cupcake? Show First Name only What is the average shoe size? Whose favorite color is Red? Show Last Name only ORDER BY ASCending order. SELECT favcolor WHERE firstname= ;, lastname WHERE shoesize= 3; WHERE favcolor= Cupcake ; SELECT AVG(shoeSize) ; SELECT lastname WHERE favcolor= 'Red' ORDER BY lastname; Two results show up: Purple and Yellow This is because has two entries in the database Aiello Krista 9.09524 Kang Khadri Parker Shahi

What is the favorite color of people with shoe size 10? Select all data for user(s) id =13 Select first name for user(s) where favorite color is red Select favorite color where shoe size greater than or equal to 12 Select shoe size where first name = Joel Select lastname where favorite color is black Insert into People Keith,Kelly, Black, 11, Tails Update First name of id 1 to Ernest SELECT favcolor WHERE shoesize= 10; SELECT * WHERE id=13; WHERE favcolor='red'; SELECT favcolor WHERE shoesize>=12; SELECT shoesize WHERE firstname='joel'; SELECT lastname WHERE favcolor='black'; INSERT INTO People (firstname, lastname,favcolor, shoesize, coinflip) VALUES ( Keith, Kelley, Black, 11, Tails ); UPDATE People SET firstname= Ernest WHERE id=1; Red Purple Red 13 Aiello Yellow3.0 Heads Chris Shawn Mihir Rohin Blue Green Red Blue 13.5 Hope 22 Keith KelleyBlack 11.0 Tails Added to table Changes to Ernest

MySQL Syntax Guide: Day 2 Database Interaction MySQL Syntax using People table What were the results? PRIMARY KEY PRIMARY KEY (id), This makes the primary key id, the very first column FOREIGN KEY FOREIGN KEY (peopleid) REFERENCES People (id) This takes the id from the People table and makes it peopleid in your new table JOIN INNER JOIN SELECT * FROM CoinFlip INNER JOIN People4 ON CoinFlip.peopleId = People4.id LEFT JOIN SELECT * FROM CoinFlip LEFT JOIN People4 ON CoinFlip.peopleId = People4.id RIGHT JOIN SELECT * FROM CoinFlip RIGHT JOIN People4 ON CoinFlip.peopleId = People4.id Shows all data that has the same id and peopleid Takes all the information from People and the overlapping info from CoinFlip Takes all the information from CoinFlip and the overlapping info from People GROUP BY SELECT COUNT(peopleId), color FROM FavColor GROUP BY color; 1 Blue 1 Green 1 Purple 2 Red SQL Snippet for creating Relational DB CONSTRAINT CoinFlip_ibfk_1 Adds another field that

FOREIGN KEY (peopleid) REFERENCES People (id) ON DELETE CASCADE ON UPDATE CASCADE has the same id as the People table