10

I'm a pretty good C# programmer who needs to learn SQL Server. What's the best way for me to learn SQL Server/Database development?

Note: I'm a total newb when it comes to DB's and SQL.

11 Answers 11

16

SQL is about set theory, or more correctly, relational algebra. Read a brief primer on that. And learn to think in sets, not in procedures.

On the practical side, there are four fundamental operations,

  • selects, which show some projection of a table(s) data
  • deletes, which remove some subset of a table's rows,
  • inserts, which add rows to a table,
  • updates, which (possibly) change data in a table

(By subset, I mean any subset, including the empty set, and not necessarily a proper subset.)

Anywhere I can write a column name in DDL (except as the target of an update), I can write an expression that uses column names, functions, or constants.

select 1, 2, 3 from table will return the resultset "1 2 3", once for each row in the table. If the column named create_date is of type date, and the function month returns a month number given a date, select month( create_date) from table will show me the month number for each create_date.

A where clause is a predicate that restricts rows selected, or deleted, or updated to those rows for which the predicate is true. A where cause can be composed of an arbitrary number of predicates connected by the logical operators and or and not. Just like the column list in a select, I can use column names, functions, and constants in my where clause. What result set do you think is returned from select * from table where 1 = 1;?

In a query, tables are related by joins, in which some datum or key in one is related by an operator to a datum or key in another table. The relational operator is often equality, but can in fact be any binary operator or even a function.

Tables are related, as I mentioned above, by keys; a row in a table may relate to zero, one, or many rows in another table; this is referred to as the cardinality of the relation. Relations may be one-to-one, one-to-many, many-to-many. There are standard ways of representing each relation. Before you look up the standard ways to do this, think about how you'd represent each one, what the minimum requirements of each kind is. You'll see that a many-to-many relation can in fact also model one-to-many and one-to-one; ask yourself why, given that, all relations are not many-to-many.

EF Codd, among other, pioneered the idea of normal form in relational databases. There are commonly held to be five or six normal forms, but the most important summary of normal form is simple: every entity that your database models should be represented by one row and one row only, every attribute should depend on the row's key, and every row should model an entity or a relationship. Read a primer on normal form, and understand why you can get data inconsistencies if a your database isn't normalized.

In all this, try to understand why I like to say "if you lie to the database, it will lie to you". By this I don't mean bad data, I mean bad design. E.g., if you model a one-to-many relation as many-to-many, what "lies" can be recorded? What "lies" can happen if your tables aren't normalized?

A view, in practical terms, is a select query given a name and stored in the database. If I often join table student to table major through the many-to-many relation student_major, maybe I can write a view that selects the columns of interest from that join, and use the view instead of alway rewriting the join.

Practical tips: first, write a view. whatever you're doing, it'll be simpler and clearer if you write a view for every calculation or sub-calculation you do. Write a view that encapsulates each join, write a view that encapsulates each transformation. Almost anything you want to do can be done in a view.

Decomposing a query into views serves the same ends as functional decomposition serves in procedural code: it allows you to concentrate on doing one thing well, makes it more easily tested, and allows you to compose more complex functionality out of simpler operations. Here's an example where I use views to transform a table into forms that more easily allow me to apply successive transformations, in order to get to a goal.

Don't conflate data. Each table ought to unambiguously model one thing (one kind of entity) and only one thing; each column should express one and only one attribute of that thing. Different kinds of entities belong in different tables.

Metadata is your friend. Your database platform will provide some metadata; what it doesn't provide you should add. Since metadata is data, all the rules for modeling data apply. You can get, for example, the names of all objects in your database from the sytem table sysobjects; syscolumns contains all the columns. To find all the columns in one table, you'd join sysobjects and syscolumns on id, and add a where clause restricting the resultset to a particular table name: where sysobjects.name = 'mytable'.

Experiment. Sit down at a database and ask yourself, "How can I represent people with hair colors and professions and residences? What tables and relations are implied in modeling that?" Then model that, as tables.

Then ask yourself, "How can I show all blonde doctors who reside in Atlanta", and write the query that does that. Piece it together by writing views that show you all blondes, all doctors, and all people who reside in Atlanta.

