A database stores data in a structured format, but a database is more valuable than merely storing structured data. When you log on to your favourite social media account with your credentials, you access your account within a millisecond, and you begin using it at once. Why aren’t logged into someone else’s account?
Databases let you search and quickly retrieve stored data, ensuring that the data is up to date and always accessible. Furthermore, data stored in a database is reliable, consistent, protected from failures, and secured from unauthorised access. Databases also allow for data that can change over time while keeping the records updated and other older data intact. A database enables several people to access and make changes simultaneously without one change overwriting the other. Databases are usually also accessible by making their data reliable to authorised users.
A Database Management System (DBMS) is software to create, define and manage databases. DBMS serves as an interface between the database and its end users or programs, allowing users to retrieve and update how information is organized and optimized. DBMS enables administrative operations such as performance monitoring, tuning, and managing databases. Examples include MySQL, Microsoft SQL Server, IBM DB2, Oracle Database, PostgreSQL, Apache Cassandra, MongoDB, and many others.
Interacting with a relational DBMS requires a programming language. Structured Query Language (SQL or SEQUEL) is a domain-specific programming language for storing, manipulating, retrieving, and managing data stored in a relational database management system (RDBMS). Structured Query Language is used to issue the CRUD commands (Create/Insert data, Read/Select data, Update data, Delete data) to a database. In this post, I will review how to create a database and a table in the database. I completed this project in MySQL workbench.
To follow along, you should have access to SQL and a DBMS installed locally or on the cloud.
Create a Database
SQL queries are usually written in capital letters to differentiate them from other text in the query. To create a database, use the SQL CREATE DATABASE keywords, followed by the database name and a semi-colon.
CREATE DATABASE table_name;
Create a database called employees.
CREATE DATABASE employees;
This query creates a database called employees.
Create a Table
A database contains tables that are related. To create a table, you need to connect to the database, use the CREATE TABLE keywords, and specify the table name, the column names, and the respective data types.
CREATE TABLE table_name ( column1 DATATYPE, column2 DATATYPE, column3 DATATYPE, …, columnN DATATYPE );
Using this syntax, create the employee records table in the employee database.
CREATE TABLE employeeRecords ( employeeID INT NOT NULL, firstName VARCHAR(20), lastName VARCHAR(20), department VARCHAR(50), startDate DATE, PRIMARY KEY (employeeID) ) ;
The employeeID column has an Integer data type and a constraint of NOT NULL and Primary Key. This means that the employeeID has a numeric value, must always have a value, and must be unique in the table. The firstName and the lastName values must be strings with a maximum character length of 20. The department value must also be a string with a character length of not more than 50. The date column has a Date data type, written as ‘YYYY-MM-DD’.
Insert Data into the Table
Now that you have created the employeeRecords table run a select all query to view the table.
SELECT * FROM employeeRecords;
The table has no rows yet, denoted by the NULL values.
You can start loading employee data into it. To insert data into a database, you start with the INSERT INTO statements, specify the column names you intend to load, and then add the column values using the VALUES keyword.
INSERT INTO table_name (column1, column2, column3, .., columnN) VALUES(value1, value2, value3, …, valueN);
Alternatively, you can insert values into all the columns in our table, omitting the column part of the query syntax.
INSERT INTO table_name VALUES(value1, value2, value3, …, valueN);
Insert the records of the first employee.
INSERT INTO employeeRecords VALUES(1, ‘Adeola’, ‘Mark’, ‘Marketing’, ‘2020-07-01’);
Insert the records of the second employee.
INSERT INTO employeeRecords VALUES(2, 'Ola', 'Davidson', 'Marketing', '2020-07-01');
Now run a select all statement to view the table.
There are now two rows in the employeeRecords table.
Insert Multiple Rows in a Table
As you've noticed, inserting data one at a time into the table will take a long time. Let’s try loading multiple rows into the table at a time.
INSERT INTO employeeRecords(employeeID, firstName, lastName, department, startDate) VALUES (3, 'Seyi', 'Wade', 'Business Developer', '2020-10-11'), (4, 'James', 'Fiyin', 'Human Capital', '2020-07-01'), (5, 'Yemi', 'Clover', 'Human Capital', '2020-10-01'), (6, 'John', 'Brown', 'Business Intelligence', '2021-01-01'), (7, 'Thomas', 'Coker', 'Networking', '2021-01-01');
Run a select query to view the newly created rows.
This post demonstrated how to create a database and a table and insert data into the table using SQL queries.
I found these resources helpful, you may find them beneficial too.
See you next time!