You can use the wp_insert_term()
function to achive this. First create the parent category, then create a child category.
$parent = wp_insert_term(
'Parent Category', // category name
'category', // taxonomy
array(
'description' => 'Category description', // optional
'slug' => 'parent-category', // optional
)
);
wp_insert_term(
'Child Category', // category name
'category', // taxonomy
array(
'description' => 'Child category description', // optional
'slug' => 'child-category', // optional
'parent' => $parent['term_id'], // set it as a sub-category
)
);