The new generation of database applications, such as design databases and scientific databases, and integrated, heterogeneous databases, requires more powerful query languages, able to support declarative programming, ad-hoc queries, complex objects, and other features not found in conventional DB languages. The GLUE/NAIL language provides, for the first time, all these capabilities in one language. It consists of a "semideclarative" language, GLUE, which is essentially logical rules connected by conventional sequencing, procedures, and modules, and NAIL, a completely declarative "view facility" for GLUE. To make the language efficient, certain optimization techniques, such as "magic sets" and "regular recursion optimization" must be used, and they must be extended to handle certain other features. These include negation in subgoals, following the well-founded semantics, aggregation (e.g., sum, avg.), and second-order logic to support sets and other object-oriented capabilities.