Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. MySQL view definition oddity

MySQL view definition oddity

Scheduled Pinned Locked Moved Database
databasequestioncsharpmysqlsql-server
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #1

    Much of my current job involves combining data from various sources into one SQL Server database. One of the sources is a MySQL database and I have noticed something odd. One of the first steps I take when I get access to a new data source is to investigate the schema. As a fan of ADO.net and database-agnosticism, my main tools for doing this are: System.Data.Common.DbConnection.GetSchema() , System.Data.Common.DbConnection.GetSchema(string) , and System.Data.IDataReader.GetSchemaTable() which every compliant ADO.net provider should implement. The way I use GetSchemaTable is to cobble up a SELECT * FROM name WHERE 0=1 query for each table and view, call ExecuteReader, call GetSchemaTable, and display the results. The WHERE 0=1 clause is intended to keep the server from doing a bunch of needless work, but just return an empty dataset. This works, but I have since found that it hides problems -- mostly related to views that are out of sync with their sources. So I removed the WHERE clause and was surprised that one column in one of the MySQL views was reported as Int64 rather than Int32 as it is when I use the WHERE clause. I got the latest version of the MySQL Connector/net and the situation persists. What I then found, by using GetSchema("VIEWS"), is that the column is defined like IF((id IS NOT NULL),id,NULL) AS 'id' (id is involved in a JOIN among several tables). I don't have access the tables so I don't know what type the actual field is. And I see comments on the Web that IF can return unexpected types. I have been unable to reproduce the oddity in my own test database. But my question is, "why use the IF function in this case?" How is this better than just returning id whether it's NULL or not? Is there some historical reason?

    J 1 Reply Last reply
    0
    • P PIEBALDconsult

      Much of my current job involves combining data from various sources into one SQL Server database. One of the sources is a MySQL database and I have noticed something odd. One of the first steps I take when I get access to a new data source is to investigate the schema. As a fan of ADO.net and database-agnosticism, my main tools for doing this are: System.Data.Common.DbConnection.GetSchema() , System.Data.Common.DbConnection.GetSchema(string) , and System.Data.IDataReader.GetSchemaTable() which every compliant ADO.net provider should implement. The way I use GetSchemaTable is to cobble up a SELECT * FROM name WHERE 0=1 query for each table and view, call ExecuteReader, call GetSchemaTable, and display the results. The WHERE 0=1 clause is intended to keep the server from doing a bunch of needless work, but just return an empty dataset. This works, but I have since found that it hides problems -- mostly related to views that are out of sync with their sources. So I removed the WHERE clause and was surprised that one column in one of the MySQL views was reported as Int64 rather than Int32 as it is when I use the WHERE clause. I got the latest version of the MySQL Connector/net and the situation persists. What I then found, by using GetSchema("VIEWS"), is that the column is defined like IF((id IS NOT NULL),id,NULL) AS 'id' (id is involved in a JOIN among several tables). I don't have access the tables so I don't know what type the actual field is. And I see comments on the Web that IF can return unexpected types. I have been unable to reproduce the oddity in my own test database. But my question is, "why use the IF function in this case?" How is this better than just returning id whether it's NULL or not? Is there some historical reason?

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #2

      Is this id existing in more than one table and is therefore used as a join condition in the view? About your question, why the use of the IF function, I can only speculate that the original developer had a brain fart.

      Be excellent to each other. And... PARTY ON, DUDES! Abraham Lincoln

      P 1 Reply Last reply
      0
      • J Jorgen Andersson

        Is this id existing in more than one table and is therefore used as a join condition in the view? About your question, why the use of the IF function, I can only speculate that the original developer had a brain fart.

        Be excellent to each other. And... PARTY ON, DUDES! Abraham Lincoln

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Jörgen Andersson wrote:

        used as a join condition in the view?

        Yes.

        Jörgen Andersson wrote:

        had a brain fart

        That's what I'm thinking, but I'm no MySQL expert.

        J 1 Reply Last reply
        0
        • P PIEBALDconsult

          Jörgen Andersson wrote:

          used as a join condition in the view?

          Yes.

          Jörgen Andersson wrote:

          had a brain fart

          That's what I'm thinking, but I'm no MySQL expert.

          J Offline
          J Offline
          Jorgen Andersson
          wrote on last edited by
          #4

          PIEBALDconsult wrote:

          but I'm no MySQL expert

          Neither am I, I have never worked with MySQL. But I have an idea that might explain the weird behavior. In Oracle 10g you could define a query as:

          SELECT ID
          FROM A,B
          WHERE A.ID = B.ID

          Note that you didn't need to specify which table the column should come from if it existed in the join condition. What happens if the ID Columns have different types, will there be an implicit cast? And which table will the selected column come from? In Oracle 11g you always have to specify the table.column

          Be excellent to each other. And... PARTY ON, DUDES! Abraham Lincoln

          P 1 Reply Last reply
          0
          • J Jorgen Andersson

            PIEBALDconsult wrote:

            but I'm no MySQL expert

            Neither am I, I have never worked with MySQL. But I have an idea that might explain the weird behavior. In Oracle 10g you could define a query as:

            SELECT ID
            FROM A,B
            WHERE A.ID = B.ID

            Note that you didn't need to specify which table the column should come from if it existed in the join condition. What happens if the ID Columns have different types, will there be an implicit cast? And which table will the selected column come from? In Oracle 11g you always have to specify the table.column

            Be excellent to each other. And... PARTY ON, DUDES! Abraham Lincoln

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Jörgen Andersson wrote:

            you always have to specify the table.column

            Yes, but that's not the issue, so I left it out for simplicity.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups