SQL: source code / under the hood - sql

I'm trying to gain a deeper understanding of SQL, and I was just wondering - what was SQL originally written in? I did some research, and according to this stackoverflow page, it seems that SQL was originally written in C. If so, is there any way I could access the original source code to see how SQL really works under the hood - its algorithms, data structures etc? If SQL was indeed written in C, its inner workings must be imperative since C is an imperative language, and since I am more versed in C++ than in SQL, it would be really rewarding to explore SQL in terms of an imperative paradigm. Thanks in advance for any insight you may have!

SQL is a language definition, which itself is not written in anything but plain text.
Also, you can implement a compiler/engine for SQL or Java or whatever high level language in C++, C, Assembly, or even directly in machine code if you like, so the language in which it is implemented doesn't tell you anything about its possibilities.

Which SQL? There are a lot of different competing SQL database flavors out there: Sql Server, Oracle, MySql, PostgreSQL, Access, SqlLite, Sybase... and that's just the tip of the iceberg. Fortunately, several of those are open source... you can go look at the code for yourself.
You can also read the Sql Language Specifications.

Related

Why is SQL called a data sublanguage?

Recently I have read (in a PDF document - SQL for dummies) that SQL is actually a data sublanguage and not a programming language like C++ or Java or C# and right now I am a bit confused, because since you can develop things through SQL, I thought it is similar to other programming languages.
Could anyone explain to me what is the difference? Thanks
Try to write a simple but non-trivial application using nothing but standard SQL that asks the user to input their name, and outputs "Hello, ."
Maybe you could do it with some vendor-specific extensions, but then it wouldn't be standard SQL.
SQL is designed to be a domain-specific language for database queries. It's meant to be used in combination with a more fully-featured language. The SQL standard defines ways that you can write lines of SQL within the code file of C, C++ or other languages. There's no standard way to write a full standalone app using just SQL.
Read the standard. The SQL/PSM part defines a full-blown programming language with loops and IF-THEN-ELSE and what have you. SQL/PSM was initially amended to the existing standard in 1996 and formally included in SQL:1999.
As Bill Karwin hinted at, SQL does not have features for UI interaction, but then the very same thing is true about the java language (no, the swing package is not part of the language), and the COBOL language, and the ALGOL language, and many many others.
SQL began life as a data sublanguage. That history is >20yrs behind us. (The "data sublanguage" portion is still the most-used and most-useful part, but that does not change the fact that technically speaking, SQL-the-language has everything it takes to be regarded as a full-blown programming language.)
According to wikipedia a sublanguage
the term "sublanguage", first used for this purpose by E. F. Codd in 1970, refers to a computer language used to define or manipulate the structure and contents of a relational database management system (RDBMS). Typical sublanguages associated with modern RDBMS's are QBE (Query by Example) and SQL (Structured Query Language).
that means that sublanguages cannot be used to develop a standalone applications but they could be incorporated with other computer programming languages to manage the application-database interaction.
Standard SQL can only be used for defining database schemas and for querying and updating data. Database vendors have extensions to SQL (like Microsoft's T-SQL) that are full-blown programming languages.

Using Oracle SQL syntax for custom developed database server

Is it possible to implement own database server taking Oracle PL/SQL syntax as the bases or i would like to ask why different database solutions have different syntax eg: SQL server, MySql, Sqlite etc. can't they have some specific standard of syntax for basic operations including PL/SQL(excluding SQLite) why everyone is having a different syntax, sorry for diversion of question into patent issues but i could not find a better place to ask this question.
Of course you can, but you have to parse the PL/SQL yourself into something other platforms understand. (You can use ANTLR for example as parser tool. There is even a full featured grammar for PL/SQL) This is possible for small solutions with a small instruction set, but for large, full support of PL/SQL you need to be Oracle-sized.
To answer the why: two reasons:
There is no standard, so everyone picks his own;
You don't want customers to leave, so your own 'best' framework that is incompatible with others, that is your USP, and it prevents users from just porting their code to the other platform. They are stuck on yours.

Which SQL Implementation can translate to many other(s)?

I'm looking for a SQL Implementation (and its Editor) that can be used for translating it to many other(s) SQL Languages.
For example, when i code in that SQL Language to script file(s), and then i translate to other(s) SQL Language script file(s) (for ex: MS SQL's , MySQL's , ...).
If you're sure to use only ANSI SQL to construct your scripts, you should be good to go.
I agree with #Justin Niessner: all SQL vendors pay attention to the SQL Standards, notably core SQL-92. To take SQL Server as an example, although they find Sybase legacy code is tricky to deprecate they are not afraid to do so and entirely new features (e.g. MERGE in MSSQL2008) tend to extend their Standard SQL equivalents, rather than reinventing the wheel.
For a product that has good Standards compliance, take a look at Mimer
Here at Mimer Information Technology, we pride ourselves on conforming
to the SQL standard and we play an active role in the Database
Languages standardization group which determines exactly what is SQL
standard.
Mimer also provide extremely useful SQL validators for SQL-92, SQL-99 and SQL:2003 respectively.
I've been researching the same thing a while ago. What I've found is that there is a project liquibase. It is aimed at change tracking but also converting between different DBMS. You can download source code and see different datatypes conversions across databases. Source at github browse for java files there, probably you'll find something helpful
If all you want are basic operations, these are fairly universal. For instance:
SELECT
INSERT
DELETE
UPDATE
FROM
WHERE
JOIN
...are all at the most basic level the same across implementations.
However, the more complicated your scripts get, the more difficult it becomes to make them "universal". Things like aggregation, subqueries, cursors, while loops, functions, indexes, constraints, temp tables, variables, string manipulation, window operations etc. are all pretty much database-specific.
Some of these do have "universal" equivalents but the more generic you make your code the worse it will perform.

internal implementation of database Queries

In my experience I have used many queries like select, order by, where clause etc.. in mysql, sql-server, oracle etc
For a moment i have thought,
1)how is this internally written to implement the above queries
2) which language do they use?
3) is that programming language? if yes which language?
4)what kind of environment required to implement this kind of complex database
1) In schematic form you would proceed as follows :
a) Split the query in its components and create a Abstract Syntax Tree (AST) of the query. There are tools to do this, in the olden days lex and yacc were used for this, now there is a lot more choice.
b) In a first step an optimizer will reorganize the tree by applying known equal transformations so the query will be most performant way by using indexes, doing queries which return little results first so you have less to join, etc....
c) You can walk this tree to implement the small operations on the database and the data returned. Typically this results in "virtual temporary" tables in the nodes of your AST
d) Collect the stuff from your top node and return it to the client
2-3) I do not think there are special languages. Many are in C, but there are Java and other languages used too
4) I think the best environment is a quiet environment for this kind of work. ;-)
The real hard work is not in the SQL interpreter/compiler but in the detailed datastructures and the nitty gritty of keeping everything efficiently organised and dynamically tuned to the situation in order to keep the database performant.
I believe the Oracle DBMS was all originally written in C, and probably still is.
MySQL is written in C,C++ according to the MySQL Launchpad page
Check the code to find out more about how MySQL is implemented.
Have a look at the Postgresql code.
In many (all?) databases you can view an explain/execution plan. This will give you a rough approximation of what is going on inside the database. In commercial RDBMSes, that's the closest you will get to any understanding of how the optimizations work. The algorithms in the query analyzers are closely guarded secrets.
Joe Chang wrote an excellent article about how SQL Server's cost based optimizer works
Likewise you can find similar information about PostgreSQL and MySQL

