SlideShare ist ein Scribd-Unternehmen logo
1 von 21
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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (6)

Sql a practical introduction
Sql   a practical introductionSql   a practical introduction
Sql a practical introduction
 
What is SQL Server?
What is SQL Server?What is SQL Server?
What is SQL Server?
 
Schemas 2 - Restricting Values
Schemas 2 - Restricting ValuesSchemas 2 - Restricting Values
Schemas 2 - Restricting Values
 
Classes
ClassesClasses
Classes
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
 
Python Advance Tutorial - Advance Functions
Python Advance Tutorial - Advance FunctionsPython Advance Tutorial - Advance Functions
Python Advance Tutorial - Advance Functions
 

Ähnlich wie CVJ531: Intro to MySQL

How Clean is your Database? Data Scrubbing for all Skill Sets
How Clean is your Database? Data Scrubbing for all Skill SetsHow Clean is your Database? Data Scrubbing for all Skill Sets
How Clean is your Database? Data Scrubbing for all Skill SetsChad Petrovay
 
PostgreSQL - It's kind've a nifty database
PostgreSQL - It's kind've a nifty databasePostgreSQL - It's kind've a nifty database
PostgreSQL - It's kind've a nifty databaseBarry Jones
 
Complete SQL Tutorial In Hindi By Rishabh Mishra.pdf
Complete SQL Tutorial In Hindi By Rishabh Mishra.pdfComplete SQL Tutorial In Hindi By Rishabh Mishra.pdf
Complete SQL Tutorial In Hindi By Rishabh Mishra.pdfssuserb5bb0e
 
springdatajpa-up.pdf
springdatajpa-up.pdfspringdatajpa-up.pdf
springdatajpa-up.pdfssuser0562f1
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersJonathan Levin
 
Data_base.pptx
Data_base.pptxData_base.pptx
Data_base.pptxMohit89650
 
ms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptxms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptxYashaswiniSrinivasan1
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQLlahin31
 
Tk2323 lecture 7 sql
Tk2323 lecture 7   sql Tk2323 lecture 7   sql
Tk2323 lecture 7 sql MengChun Lam
 
SQL Server 2014 Monitoring and Profiling
SQL Server 2014 Monitoring and ProfilingSQL Server 2014 Monitoring and Profiling
SQL Server 2014 Monitoring and ProfilingAbouzar Noori
 

Ähnlich wie CVJ531: Intro to MySQL (20)

IR SQLite Session #1
IR SQLite Session #1IR SQLite Session #1
IR SQLite Session #1
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
How Clean is your Database? Data Scrubbing for all Skill Sets
How Clean is your Database? Data Scrubbing for all Skill SetsHow Clean is your Database? Data Scrubbing for all Skill Sets
How Clean is your Database? Data Scrubbing for all Skill Sets
 
PostgreSQL - It's kind've a nifty database
PostgreSQL - It's kind've a nifty databasePostgreSQL - It's kind've a nifty database
PostgreSQL - It's kind've a nifty database
 
Complete SQL Tutorial In Hindi By Rishabh Mishra.pdf
Complete SQL Tutorial In Hindi By Rishabh Mishra.pdfComplete SQL Tutorial In Hindi By Rishabh Mishra.pdf
Complete SQL Tutorial In Hindi By Rishabh Mishra.pdf
 
springdatajpa-up.pdf
springdatajpa-up.pdfspringdatajpa-up.pdf
springdatajpa-up.pdf
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Introduction to mysql part 1
Introduction to mysql part 1Introduction to mysql part 1
Introduction to mysql part 1
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
Sql introduction
Sql introductionSql introduction
Sql introduction
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for Developers
 
The perfect index
The perfect indexThe perfect index
The perfect index
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
Data_base.pptx
Data_base.pptxData_base.pptx
Data_base.pptx
 
ms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptxms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptx
 
Android Database
Android DatabaseAndroid Database
Android Database
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
Unit - II.pptx
Unit - II.pptxUnit - II.pptx
Unit - II.pptx
 
Tk2323 lecture 7 sql
Tk2323 lecture 7   sql Tk2323 lecture 7   sql
Tk2323 lecture 7 sql
 
SQL Server 2014 Monitoring and Profiling
SQL Server 2014 Monitoring and ProfilingSQL Server 2014 Monitoring and Profiling
SQL Server 2014 Monitoring and Profiling
 

Mehr von Clay Ewing

Hypothesis Tested: Designing Games with Theoretical Frameworks
Hypothesis Tested: Designing Games with Theoretical FrameworksHypothesis Tested: Designing Games with Theoretical Frameworks
Hypothesis Tested: Designing Games with Theoretical FrameworksClay Ewing
 
Game Design Workshop for SEEDS at the University of Miami
Game Design Workshop for SEEDS at the University of MiamiGame Design Workshop for SEEDS at the University of Miami
Game Design Workshop for SEEDS at the University of MiamiClay Ewing
 
Getting Started with Queso
Getting Started with QuesoGetting Started with Queso
Getting Started with QuesoClay Ewing
 
Twine Workshop
Twine WorkshopTwine Workshop
Twine WorkshopClay Ewing
 
Win Win: Models for Creating a Social Impact Game on a Budget
Win Win: Models for Creating a Social Impact Game on a BudgetWin Win: Models for Creating a Social Impact Game on a Budget
Win Win: Models for Creating a Social Impact Game on a BudgetClay Ewing
 
Game Design dorkShop at LAB Miami
Game Design dorkShop at LAB MiamiGame Design dorkShop at LAB Miami
Game Design dorkShop at LAB MiamiClay Ewing
 
Defining games feedback
Defining games   feedbackDefining games   feedback
Defining games feedbackClay Ewing
 
Welcome to CMP 394
Welcome to CMP 394Welcome to CMP 394
Welcome to CMP 394Clay Ewing
 
Cordova: Making Native Mobile Apps With Your Web Skills
Cordova: Making Native Mobile Apps With Your Web SkillsCordova: Making Native Mobile Apps With Your Web Skills
Cordova: Making Native Mobile Apps With Your Web SkillsClay Ewing
 

Mehr von Clay Ewing (9)

Hypothesis Tested: Designing Games with Theoretical Frameworks
Hypothesis Tested: Designing Games with Theoretical FrameworksHypothesis Tested: Designing Games with Theoretical Frameworks
Hypothesis Tested: Designing Games with Theoretical Frameworks
 
Game Design Workshop for SEEDS at the University of Miami
Game Design Workshop for SEEDS at the University of MiamiGame Design Workshop for SEEDS at the University of Miami
Game Design Workshop for SEEDS at the University of Miami
 
Getting Started with Queso
Getting Started with QuesoGetting Started with Queso
Getting Started with Queso
 
Twine Workshop
Twine WorkshopTwine Workshop
Twine Workshop
 
Win Win: Models for Creating a Social Impact Game on a Budget
Win Win: Models for Creating a Social Impact Game on a BudgetWin Win: Models for Creating a Social Impact Game on a Budget
Win Win: Models for Creating a Social Impact Game on a Budget
 
Game Design dorkShop at LAB Miami
Game Design dorkShop at LAB MiamiGame Design dorkShop at LAB Miami
Game Design dorkShop at LAB Miami
 
Defining games feedback
Defining games   feedbackDefining games   feedback
Defining games feedback
 
Welcome to CMP 394
Welcome to CMP 394Welcome to CMP 394
Welcome to CMP 394
 
Cordova: Making Native Mobile Apps With Your Web Skills
Cordova: Making Native Mobile Apps With Your Web SkillsCordova: Making Native Mobile Apps With Your Web Skills
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