cvj531: intro to mysql
TRANSCRIPT
![Page 1: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/1.jpg)
MySQL
Let’s Get Relational
![Page 2: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/2.jpg)
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
![Page 3: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/3.jpg)
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
![Page 4: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/4.jpg)
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
![Page 5: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/5.jpg)
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
![Page 6: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/6.jpg)
User Table Example
• Username
– Varchar
• Password
– Varchar
• ID
– Integer
• AccountCreated
– DateTime
![Page 7: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/7.jpg)
PHPMYADMIN
![Page 8: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/8.jpg)
CRUD
• Create
• Retrieve
• Update
• Delete
![Page 9: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/9.jpg)
Create
• INSERT is used to create a new record in the database
• INSERT VALUES (username, password) INTO users (‘fluffybunny’, ‘123456’)
![Page 10: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/10.jpg)
Retrieve
• SELECT is used to retrieve a record in the database
• SELECT username, password FROM users WHERE ID = 1
![Page 11: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/11.jpg)
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
![Page 12: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/12.jpg)
UPDATE
• UPDATE is used to change record(s) in the database
• UPDATE users SET username = ‘FluffyBuns’ WHERE ID = 1
![Page 13: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/13.jpg)
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
![Page 14: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/14.jpg)
Users and Groups
ID User
1 John
2 Jane
3 Sally
4 Ryan
5 Joe
ID Group
1 Sharks
2 Ducks
3 Lemurs
UserID GroupID
1 2
2 2
3 1
4 1
5 3
Users Groups UserGroups
![Page 15: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/15.jpg)
Joins
• SELECT User, Group FROM UsersJOIN UserGroups ON Users.ID = UserGroups.UserIDJOIN Groups ON UserGroups.GroupID = Groups.IDWHERE Group = ‘Sharks’
![Page 16: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/16.jpg)
IN
• SELECT User FROM Users WHERE ID IN (1,2)
• SELECT User FROM Users WHERE ID IN (SELECT UserID FROM UserGroups WHERE GroupID = 2)
![Page 17: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/17.jpg)
BETWEEN
• SELECT User FROM Users WHERE ID BETWEEN 3 AND 5
![Page 18: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/18.jpg)
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’
![Page 19: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/19.jpg)
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
![Page 20: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/20.jpg)
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
![Page 21: CVJ531: Intro to MySQL](https://reader034.vdocuments.mx/reader034/viewer/2022050719/559f0edd1a28ab614d8b456f/html5/thumbnails/21.jpg)
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