SlideShare a Scribd company logo

Database 101

thehoagie
thehoagie

An introduction to databases, MySQL in particular, as well as an introduction how to write SELECT, INSERT, UPDATE, and DELETE statements

Database 101

1 of 24
Download to read offline
Database 101
A hands on tech talk
What Is a Database?
● A collection of data that is organized in a way that makes retrieval
relatively easy.
● Typically has logical groupings of schemas, and tables, (though document
storage database do not)
● Typically relational, though not always (noSQL)
Kinds of Databases
● Relational - MySQL, PostgreSQL, Sqlite
● Columnar - Redshift, Cassandra, BigQuery
● Document - MongoDB, CouchDB
● Key/value (in memory) - Memcached, Redis
● Full text (search engines) - Solr, ElasticSearch, SphinxSE
MySQL
● Built by Michael Widenius
● Named after his daughter “My”. MariaDB, the successor to MySQL is
named after his other daughter “Maria”. - Dad points!
● Purchased by Sun in 2008, by Oracle in 2010
● Popular because its relational, open source, works on many OSes, and
works well for the average use case
Connecting
● mysql -h bookshelf.crm7sspivlug.us-east-1.rds.amazonaws.com -u
persuade -p -D bookshelf
● -h is the hostname
● -u is the user to connect as
● -p is the password - but don’t type it as we will be prompted with a masked
input
● -D is the database to use
Looking around
● show databases
● show tables
● explain <table>
● ; vs G

More Related Content

Viewers also liked

Git Pro Tips
Git Pro TipsGit Pro Tips
Git Pro Tipsthehoagie
 
Cloud Computing 101
Cloud Computing 101Cloud Computing 101
Cloud Computing 101Chris Yeo
 
Hacker's and painters Hardware Hacking 101 - 10th Oct 2014
Hacker's and painters Hardware Hacking 101 - 10th Oct 2014Hacker's and painters Hardware Hacking 101 - 10th Oct 2014
Hacker's and painters Hardware Hacking 101 - 10th Oct 2014Takeda Pharmaceuticals
 
Cloud 101: The Basics of Cloud Computing
Cloud 101: The Basics of Cloud ComputingCloud 101: The Basics of Cloud Computing
Cloud 101: The Basics of Cloud ComputingHostway|HOSTING
 

Viewers also liked (6)

Testing
TestingTesting
Testing
 
Hubot
HubotHubot
Hubot
 
Git Pro Tips
Git Pro TipsGit Pro Tips
Git Pro Tips
 
Cloud Computing 101
Cloud Computing 101Cloud Computing 101
Cloud Computing 101
 
Hacker's and painters Hardware Hacking 101 - 10th Oct 2014
Hacker's and painters Hardware Hacking 101 - 10th Oct 2014Hacker's and painters Hardware Hacking 101 - 10th Oct 2014
Hacker's and painters Hardware Hacking 101 - 10th Oct 2014
 
Cloud 101: The Basics of Cloud Computing
Cloud 101: The Basics of Cloud ComputingCloud 101: The Basics of Cloud Computing
Cloud 101: The Basics of Cloud Computing
 

Similar to Database 101

Big Data Grows Up - A (re)introduction to Cassandra
Big Data Grows Up - A (re)introduction to CassandraBig Data Grows Up - A (re)introduction to Cassandra
Big Data Grows Up - A (re)introduction to CassandraRobbie Strickland
 
Introduction to Databases - query optimizations for MySQL
Introduction to Databases - query optimizations for MySQLIntroduction to Databases - query optimizations for MySQL
Introduction to Databases - query optimizations for MySQLMárton Kodok
 
How MySQL can boost (or kill) your application v2
How MySQL can boost (or kill) your application v2How MySQL can boost (or kill) your application v2
How MySQL can boost (or kill) your application v2Federico Razzoli
 
MySQL Query Optimisation 101
MySQL Query Optimisation 101MySQL Query Optimisation 101
MySQL Query Optimisation 101Federico Razzoli
 
MySQL Performance Optimization
MySQL Performance OptimizationMySQL Performance Optimization
MySQL Performance OptimizationMindfire Solutions
 
Full Text search in Django with Postgres
Full Text search in Django with PostgresFull Text search in Django with Postgres
Full Text search in Django with Postgressyerram
 
Intro To TSQL - Unit 1
Intro To TSQL - Unit 1Intro To TSQL - Unit 1
Intro To TSQL - Unit 1iccma
 
How MySQL can boost (or kill) your application
How MySQL can boost (or kill) your applicationHow MySQL can boost (or kill) your application
How MySQL can boost (or kill) your applicationFederico Razzoli
 
Database Design most common pitfalls
Database Design most common pitfallsDatabase Design most common pitfalls
Database Design most common pitfallsFederico Razzoli
 
PostgreSQL - Object Relational Database
PostgreSQL - Object Relational DatabasePostgreSQL - Object Relational Database
PostgreSQL - Object Relational DatabaseMubashar Iqbal
 
Basics on SQL queries
Basics on SQL queriesBasics on SQL queries
Basics on SQL queriesKnoldus Inc.
 
Intro to tsql unit 1
Intro to tsql   unit 1Intro to tsql   unit 1
Intro to tsql unit 1Syed Asrarali
 
PostgreSQL talk, Database 2011 conference
PostgreSQL talk, Database 2011 conferencePostgreSQL talk, Database 2011 conference
PostgreSQL talk, Database 2011 conferenceReuven Lerner
 
Basic Introduction to Python Programming
Basic Introduction to Python ProgrammingBasic Introduction to Python Programming
Basic Introduction to Python ProgrammingSubashiniRathinavel
 
Sql coding-standard-sqlserver
Sql coding-standard-sqlserverSql coding-standard-sqlserver
Sql coding-standard-sqlserverlochaaaa
 
Introducing Apache Spark's Data Frames and Dataset APIs workshop series
Introducing Apache Spark's Data Frames and Dataset APIs workshop seriesIntroducing Apache Spark's Data Frames and Dataset APIs workshop series
Introducing Apache Spark's Data Frames and Dataset APIs workshop seriesHolden Karau
 

Similar to Database 101 (20)

Big Data Grows Up - A (re)introduction to Cassandra
Big Data Grows Up - A (re)introduction to CassandraBig Data Grows Up - A (re)introduction to Cassandra
Big Data Grows Up - A (re)introduction to Cassandra
 
Introduction to Databases - query optimizations for MySQL
Introduction to Databases - query optimizations for MySQLIntroduction to Databases - query optimizations for MySQL
Introduction to Databases - query optimizations for MySQL
 
How MySQL can boost (or kill) your application v2
How MySQL can boost (or kill) your application v2How MySQL can boost (or kill) your application v2
How MySQL can boost (or kill) your application v2
 
Modern sql
Modern sqlModern sql
Modern sql
 
Cassandra
CassandraCassandra
Cassandra
 
MySQL Query Optimisation 101
MySQL Query Optimisation 101MySQL Query Optimisation 101
MySQL Query Optimisation 101
 
MySQL Performance Optimization
MySQL Performance OptimizationMySQL Performance Optimization
MySQL Performance Optimization
 
Meet the-other-elephant
Meet the-other-elephantMeet the-other-elephant
Meet the-other-elephant
 
Full Text search in Django with Postgres
Full Text search in Django with PostgresFull Text search in Django with Postgres
Full Text search in Django with Postgres
 
Intro To TSQL - Unit 1
Intro To TSQL - Unit 1Intro To TSQL - Unit 1
Intro To TSQL - Unit 1
 
How MySQL can boost (or kill) your application
How MySQL can boost (or kill) your applicationHow MySQL can boost (or kill) your application
How MySQL can boost (or kill) your application
 
Database Design most common pitfalls
Database Design most common pitfallsDatabase Design most common pitfalls
Database Design most common pitfalls
 
