graphical query language algorithms - sql

My intention is to write a graphical query language where users can drag and drop criteria into a GUI query designer, then the designer will generate appropriate SQL query code and return the appropriate result set. Do you know where I can learn more about SQL code generation or if similar tools like this exist?

You may want to look at Microsoft Access's query designer to see how it works; it's relatively simple to use and you can compare how changes in the graphical representation affect the SQL statement and vice-versa. It should give you a pretty good view of how joins are translated into SQL.
(I cringe to suggest Microsoft Access for anything, but in this case, it might be useful to you).

Related

SQL Query to ER-type diagram

I'm getting started with SQL. So far, most of my experience is either with simple queries using C# (then performing operations on the data in C#), or designing [relatively] more complex queries with MS Access' design view.
I was recently assigned a task to review a relatively large query (hundreds of rows for a single Select), and there are minimal comments in the code. Also, I'm not familiar with the database being queried. Even though the syntax seems relatively simple, there are so many joins on top of joins that it's hard to wrap my head around.
Ideally, I'd like to have some graphical representation, similar to MS Access' design view. Note that my current position uses SSMS. I'm aware there are tools that can create SQL code from a UML diagram. I'm also aware that there are tools that can create a ER diagram from SQL code (see this question), which is closer to what I'm looking for. Are these ER tools my only option? As far as I can tell, the ER tools only show db relationships, and not specific queries with special types of joins or functions.
Thank you.
If you're using SQL Management Studio (I use 2008) you can just right click on the diagrams drop down in the explorer and click Create New Diagram. I'm not sure if this is what you are looking for but this is how I make all of my ER diagrams for my database.
No, It is impossible to do this by using standart tools like SQL Management Studio or others.
Maybe there exist some specific tools for this.
It is difficult and interesting task to generate joins diagram from SQL query (It is simple for "joins" but how does "union" and "where" clauses should be explained on diagram?)
And I think it doesnt simplify you task.

Fast way to build database alteration queries in SQL server

(sorry - the question wasn't originally clear. I'm not looking for the fastest ways to build insert, delete statements etc, but the fastest way to build queries to alter, for example, tables in the database e.g. adding or removing a column)
So, what's the fastest and best way to build database alteration queries in SQL server? I'm going down the manual route of writing the SQL as it's given me the best result in the past with the IBM Informix database. Points to note:
I've found that using the table designer in management studio is a poor method (in my experience this is too simplistic and GUI driven to be of use, and often requires tables to be completely rebuilt to work well)
The query designer only allows simple select, update delete etc. queries to be built
Any recommended tools that make this easier?
Am I missing something about the SQL management studio that I should know? Seems to create overly complex SQL for building a table, that's difficult to comprehend and edit
Do you write Java, .Net, Python or code by hand? What's the difference with SQL?
If there are patterns and code generation can be automated, (write and) use a code generator (but that goes for the other languages I listed too).
SQL is a lot more than just SELECT ... WHERE ... or UPDATE ... SET ... WHERE id = value.
for most kinds of scripts, I ask SMS to generate the initial script (create, drop, alter, select, insert, delete, execute, etc), and then tweak to taste. not sure if this is sufficient to your needs, but I find it useful
to generate scripts, Right click an object (table/view/sproc/fn/etc) and expand "Script ".
hope that helps.

Confused about the role of a query language

So, I haven't had any luck finding any articles or forum posts that have explained to me how exactly a query language works in conjunction with a general use programming language like c++ or vb. So I guess it wont hurt to ask >.<
Basically, I've been having a hard time understanding what the roles of the query language are ( we'll use SQL as an example for query language and VB6 for norm language) if i'm creating a simple database query that fills a table with normal information (first name, last name, address etc). I somewhat know the steps in setting up a program like this using ado objects for the connection and whatnot, but how do we decide which language of the 2 gets used for certain things ? Does vb6 specifically handle the basics like loops, if else's, declarations of your vars, and SQL specifically handles things like connecting to the database and doing the searching, filtering and sorting ? Is it possible to do certain general use vb6 actions (loops or conditionals) in SQL syntax instead ? Any help would be GREATLY appreciated.
SQL is a language to query a database. SQL is an ISO standard and relational database vendors implement to the ISO standard and then add on their own customizations. For example in SQL Server it is called T-SQL and in Oracle it is called PL-SQL. They both implement ISO standards and so each will have identical queries for a simple select like
select columname from tablename where columnname=1
However, each have different syntax for string functions, date functions, etc....
The ISO SQL standard by design is not a full procedural language with looping, subroutines, ect as in a full procedural language like VB.
However, each vendor has added capabilities to their version to add some of this functionality in.
For example both T-SQL and PL-SQL can "loop" through records using various constructs in their language.
There is also a difference when working with data that many developers are not well in tuned with. That is set based operations vs. procedural based.
Databases can work with procedural constructs but are often more performant with set based. A developer who is not versed in this concept may end up creating a very innefficient query. Here's an example of this discussion.
With any situation you have to weight out the pro's/con's of where it is best to do this work.
I tend to favor using procedural constructs such as loops in the language I am using over SQL. I find it easier to maintain and the language I am using offers more powerful syntax for me to get the job done.
However, I keep both options as a tool in the toolbox. For example, I have written data conversion scripts in SQL and in this case I have used the looping constructs in SQL.
Usually programming language are executed in the client side (app server too), and query languages are executed in the db server, so in the end it depends where you want to put all the work. Sometimes you can put lot of work in the client side by doing all the calculations with the programming language and other times you want to use more the db server and you end up using the query language or even better tsql/psql or whatever.
Relational databases are designed to manage data. In particular, they provide an efficient mechanism for managing memory, disk, and processors for large quantities of data. In addition, relational databases can handle multiple clients, guarantee transactional integrity, security, backups, persistence, and numerous other functions.
In general, if you are using an RDBMS with another language, you want to design the data structure first and then think about the API (applications programming interface) between the two. This is particularly true when you have an app/server relationship.
For a "simple" type of application, which uses a lot of data but with minimal or batch changes to it, you want to move as much of the processing into the database as is reasonable. Here are things you do not want to do:
Use queries to load things into arrays, and then do array manipulations at the language level. SQL provides joins for this.
Load data into an array and do manipulations and summaries on the array. SQL provides aggregations for this.
Save data into a file to have a backup. Databases provide backup mechanisms.
If you data fits into an array or on an Excel spreadsheet, it is often sufficient to get started with the data stored there. Only when you start to expand the needs (multiple clients, security, integration with other data) do the advantages of a database become more apparent.
These are just for guidance and to give you some ideas.
In terms of doing what where, do as much as is sensible in SQL (given it runs on a server) as you can.
So for instance don't do stuff like this (psuedo code)
foreach(row in "Select * from Orders")
if (row[CustomerID] = 876)
Display(row)
Do
foreach(row in "Select * from Orders where CustomerId = 876")
Display(row)
First it's likely Orders is indexed by CustomerID so it will find all 876s order way quicker.
Second to do the first one you just sucked every record in that table into the client's memory space probably across your network.
What language is used is essentially irrelevant, you could invent your own DBMS with it's own language.
It's where you do what processing that matters. It's Rule with exceptions, but the essential idea is let your backend do as much as it can.

