Difference between InstallSchema and InstallData

First both InstallSchema And InstallData both method are called when you install any new module

Schema setup scripts change database schema, they create or change needed database tables. If module is installing, Setup\InstallSchema::install() is executed.

It means it create new table and its fields with its structure.

InstallData setup scripts contain entries module needs to insert into database. Attributes that come with Magento by default,Cms pages, various default groups and roles, are all examples of data setup.

Data setup is executed after Schema setup, they function in a similar fashion.

So come to the conclustion, Main difference between both is InstallSchema is used to make table and its structure. While InstallData is used to insert/add data into table.


There are following difference,

InstallSchema

InstallSchema.php purpose is to create new table/attribute/fields in Database

InstallData

InstallData.php purpose is to insert Data/value into database.


Magento2 uses Four Type of files for the Initial operations related to database that are executed just with the installation process of module (These files runs only once through out of their existence). Those files are as follows

  1. InstallSchema.php
  2. InstallData.php
  3. UpgradeSchema.php
  4. UpgradeData.php

All of these files are present ate Magento_root/{app/Vendor}/{Vendor_Name}/{Module_Name}/Setup Name space of your magento module. Here I have described all the files with their use.

InstallSchema.php

This file is executed first just after your modules registration (Means just after your module & its version entries are done in to the table -> setup_module). This file is used to create tables with their columns attribute into your database that are later used by the new installed module.

InstallData.php

This file is executed after InstallSchema.php. It is used to add data to the newly created table or any existing table.

UpgradeSchema.php

This file comes with the module & runs only then, if you are already having that modules previous version installed in your magento(Means it has entry of its previous version into the table -> setup_module). It is used to manipulate the table related to the module(Means it is used to alter the table schema means columns attribute & to add new column into that table).

UpgradeData.php

This file runs after UpgradeSchema.php. It is having the same concept as InstallData.php has but using this file you can change/alter the database contents without the use of model files. You can also use this file to add new content to the database same us InstallData.php. But same like UpgradeSchema.php it will also runs only then if you are having that modules previous version installed in your magento.

As I said in starting these files runs only once so the basic use of this files is to prepare the tables & data for the Module which is been get used by the module through out its existence.

Note: Magento uses these files to create tables that are used into Magentos functionalities & to add the contents like cms_contents, customer_groups & attributes, Products_Types & attributes, Taxation groups etc.