PostgreSQL - Object Relational Database
PostgreSQL - Object Relational DatabasePostgreSQL - Object Relational Database
PostgreSQL - Object Relational Database
 
Basics on SQL queries
Basics on SQL queriesBasics on SQL queries
Basics on SQL queries
 
Intro to tsql unit 1
Intro to tsql   unit 1Intro to tsql   unit 1
Intro to tsql unit 1
 
PostgreSQL
PostgreSQLPostgreSQL
PostgreSQL
 
PostgreSQL talk, Database 2011 conference
PostgreSQL talk, Database 2011 conferencePostgreSQL talk, Database 2011 conference
PostgreSQL talk, Database 2011 conference
 
Basic Introduction to Python Programming
Basic Introduction to Python ProgrammingBasic Introduction to Python Programming
Basic Introduction to Python Programming
 
Sql coding-standard-sqlserver
Sql coding-standard-sqlserverSql coding-standard-sqlserver
Sql coding-standard-sqlserver
 
Introducing Apache Spark's Data Frames and Dataset APIs workshop series
Introducing Apache Spark's Data Frames and Dataset APIs workshop seriesIntroducing Apache Spark's Data Frames and Dataset APIs workshop series
Introducing Apache Spark's Data Frames and Dataset APIs workshop series
 

More from thehoagie

Pair programming
Pair programmingPair programming
Pair programmingthehoagie
 
Null object pattern
Null object patternNull object pattern
Null object patternthehoagie
 
Big tables and you - Keeping DDL operatations fast
Big tables and you - Keeping DDL operatations fastBig tables and you - Keeping DDL operatations fast
Big tables and you - Keeping DDL operatations fastthehoagie
 
Angular.js - An introduction for the unitiated
Angular.js - An introduction for the unitiatedAngular.js - An introduction for the unitiated
Angular.js - An introduction for the unitiatedthehoagie
 
Regular expression presentation for the HUB
Regular expression presentation for the HUBRegular expression presentation for the HUB
Regular expression presentation for the HUBthehoagie
 
Converting your JS library to a jQuery plugin
Converting your JS library to a jQuery pluginConverting your JS library to a jQuery plugin
Converting your JS library to a jQuery pluginthehoagie
 
Active records before_type_cast
Active records before_type_castActive records before_type_cast
Active records before_type_castthehoagie
 

More from thehoagie (7)

Pair programming
Pair programmingPair programming
Pair programming
 
Null object pattern
Null object patternNull object pattern
Null object pattern
 
Big tables and you - Keeping DDL operatations fast
Big tables and you - Keeping DDL operatations fastBig tables and you - Keeping DDL operatations fast
Big tables and you - Keeping DDL operatations fast
 
Angular.js - An introduction for the unitiated
Angular.js - An introduction for the unitiatedAngular.js - An introduction for the unitiated
Angular.js - An introduction for the unitiated
 
Regular expression presentation for the HUB
Regular expression presentation for the HUBRegular expression presentation for the HUB
Regular expression presentation for the HUB
 
Converting your JS library to a jQuery plugin
Converting your JS library to a jQuery pluginConverting your JS library to a jQuery plugin
Converting your JS library to a jQuery plugin
 
Active records before_type_cast
Active records before_type_castActive records before_type_cast
Active records before_type_cast
 

Recently uploaded

M.Aathiraju Self Intro.docx-AD21001_____
M.Aathiraju Self Intro.docx-AD21001_____M.Aathiraju Self Intro.docx-AD21001_____
M.Aathiraju Self Intro.docx-AD21001_____Aathiraju
 
Manual sensor Zigbee 3.0 MOES ZSS-X-PIRL-C
Manual  sensor Zigbee 3.0 MOES ZSS-X-PIRL-CManual  sensor Zigbee 3.0 MOES ZSS-X-PIRL-C
Manual sensor Zigbee 3.0 MOES ZSS-X-PIRL-CDomotica daVinci
 