Does MSSQL currently have any features similar to "PERIOD" columns in SQL2011?

I have a project where the PERIOD columns defined in the SQL2011 spec are the perfect solution. Unfortunately, I am forced to use MSSQL 2008R2 (or possibly MSSQL 2012) as my database, which does not support this feature.
Is there any proprietary feature that resembles the PERIOD features in SQL2011 currently in MSSQL? If not, any advice for the best way to try to implement something resembling it?
Take a look at Anchor Modelling. I know it's not exactly what you're looking for, (a PERIOD equivalent) but databases implemented as an Anchor model can include bi-temporal aspects. The generated SQL code when exporting the model primarily supports MS SQL. Oracle is available too but a lot of work when into optimizing the schema, trigger and view SQL code for MS SQL. Maybe it'll help, maybe you can see how they implemented bi-temporal data in a way that works really well with MS SQL.

SQL Query builder in Delphi

I need to give users the ability to build a simple SQL query against our database. Our application is written in Delphi.
I am assuming only moderate levels of knowledge by the user, but they need the ability to build a simple select statement to be able to query against a couple of tables. If I can make this easy for them, that would be most wondrous.
Does anyone know of a tool or a set of components that I can use to help the users build SQL SELECT statements...
I've used the DevExpress ExpressFilter control to allow users to specify SQL where constraints before.
I've come across FastQueryBuilder http://fast-report.com/en/products/visual-query-builder.html
and
OpenQueryBuilder http://fast-report.com/en/products/free-query-builder.html
(apparently differing versions of the same tool) at Fast-Report.
It looks like the right idea. Anyone have any experience with it?
I've used SimpleQuery for years and am quite happy with it. (http://devtools.korzh.com/eq/vcl/) It's not completely intuitive, but once you get the hang of it, you can present fields to the user from your database(s) and they can combine any series of them with ANDs and ORs to make very complex queries. You get a separate SQL window to save the resulting code. I make The resulting dataset available for printout with PrintDat! or to save as CSV data from the Woll2Woll Infopower grid that I use for display. I occasionally allow for XLS export too with XLSReadWrite. It's all quite automatic at this point. Just take the SimpleQuery example and mold that to your particular needs. You should be able to have something very usable in a day. There is a trial and an example free query builder standalone program that will give you an idea of the final look.
TMS Query Studio? Good value at 75 Euro..
Try EMS Advanced Query Builder.
is a powerful component suite for
Borland® Delphi® and C++ Builder®
intended for visual building SQL
statements for the SELECT, INSERT,
UPDATE and DELETE clauses. It allows
you to build new queries visually
and/or graphically represent the
existing queries in your own
applications. The suite includes
components for working with standard
SQL, MS SQL, InterBase/Firebird,
MySQL, PostgreSQL and many more
dialects. Advanced Query Builder
enables users to make up large and
complicated SQL queries with unions
and subqueries for different servers
without any knowledge of the SQL
syntax.
(source: sqlmanager.net)
(source: sqlmanager.net)
I think the most powerful one is ActiveQueryBuilder, which now included with Delphi IDE (Since 2007 I think)
(source: activequerybuilder.com)
I've written a couple of similar things in Delphi. It's easy enough to allow the user to pick table and column names by querying the metadata and using lists and tree views for display. The difficulty comes when trying to implement things like joins. I've never come up with a good interface for this, and in my experience neither have many of the major data tools players.
You may want to try this free builder:
GSC Visual Query Builder v.0.7
Visual Query Builder v.1.0
and some other in Torry:
http://www.torry.net/pages.php?id=546