Is it a good idea to learn LINQ first, then SQL?

I know that in most cases it`s more useful to learn more complicated technology/language and then easy one than vice versa.
But, actually, time to do university tasks is limited. If I first learn LINQ, then go for SQL, would it be hard for me to use and learn sql?
EDIT
The task I need to do is to work with database and get some data from it, so the question is almost about LINQ to SQL.
It is a bad idea.
And if today's universities teach you LINQ instead of giving you foundation to build your knowledge upon, I can only feel sorry and pity for their students.
Time is always limited. Don't waste it on things that are subject to constant change.
SQL will be there tomorrow, LINQ.... well who knows.
SQL is applicable anywhere, LINQ only in .NET world.
Either LINQ or something else, it will be easy to "learn" it afterwards. When you have the knowledge of SQL, it will just me a matter of hours/days/weeks, hardly longer.
Well, the 2 things are very different. LINQ (in the pure sense) isn't actually related to databases at all - it can be used quite happily just with in memory objects, or against web services, etc.
If you are mainly interested in writing better .NET code, then learn LINQ - but learn it properly - perhaps pick up C# in Depth, for example - which covers it very well in the last few chapters.
If you want to know about databases, then sure: learn SQL (such as TSQL) - but understand the differences. Databases are good if you need to write enterprise software, but not necessarily if you just want to do simple tasks.
edit re edit to the question
If you are just getting simple data in and out of the database, then you probably don't need to know much about SQL. Just use LINQ-to-SQL (or whatever tool), and let the ORM tooling worry about it.
Learn SQL first, then LINQ.
That way you'll understand how LINQ-to-SQL is working behind the scenes, but you'll also know enough to be able to cope when LINQ can't do what you need.
SQL is a standard, learn the standard.
More precisely :
learn database theory
learn CODD algebra
then pick up a "common database", do some tutorials on it, ...
I personnaly really like PostgreSQL tutorial
I submit that you cannot effectively use LINQ unless you have SQL knowledge. If you don't understand, at a minimum, the following, you cannot effectively query a database in any fashion:
select
insert
delete
update
joins
group by
boolean algebra
relational theory
set theory
Learning SQL will give you the concepts you need even if you use LINQ later.
Sql. However you could play with linq pad for a while - it is a freeware and realize that LINQ is a nice hybrid between SQL and C#