CIRCLE geometry lesson 2nd quarter grade 10
CIRCLE geometry lesson 2nd quarter  grade 10CIRCLE geometry lesson 2nd quarter  grade 10
CIRCLE geometry lesson 2nd quarter grade 10RicFernandez4
 
AWS for the beginning is cloud computing
AWS for the beginning  is  cloud computingAWS for the beginning  is  cloud computing
AWS for the beginning is cloud computingkajalghule1
 
5 Things You Shouldn’t Do at Salesforce World Tour Sydney 2024!
5 Things You Shouldn’t Do at Salesforce World Tour Sydney 2024!5 Things You Shouldn’t Do at Salesforce World Tour Sydney 2024!
5 Things You Shouldn’t Do at Salesforce World Tour Sydney 2024!XfilesPro
 
Manual Eurotronic Thermostatic Valve Comry Z-Wave
Manual Eurotronic Thermostatic Valve Comry Z-WaveManual Eurotronic Thermostatic Valve Comry Z-Wave
Manual Eurotronic Thermostatic Valve Comry Z-WaveDomotica daVinci
 
AUGMENTED REALITY (AR) IN DAILY LIFE: EXPANDING BEYOND GAMING
AUGMENTED REALITY (AR) IN DAILY LIFE: EXPANDING BEYOND GAMINGAUGMENTED REALITY (AR) IN DAILY LIFE: EXPANDING BEYOND GAMING
AUGMENTED REALITY (AR) IN DAILY LIFE: EXPANDING BEYOND GAMINGLiveplex
 
Bluetooth Low Energy(BLE) and beacons working
Bluetooth Low Energy(BLE) and beacons workingBluetooth Low Energy(BLE) and beacons working
Bluetooth Low Energy(BLE) and beacons workingshrey Ansh
 
My sample product research idea for you!
My sample product research idea for you!My sample product research idea for you!
My sample product research idea for you!KivenRaySarsaba
 
Zi-Stick UBS Dongle ZIgbee from Aeotec manual
Zi-Stick UBS Dongle ZIgbee from  Aeotec manualZi-Stick UBS Dongle ZIgbee from  Aeotec manual
Zi-Stick UBS Dongle ZIgbee from Aeotec manualDomotica daVinci
 
A Comprehensive Theoretical Overview of Self-Driving Car Technology
A Comprehensive Theoretical Overview of Self-Driving Car TechnologyA Comprehensive Theoretical Overview of Self-Driving Car Technology
A Comprehensive Theoretical Overview of Self-Driving Car TechnologyKumar Bipin
 
Cfgmgmtcamp 2024 — eBPF-based Security Observability & Runtime Enforcement wi...
Cfgmgmtcamp 2024 — eBPF-based Security Observability & Runtime Enforcement wi...Cfgmgmtcamp 2024 — eBPF-based Security Observability & Runtime Enforcement wi...
Cfgmgmtcamp 2024 — eBPF-based Security Observability & Runtime Enforcement wi...Raphaël PINSON
 
DNA LIGASE BIOTECHNOLOGY BIOLOGY STUDY OF LIFE
DNA LIGASE BIOTECHNOLOGY BIOLOGY STUDY OF LIFEDNA LIGASE BIOTECHNOLOGY BIOLOGY STUDY OF LIFE
DNA LIGASE BIOTECHNOLOGY BIOLOGY STUDY OF LIFEandreiandasan
 
Importance of magazines in education ppt
Importance of magazines in education pptImportance of magazines in education ppt
Importance of magazines in education pptsafnarafeek2002
 
"Journey of Aspiration: Unveiling the Path to Becoming a Technocrat and Entre...
"Journey of Aspiration: Unveiling the Path to Becoming a Technocrat and Entre..."Journey of Aspiration: Unveiling the Path to Becoming a Technocrat and Entre...
"Journey of Aspiration: Unveiling the Path to Becoming a Technocrat and Entre...shaiyuvasv
 
