Voloda2
Voloda2

Reputation: 12607

Is exist a simple way to select all data from specific table

I have two tables: A, B. I need to select all data from B. I could do this like

SELECT id, b1, b2, ... b20 FROM A,B WHERE A.id = B.id; 

it's not cool solution. I will need to update this statement if I modify B's database. Is exist something like ?

SELECT *(B) 

It selected all data from B and didn't selected any data from A.

My databases

A
id
a1
a2
...
a20 

B
id
b1
b2
...
b20

Upvotes: 1

Views: 1163

Answers (4)

Rob
Rob

Reputation: 437482

I suspect that the others have sufficiently answered your question about selecting all fields from table B. That's great, as you really should understand the SQL basics. If they haven't, I'd also advise that you check out SQLite.org site for a clarification on SQL syntax understood by SQLite.

But, assuming you've answered your question, I just want to voice two words of caution about using the asterisk syntax.

  1. First, what if, at some later date, you add a column to B that is a big hairy blob (e.g. a multimegabyte image). If you use the * (or B.*) syntax to retrieve all of the columns from B, you may be retrieving a ton of information you might not need for your particular function. Don't retrieve data from SQLite if you don't need it.

  2. Second, is your Objective C retrieving the data from your select statement on the basis of the column names of the result, or based upon the index number of the column in question. If you're doing the latter, then using the * syntax can be dangerous, because you can break your code if the physical order of columns in your table ever changes.

Using named columns can solve the problem of memory/performance issues in terms of retrieving too much data, as well as isolating the Objective C from the physical implementation of the table in SQLite. Generally, I would not advise developers to use the * syntax when retrieving data from a SQL database. Perhaps this isn't an issue for a trivial project, but as projects become more complicated, you may want to think carefully about the implications of the * syntax.

Upvotes: 1

Simon Dorociak
Simon Dorociak

Reputation: 33505

So if you want create database seriously you shouldn't see on complexity of way but on efficiency and speed. So my advice to you is use JOIN that is the best solution for selecting data from two and more tables, because this way is fast as possible, at least for my more cleaner like for example inserted select.

You wrote: I need to select all data from B this means SELECT * FROM B not that you wrote.

My advice to your is using this:

SELECT * FROM A <INNER / LEFT / RIGHT / NATURAL> JOIN B ON A.id = B.id;

or to select specific columns

SELECT A.column1, A.column2, ... FROM A <INNER / LEFT / RIGHT / NATURAL> JOIN B ON A.id = B.id;

Note:

NATURAL JOIN will work in above example since the primary key and the foreign key in the two tables have the same name. So you must be very careful in using NATURAL JOIN queries in the absence of properly matched columns.

So you really should think about how you will create database and how you will working with database, how you will pulling data for View from database, how you will insert new potential data to database etc.

Regards man!

Upvotes: 1

Aitor
Aitor

Reputation: 3429

I don't know if the following query would work in sqlite, I know it works in Oracle, but you can give it a try...

SELECT B.* FROM A,B WHERE A.id = B.id; 

Upvotes: 0

Ruben
Ruben

Reputation: 2558

Use following query:

SELECT * FROM B;

or

SELECT * FROM B INNER JOIN A ON A.id = B.id;

if you want to join tables A and B.

Upvotes: 1

Related Questions