0

So this is more of a theory question than practical but I'd appreciate any help. My friend and I are doing a project to learn new stuff and have fun. He's a coder and I like to think that I'm good with databases. Today an issue arose because he said that he thinks we shouldn't have repetitive queries just for different tables and had an idea of using ? or some other symbol in my queries so he can replace it with appropriate word in code.

However, I don't agree, partly because I like having all of my queries in pure SQL and partly because I don't think that it's a good permanent solution. For an example what if a table gets new fields? The query is automatically not valid anymore.

Even if there are tables that are relatively similar and could use the same query is his idea a good practice or am I right about separating the queries?

Sorry if this is not the right place to post the question since it's not really a specific problem.

1
  • If these queries are returning different data for different purposes, I'd suggest making them distinct stored procedures. He shouldn't be doing many (or any, really) direct queries via his code. Commented Apr 3, 2015 at 13:31

1 Answer 1

0

When you add a column to a table, most, if not all, queries referencing the table will need changing. That is the cost of adding a column.

Using ? is good. It (or the equivalent) is available in most client languages. But... It only lets you change numbers and strings, not table names. Numbers and strings are the flexibility you need. A parameterized table name implies that the schema is poorly designed -- It is usually bad to have multiple tables with the same sets of columns.

2
  • What I meant is using the ? in my queries which would then be changed to the table name in code before querying the database. For example I had two tables: cities and schools. For now, both of them have ID and schoolName or cityName depending on the table. The query for this would just be select * from some table which would then be added in code. I didn't like this solution though. Commented Apr 3, 2015 at 18:49
  • Bad solution. (1) ? would put quotes around table name, giving you a syntax error, and (2) you should not use "*" without understanding the ramifications when you do add a new column.
    – Rick James
    Commented Apr 3, 2015 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.