zigbee motion sensor user manual NAS-PD07B2.pdf
zigbee motion sensor user manual NAS-PD07B2.pdfzigbee motion sensor user manual NAS-PD07B2.pdf
zigbee motion sensor user manual NAS-PD07B2.pdfDomotica daVinci
 
Microsoft Azure News - Feb 2024
Microsoft Azure News - Feb 2024Microsoft Azure News - Feb 2024
Microsoft Azure News - Feb 2024Daniel Toomey
 
Q1 Memory Fabric Forum: SMART CXL Product Lineup
Q1 Memory Fabric Forum: SMART CXL Product LineupQ1 Memory Fabric Forum: SMART CXL Product Lineup
Q1 Memory Fabric Forum: SMART CXL Product LineupMemory Fabric Forum
 

Recently uploaded (20)

M.Aathiraju Self Intro.docx-AD21001_____
M.Aathiraju Self Intro.docx-AD21001_____M.Aathiraju Self Intro.docx-AD21001_____
M.Aathiraju Self Intro.docx-AD21001_____
 
Manual sensor Zigbee 3.0 MOES ZSS-X-PIRL-C
Manual  sensor Zigbee 3.0 MOES ZSS-X-PIRL-CManual  sensor Zigbee 3.0 MOES ZSS-X-PIRL-C
Manual sensor Zigbee 3.0 MOES ZSS-X-PIRL-C
 
CIRCLE geometry lesson 2nd quarter grade 10
CIRCLE geometry lesson 2nd quarter  grade 10CIRCLE geometry lesson 2nd quarter  grade 10
CIRCLE geometry lesson 2nd quarter grade 10
 
AWS for the beginning is cloud computing
AWS for the beginning  is  cloud computingAWS for the beginning  is  cloud computing
AWS for the beginning is cloud computing
 
5 Things You Shouldn’t Do at Salesforce World Tour Sydney 2024!
5 Things You Shouldn’t Do at Salesforce World Tour Sydney 2024!5 Things You Shouldn’t Do at Salesforce World Tour Sydney 2024!
5 Things You Shouldn’t Do at Salesforce World Tour Sydney 2024!
 
Manual Eurotronic Thermostatic Valve Comry Z-Wave
Manual Eurotronic Thermostatic Valve Comry Z-WaveManual Eurotronic Thermostatic Valve Comry Z-Wave
Manual Eurotronic Thermostatic Valve Comry Z-Wave
 
AUGMENTED REALITY (AR) IN DAILY LIFE: EXPANDING BEYOND GAMING
AUGMENTED REALITY (AR) IN DAILY LIFE: EXPANDING BEYOND GAMINGAUGMENTED REALITY (AR) IN DAILY LIFE: EXPANDING BEYOND GAMING
AUGMENTED REALITY (AR) IN DAILY LIFE: EXPANDING BEYOND GAMING
 
Bluetooth Low Energy(BLE) and beacons working
Bluetooth Low Energy(BLE) and beacons workingBluetooth Low Energy(BLE) and beacons working
Bluetooth Low Energy(BLE) and beacons working
 
My sample product research idea for you!
My sample product research idea for you!My sample product research idea for you!
My sample product research idea for you!
 
Zi-Stick UBS Dongle ZIgbee from Aeotec manual
Zi-Stick UBS Dongle ZIgbee from  Aeotec manualZi-Stick UBS Dongle ZIgbee from  Aeotec manual
Zi-Stick UBS Dongle ZIgbee from Aeotec manual
 
A Comprehensive Theoretical Overview of Self-Driving Car Technology
A Comprehensive Theoretical Overview of Self-Driving Car TechnologyA Comprehensive Theoretical Overview of Self-Driving Car Technology
A Comprehensive Theoretical Overview of Self-Driving Car Technology
 
