Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

MySQL

  • A true and tested relational database released in 1995.

    • Originally from Swedish company MySQL AB.

    • Later acquired by Sun who were then acquired by Oracle.

  • Freeware version and full enterprise versions for many platforms.

  • Lately also supports native JSON and NoSQL document store.

  • Easily installed directly in Windows, Mac, Linux, etc. or using Docker, Homebrew, etc.

Instructor’s database

  • An example of accessing an online, editable database.

  • Some VPN connections block the chosen port, e.g., NMBU’s network.

Revealing structure

If the tables of a database and their structure is given in advance, this can be queried.

Extract data

Example of the power of AI tools

  • Instructor wrote “Select only students whose name starts with ‘J’” with Copilot activated.

  • Copilot automatically generated the query.

  • Instructor corrected ‘name’ to ‘first_name’

  • It worked!

Add new data

  • If the primary key is duplicated, this will return an error

Removing data

  • Delete a single record or data that follows a pattern.

Commiting changes

  • All changes to the data using mysql.connector.cursor are local until committed.

Exercise

  1. Make a Python function that takes an SQL query as input, opens a connection, executes the statement, closes the connection and returns any results.

  2. Assume that password, username, database name, etc. are stored in a dictionary inside the function. Make another version of the function that takes a query and the dictionary as input and returns any results.

  3. Test both functions with SELECT and INSERT INTO statements.