SELECT CASE WHEN
  (SELECT COUNT(*) FROM `mdp_stg_so`
   WHERE `tenant_id` IS NULL OR `tenant_id` IN (0,1,1300000000001)
      OR (`tenant_id`>0 AND `factory_id`=`tenant_id`))=0
  AND (SELECT COUNT(*) FROM `mdp_std_so`
   WHERE `tenant_id` IS NULL OR `tenant_id` IN (0,1,1300000000001)
      OR (`tenant_id`>0 AND `factory_id`=`tenant_id`))=0
  AND (SELECT COUNT(*) FROM `dwd_ship_trans`
   WHERE `tenant_id` IN (0,1,1300000000001) OR `factory_id`=`tenant_id`)=0
  AND (SELECT COUNT(*) FROM `dwd_requirement_examine_detail`
   WHERE `tenant_id` IN (0,1,1300000000001) OR `factory_id`=`tenant_id`)=0
  AND (SELECT COUNT(*) FROM `ado_smart_ops_kpi_atomic_day`
   WHERE `tenant_id` IN (0,1,1300000000001) OR `factory_id`=`tenant_id`)=0
  AND (SELECT COUNT(*) FROM `ado_module_dashboard_rebuild_job`
   WHERE `tenant_id` IN (0,1,1300000000001) OR `factory_id`=`tenant_id`)=0
  AND (SELECT COUNT(*) FROM information_schema.TABLE_CONSTRAINTS
   WHERE CONSTRAINT_SCHEMA=DATABASE()
     AND CONSTRAINT_NAME IN (
       'ck_mdp_stg_so_valid_scope',
       'ck_mdp_std_so_valid_scope',
       'ck_dwd_ship_trans_valid_scope',
       'ck_dwd_requirement_valid_scope',
       'ck_kpi_atomic_valid_scope',
       'ck_rebuild_job_valid_scope'))=6
THEN 1 ELSE 0 END AS ok;
