Hi,
It explains insert operation in different ways with simple examples.
Example for Custom objects with external id:
Department__c departmentRef = new Department__c(
Department_Id__c='DEPT123');
Employee__c empObj = new Employee__c();
empObj.Employee_Name__c = 'Ritvik';
empObj.Basic__c = 20000;
empObj.HRA__c = 2000;
empObj.Gross__c = 25000;
empObj.Department__r = departmentRef;
insert empObj;
Reference:
https://trailhead.salesforce.com/content/learn/modules/apex_database/apex_database_dml
https://trailhead.salesforce.com/en/content/learn/modules/apex_database/apex_database_dml
https://developer.salesforce.com/docs/atlas.en-us.234.0.apexcode.meta/apexcode/langCon_apex_dml_section.htm