You'll find that in asking "how can I find that", you'll expose deficiencies in your model, and you'll find that you want or even need to change the way your model works. Make the changes, see how they make your queries easier or harder to write.

1
  • Lengthy, but totally worth it! Are you a writer, if not, start writing tutorials.
    – Kredns
    Apr 16, 2009 at 0:09
4

I love Joe Celko books from novice to advanced. I also think virtual labs are great.

2
  • Those SQL Server virtual labs are a great idea. Apr 15, 2009 at 0:31
  • These are really great seeing as how I can't seem to get SQL Server to work on my machine.
    – Kredns
    Apr 15, 2009 at 0:33
3

An easy way to learn SQL syntax?

Use Microsoft Access. Use the Northwind sample database, open Access up in Query view and run some queries.

Creating a Simple Query

Start with SELECT * FROM and work your way up to more complicated examples.

3

One of the Best resources is http://www.sqlservercentral.com/ Tons of articles

Another good resource is http://www.trainingspot.com/VideoLibrary/Default.aspx

And here is a list of books my DBA suggested I read for learning SQL

  1. Best Damn Exchange, SQL and IIS Book Period or on google books
  2. Beginning SQL Server 2008 Developers or on Google books
2

Here are the three books I strongly recommend you read in order.

Begining SQL Server 2005 Programming

Professional SQL Server 2005 Programming

The Gurus Guide to Transact SQL

1
  • +1 for Professional SQL Server 2005 Programming; it is an excellent book.
    – Steve
    Apr 15, 2009 at 15:48
1

W3Schools has a nice tutorial with try by example setup. But other than just installing a express edition and having a bunch of trials runs with the demo databases, I'd say no book will teach you better.

1

I would say your very best bet is to sign up for a DB class at a local college. You can usually find an evening class. You will start with simple Database concepts like what is a database, and what are tables.

The instructor will usually give you a project as homework about halfway though the class where you will design and implement a simple database for something like a video store. You will have interaction with other students who are at your same level and will be interested in discussing the technical details from a new DB guy standpoint. And you will have an experienced instructor you can ask questions of and get timely interaction from, who won't be snarky like us internet posters :)

3
  • I would do this but I'm still in high school and don't have a job.
    – Kredns
    Apr 15, 2009 at 0:35
  • Community college is like $45 per class. Mow 5 lawns and you can pay for it. Are your parents going to get mad at you for taking a college class after school????
    – Zak
    Apr 15, 2009 at 0:43
  • I have a busy schedule, I play alot of sports.
    – Kredns
    Apr 15, 2009 at 0:49
1

Get it from horse's mouth --> http://www.asp.net/learn/videos/default.aspx?tabid=63#sql

These days most of the universities have their courses online. Try to research some good professors and learn the fundamentals. Their assignments are also useful.

of the top of my head, I can think of MIT opencourseware (OCW)

1

This depends on what you will need to do. If you just need to access databases, you should have a look the various access strategies - DataReader, DataSet, LINQ to SQL, Entity Framework, NHibernate - and pick a solution.

If you need to develope database, get a good book on that topic. Get familar with the theoretical stuff - relational algebra, keys, referential integrity and normalisation. Then have a look at SQL and finally you may have a closer look at ACID transaction, locking, concurency control, indexes, and all the technical details that make a database server work.

I would suggest to read the wikipedia articles - may be the 100 most important ones - to get the big picture and then approch the details where required. But this will probably be no replacement for a good book if you want to get a good database developer.

0

I tend to like books because I can read them anywhere, I can go at my own pace and I can get eBook copies (when using apress). I also happen to learn more efficiently in this manner as I already know most of the concepts, like database types.. int, bool, guid, etc... you will know those as well. So, essentially, I would recommend the apress series of books - very comprehensive IMO. And you can generally find them used for very cheap on Amazon... Here is one tailored to you:

http://www.amazon.com/Beginning-SQL-Server-2008-Developers/dp/1590599586/ref=sr_1_1?ie=UTF8&s=books&qid=1239758026&sr=1-1

0

When you sign up to Microsoft Books Newsletters (From Microsoft Press) they actually give you (free) an ebook called Introducing SQL Server 2008.

http://csna01.libredigital.com/?urss1q2we6

Not the answer you're looking for? Browse other questions tagged or ask your own question.