Cfgmgmtcamp 2024 — eBPF-based Security Observability & Runtime Enforcement wi...
Cfgmgmtcamp 2024 — eBPF-based Security Observability & Runtime Enforcement wi...Cfgmgmtcamp 2024 — eBPF-based Security Observability & Runtime Enforcement wi...
Cfgmgmtcamp 2024 — eBPF-based Security Observability & Runtime Enforcement wi...
 
DNA LIGASE BIOTECHNOLOGY BIOLOGY STUDY OF LIFE
DNA LIGASE BIOTECHNOLOGY BIOLOGY STUDY OF LIFEDNA LIGASE BIOTECHNOLOGY BIOLOGY STUDY OF LIFE
DNA LIGASE BIOTECHNOLOGY BIOLOGY STUDY OF LIFE
 
Importance of magazines in education ppt
Importance of magazines in education pptImportance of magazines in education ppt
Importance of magazines in education ppt
 
"Journey of Aspiration: Unveiling the Path to Becoming a Technocrat and Entre...
"Journey of Aspiration: Unveiling the Path to Becoming a Technocrat and Entre..."Journey of Aspiration: Unveiling the Path to Becoming a Technocrat and Entre...
"Journey of Aspiration: Unveiling the Path to Becoming a Technocrat and Entre...
 
zigbee motion sensor user manual NAS-PD07B2.pdf
zigbee motion sensor user manual NAS-PD07B2.pdfzigbee motion sensor user manual NAS-PD07B2.pdf
zigbee motion sensor user manual NAS-PD07B2.pdf
 
Microsoft Azure News - Feb 2024
Microsoft Azure News - Feb 2024Microsoft Azure News - Feb 2024
Microsoft Azure News - Feb 2024
 
GTA 6.pdf
GTA 6.pdfGTA 6.pdf
GTA 6.pdf
 
Q1 Memory Fabric Forum: SMART CXL Product Lineup
Q1 Memory Fabric Forum: SMART CXL Product LineupQ1 Memory Fabric Forum: SMART CXL Product Lineup
Q1 Memory Fabric Forum: SMART CXL Product Lineup
 
My Country Mobile
My Country MobileMy Country Mobile
My Country Mobile
 

