SQLite User Forum

query result with table name
Login

query result with table name

(1) By tomasotosolini on 2022-06-16 10:19:11 [link] [source]

Hello,

I have this example tables:

Table1 =
(
    id integer not null,
    name string,

    PRIMARY KEY(id)
)

Table2 =
(
    id integer not null,
    refTable1 integer not null,
    name string,

    PRIMARY KEY(id),
    FOREIGN KEY(refTable1 ) REFERENCES Table1 (id)
)

I have a query like

select Table1.*, Table2.* from Table1 INNER JOIN Table2 on (Table1.id = Table2.refTable1);

If I use sqlite3 when executing the query, in the result set I see simply

id | name | id | refTable1 | name

I would like to know if there is some extra command so that in the result set i get

Table1.id | Table1.name | Table2.id | Table2.refTable1 | Table2.name

Does this exist?

To overcome this I tried a workaround: rewrite the query like this

select Table1.id "Table1.id", Table1.name "Table1.name", Table2.id "Table2.id", Table2.refTable1 "Table2.refTable1", Table2.name "Table2.name" from Table1 INNER JOIN Table2 on (Table1.id = Table2.refTable1);

and this actually shows

Table1.id | Table1.name | Table2.id | Table2.refTable1 | Table2.name

but this approach requres to construct the list of fields (so a lot of extra code if this can be done by SQLite) and I would like to avoid, unless necessary.

Thank you

(2) By Larry Brasfield (larrybr) on 2022-06-16 14:54:54 in reply to 1 [link] [source]

There is no recommended way, other than the one you seek to avoid, to get those fancy column names in your results. There is a discouraged way here which could fail with a future release of SQLite.

(3) By Chris Locke (chrisjlocke1) on 2022-06-18 14:39:53 in reply to 1 [link] [source]

I've a 'working with databases' book (forget the proper title) which recommends every field in the database has a unique name. This can be accomplished by prefixing the field with a shortened table name, eg, ordId if its an orders table, cliId if its a client, etc. This gets around this issue and does help if you're coding an application - nothing worse than seeing 'name' as a database field - name of what? Or worse, 'value'. Might as well call it 'Bob'.

(4) By tomasotosolini on 2022-07-07 07:19:53 in reply to 2 [link] [source]

Thank you

(5) By tomasotosolini on 2022-07-07 07:19:59 in reply to 3 [source]

Thank you

(6) By tomasotosolini on 2022-07-07 08:29:36 in reply to 3 [link] [source]

I only note that the approach where column names contain a prefix somehow related to table name has a negative effect, though: if table needs to be renamed, also column names should be renamed to maintain consistency.

(7) By Ryan Smith (cuz) on 2022-07-07 09:27:38 in reply to 6 [link] [source]

Quite right, the approach is discouraged these days as properties always relate to the entity they are in, so the field "make" may seem silly by itself, but inside a "vehicle" table it becomes "vehicle.make" which is no longer silly and indeed much preferred over "vehicle.vehicle_make" or an even more confusing "vehicle.vehMake".

That said, if your goal is to have unique column names across the entire framework, it's a different goal to naming simplicity, and in that case, Chris' suggestion is the only real solution.

The problem here is with the unique column-name goal, not its solution.

In my opinion column names (or property names in any entity) should be unique within the entity, but completely clear in meaning, recognizable throughout, no longer than is needed, but long enough to derive its full meaning easily, both by the current programmer(s) and by any future reader. Further, within any framework or project, all names should have a common format that is strictly adhered to.

This opinion may not be held by all, and I would lie if I claimed I have always adhered to it faithfully, but I find when I do adhere to it, projects are much more accessible/legible to all who work on it, upon design and thereafter.

(8) By Chris Locke (chrisjlocke1) on 2022-07-07 12:11:45 in reply to 7 [link] [source]

no longer than is needed, but long enough to derive its full meaning easily,

Does anyone else want to grab a bat and cave in someone's head when they see a field called 'telno' or even 'phoneno' instead of 'telephoneNumber'. I groan at 'membershipno' (they typed out membership, but can't be bothered to type number). Consistency is also key. I maintain (or cry regularly over) a system where we call one particular field a 'gdslocator', but the system we talk to called it a 'recordlocatornumber'. Two different field names ... for the same thing. Why ... just ... argh! This means in other tables we have variants like 'gdsNo' and 'rlNo'. Just ... no. Never. Never do this. Ever.

(9) By Keith Medcalf (kmedcalf) on 2022-07-07 15:21:42 in reply to 7 [link] [source]

The origin of this practice (diddling field names to include contractions of the containing entity name) was a side effect of a bunch of early software that used a single namespace for all entity-names (tables, columns, indexes, etc). This meant that you could not have fields with the same name in two different tables/records.

By the mid-1970's all such software had been fixed.

However, like avoiding ethernet auto-configuration because once upon a time some company (that shall remain nameless) really buggered up their implementation, and that companies products were so widely used that it broke auto-configuration world-wide; that issue was also fixed long long ago (by the 1995 if not before).

However, there are classes of people that still insist on doing both of these things, to their own detriment, just because there is an "old wives tale" about some idiot implementation of whatever they are doing. Just like the "old wives tale" about using floating-point for money should not be done, when in fact it is a moron problem: Moron's should not attempt to use floating point (or computers, really) to do things they do not understand.

(10) By Harald Hanche-Olsen (hanche) on 2022-07-07 16:04:57 in reply to 9 [link] [source]

Agreed, but I usually make an exception for primary keys, preferring something more specific than id for the name of the field.

After all, I find SELECT … FROM foo JOIN bar USING(fooid) easier to read than SELECT … FROM foo JOIN bar ON foo.id=bar.fooid.

(11) By Ryan Smith (cuz) on 2022-07-07 16:34:38 in reply to 10 [link] [source]

I find SELECT … FROM foo JOIN bar USING(fooid) easier to read than SELECT … FROM foo JOIN bar ON foo.id=bar.fooid.

A very good example of different strokes for different folks.

While I absolutely 100% see your reasoning, and cannot fault it, my preference is quite the opposite.

To me,

SELECT foo.id, bar.id, moe.id, ... 
  FROM foo 
  JOIN bar ON bar.foo_id = foo.id
  JOIN chu ON chu.bar_id = bar.id
  JOIN moe ON moe.bar_id = bar.id AND moe.xxx = 1
 WHERE...

reads significantly easier (with less cognitive load) than

SELECT foo.foo_id, bar.bar_id, moe.moe_id, ... 
  FROM foo 
  JOIN bar USING (foo_id)
  JOIN chu USING (bar_id)
  JOIN moe ON moe.bar_id = bar.bar_id AND moe.xxx = 1
 WHERE...

but I'll immediately concede that this is probably more to do with my being used to it/habit than with any demonstrable advantage.

I'd further say, to Chris' point, that both ways, if at least done consistently and adhering to form, would indeed be a pleasure of a project to work on.

(12) By Gary (1codedebugger) on 2022-07-07 16:56:18 in reply to 9 [link] [source]

I just wanted to say that I appreciate these perspectives that you share here. I'm not always clear when you're being humorous, expressing dissatisfaction, or stating the plain truth of the matter; but I almost always appreciate the broad perspective you provide.

I'm an old man learning new things for what might be referred to as old purposes. So, I've been a bit overwhelmed the past four years trying to figure out in what tools to accomplish my tasks; and I keep coming back to SQLite, Tcl, and C. I'm quite weary of reading how some youngster sandwiched together eight enormous applications, the inner workings of which he has no understanding whatsoever, and developed something "new."