0

i'm just starting out with ms sql server (2008 express) and spent the last hour or so trying out some basic queries. how does sql scripting (correct term?) compare to general computer programming? is writing sql queries as involved, lengthy to learn, needs as much practice, etc. as software development? is writing advanced sql queries comparable to software development, at least to some degree, and if so could you explain that?

also, i found a couple of tutorial and reference sites for learning sql but could you also recommend some other sites.

also(2), i was playing around with the sql query designer in msSQL and it seems like a good tool to learn writing sql commands, but is there a way to use the designer to INSERT data. it seems that it's only for SELECT ing data.

any other general comments for learning, better understanding, etc SQL would be appreciated. thanks

400_the_cat
  • 873
  • 3
  • 16
  • 29
  • 1
    To quote Wikipedia, "ANSI SQL is a Turing complete programming language". Although the setup of SQL falls under system administration, using SQL falls under programming, so I'd suggest moving this question to Stack Overflow. –  Jul 22 '10 at 03:02
  • As Andrew said, we're not programmers so we can't provide a good comparison between them. Your question will be migrated to Stack Overflow once enough people vote for it. – Mark Henderson Jul 22 '10 at 03:22
  • ah, sorry about that. actually, i posted this at stackoverflow too. http://stackoverflow.com/questions/3305402/ms-sql-server-sql-queries-compared-to-general-programming –  Jul 22 '10 at 04:23

3 Answers3

1

First at all, SQL is more about databases and less about programming, in that way that you you cannot just succeed by "writing good queries": you must also know how to structure your data, make optimized tables, choose appropriate types, etc. You can spend a day thinking about how your data will be stored, without really writing any queries. SQL is not a way to solve an abstract problem, but a way to store and retrieve data efficiently and safely. For example, making maintenance and backup plans is purely a DBA job, and has nothing to do with SQL queries.

Is it lengthy to learn? Well, here, it is quite similar to general development. Basic SQL syntax is pretty simple, so after reading the first page of SQL book, you will probably be able to insert, retrieve and remove data. But to master SQL and database stuff, you must be ready to spend years and years of practice. Just like CSS: writing CSS is easy. Mastering it is hard.

Some advices:

Take in account security.

You communicate with SQL Server by sending strings, and the server must interpret them. The big mistake is to let the end user participate in building your queries: it leads to security leaks, with the ability for the hacker to do whatever he want with your data (it's called SQL Injection). It's just like letting everyone write any source code they want, and execute it on your machine. In practice, nobody let a third person write arbitrary code on her machine, but plenty of developers forget to sanitize user input before querying the database.

Use parametrized queries and stored procedures.

You may want to consider as soon as possible using parametrized queries. It will increase security, optimize performance and force you somehow to write better queries (even if it is debatable).

After learning SQL for a few weeks or months, you may also want to learn what are stored procedures and how to use them. They have their strong points, but don't make an error I made when I started learning SQL: do not decide to use stored procedures everywhere.

Use frameworks.

If you are a .NET developer, learn to use Linq-to-SQL. If you already used Linq on .NET objects (lists, collections, etc.), it can be very helpful to figure out how to do some queries. By the way, remember you can use Linq queries and see how Linq transforms them into SQL queries.

Keep in mind that using a framework or an abstraction layer will not make you a database guru. Use it as a helpful tool, but do sometimes SQL stuff yourself. Yes, it can free you from writing SQL queries by hand, even on large-scale projects (for example, StackOverflow uses Linq-to-SQL). But soon or later, you will either need to work on a project which does not use Linq, or you will see some possible limitations of Linq versus plain SQL.

Borrow a book.

Seriously, if you want to learn stuff, buy or borrow a book. Tutorials will explain you how to do a precise thing, but you will lose an opportunity to learn something you never thought about. For example, database partitioning or mirroring is something you must know if you want to work as a DBA. Any book about databases will talk about partitioning; on the other hand, there are few tutorials which will lead you to this subject by themselves.

Test, evaluate, profile.

SQL is about optimized queries. Anybody can write a select statement, but many people will write it in a non-optimized form.

If you are dealing with a few kilobytes database which have a maximum of hundred records, all your queries will perform well, but when the things will scale up, you will notice that a simple select query spends three seconds on a few billions of rows database instead of a few milliseconds.

To learn how to write optimized queries and create optimized databases, try to work on large sets. AdventureWorks demo database from Microsoft is a good start point, but you may also need sometimes to fill the database with random stuff just to have enough data to measure performance correctly.

Use Microsoft SQL Profiler (included in SQL Server 2008 Enterprise). It will help you to know what the server is really doing and how fast, and to find bottlenecks and poorly-written queries.

Learn from others.

Reading a book is a good point to start, but is not enough. Read the stuff on StackOverflow, especially the questions related to developers doing DBA work. For example, see Database Development Mistakes Made by App Developers question, and return reading the answers from time to time while learning SQL.

If you have any precise question (a query which does not produce what you expected, a strange performance issue, etc.), feel free to ask it on StackOverflow. The community is great, and there are plenty of people who know extremely well the stuff.

Sometimes, talking to DBA in your company (if there is one) can be also an opportunity to learn things.

is there a way to use the designer to INSERT data. it seems that it's only for SELECT ing data

If I remember well, query designed in Visual Studio let you build insert statements too. But maybe I'm wrong. In all cases, you can use Microsoft SQL Management Studio (included with Microsoft SQL 2008 Enterprise), which let you see how to build some cool queries (right-click on an element in Object Explorer, than use "Script database as..." menu).

Community
  • 1
  • 1
Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
  • Well not LinqToSQL I hope because that has now been depricated in favor of the Entity Framework.. but good overview otherwise – Earlz Jul 22 '10 at 04:17
  • @earlz: I still use LinqToSql and am happy with it. But it's because I always do just a drag-drop of tables without modifying the structure at all, because I'm using Microsoft SQL Server only, and because I used to work with EF in .NET 3.5 when it was just released, and was a complete crap, terribly unusable. Of course, in a different context and since EF is more mature now, it *must* be used instead of LinqToSql. – Arseni Mourzenko Jul 22 '10 at 04:25
  • Thanks for taking the time to write a thorough answer, you answered my question as well as some other questions i've been meaning to ask. –  Jul 22 '10 at 04:34
1

I think you'll find that they key issue is that SQL is declarative, unlike most computing languages you're likely familiar with. This is fundamental. Grab any computer science text book and start there.

SQL is no more or less difficult than anything else in my view. Historically it was an area which people would tend to specialize in, but that was a consequence of the technology available at the time. It's now more accessible and the tools are significantly better, so expertise is generally spread more widely now.

philw
  • 661
  • 10
  • 20
0

It is different, SQL programming is quite restricted, when writing complex logic you might find it cumbersome with its limited programming options, unclear code as there is no modular programming, and bad implementations of stuff like cursors.

I read somewhere on SO, that database is not for coding, its only for storing data and querying. Its well said in some sense.

What I believe important to learn in that area is first of knowing all the features available in the db so that you make it use efficiently. Secondly improve querying/analytical skills.

  • Basic SQL features can be learnt from w3schools(joins , grouping etc)
  • Advance db features can be learnt from your dbms certification exam book. (the most basic certification exam be it oracle/sql server)
  • Analytical skills and some fun - puzzles by Joe Celko
ns12345
  • 3,079
  • 5
  • 41
  • 62