r/AskProgramming • u/tasstoss • Jun 13 '26
Databases I need help with SQL
(For context I'm a digital development trainee first year , preparing for this subject exam called manipulating databases)
I'm just completely and utterly frustrated with this language , so the most used command in SQL is SELECT right ? DDL DML that's just 20% of the whole thing , grabbing data from the database and knowing how to structure those queries and understanding the schema is what counts more, cause I have no problem creating tables, adding a new colum populating the tables... Etc but even a baby can learn that crap in a day , what Im struggling with is grabbing data and understanding the relationships between the tables , do you guys have any valuable advice that could help make it click ? Or perhaps some exercises that start from beginner all the way to advanced level select queries with detailed explanations , and thank you very much !!!!
View all comments
Show parent comment
1
u/johnpeters42 Jun 13 '26
It may help to draw a graph of the tables, their columns, and their relationships. (Some software tools will actually do this on-screen.) Then it becomes pretty obvious: "okay, I want puppy names so I need to include the Puppies table, I need trick names so I need the Tricks table, and I need to cross-reference them so I need the PuppyTricks table."
Typical practice is to define some primary keys, unique keys, and indexes per table, and then some foreign keys from one table to another, e.g. PuppyTricks.PuppyID -> Puppies.PuppyID:
* You can't insert a row into PuppyTricks with a PuppyID that doesn't already exist in Puppies, SQL will throw an exception if you try.
* You also can't delete a row from Puppies whose PuppyID also exists in PuppyTricks, SQL will throw an exception if you try. Or alternatively, you could set it up so that deleting such a row from Puppies also automatically deletes any matching rows in PuppyTricks.
(Sometimes you don't want to delete stuff anyway, just flag it as inactive, but you still have data for what it used to look like. This depends on context.)