Database 101

  • 1. Database 101 A hands on tech talk
  • 2. What Is a Database? ● A collection of data that is organized in a way that makes retrieval relatively easy. ● Typically has logical groupings of schemas, and tables, (though document storage database do not) ● Typically relational, though not always (noSQL)
  • 3. Kinds of Databases ● Relational - MySQL, PostgreSQL, Sqlite ● Columnar - Redshift, Cassandra, BigQuery ● Document - MongoDB, CouchDB ● Key/value (in memory) - Memcached, Redis ● Full text (search engines) - Solr, ElasticSearch, SphinxSE
  • 4. MySQL ● Built by Michael Widenius ● Named after his daughter “My”. MariaDB, the successor to MySQL is named after his other daughter “Maria”. - Dad points! ● Purchased by Sun in 2008, by Oracle in 2010 ● Popular because its relational, open source, works on many OSes, and works well for the average use case
  • 5. Connecting ● mysql -h bookshelf.crm7sspivlug.us-east-1.rds.amazonaws.com -u persuade -p -D bookshelf ● -h is the hostname ● -u is the user to connect as ● -p is the password - but don’t type it as we will be prompted with a masked input ● -D is the database to use
  • 6. Looking around ● show databases ● show tables ● explain <table> ● ; vs G
  • 7. SELECT from a table ● A SELECT returns one or more results ● -SELECT ● * is a column matcher - we can also specify individual columns ● -LIMIT our result set SELECT * FROM books LIMIT 1;
  • 8. Exercises ● Select the names of all the publishers ● Select the titles of the first 5 books
  • 9. Functions ● Functions can either return a product on each row (e.g. length), or act as an aggregate function (e.g. avg). Note that aggregates collapse the result set SELECT count(1) FROM books; SELECT length(title) FROM books; SELECT avg(length(title)) FROM books;
  • 10. Conditions ● You can pass many expressions to WHERE including column names (e.g. name), or logic expressions (e.g. 1=1). ● Note that “=” is a comparison operator in SQL - not an assignment ● Other comparison operators include >, >=, <, <=, <>, !=, LIKE, IN, etc SELECT * FROM authors WHERE name LIKE '%Crichton'; SELECT * FROM authors WHERE name = 'Michael Crichton'; SELECT * FROM authors WHERE id = 28;
  • 11. Exercises ● Count the number of books with a rating of 4 or higher
  • 12. Ordering ● Ordering can be done a column, or an expression ● Can be ASC, or DESC ● Can use multiple columns SELECT name FROM publishers ORDER BY name ASC; SELECT name FROM publishers ORDER BY name DESC; SELECT name FROM publishers ORDER BY name, created_at ASC;
  • 13. Exercises ● Return the title of the oldest published book ● Return the top 5 newest books
  • 14. Joining to another table ● Relational tables excel at querying database that is normalized via JOIN ● There are several types of JOIN - inner, left outer, full outer SELECT title FROM books INNER JOIN authors on books.author_id = authors.id WHERE authors.name like '%Crichton'; SELECT * FROM authors LEFT OUTER JOIN books on books.author_id = authors.id WHERE books.id IS NULL; -- authors without a book (are they really authors then?!)
  • 15. Exercises ● Return all books with title, publisher name, and author name
  • 16. Grouping ● Aggregates results into groups ● A common use case is to count groups of results ● Use in conjunction with HAVING to use conditions on the resulting grups SELECT author_id, count(1) FROM books GROUP BY author_id SELECT author_id, count(1) as book_count FROM books GROUP BY author_id HAVING book_count > 10;
  • 17. Exercises ● Calculate the average rating of all books ● Calculate the breakdown of ratings ● Return the average rating of all books by author ● Return the top 5 most prolific author names, and a count of their books
  • 18. Autocommit SET AUTOCOMMIT = 0 BEGIN WORK INSERT INTO … SELECT * FROM … (includes record above) ROLLBACK WORK SELECT * FROM … (no longer includes record above)
  • 19. Turn Autocommit on now In your MySQL prompt, type: SET AUTOCOMMIT = 0; BEGIN WORK; We will use a transaction for the remainder of the slides to isolate changes to the database to just your session. If you need to rollback, do: ROLLBACK WORK; BEGIN WORK;
  • 20. INSERT ● Creates one or more new records ● Uses keyword INSERT ● Columns to insert are in parenthesis after table name ● VALUES keyword precedes values in the order of the columns listed ● Note that the primary key is automatically populated ● Ways to batch insert include LOAD INFILE, and INSERT INTO SELECT FROM INSERT INTO authors (name) VALUES ('Ben Simpson');
  • 21. UPDATE ● Modifies one or more existing records ● Can be used with conditions to update all records that match UPDATE authors SET name = 'Benjamin Lee Simpson' WHERE id = 1957;
  • 22. DELETE ● Deletes one or more records! ● PRO TIP! Write your statement first as a SELECT along with conditions before writing the word DELETE ● Note that DELETE from <table> is valid and will affect all records ● Primary keys will be preserved. To reset these use TRUNCATE DELETE FROM authors WHERE id = 1957;
  • 23. EXPLAIN ● Shows how the database is planning to execute your SQL statement. ● Useful for diagnosing why a query is slow. Helps find where an index would be beneficial* ● Prefix your query with keyword EXPLAIN ● Operations like filesort, copying to tmp table, etc are BAD EXPLAIN SELECT * FROM books WHERE rating > 4;
  • 24. Indexes ● Quick reference to a subset of records that match a common condition ● Foreign keys are often used in JOINs and typically benefit from indexing ● Indexes are not free - when a row is inserted/updated/deleted each index containing that row must be updated as well SHOW INDEXES FROM authors; CREATE INDEX books_rating on books (rating); EXPLAIN SELECT * FROM books WHERE rating > 4;