MySQL CRUD Basics - Creating a Database
Structured Query Language (SQL) is used to manage data in all relational database management systems such as MySQL, Oracle, SQL Server, and more. SQL standards are maintained by ISO. While most database products comply with the ISO standard, they also offer additional proprietary features. In this blog post we're going to restrict ourselves to the feature set offered by MySQL databases.
MySQL data types and operators
Each column in a database table is required to have a name and a data type. When designing databases we have to decide what kind of data will be stored in its database tables. Data types allow SQL to understand and interact with the stored data within database table columns. MySQL supports SQL data types in multiple categories: numeric types, date and time types, string (character and byte) types, spatial types, and the JSON data type.
Also, it's noteworthy to mention the operators that we can use in SQL expressions. Operators can be broken into three main categories: arithmetic, comparison, and logical.
Simple MySQL example
Imagine that you own a business. This business sells video games via its website and also via its physical store location. Customers can place orders with employees on-site or buy video games online without an employee's assistance. The order table links both employees and customers, however each order doesn't necessarily need an employee foreign key as mentioned before! A database and tables would help us manage all of this information:
How would we create and populate the database and tables pictured above? It's quite straightforward, don't worry. Also, I'll dive into how to read data and use update/delete statements to alter data.
Creating a database and tables
In order to start working with databases we need to create one. With the
CREATE DATABASE statement we can do just that. Interacting with the database is as simple as using the
USE DATABASE statement followed by any other statements you'd like.
CREATE TABLE statement allows us to create a table with a given name. There are many aspects to the
CREATE TABLE statement described under the following topics: table name, temporary tables, table cloning and copying, column data types and attributes, indexes/foreign keys/check constraints, table options, and table partitioning. Let's put this into practice and create a database with tables:
As you can see, each table has a unique primary key column to identify each table row. In the order table, there are two foreign key columns to link back to employees that sold customers video games at our physical store. Moreover, the order table tracks all orders for us including online orders.
Let's insert some data into these bad boys. This is done via the
INSERT INTO statement. When we use an
INSERT statement, we have to match up our data to conform to the data types and constraints we defined in our
CREATE TABLE statement.
Note the order I insert the data as well. I insert data into the employee and customer tables first since the order table contains rows that reference back to those entries.
We can query our database tables using the
SELECT queries can become quite complex when dealing with a production level database. Here, I demonstrate how you can get all the data from all three tables we created.
Updating and deleting data
For updating and deleting data in our database tables we can use the
DELETE statements. All that's needed is to specify the primary key of the row we'd like to either update or delete.
Sidenote: the variable "SQL_SAFE_UPDATES" is enabled by default. This prevents the MySQL database engine from executing
DELETE statements that don't include WHERE clauses that reference the primary key column(s). This saves you from affecting every row in the given table.
I hope this post helped boost your MySQL power level. If you like video format better, I have a YouTube playlist where I demonstrate everything in the MySQL workbench. MySQL is a database management system that allows us to interact with and design databases. With a solid foundation of MySQL basics, you can start designing and interacting with databases.
- “MySQL 8.0 Reference Manual.” MySQL, https://dev.mysql.com/doc/refman/8.0/en/.