Why isn't SQL ANSI-92 standard better adopted over ANSI-89?

后端 未结 16 1755
日久生厌
日久生厌 2020-11-22 10:59

At every company I have worked at, I have found that people are still writing their SQL queries in the ANSI-89 standard:

select a.id, b.id, b.address_1
from          


        
相关标签:
16条回答
  • 2020-11-22 11:27

    I was taught ANSI-89 in school and worked in industry for a few years. Then I left the fabulous world of DBMS for 8 years. But then I came back and this new ANSI 92 stuff was being taught. I have learned the Join On syntax and now I actually teach SQL and I recommend the new JOIN ON syntax.

    But the downside that I see is correlated subqueries don't seem to make sense in the light of ANSI 92 joins. When join information was included in the WHERE and correlated subqueries are "joined" in the WHERE all seemed right and consistent. In ANSI 92 table join criteria is not in the WHERE and subquery "join" is, the syntax seems inconsistent. On the other hand, trying to "fix" this inconsistency would probably just make it worse.

    0 讨论(0)
  • 2020-11-22 11:28

    Reasons people use ANSI-89 from my practical experience with old and young programmers and trainees and fresh graduates:

    • They learn SQL from existing code they see (rather than books) and learn ANSI-89 from code
    • ANSI-89 because is less typing
    • They do not think about it and use one or other style and do not even know which of both is considered new or old and do not care either
    • The idea that code is also a communication to the next programmer coming along maintaining the code does not exist. They think they talk to the computer and the computer does not care.
    • The art of "clean coding" is unknown
    • Knowledge of programming language and SQL specifically is so poor that they copy and paste together what they find elsewhere
    • Personal preference

    I personally prefer ANSI-92 and change every query I see in ANSI-89 syntax sometimes only to better understand the SQL Statement at hand. But I realized that the majority of people I work with are not skilled enough to write joins over many tables. They code as good as they can and use what they memorized the first time they encountered a SQL statement.

    0 讨论(0)
  • 2020-11-22 11:30

    Well the ANSI092 standard includes some pretty heinous syntax. Natural Joins are one and the USING Clause is another. IMHO, the addition of a column to a table shouldn't break code but a NATURAL JOIN breaks in a most egregious fashion. The "best" way to break is by compilation error. For example if you SELECT * somewhere, the addition of a column could fail to compile. The next best way to fail would be a run time error. It's worse because your users may see it, but it still gives you a nice warning that you've broken something. If you use ANSI92 and write queries with NATURAL joins, it won't break at compile time and it won't break at run time, the query will just suddenly start producing wrong results. These types of bugs are insidious. Reports go wrong, potentially financial disclosure are incorrect.

    For those unfamiliar with NATURAL Joins. They join two tables on every column name that exists in both tables. Which is really cool when you have a 4 column key and you're sick of typing it. The problem comes in when Table1 has a pre-existing column named DESCRIPTION and you add a new column to Table2 named, oh I don't know, something innocuous like, mmm, DESCRIPTION and now you're joining the two tables on a VARCHAR2(1000) field that is free form.

    The USING clause can lead to total ambiguity in addition to the problem described above. In another SO post, someone showed this ANSI-92 SQL and asked for help reading it.

    SELECT c.* 
    FROM companies AS c 
    JOIN users AS u USING(companyid) 
    JOIN jobs AS j USING(userid) 
    JOIN useraccounts AS us USING(userid) 
    WHERE j.jobid = 123
    

    This is completely ambiguous. I put a UserID column in both Companies and user tables and there's no complaint. What if the UserID column in companies is the ID of the last person to modify that row?

    I'm serious, Can anyone explain why such ambiguity was necessary? Why is it built straight into the standard?

    I think Bill is correct that there is a large base of developer who copy/paste there way through coding. In fact, I can admit that I'm kind of one when it comes to ANSI-92. Every example I ever saw showed multiple joins being nested in parentheses. Honesty, that makes picking out the tables in the sql difficult at best. But then an SQL92 evangilist explained that would actually force a join order. JESUS... all those Copy pasters I've seen are now actually forcing a join order - a job that's 95% of the time better left to optimizers especially a copy/paster.

    Tomalak got it right when he said,

    people don't switch to new syntax just because it is there

    It has to give me something and I don't see an upside. And if there is an upside, the negatives are an albatross too big to be ignored.

    0 讨论(0)
  • 2020-11-22 11:30

    I can't speak for all schools but at my university when we were doing the SQL module of our course, they didn't teach ANSI-92, they taught ANSI-89 - on an old VAX system at that! I wasn't exposed to ANSI-92 until I started digging around in Access having built some queries using the query designer and then digging into the SQL code. Realising I had no idea how it was completing the joins, or the implications of the syntax I started digging deeper so I could understand it.

    Given that the available documentation isn't exactly intuitive in a lot of cases, and that people tend to stick to what they know and in many cases don't strive to learn any more than they need in order to get their job done, it's easy to see why adoption is taking so long.

    Of course, there are those technical evangelists that like to tinker and understand and it tends to be those types that adopt the "newer" principles and try to convert the rest.

    Oddly, it seems to me that a lot of programmers come out of school and stop advancing; thinking that because this is what they were taught, this is how it's done. It's not until you take off your blinkers that you realise that school was only meant to teach you the basics and give you enough understanding to learn the rest yourself and that really you barely scratched the surface of what there is to know; now it's your job to continue that path.

    Of course, that's just my opinion based on my experience.

    0 讨论(0)
提交回复
热议问题