Jimmy
Jimmy

Reputation: 12497

Storing a database reference within the database

I want to be able to label the database with a single value, i.e its name, from within the database instead of my application, since it will always be one ID per database. For example, something like this:

DATABASE_A.sql

-- Database Name Table
CREATE TABLE database (
    name VARCHAR(10) NOT NULL UNIQUE,
);

CREATE TABLE item (
    id SERIAL PRIMARY KEY,
    name VARCHAR(10) NOT NULL UNIQUE,
);

Insert Into database (name) values ('A');

DATABASE_B.sql

-- Database Name Table
CREATE TABLE database (
    name VARCHAR(10) NOT NULL UNIQUE,
);

CREATE TABLE item (
    id SERIAL PRIMARY KEY,
    name VARCHAR(10) NOT NULL UNIQUE,
);

Insert Into database (name) values ('B');

This is because when they are combined and stored on a SOLR search server their ID is a combination of their database name and their item ID, such as this:

SOLR ITEM ID's

A1
A2
A3
B1

Is it ok to have a single table to define the prefix so that when I do the look up from my SQL website to SOLR I can just do the following query:

database (name) + item (id) = SolrID

Upvotes: 0

Views: 55

Answers (1)

user1596371
user1596371

Reputation:

I'd be more inclined to build a procedure in each database that contained the database ID, for example:

CREATE OR REPLACE FUNCTION solrid(IN local_id INTEGER, OUT result TEXT) AS $$
DECLARE
  database_id TEXT := 'A';
BEGIN
  result := database_id || local_id::TEXT;
END;
$$ LANGUAGE PLPGSQL;

Then you could write your select statement like:

SELECT solrid(id), name FROM item;

which seems to be a cleaner solution.

Upvotes: 2

Related Questions