SlideShare a Scribd company logo
MySQL

Let’s Get Relational
Tables
• Think of each table as a spreadsheet
• We define columns, also known as fields,
  which classify our data
• Each record in the table is a row
Data Types
• Varchar
  – Variable Characters, specify up to how many
    characters something will be
• Char
  – A set number of characters, good for things like
    state abbreviations
• Int
  – Whole numbers, positive or negative
Data Types
• Float
  – Floating Point, also known as a decimal
• Text
  – A huge blob of text, like a paragraph or more
• TinyInt / Bit / Boolean
  – 0 or 1, True or False
• DateTime
  – A date and time 0000-00-00 00:00:00
Data Types
• Depending on the flavor of SQL (Oracle,
  MySQL, MSSQL, PostgreSQL, etc) there are
  many more
• Don’t get overwhelmed, just think of what will
  be best in terms of sorting and lookups
User Table Example
• Username
  – Varchar
• Password
  – Varchar
• ID
  – Integer
• AccountCreated
  – DateTime
PHPMYADMIN
CRUD
•   Create
•   Retrieve
•   Update
•   Delete
Create
• INSERT is used to create a new record in the
  database



• INSERT VALUES (username, password) INTO
  users (‘fluffybunny’, ‘123456’)
Retrieve
• SELECT is used to retrieve a record in the
  database



• SELECT username, password FROM users
  WHERE ID = 1
Anatomy of SELECT
• SELECT [column]
  – The command
• FROM [table]
  – The table you want to select from
• WHERE *column+ = ‘value’
  – Specifics
• ORDER BY [column] [ASC/DESC]
  – Sort by column
• LIMIT [Number]
  – Limit the number of records returned
UPDATE
• UPDATE is used to change record(s) in the
  database



• UPDATE users SET username = ‘FluffyBuns’
  WHERE ID = 1
DELETE
• DELETE is used to remove records from the
  database



• DELETE FROM users WHERE ID = 1

** if you do not specify anything in the WHERE
clause, it will delete everything in that table
Users and Groups

Users                Groups            UserGroups
ID      User         ID       Group    UserID   GroupID
1       John         1        Sharks   1        2
2       Jane         2        Ducks    2        2
3       Sally        3        Lemurs   3        1
4       Ryan                           4        1
5       Joe                            5        3
Joins
• SELECT User, Group FROM Users
  JOIN UserGroups ON Users.ID =
  UserGroups.UserID
  JOIN Groups ON UserGroups.GroupID =
  Groups.ID
  WHERE Group = ‘Sharks’
IN
• SELECT User FROM Users WHERE ID IN (1,2)

• SELECT User FROM Users WHERE ID IN
  (SELECT UserID FROM UserGroups WHERE
  GroupID = 2)
BETWEEN
• SELECT User FROM Users WHERE ID BETWEEN
  3 AND 5
Conditions and Operators
• WHERE can also use wildcards for text
   – WHERE Column IS LIKE ‘%something%’

• WHERE can use more than =
   – WHERE ID < 4
   – WHERE ID <= 4

• WHERE can combine conditions
   – WHERE Column = ‘A’ AND Column2 = ‘B’
   – WHERE Column = ‘A’ OR Column2 = ‘B’
   – WHERE (Column = ‘A’ OR Column2 = B’) AND Other = ‘C’
Nerding Out
• SQL has functions, like COUNT and SUM

• SELECT Customer, SUM(Amount) FROM
  Orders GROUP BY Customer

• SELECT COUNT(Customer) FROM Orders
  GROUP BY Customer
Indexes
• It’s a good habit to create a column for each
  table that acts as an ID
• We can put an index on the ID and it can
  speed up the query time
• Unless you’re dealing with really big datasets,
  you probably won’t have to worry about this
Database Design
• Design a schema for a social network
  – I’m cool with stretching this to something else
• At a minimum it must contain:
  – Users
  – Posts
  – Friend Relationships
  – Likes
• You will create a web interface for this
  database later

More Related Content

PPTX
Mysql Crud, Php Mysql, php, sql
PPTX
Oracle SQL - Grants, filters, groups and more
PPT
Intro to Java
PPTX
Db pre
PPTX
Java- Nested Classes
PPTX
Oracle SQL - Select Part -1 let's write some queries!
PPT
Introduction to sql
PPTX
Sql - Structured Query Language
Mysql Crud, Php Mysql, php, sql
Oracle SQL - Grants, filters, groups and more
Intro to Java
Db pre
Java- Nested Classes
Oracle SQL - Select Part -1 let's write some queries!
Introduction to sql
Sql - Structured Query Language

What's hot (6)

PDF
Sql a practical introduction
PPTX
What is SQL Server?
PPTX
PPTX
PDF
Java Inner Classes
PPTX
Python Advance Tutorial - Advance Functions
Sql a practical introduction
What is SQL Server?
Java Inner Classes
Python Advance Tutorial - Advance Functions
Ad

Similar to CVJ531: Intro to MySQL (20)

