You don't need to change the table and key structures. In SQL, the SELECT statement is used to get data, and you need to modify it just for the details(TSQLQuery), instead of:Thank you for reply, but it is not a solution. Your solution just show textual representation from related table, but foreign key was lost. For example if I need to implement CRUD functionality, I need foreign key too. User should see text, but I need foreign key for INSERT operation for example.
SELECT * FROM cities WHERE cities.COUNTRY_ID = :COUNTRY_ID
set:
SELECT m.CITY_ID, m.CITY_NAME, k.COUNTRY_NAME FROM cities m INNER JOIN countries k ON (m.COUNTRY_ID=k.COUNTRY_ID) WHERE m.COUNTRY_ID = :COUNTRY_ID
change the query as follows:Thank you, but I need to be more clear. My problem is not with SQL and retrieving data from a database. My problem is with TDBGrid. Let me simplify the question. Let's remove master table from UI and keep only cities. When a user added a new row to cities, he should choose country, from combobox or from custom dialog, it's not matter. country_name I need to display for a user in a list of countries, I cannot allow user to choose country by country_id, so I need country_name. But when I insert a new row to database table (in INSERT statement - insert into cities(city_id, city_name, country_id) values...), I need country_id, not country_name. My question is: how to do this? It seems to me every cell in country TDBGrid-column should know not only country_name but also country_id (foreign key for countries).
SELECT m.CITY_ID, m.CITY_NAME, m.COUNTRY_ID, k.COUNTRY_NAME FROM cities m INNER JOIN countries k ON (m.COUNTRY_ID=k.COUNTRY_ID) WHERE m.COUNTRY_ID = :COUNTRY_ID
you can also get the COUNTRY_ID from the master query
It seems to me every cell in country TDBGrid-column should know not only country_name but also country_id (foreign key for countries).
Thank you for clarification.It seems to me every cell in country TDBGrid-column should know not only country_name but also country_id (foreign key for countries).
Wrong.
Your Grid is a textual representation of your Data, and paweld showed you how to display Country-Name instead of CountryID in your Cities-Grid (CountryID being the ForeignKey in Cities)
When you ADD a new City, its ForeignKey is unknown at that moment.
What you need is the Primary-Key of your Country-Table to be available when the user selects a Country from your Combo-Box (at a guess stored in the Objects-List)
THIS IS AIRCODE
parmCityName:=txtCityName.Text; parmCountryID:=Integer(ComboCountries.Items[ComboCountries.ItemIndex].Objects[ComboCountries.ItemIndex]); SQL:='INSERT INTO cities("CityName", "CountryID") VALUES(:parmCityName, :parmCountryID)'; //Execute
Another approach might be to use a "real" Primary-/Foreign Key combination instead of a surrogate one. I'll let you guess what field that could be
... or have 2 fields:Thank you Nicole, I've already done exactly like you advise.
The foreign-key hidden or far to the right, the "beautiful" cell shown
if (Column.FieldName='id') then begin i:=DataSource1.DataSet.FindField('id').AsInteger; s_cell:=IntToStrF(i); end;
Both I did in the DBGrid DrawColumnCell event,
which must have ato make the text visible
Canvas.TextOut(Rect.Left + 2, Rect.Top + 1,s_cell);
If you do not know, how this event works exactly, please post again.