public static void addConstraints(AbstractAlterTableWithConstraintsDesc desc, Hive db) throws HiveException {
    try {
      Constraints constraints = desc.getConstraints();
      // This is either an alter table add foreign key or add primary key command.
      if (CollectionUtils.isNotEmpty(constraints.getPrimaryKeys())) {
        db.addPrimaryKey(constraints.getPrimaryKeys());
      }
      if (CollectionUtils.isNotEmpty(constraints.getForeignKeys())) {
        try {
          db.addForeignKey(constraints.getForeignKeys());
        } catch (HiveException e) {
          if (e.getCause() instanceof InvalidObjectException && desc.getReplicationSpec() != null &&
              desc.getReplicationSpec().isInReplicationScope()) {
            // During repl load, NoSuchObjectException in foreign key shall
            // ignore as the foreign table may not be part of the replication
            LOG.debug("InvalidObjectException: ", e);
          } else {
            throw e;
          }
        }
      }
      if (CollectionUtils.isNotEmpty(constraints.getUniqueConstraints())) {
        db.addUniqueConstraint(constraints.getUniqueConstraints());
      }
      if (CollectionUtils.isNotEmpty(constraints.getNotNullConstraints())) {
        db.addNotNullConstraint(constraints.getNotNullConstraints());
      }
      if (CollectionUtils.isNotEmpty(constraints.getDefaultConstraints())) {
        db.addDefaultConstraint(constraints.getDefaultConstraints());
      }
      if (CollectionUtils.isNotEmpty(constraints.getCheckConstraints())) {
        db.addCheckConstraint(constraints.getCheckConstraints());
      }
    } catch (NoSuchObjectException e) {
      throw new HiveException(e);
    }
  }
Operation process of adding a new constraint.
ql/src/java/org/apache/hadoop/hive/ql/ddl/table/constraint/add/AlterTableAddConstraintOperation.java