PPTX
Introduction to MySQL in PHP
PPT
mySQL and Relational Databases
PDF
My sql102
PDF
MySQL for beginners
PPTX
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
PPTX
UNIT V (5).pptx
PPTX
MSAvMySQL.pptx
PPTX
The SQL Query Language: Simple SELECT Commands
PPTX
Introduction to SQL
PPT
mysql.ppt
PPT
PPT
Lecture 15 - MySQL- PHP 1.ppt
PPT
qwe.ppt
PPTX
Class 8 - Database Programming
PPT
MY SQL
PPTX
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
Introduction to MySQL in PHP
mySQL and Relational Databases
My sql102
MySQL for beginners
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
UNIT V (5).pptx
MSAvMySQL.pptx
The SQL Query Language: Simple SELECT Commands
Introduction to SQL
mysql.ppt
Lecture 15 - MySQL- PHP 1.ppt
qwe.ppt
Class 8 - Database Programming
MY SQL
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
Ad

More from Clay Ewing (9)

PPTX
Hypothesis Tested: Designing Games with Theoretical Frameworks
PPTX
Game Design Workshop for SEEDS at the University of Miami
PPTX
Getting Started with Queso
PDF
Twine Workshop
PDF
Win Win: Models for Creating a Social Impact Game on a Budget
PPTX
Game Design dorkShop at LAB Miami
PDF
Defining games feedback
PPTX
Welcome to CMP 394
PPTX
Cordova: Making Native Mobile Apps With Your Web Skills
Hypothesis Tested: Designing Games with Theoretical Frameworks
Game Design Workshop for SEEDS at the University of Miami
Getting Started with Queso
Twine Workshop
Win Win: Models for Creating a Social Impact Game on a Budget
Game Design dorkShop at LAB Miami
Defining games feedback
Welcome to CMP 394
Cordova: Making Native Mobile Apps With Your Web Skills

CVJ531: Intro to MySQL

  • 2. Tables • Think of each table as a spreadsheet • We define columns, also known as fields, which classify our data • Each record in the table is a row
  • 3. Data Types • Varchar – Variable Characters, specify up to how many characters something will be • Char – A set number of characters, good for things like state abbreviations • Int – Whole numbers, positive or negative
  • 4. Data Types • Float – Floating Point, also known as a decimal • Text – A huge blob of text, like a paragraph or more • TinyInt / Bit / Boolean – 0 or 1, True or False • DateTime – A date and time 0000-00-00 00:00:00
  • 5. Data Types • Depending on the flavor of SQL (Oracle, MySQL, MSSQL, PostgreSQL, etc) there are many more • Don’t get overwhelmed, just think of what will be best in terms of sorting and lookups
  • 6. User Table Example • Username – Varchar • Password – Varchar • ID – Integer • AccountCreated – DateTime
  • 8. CRUD • Create • Retrieve • Update • Delete
  • 9. Create • INSERT is used to create a new record in the database • INSERT VALUES (username, password) INTO users (‘fluffybunny’, ‘123456’)
  • 10. Retrieve • SELECT is used to retrieve a record in the database • SELECT username, password FROM users WHERE ID = 1
  • 11. Anatomy of SELECT • SELECT [column] – The command • FROM [table] – The table you want to select from • WHERE *column+ = ‘value’ – Specifics • ORDER BY [column] [ASC/DESC] – Sort by column • LIMIT [Number] – Limit the number of records returned
  • 12. UPDATE • UPDATE is used to change record(s) in the database • UPDATE users SET username = ‘FluffyBuns’ WHERE ID = 1
  • 13. DELETE • DELETE is used to remove records from the database • DELETE FROM users WHERE ID = 1 ** if you do not specify anything in the WHERE clause, it will delete everything in that table
  • 14. Users and Groups Users Groups UserGroups ID User ID Group UserID GroupID 1 John 1 Sharks 1 2 2 Jane 2 Ducks 2 2 3 Sally 3 Lemurs 3 1 4 Ryan 4 1 5 Joe 5 3
  • 15. Joins • SELECT User, Group FROM Users JOIN UserGroups ON Users.ID = UserGroups.UserID JOIN Groups ON UserGroups.GroupID = Groups.ID WHERE Group = ‘Sharks’
  • 16. IN • SELECT User FROM Users WHERE ID IN (1,2) • SELECT User FROM Users WHERE ID IN (SELECT UserID FROM UserGroups WHERE GroupID = 2)
  • 17. BETWEEN • SELECT User FROM Users WHERE ID BETWEEN 3 AND 5
  • 18. Conditions and Operators • WHERE can also use wildcards for text – WHERE Column IS LIKE ‘%something%’ • WHERE can use more than = – WHERE ID < 4 – WHERE ID <= 4 • WHERE can combine conditions – WHERE Column = ‘A’ AND Column2 = ‘B’ – WHERE Column = ‘A’ OR Column2 = ‘B’ – WHERE (Column = ‘A’ OR Column2 = B’) AND Other = ‘C’
  • 19. Nerding Out • SQL has functions, like COUNT and SUM • SELECT Customer, SUM(Amount) FROM Orders GROUP BY Customer • SELECT COUNT(Customer) FROM Orders GROUP BY Customer
  • 20. Indexes • It’s a good habit to create a column for each table that acts as an ID • We can put an index on the ID and it can speed up the query time • Unless you’re dealing with really big datasets, you probably won’t have to worry about this
  • 21. Database Design • Design a schema for a social network – I’m cool with stretching this to something else • At a minimum it must contain: – Users – Posts – Friend Relationships – Likes • You will create a web interface for this database later