private List<SQLPrimaryKey> addPrimaryKeys(List<SQLPrimaryKey> pks, boolean retrieveCD) throws InvalidObjectException,
    MetaException {
    List<MConstraint> mpks = new ArrayList<>();
    String constraintName = null;

    for (SQLPrimaryKey pk : pks) {
      final String catName = normalizeIdentifier(pk.getCatName());
      final String tableDB = normalizeIdentifier(pk.getTable_db());
      final String tableName = normalizeIdentifier(pk.getTable_name());
      final String columnName = normalizeIdentifier(pk.getColumn_name());

      // If retrieveCD is false, we do not need to do a deep retrieval of the Table Column Descriptor.
      // For instance, this is the case when we are creating the table.
      AttachedMTableInfo nParentTable = getMTable(catName, tableDB, tableName, retrieveCD);
      MTable parentTable = nParentTable.mtbl;
      if (parentTable == null) {
        throw new InvalidObjectException("Parent table not found: " + tableName);
      }

      MColumnDescriptor parentCD = retrieveCD ? nParentTable.mcd : parentTable.getSd().getCD();
      int parentIntegerIndex = getColumnIndexFromTableColumns(parentCD == null ? null : parentCD.getCols(), columnName);
      if (parentIntegerIndex == -1) {
        if (parentTable.getPartitionKeys() != null) {
          parentCD = null;
          parentIntegerIndex = getColumnIndexFromTableColumns(parentTable.getPartitionKeys(), columnName);
        }
        if (parentIntegerIndex == -1) {
          throw new InvalidObjectException("Parent column not found: " + columnName);
        }
      }
      if (getPrimaryKeyConstraintName(parentTable.getDatabase().getCatalogName(),
          parentTable.getDatabase().getName(), parentTable.getTableName()) != null) {
        throw new MetaException(" Primary key already exists for: " +
            TableName.getQualified(catName, tableDB, tableName));
      }
      if (pk.getPk_name() == null) {
        if (pk.getKey_seq() == 1) {
          constraintName = generateConstraintName(parentTable, tableDB, tableName, columnName, "pk");
        }
      } else {
        constraintName = normalizeIdentifier(pk.getPk_name());
        if (constraintNameAlreadyExists(parentTable, constraintName)) {
          String fqConstraintName = String.format("%s.%s.%s", parentTable.getDatabase().getName(),
              parentTable.getTableName(), constraintName);
          throw new InvalidObjectException("Constraint name already exists: " + fqConstraintName);
        }
      }

      int enableValidateRely = (pk.isEnable_cstr() ? 4 : 0) +
      (pk.isValidate_cstr() ? 2 : 0) + (pk.isRely_cstr() ? 1 : 0);
      MConstraint mpk = new MConstraint(
          constraintName,
          pk.getKey_seq(),
          MConstraint.PRIMARY_KEY_CONSTRAINT,
          null,
          null,
          enableValidateRely,
          parentTable,
          null,
          parentCD,
          null,
          null,
        parentIntegerIndex);
      mpks.add(mpk);

      // Add normalized identifier back to result
      pk.setCatName(catName);
      pk.setTable_db(tableDB);
      pk.setTable_name(tableName);
      pk.setColumn_name(columnName);
      pk.setPk_name(constraintName);
    }
    pm.makePersistentAll(mpks);
    return pks;
  }
Called right before an action that would drop a storage descriptor.
This function makes the SD's reference to a CD null, and then deletes the CD
if it no longer is referenced in the table.
@param msd the storage descriptor to drop
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java