How to in Mockaroo: create related datasets with a foreign key

by Elena Raluca Mazareanu

In this article, I will show you how to create two datasets that are related by a foreign key column using Mockaroo.

Mockaroo.com is an online tool for generating realistic test data for use in software testing, database population, and other purposes. It allows users to create custom datasets with various data types and formats, such as CSV, SQL, JSON, and Excel, and customize them to meet their specific needs. Mockaroo offers a wide range of data types, including names, addresses, dates, phone numbers, and more, as well as the ability to generate data in multiple languages and locales. Additionally, it provides features such as data validation, formula-based data generation, and the ability to save and share datasets. It is commonly used by developers, QA testers, and database administrators to quickly generate large volumes of test data for their applications.

Assuming we wish to establish a correlation between employees and companies, we can achieve this by creating two schemas - Employees and Companies. Each employee is associated with a single company, and the employees’ table contains a foreign key (company id) that references the primary key (id) in the companies’ table.

We will begin by creating a schema for the companies table. From the Schemas menu, click on Create a Schema. The id column will be assigned a row number type, and we will include additional details such as company name, country, and city.

This schema will be saved as Companies, and we will then generate a dataset for the companies table by selecting "create dataset" from the triangle icon at the bottom of the page. The data will be generated and saved as a dataset within Mockaroo, and we can preview the first 20 rows of the company’s data.

We can then create a schema for the employees’ table, building upon the previous schema and adding a foreign key for the company id. The dataset column type will be selected along with the Companies dataset, and the id column will be chosen as the primary key.

This schema will be saved as Employees, and we can preview the results to ensure that the company id foreign key is pulling data from the Companies dataset. We can now download and upload the data into our database, with the two CSV datasets related by a consistent foreign key.

Avatar

Elena Raluca Mazareanu

Tue 04 Apr 2023

Wed 29 Mar 2023