I have another attempt at the exam coming up this Friday which fine tuning my studies to pass this time. I need to spend more time on querie optimization and tighten up my knowledge of XML.
I know that I have covered every topic in the book because I have read the book cover to cover. It makes me think that I have the best broad understanding someone can have about the subject matters but because I don't use SQL for eight hours every day I have a hard time applying the knowledge to specific situations that are mentioned during the test. Anyways, not passing the second time is not a downer... it's an igniter!!!
Wednesday, May 28, 2014
Friday, May 23, 2014
625, not enough
So another shot at the test coming up next week. Scored 625 on my previous attempt, which was close but no cigar.
I felt like I visualized what the test was asking much clearer in terms of the TSQL pieces that needed to be answered. I need work on query optimization, and creating file objects according to my post test strength/weakness hand out. When I saw this I knew exactly which questions that I had missed… because I was unsure when I answered them the first time.
That was last Friday morning, over the weekend I went to New Orleans for vacation, after catching up on work when I got back, I'm now working further on my studies. My plan for studying this week is a lot of flash cards with definitions on the back.
Cheers!
I felt like I visualized what the test was asking much clearer in terms of the TSQL pieces that needed to be answered. I need work on query optimization, and creating file objects according to my post test strength/weakness hand out. When I saw this I knew exactly which questions that I had missed… because I was unsure when I answered them the first time.
That was last Friday morning, over the weekend I went to New Orleans for vacation, after catching up on work when I got back, I'm now working further on my studies. My plan for studying this week is a lot of flash cards with definitions on the back.
Cheers!
Wednesday, May 14, 2014
Countdown
T minus two days until the test, lots of practice in sandbox today, and lots of XML tomorrow. Cheers all!
Tuesday, May 6, 2014
Chapter 16 written review
Chapter 16 dealt with cursors, and the explanation of cursors vs set based operations (kind of like in chapter 1). It talked about loops a little more, and how a loop is more of a cursor then a set based operation, and how a set based operation can be more efficient because it can be tuned and optimized. The things that I got from lesson 1 was that if you are going to use a cursor you must first declare it, open a statement (batch), fetch in a loop, close, then deallocate the cursor.
Lesson 2 was interesting because it talked about temp tables vs table variables. Interesting because this was discussed at SQL Saturday a week or so ago. Brent Ozark talked about this in his seminar "think like an engine" where he compared indexes, and brought up the idea of variable tables. From what I learned about them from him is that they are virtually useless. One of the lesson review questions gives choices as to when is a table variable useful (choose all), and the answers were: "when the tables are very small and the plan is trivial, when the tables are very small and the plan is nontrivial, when the tables are large and the plan is trivial"; which to me seems like, pretty much never. Other things I learned that I thought were useful in this lesson were that if you are going to create a temp table that can be seen on a local level you use the "#" sign; such as:
CREATE TABLE #temp1
(
col1 INT NOT NULL,
col2 INT NOT NULL
)
But if you wanted to create one and have it be seen on a global level you use '##'; such as:
CREATE TABLE ##glob1
(
col1 INT NOT NULL,
col2 INT NOT NULL
)
An interesting chapter again, I will be reading the last chapter maybe tomorrow (maybe not, it's my birthday), then doing some sand box homework in the next few evenings.
Cheers!!!
Lesson 2 was interesting because it talked about temp tables vs table variables. Interesting because this was discussed at SQL Saturday a week or so ago. Brent Ozark talked about this in his seminar "think like an engine" where he compared indexes, and brought up the idea of variable tables. From what I learned about them from him is that they are virtually useless. One of the lesson review questions gives choices as to when is a table variable useful (choose all), and the answers were: "when the tables are very small and the plan is trivial, when the tables are very small and the plan is nontrivial, when the tables are large and the plan is trivial"; which to me seems like, pretty much never. Other things I learned that I thought were useful in this lesson were that if you are going to create a temp table that can be seen on a local level you use the "#" sign; such as:
CREATE TABLE #temp1
(
col1 INT NOT NULL,
col2 INT NOT NULL
)
But if you wanted to create one and have it be seen on a global level you use '##'; such as:
CREATE TABLE ##glob1
(
col1 INT NOT NULL,
col2 INT NOT NULL
)
An interesting chapter again, I will be reading the last chapter maybe tomorrow (maybe not, it's my birthday), then doing some sand box homework in the next few evenings.
Cheers!!!
Monday, May 5, 2014
Chapter 15 Written review
Chapter 15 has three sections about indexes and statistics.
Lesson 1: This lesson deals with indexes, but before it does that it talks about the different ways that data is stored. A heap is one way that the data is stored, I think of this as a book, with a table of contents at the beginning known as a Index Allocation Map. Here is more info on heaps from the experts, I looked and stared at the diagrams (pictures) in the book for a long time as I read through this stuff. Then there are balanced trees that have levels of roots, intermediates, and leafs(data). Think of an upside down tree for this one, with the intermediates being the branches. The main idea in this lesson after explaining heaps and balanced trees (b-tree?) was that you can create clustered indexes on B-Trees that can seek data (you can only have one per table), and non-clustered indexes that can point to data. Here are a couple of important things to remember about clustered indexes vs non clustered: Rakesh Blog. I think of them as a clustered index is great for finding and seeking data, non-clustered can help in finding data in a heap, but it can also be helpful if you need to input data since clustered indexes are not so good at that. This lesson also dealt with indexed views, which is (as it sounds) a view with an index on it. These can be useful in ETL transactions.
Lesson 2: Discussed SARG's (search arguments), and introduced the reader to the idea that you can use indexes when querying data and it will find your data faster. No brainer right? Well it also explains, when the optimizer decides to use a query or not, and where it is a good idea to put an index. Simply put, it's best to have a clustered index on a column that is a unique identifier, or INT, make sure that it is short though so that it can easily seeked. This lesson discusses the Where clause too, and how it can be a powerful tool in using an index during a query which makes sense if you remember the logical way that SQL will execute a query from, WHERE, group by, having, select, order by. I will add, that of course it's not a good idea to have a function on your where clause because when a calculation is performed SQL can no longer seek to a value, it would need to perform the calculation then check each row whether or not that value matches, which is a scan by definition. This lesson does not talk about query hints which was a little disappointing to me, I think that's in chapter 17.
Lesson 3: Was interesting because it talked about statistics, which from what I understand is a HUGE topic. From what I've gathered I don't think I would ever mess with the statistics generated by SQL. The book tries to give examples of when you would want to manage or create your own statistics, but geez.. I find that VERY advanced at least for my level. The basics of statistics is that SQL is always creating and updating statistics on the distribution of key values.
All in all this was a very informative chapters, the index stuff was good but I enjoyed learning about the heap & B-trees.
Cheers!
Lesson 1: This lesson deals with indexes, but before it does that it talks about the different ways that data is stored. A heap is one way that the data is stored, I think of this as a book, with a table of contents at the beginning known as a Index Allocation Map. Here is more info on heaps from the experts, I looked and stared at the diagrams (pictures) in the book for a long time as I read through this stuff. Then there are balanced trees that have levels of roots, intermediates, and leafs(data). Think of an upside down tree for this one, with the intermediates being the branches. The main idea in this lesson after explaining heaps and balanced trees (b-tree?) was that you can create clustered indexes on B-Trees that can seek data (you can only have one per table), and non-clustered indexes that can point to data. Here are a couple of important things to remember about clustered indexes vs non clustered: Rakesh Blog. I think of them as a clustered index is great for finding and seeking data, non-clustered can help in finding data in a heap, but it can also be helpful if you need to input data since clustered indexes are not so good at that. This lesson also dealt with indexed views, which is (as it sounds) a view with an index on it. These can be useful in ETL transactions.
Lesson 2: Discussed SARG's (search arguments), and introduced the reader to the idea that you can use indexes when querying data and it will find your data faster. No brainer right? Well it also explains, when the optimizer decides to use a query or not, and where it is a good idea to put an index. Simply put, it's best to have a clustered index on a column that is a unique identifier, or INT, make sure that it is short though so that it can easily seeked. This lesson discusses the Where clause too, and how it can be a powerful tool in using an index during a query which makes sense if you remember the logical way that SQL will execute a query from, WHERE, group by, having, select, order by. I will add, that of course it's not a good idea to have a function on your where clause because when a calculation is performed SQL can no longer seek to a value, it would need to perform the calculation then check each row whether or not that value matches, which is a scan by definition. This lesson does not talk about query hints which was a little disappointing to me, I think that's in chapter 17.
Lesson 3: Was interesting because it talked about statistics, which from what I understand is a HUGE topic. From what I've gathered I don't think I would ever mess with the statistics generated by SQL. The book tries to give examples of when you would want to manage or create your own statistics, but geez.. I find that VERY advanced at least for my level. The basics of statistics is that SQL is always creating and updating statistics on the distribution of key values.
All in all this was a very informative chapters, the index stuff was good but I enjoyed learning about the heap & B-trees.
Cheers!
Saturday, May 3, 2014
Chapter 14 Written Review
Chapter 14 dealt with query optimization which is a really cool topic! I've listened to one particular podcast probably 5 times about query optimization (podcast is SQL Down Under, and the guest is Grant Fritchey).
Lesson 1: Query optimization is exactly as it sounds, it's trying to find the most optimal way of executing code. The SQL optimizer estimates a cost on the hardware when it begins a transaction. The cost part is only one part of about five that the SQL engine will execute before returning the result set of the query. Here are the 5 pieces in order: T-SQL, Parsing, Binding, Optimization, Execution... google these or look at page 520 in the kit to understand these further. SQL has a few tools to help analyze queries, Extended Events, SQL Trace, and SQL Profiler. Note that SQL Trace, and Profiler will be deprecated in future versions of SQL, so it's better to use Extended Events.
Lesson 2: Talks about execution plans, and how to read graphical plans. Make sure to read them from right to left, and the top down. As I learned in the podcast it's important to remember that actual execution plans, and estimated execution plans, are still all only ESTIMATED.
Lesson 3: Was about dynamic management objects (DMOs). DMO's are another tool for finding queries that don't run optimally. There are more then 130 DMOs available in SS2012. Important DMO's to remember at ones that deal with SQL Server operating system, execution-related DMOs, and Index-related DMOs.
Credit: Querying Microsoft SQL Server 2012; Itzik Ben-Gan, Dejan Sarka, Ron Talmage
Lesson 1: Query optimization is exactly as it sounds, it's trying to find the most optimal way of executing code. The SQL optimizer estimates a cost on the hardware when it begins a transaction. The cost part is only one part of about five that the SQL engine will execute before returning the result set of the query. Here are the 5 pieces in order: T-SQL, Parsing, Binding, Optimization, Execution... google these or look at page 520 in the kit to understand these further. SQL has a few tools to help analyze queries, Extended Events, SQL Trace, and SQL Profiler. Note that SQL Trace, and Profiler will be deprecated in future versions of SQL, so it's better to use Extended Events.
Lesson 2: Talks about execution plans, and how to read graphical plans. Make sure to read them from right to left, and the top down. As I learned in the podcast it's important to remember that actual execution plans, and estimated execution plans, are still all only ESTIMATED.
Lesson 3: Was about dynamic management objects (DMOs). DMO's are another tool for finding queries that don't run optimally. There are more then 130 DMOs available in SS2012. Important DMO's to remember at ones that deal with SQL Server operating system, execution-related DMOs, and Index-related DMOs.
Credit: Querying Microsoft SQL Server 2012; Itzik Ben-Gan, Dejan Sarka, Ron Talmage
Friday, May 2, 2014
Just a little recap Ch. 12 & 13 written review
So beginning with chapter 12;
Lesson 1 was about transaction commands, how SQL handles transactions, how the user is able to see where SQL is in a transaction, and locking for readers and writers. It also talked about rollbacks, which is essentially not committing a transaction. Lots of lingo in this chapter, hence the flash cards that I made for myself.
Lesson 2 dealt with errors (raising errors, and throwing errors), one interesting note that I found out was that Microsoft is suggesting to use the THROW statement instead of RAISERROR in new applications, and RAISERROR can't be used in SQL Server's 2014's natively compiled stored procedures. It also talked about XACT_ABORT (note the XACT part means 'transaction'), as you might imagine this command aborts a transaction. Lastly, I think of Try/Catch kind of like an IF statement, because if an error occurs during the try part of transaction control is passed to the catch part of the batch.
Lesson 3 Was about Dynamic SQL, and SQL injection. I had a post on SSC a while back when I first started studying that someone commented that I needed to use dynnamic SQL, and this example from the book is kind of what I was getting at in my post:
USE TSQL2012;
GO
DECLARE @SQLString AS NVARCHAR(4000), @address AS NVARCHAR(60);
SET @SQLString = N'
SELECT custid, companyname, contactname, contacttitle, address
FROM [Sales].[Customers]
WHERE address = @address';
SET @address = N'5678 rue de l''Abbaye';
EXEC sp_executesql
@statement = @SQLString
, @params = N'@address NVARCHAR(60)'
, @address = @address;
This lesson also talks about branching logics for SQL code, these branching logics are statements such as: IF/ELSE, WHILE, WAITFOR, GOTO, RETURN. When I read through this part of the book I had to remind myself that these branching logics are great, but remember that SQL is not an object oriented language, so when you start running this while loops and what not, it's going to kill your performance. I use a while loops a bunch for the development work that I do, but that's a totally different animal. Just a thought on this. Also remember that procs (stored procedures) can all other procs... again the code can get messy.
Lesson 2 dealt with Triggers. Triggers are just like procs, but there needs to be something has to happen in order for them to be executed. SQL supports triggers for two different kinds of events, a data manipulation event (insert, updates, and deletes), and a data definition event (creating objects).
Lesson 3 is about user defined functions. I found this great page on stack over flow a while back that I bookmarked that talked about the differences between user defined functions, and procs... make sure to review this: difference between udf's and procs
Sources: sqlhints.com, stackoverflow, Querying Microsoft SQL Server 2012; Itzik Ben-Gan, Dejan Sarka, Ron Talmage
Lesson 1 was about transaction commands, how SQL handles transactions, how the user is able to see where SQL is in a transaction, and locking for readers and writers. It also talked about rollbacks, which is essentially not committing a transaction. Lots of lingo in this chapter, hence the flash cards that I made for myself.
Lesson 2 dealt with errors (raising errors, and throwing errors), one interesting note that I found out was that Microsoft is suggesting to use the THROW statement instead of RAISERROR in new applications, and RAISERROR can't be used in SQL Server's 2014's natively compiled stored procedures. It also talked about XACT_ABORT (note the XACT part means 'transaction'), as you might imagine this command aborts a transaction. Lastly, I think of Try/Catch kind of like an IF statement, because if an error occurs during the try part of transaction control is passed to the catch part of the batch.
Lesson 3 Was about Dynamic SQL, and SQL injection. I had a post on SSC a while back when I first started studying that someone commented that I needed to use dynnamic SQL, and this example from the book is kind of what I was getting at in my post:
USE TSQL2012;
GO
DECLARE @SQLString AS NVARCHAR(4000), @address AS NVARCHAR(60);
SET @SQLString = N'
SELECT custid, companyname, contactname, contacttitle, address
FROM [Sales].[Customers]
WHERE address = @address';
SET @address = N'5678 rue de l''Abbaye';
EXEC sp_executesql
@statement = @SQLString
, @params = N'@address NVARCHAR(60)'
, @address = @address;
In this example, we declare a parameter for sp_executesql command that is actually some SQL code... doing this is the best way to protect against SQL injection.
Chapter 13
Lesson 1 is all about my fav, stored procedures! This is simple, if you have a query like:
SELECT name
FROM customers
WHERE firstname = 'Bob';
You can create a procedure out of this, and interchange 'Bob' with a different parameter. The code would look something like this:
USE TSQL
IF OBJECT_ID('name','P') IS NOT NULL DROP PROC findbyname
CREATE PROC findbyname
@custname AS VARCHAR(30)
AS
BEGIN
SELECT name
FROM customers
WHERE firstname = @custname
RETURN
END;
--Now you can execute the procedure by using EXECUTE, or EXEC for short:
EXEC findbyname
@custname = 'Bob' --Or steve, or whatever name you want
This lesson also talks about branching logics for SQL code, these branching logics are statements such as: IF/ELSE, WHILE, WAITFOR, GOTO, RETURN. When I read through this part of the book I had to remind myself that these branching logics are great, but remember that SQL is not an object oriented language, so when you start running this while loops and what not, it's going to kill your performance. I use a while loops a bunch for the development work that I do, but that's a totally different animal. Just a thought on this. Also remember that procs (stored procedures) can all other procs... again the code can get messy.
Lesson 2 dealt with Triggers. Triggers are just like procs, but there needs to be something has to happen in order for them to be executed. SQL supports triggers for two different kinds of events, a data manipulation event (insert, updates, and deletes), and a data definition event (creating objects).
Lesson 3 is about user defined functions. I found this great page on stack over flow a while back that I bookmarked that talked about the differences between user defined functions, and procs... make sure to review this: difference between udf's and procs
Sources: sqlhints.com, stackoverflow, Querying Microsoft SQL Server 2012; Itzik Ben-Gan, Dejan Sarka, Ron Talmage
Thursday, May 1, 2014
May 1st, Date Set!
After a long while, I'm making another post to update myself where I am currently. A while back I posted a picture of some flash cards that I was using for chapter 12 I believe (the heart of SQL transactions). Since then I've read through chapter 13, and am almost done with 14. Stored procedures, UDF's were some of the things that were covered during these chapters, and as I alluded too before, what I feel like is my bread and butter as far as writing TSQL goes.
As far as the test taking goes, I'm so happy that a lot of the folks on born to learn have taken and completed the test, it totally inspires me to do the same! I know that the training kit allows for "self paced" learning, but I feel like I've been studying for years! Partially it's me, not thinking that I understand the material well enough, and want to have an even firmer handle on it then I do, and part of it is the fact that in my daily job (which is somewhat seasonal, spring being the busiest season) I've been massively slammed. I'm learning all of the SQL material on my own which forces me to prioritize my daily routine. Things as simple as getting hair cut have been put off recently (which I will be doing today)!
None the less I have set a date to take the test May 16th! My plan of action is to finish reading the book by next Friday, and make flash cards for the conceptual pieces of it. Then work hard on sand box work over the course of the following week until I take the test Friday the 16th. I honestly think that I could pass the test today, but ensure that I WILL pass it the 16th I'm going to do a lot of reviewing topics, and scripting TSQL.
I think it would be healthy for me to spend time writing down what I've learned in the last few chapters since I last posted, so, that would be 11, 12, 13, and now 14 so that I have them for my own reference as I review in the next two weeks.
Cheers!
As far as the test taking goes, I'm so happy that a lot of the folks on born to learn have taken and completed the test, it totally inspires me to do the same! I know that the training kit allows for "self paced" learning, but I feel like I've been studying for years! Partially it's me, not thinking that I understand the material well enough, and want to have an even firmer handle on it then I do, and part of it is the fact that in my daily job (which is somewhat seasonal, spring being the busiest season) I've been massively slammed. I'm learning all of the SQL material on my own which forces me to prioritize my daily routine. Things as simple as getting hair cut have been put off recently (which I will be doing today)!
None the less I have set a date to take the test May 16th! My plan of action is to finish reading the book by next Friday, and make flash cards for the conceptual pieces of it. Then work hard on sand box work over the course of the following week until I take the test Friday the 16th. I honestly think that I could pass the test today, but ensure that I WILL pass it the 16th I'm going to do a lot of reviewing topics, and scripting TSQL.
I think it would be healthy for me to spend time writing down what I've learned in the last few chapters since I last posted, so, that would be 11, 12, 13, and now 14 so that I have them for my own reference as I review in the next two weeks.
Cheers!
Saturday, April 26, 2014
SQL Saturday Chicago!
What a great turn out for SQL Saturday Chicago! Attended Brent Ozark's session in the morning, a brilliant, funny, entertaining class about how to think the sql engine! Here's a pic from the class:
Friday, April 25, 2014
Post Easter Weekend Transaction Level and Isolation
Ok, so I won't lie... chapter 12 was about 70 pages long, and I only read it twice. This chapter along with chapter 6 are very conceptual, which make them a slog to get through. I enjoy the deep thinking involved with this, but it is difficult to keep up with every idea, and execution. I'll be working through the practice exercises in the book this evening, and will try to remember the lingo for the test (QUE THE FLASH CARDS!)
It was largely about the idea of transactions. A transaction is a process of getting things done... that's a very dumbed down definition but either a transaction is completed successfully, or not. In SQL there are different transaction levels that can be set before a running code. I'm going to try to explain the five transaction levels introduced in the book in my own words:
Read Committed: The default Isolation level for SQL Server. A query can only read data that has been committed
Read Uncommitted: Removes shared locks in select statement, reads uncommitted data; DIRTY READS!
Read Uncommitted Snapshot: (RCSI) is the default Iso level for Azure, stores original data in Tempdb so that it can be reread later
Repeatable Read: data in a transaction can be read later in the transaction, updates and deletes of rows that are selected are prevented, but it can still read new rows PHANTOM READS
Snapshot: Just like RCSI, cannot see phantom reads
Serializable: Highest Iso level, all reads are repeatable, new rows are not allowed in underlying tables
It was largely about the idea of transactions. A transaction is a process of getting things done... that's a very dumbed down definition but either a transaction is completed successfully, or not. In SQL there are different transaction levels that can be set before a running code. I'm going to try to explain the five transaction levels introduced in the book in my own words:
Read Committed: The default Isolation level for SQL Server. A query can only read data that has been committed
Read Uncommitted: Removes shared locks in select statement, reads uncommitted data; DIRTY READS!
Read Uncommitted Snapshot: (RCSI) is the default Iso level for Azure, stores original data in Tempdb so that it can be reread later
Repeatable Read: data in a transaction can be read later in the transaction, updates and deletes of rows that are selected are prevented, but it can still read new rows PHANTOM READS
Snapshot: Just like RCSI, cannot see phantom reads
Serializable: Highest Iso level, all reads are repeatable, new rows are not allowed in underlying tables
SQL Saturday Chicago begins tomorrow!
After rearranging my schedule I'll be attending SQL Saturday 291... CHICAGO STYLE! I'm excited for this because with a lot of stuff going on right now, I wouldn't say that my studies have been lacking, but more or less stalled. I think that's a perception thing on my part because I think about my progression towards passing this exam as an exercise of 'doing' but the last few chapters have been more or less 'absorbing'. Understanding concepts, nailing down definitions, but not the 'rada-tap-tap' on the keyboard that I'm used to.
I've given thought to reading the rest of the book through one time, then going back and revisiting things that I either need to learn more about or need to remind myself of.
As of now, I'm reading through chapter 13 that I understand fairly well. Implementing stored procs is really my bread and butter on this exam since that is mostly what my development background has taught me how to do. A quick glance through this, and then on to chapter 14 in which I'm very eager to learn about query performance tuning.
Cheers all, I'll take a pic of SQL Saturday tomorrow morning!
I've given thought to reading the rest of the book through one time, then going back and revisiting things that I either need to learn more about or need to remind myself of.
As of now, I'm reading through chapter 13 that I understand fairly well. Implementing stored procs is really my bread and butter on this exam since that is mostly what my development background has taught me how to do. A quick glance through this, and then on to chapter 14 in which I'm very eager to learn about query performance tuning.
Cheers all, I'll take a pic of SQL Saturday tomorrow morning!
Friday, April 18, 2014
Insert, Update, Delete Sandbox fun
After reading through merging, inserting, updating, and deleting I performed the exercises in the book, but it didn't stick in my head as well I thought it would. For that reason I began to play on my own using the database that I had created in the past from the contacts in my phone. Using the phone number as the primary key I had two tables (the old table, and a new table) and I began to update, and insert new contacts on the old table.
Sandbox practice is a great way to learn, and can be more relative in the real world as well. I will post the DDL from my update statements when I get back to my home cpu...
Cheers!
Sandbox practice is a great way to learn, and can be more relative in the real world as well. I will post the DDL from my update statements when I get back to my home cpu...
Cheers!
Monday, April 14, 2014
Chapter 11 practice exercises from book.. do over again!
After reading through chapter 11 a few times and getting my head around the ideas I started doing the exercises in the book tonight... all I can say are they are great!
I think this is about the third time that I have written this, but the book does a wonderful job keeping things simple at first then expanding on the ideas. Chapter 11's exercises def prove this, and I have noted to my self (by way of this post in this blog) that I will go back and do these exercises again before test time.
On to chapter 12!
I think this is about the third time that I have written this, but the book does a wonderful job keeping things simple at first then expanding on the ideas. Chapter 11's exercises def prove this, and I have noted to my self (by way of this post in this blog) that I will go back and do these exercises again before test time.
On to chapter 12!
Saturday, April 12, 2014
Merge read, not written...
I said that I would write about merge last night, but had family over so was a bit busy cooking/cleaning last night. Perhaps I'll have time to write down my thoughts on merge tonight.
I did however read chapter 11 yesterday (the first two lessons). I always read through a chapter once quickly knowing that I won't absorb every piece of information, but instead exposing myself to the information that the chapter involves. This seems to be the best way for me to learn new material... read through once to get the lay of the land, let it sink in for a day or two, then go back in and read through it again and do the examples provided by the book, then if needed go through it a third time. If done correctly this can be completed in 5 days to a week. Remembering the content is the more challenging part of it in which I need to go back at least once a week and review what the chapter covered. Reviewing and doing homework is where my attention has been recently as I try to learn new content and keep the maintain my performance with the old.
Anywho, yes, more chapter 11 and merge statements tonight (fingers crossed)!
UPDATE, now written and updated in previous post
I did however read chapter 11 yesterday (the first two lessons). I always read through a chapter once quickly knowing that I won't absorb every piece of information, but instead exposing myself to the information that the chapter involves. This seems to be the best way for me to learn new material... read through once to get the lay of the land, let it sink in for a day or two, then go back in and read through it again and do the examples provided by the book, then if needed go through it a third time. If done correctly this can be completed in 5 days to a week. Remembering the content is the more challenging part of it in which I need to go back at least once a week and review what the chapter covered. Reviewing and doing homework is where my attention has been recently as I try to learn new content and keep the maintain my performance with the old.
Anywho, yes, more chapter 11 and merge statements tonight (fingers crossed)!
UPDATE, now written and updated in previous post
Friday, April 11, 2014
Ch. 11 Sequence/Identity and Merge
Chapter 11 lessons one and two deal with sequence/identity and merge statements respectfully.
I've learned about these in the past, but it was great to review these ideas again learn a few new things about them. For example, the indentity property has been around in SQL for sometime now, but in 2012 Sequence was introduced. There are a few key differences between sequence and identity which I will get to in a minute, but first it's important to understand what these properties are used for.
Identity and Sequence both automatically assign a value to the column in a table once the row is inserted. Only one column in a table can have an identity property.
As of now the most common way that I see the identity property used has been in practice exercises such as my example of finding the sum of all rows in the table back in my chapter 5 homework (I remember this because I just did the homework Tuesday :)).. In that example you first had to create a table, then insert values; here's how that was done:
USE TestDB
IF OBJECT_ID('sumquestion') IS NOT NULL DROP TABLE sumquestion;
CREATE TABLE sumquestion (
ID INT IDENTITY (1,1)
Name VARCHAR(10)
);
INSERT INTO sumquestion (Name)
VALUES ('Alpha'), ('Beta'), ('Beta'), ('Charlie'), ('Charlie'), ('Charlie');
SELECT * FROM sumquestion
Notice that when I inserted values into table sumquestion that I didn't have to insert for ID, just name, because the IDENTITY property on the ID column automatically assigns a value for the row. Placing (1,1) next to INT tells the operator to start at number 1, and increase each value by 1 (this is the default).
Important items that are pointed out in this section are:
--
I've learned about these in the past, but it was great to review these ideas again learn a few new things about them. For example, the indentity property has been around in SQL for sometime now, but in 2012 Sequence was introduced. There are a few key differences between sequence and identity which I will get to in a minute, but first it's important to understand what these properties are used for.
Identity and Sequence both automatically assign a value to the column in a table once the row is inserted. Only one column in a table can have an identity property.
As of now the most common way that I see the identity property used has been in practice exercises such as my example of finding the sum of all rows in the table back in my chapter 5 homework (I remember this because I just did the homework Tuesday :)).. In that example you first had to create a table, then insert values; here's how that was done:
USE TestDB
IF OBJECT_ID('sumquestion') IS NOT NULL DROP TABLE sumquestion;
CREATE TABLE sumquestion (
ID INT IDENTITY (1,1)
Name VARCHAR(10)
);
INSERT INTO sumquestion (Name)
VALUES ('Alpha'), ('Beta'), ('Beta'), ('Charlie'), ('Charlie'), ('Charlie');
SELECT * FROM sumquestion
Notice that when I inserted values into table sumquestion that I didn't have to insert for ID, just name, because the IDENTITY property on the ID column automatically assigns a value for the row. Placing (1,1) next to INT tells the operator to start at number 1, and increase each value by 1 (this is the default).
Important items that are pointed out in this section are:
- There are differences when using delete and truncate and the identity property. Using delete in the where clause does not affect the value of identity, however truncate does.
- Identity does not guarantee the uniqueness of values.. if you try to insert a value into a table and the current seed of identity is 5, and the insert fails, this insert would have taken the value 6.. but because it failed, 6 will be skipped. Try to make another insert and it will insert 7, your identity column will now show 4, 5, 7, 8, etc...
- The following functions help you with identity: SCOPE_IDENTITY returns the last identity value in the session in the current scope. @@IDENTITY returns the last identity value generated in your session regardless of scope. IDENT_CURRENT accepts a table as input and returns the last identity value generate in the input table regardless of session
As far as SEQUENCE goes, it's important to remember that sequence is actually an object in the DB, not just a function. Here are the key differences between IDENTITY and SEQUENCE according to the book:
- Identity is tied to a particular column in your table so you can't remove the property and add it to something else
- Identity is table specific, sequence is not (because it's a file object)
- With identity you need to create a row before the value is assigned, this is not true with sequence
- You can't update the identity column
- Identity can't support cycling (meaning once it runs out of values it can't go back to the start)
- Truncate resets the identity value
The way that I remember the differences between the two is that first off SEQUENCE is a newer to SQL so so it comes with more customization. For example with sequence you're able to tell it to start at a minimum value, how much to increment by, a max value, whether it's allowed to cycle or not cycle, and a starting value. An example of the syntax of SEQUENCE is something like this:
CREATE SEQUENCE Sales.SequOrderIDs AS INT
MINVALUE 1
CYCLE;
--
So, as far as merge statement, it's just as it seems in the spoken word… it deals with moving data from one table to another based on certain clauses. Here is how tech net describes merge which I believe is very concise and simple:
The MERGE syntax consists of five primary clauses:
- The MERGE clause specifies the table or view that is the target of the insert, update, or delete operations.
- The USING clause specifies the data source being joined with the target.
- The ON clause specifies the join conditions that determine where the target and source match.
- The WHEN clauses (WHEN MATCHED, WHEN NOT MATCHED BY TARGET, and WHEN NOT MATCHED BY SOURCE) specify the actions to take based on the results of the ON clause and any additional search criteria specified in the WHEN clauses.
- The OUTPUT clause returns a row for each row in the target that is inserted, updated, or deleted.
Tuesday, April 8, 2014
Lunch Time HW 4-8 As planned
Here is my lunch time project...
/*USE TSQL2012
SELECT categoryid, MIN(unitprice) AS mn
FROM production.products
GROUP BY categoryid
--
WITH cte AS(
SELECT categoryid, MIN(unitprice) AS mn
FROM production.products
GROUP BY categoryid)
SELECT p.productid, p.productname
FROM Production.Products AS p
INNER JOIN cte AS c
ON c.categoryid = p.categoryid
AND p.unitprice = c.mn
--WHERE p.unitprice = c.mn
--
SELECT s.supplierid, p.productname, p.productid, p.unitprice
FROM production.Suppliers AS s
INNER JOIN production.Products AS p
ON p.supplierid = s.supplierid
WHERE s.country LIKE 'Japan'
--
SELECT c.custid, c.companyname, o.orderdate
FROM sales.Customers AS c
RIGHT OUTER JOIN sales.Orders AS o
ON o.custid = c.custid
WHERE o.orderdate = '20070212'
--
IF OBJECT_ID('anyname','if') IS NOT NULL DROP FUNCTION anyname
CREATE FUNCTION anyname (@empid, INT)
RETURN TABLE AS();
--
SELECT TOP (2) productid, productname, supplierid, MIN(unitprice)
FROM Production.Products
WHERE supplierid = 1
GROUP BY productid, productname, supplierid
ORDER BY MIN(unitprice) ASC
*/
--
WITH cte AS(
SELECT TOP (2) productid, productname, supplierid, MIN(unitprice) AS mn
FROM Production.Products
WHERE supplierid = 1
GROUP BY productid, productname, supplierid
ORDER BY MIN(unitprice) ASC)
SELECT s.country, s.supplierid, A.*
FROM production.Suppliers AS s
CROSS APPLY cte AS A
WHERE s.country = 'Japan'
/*USE TSQL2012
SELECT categoryid, MIN(unitprice) AS mn
FROM production.products
GROUP BY categoryid
--
WITH cte AS(
SELECT categoryid, MIN(unitprice) AS mn
FROM production.products
GROUP BY categoryid)
SELECT p.productid, p.productname
FROM Production.Products AS p
INNER JOIN cte AS c
ON c.categoryid = p.categoryid
AND p.unitprice = c.mn
--WHERE p.unitprice = c.mn
--
SELECT s.supplierid, p.productname, p.productid, p.unitprice
FROM production.Suppliers AS s
INNER JOIN production.Products AS p
ON p.supplierid = s.supplierid
WHERE s.country LIKE 'Japan'
--
SELECT c.custid, c.companyname, o.orderdate
FROM sales.Customers AS c
RIGHT OUTER JOIN sales.Orders AS o
ON o.custid = c.custid
WHERE o.orderdate = '20070212'
--
IF OBJECT_ID('anyname','if') IS NOT NULL DROP FUNCTION anyname
CREATE FUNCTION anyname (@empid, INT)
RETURN TABLE AS();
--
SELECT TOP (2) productid, productname, supplierid, MIN(unitprice)
FROM Production.Products
WHERE supplierid = 1
GROUP BY productid, productname, supplierid
ORDER BY MIN(unitprice) ASC
*/
--
WITH cte AS(
SELECT TOP (2) productid, productname, supplierid, MIN(unitprice) AS mn
FROM Production.Products
WHERE supplierid = 1
GROUP BY productid, productname, supplierid
ORDER BY MIN(unitprice) ASC)
SELECT s.country, s.supplierid, A.*
FROM production.Suppliers AS s
CROSS APPLY cte AS A
WHERE s.country = 'Japan'
Monday, April 7, 2014
Updating HW sets
I haven't written for a while because I continue to work through the homework sets, specifically the ones that I posted on chapters 6-10 (even though I haven't even written HW for chapters 9 and 10 at this point). My main focus has been on the XML part of it, because while I previously had been working with a lot of element centric xml, the HW that I posted contained an element/attribute combination. This totally through me for a loop... I was having a hard time remember how to find a value in XML that was an attribute, but the more I worked on it I began to remember that attributes are the easy ones because they are always in quotes, and can be queried by "@"! Once I figured that out everything made a lot more sense, but it did take me a number of trips through the book, as well as a post on SSC.
Anyways, that's my update, more posts as soon as I get through my self prescribed HW set! I think I'll spend time tomorrow during the day doing chapter 4, I'm also going to hold my self accountable by posting the HW that I do from chapter 4 on here tomorrow afternoon.
Cheers!
Anyways, that's my update, more posts as soon as I get through my self prescribed HW set! I think I'll spend time tomorrow during the day doing chapter 4, I'm also going to hold my self accountable by posting the HW that I do from chapter 4 on here tomorrow afternoon.
Cheers!
Sunday, April 6, 2014
Answers 6-10
Here's what I've got so far, working on the rest of Q's and A's
2) LIKE and CONTAINS are similar but against large text scans CONTAINS works better (faster). LIKE can't perform scans on formatted binary data, LIKE performs a pattern scan of each table, CONTAINS "appl(ies) specific transformations at index and query time", it's smarter and goes faster for big stuff.
3) Stemming is a fancy way of saying a word has a different tense... Past tense, future tense, etc (are there any others?) So a word like "drink" would be the Stemmer for, drank, drunk, drinking, drinks. This goes hand in hand with the last question about CONTAINTS() because CONTAINS can find the inflection of drink (meaning CONTAINS can find drank, drunk, drinking, drinks when you just tell it to find "drink"). Interestingly enough CONTAINS can also find a synonym for another word, meaning if I type in the word car, it could find the word "auto" or "automobile" if those words are in the Thesaurus that it uses (and you can edit the Thesaurus if you want).
Again with the XML....
4) Ok, so the main differences in the different types are that they each get laid out differently... recognizing how the nodes look, and their attributes is important, and something that I should be able to visualize... in case I can't here is some data that can help me:
IF OBJECT_ID('XMLForHomework') IS NOT NULL DROP TABLE XMLForHomework
CREATE TABLE XMLForHomework
(
ID INT PRIMARY KEY IDENTITY (1,1),
FirstName varchar(20),
LastName varchar(20),
FavoriteTeam varchar(10)
)
-- Add some stuff
INSERT INTO XMLForHomework VALUES
('Ben', 'Jamin', 'Tribe'),
('Chris', 'Smith', 'Sox'),
('Beth', 'Smith', 'Cubs'),
('Your', 'Mom', 'Yanks'),
('Your', 'Dad', 'Mets')
--
SELECT * FROM XMLForHomework FOR XML AUTO
SELECT * FROM XMLForHomework FOR XML RAW
SELECT * FROM XMLForHomework FOR XML RAW, ELEMENTS
SELECT * FROM XMLForHomework FOR XML PATH('Path')
--
DROP TABLE XMLForHomework
After looking at the different outcomes of the XML I should be able to tell the differences between them. My understanding is that you may need different forms of XML in different situations, and for different applications.
5) So, check this out, I kept on getting frustrated with this question because I wrote the first batch of code, and kept on getting an error, then fixed my "bugs" with the second line of code.. can you find the 3 differences between them? The correct answer is the bottom query.
USE AdventureWorks2012
WITH XMLNAMESPACE(
'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume' AS ns)
SELECT [Resume].value('(/ns:Resume/ns:EMail)', 'nvarchar(50)') AS emal
FROM HumanResources.JobCandidate;
--
WITH XMLNAMESPACES(
'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume' AS ns)
SELECT [Resume].value('(/ns:Resume/ns:EMail)[1]','varchar(50)') AS emal
FROM HumanResources.JobCandidate;
6) WITH XMLNAMESPACES(
'http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/Resume' AS ns)
SELECT [Resume].value('(/ns:Resume/ns:Name/ns:Name.First)[1]', 'varchar(50)') AS firstname,
[Resume].value('(/ns:Resume/ns:Name/ns:Name.Last)[1]', 'varchar(50)') AS lastname
FROM HumanResources.JobCandidate
WHERE
[Resume].exist('/ns:Resume/ns:Education/ns:Edu.Location/ns:Location/ns:Loc.State[.="IA "]')= 1
7)
a. So a note on this one, I got really confused when I started working with attribute centric XML because most of the work I had done up to this point had been in element centric. I actually had to post this question on SSC to obtain a hint on the answer.
SELECT
@x.value('(root/production.Categories[@categoryname="Beer"]/@categoryid)[1]','INT') AS catid,
@x.value('(root/production.Categories[@categoryname="Beer"]/@categoryname)[1]', 'NVARCHAR(50)') AS catname
b. This is another tough one because it's important how you define "everything" and I wasn't very specific... So I meant it as how do you return the entire XML document? Which would be this:
SELECT @x.query('*') AS everything
c. The key to problem c is to shred everything and make a big table first with all of the data in it (a cte) then query that table, here's what that would look like:
WITH CTE AS(
SELECT
categoryid = Foo.value('(@categoryid)','NVARCHAR(250)'),
descrip = Foo.value('(@description)','NVARCHAR(250)'),
categoryname = Foo.value('(@categoryname)','NVARCHAR(250)')
FROM @x.nodes('/root/production.Categories') AS Tbl(Foo)
)
SELECT * FROM CTE
WHERE categoryid BETWEEN 9 AND 10
8) There are two types of indexes for XML a primary index, then after you have a primary index you can have a secondary index that has three different flavors: Path, Value, and Property. The primary index "contains a shredded persisted representation of the XML values"... basically for each node a row is created, then you can search the rows. Path in the secondary index speeds up queries that use exist() or value(). Value helps if you use queries that are value based or have wildcards. Property is useful for queries that "retrieve one or more values from individual XML instances by using the value() method."
9) The differences between element centric and attribute centric XML in my head are first off the way that they look... element centric uses a lot of nodes, which are things that look like this <root> value</root>, and attribute centric looks like this root = "value"... or something to that affect. Either are fine, however with element centric XML you may want to describe a namespace.
10-12) /*USE TestDB
IF OBJECT_ID('QuestionTen') IS NOT NULL DROP TABLE QuestionTen
--
CREATE TABLE QuestionTen(
PlayerID INT NOT NULL,
FirstName VARCHAR(25) NULL)
--
ALTER TABLE QuestionTen
ADD Lastname VARCHAR(25) NOT NULL
--
ALTER TABLE QuestionTen
ADD CONSTRAINT PK_PlayerID PRIMARY KEY(PlayerID)*/
13) Well, with a primary key you can only have one per table, the primary key can not have a NULL value, and it must be unique. A unique key is just like it sounds, it is unique but it can be NULL ( I think there can only be one null unique key per table though, because then it would still be unique).
14) Sticking with table "ChapterTen" here is what adding a default constraint would look like:
ALTER TABLE QuestionTen
ADD AddedDate date DEFAULT GETDATE()
15) Again, sticking with table "ChapterTen" here is what adding a check constraint would look like:
ALTER TABLE QuestionTen
ADD CONSTRAINT Chk_FirstName CHECK (Firstname <> 'Bob')
--This Insert will error out because FirstName = Bob
INSERT INTO dbo.QuestionTen (PlayerID, FirstName, LastName)
VALUES (1, 'Bob', 'Smith')
8) There are two types of indexes for XML a primary index, then after you have a primary index you can have a secondary index that has three different flavors: Path, Value, and Property. The primary index "contains a shredded persisted representation of the XML values"... basically for each node a row is created, then you can search the rows. Path in the secondary index speeds up queries that use exist() or value(). Value helps if you use queries that are value based or have wildcards. Property is useful for queries that "retrieve one or more values from individual XML instances by using the value() method."
9) The differences between element centric and attribute centric XML in my head are first off the way that they look... element centric uses a lot of nodes, which are things that look like this <root> value</root>, and attribute centric looks like this root = "value"... or something to that affect. Either are fine, however with element centric XML you may want to describe a namespace.
10-12) /*USE TestDB
IF OBJECT_ID('QuestionTen') IS NOT NULL DROP TABLE QuestionTen
--
CREATE TABLE QuestionTen(
PlayerID INT NOT NULL,
FirstName VARCHAR(25) NULL)
--
ALTER TABLE QuestionTen
ADD Lastname VARCHAR(25) NOT NULL
--
ALTER TABLE QuestionTen
ADD CONSTRAINT PK_PlayerID PRIMARY KEY(PlayerID)*/
13) Well, with a primary key you can only have one per table, the primary key can not have a NULL value, and it must be unique. A unique key is just like it sounds, it is unique but it can be NULL ( I think there can only be one null unique key per table though, because then it would still be unique).
14) Sticking with table "ChapterTen" here is what adding a default constraint would look like:
ALTER TABLE QuestionTen
ADD AddedDate date DEFAULT GETDATE()
15) Again, sticking with table "ChapterTen" here is what adding a check constraint would look like:
ALTER TABLE QuestionTen
ADD CONSTRAINT Chk_FirstName CHECK (Firstname <> 'Bob')
--This Insert will error out because FirstName = Bob
INSERT INTO dbo.QuestionTen (PlayerID, FirstName, LastName)
VALUES (1, 'Bob', 'Smith')
Thursday, April 3, 2014
Going through homework and reviews with limited time
The last few days I've been scrolling through my previous posts reviewing things such as pivoting, ordering, windowing, and the like in an effort to refresh my brain on the syntax for T-SQL. One of the questions in chapter 5 I think it was on grouping has to do with creating a table then inputting data like 'alpha', 'beta', etc which I think is good practice in itself... how to create a table, because creating views is similar although you have a few more options with a view.
I'm getting antsy to take the exam again but I know i need to be patient and work through what I've done so far. Time is my biggest obstacle right now, but I believe that if something is important to you, you'll make time for it. This is important to me, so working on homework during lunch, or at night is my best option right now. That's why I haven't been doing many posts on here...
Ok back to working on Chapter 5 "all lesson sets"
Cheers!
I'm getting antsy to take the exam again but I know i need to be patient and work through what I've done so far. Time is my biggest obstacle right now, but I believe that if something is important to you, you'll make time for it. This is important to me, so working on homework during lunch, or at night is my best option right now. That's why I haven't been doing many posts on here...
Ok back to working on Chapter 5 "all lesson sets"
Cheers!
Tuesday, April 1, 2014
Momentum from Saturday carrying me through exam & #sqlhelp
I'm still feeling the Saturday momentum that I got this weekend especially after being on twitter more today, and checking out some of the posts about MVP's.
A cool thing that I didn't realize is that if you put in the hash tag #SQLhelp a lot of people will respond to your post. I haven't written this in the forum yet, but I will tomorrow. Tonight I did some pivoting & chapter 5 homework, and got a massage for my ailing shoulder (which is feeling better).
I am going to finish chapter 5 homework tomorrow at lunch hopefully, then another rep of it tomorrow night, chapter 7 homework on Thursday, then back to reviewing 6-10. A full week of reviews for me, followed by nailing down the remaining chapters (writing out cliff notes and reviews) next week. One more week of reviews then the test! That's the plan at least…
Hopefully a month from now I'll have 70-461 under my belt!
Cheers all!
A cool thing that I didn't realize is that if you put in the hash tag #SQLhelp a lot of people will respond to your post. I haven't written this in the forum yet, but I will tomorrow. Tonight I did some pivoting & chapter 5 homework, and got a massage for my ailing shoulder (which is feeling better).
I am going to finish chapter 5 homework tomorrow at lunch hopefully, then another rep of it tomorrow night, chapter 7 homework on Thursday, then back to reviewing 6-10. A full week of reviews for me, followed by nailing down the remaining chapters (writing out cliff notes and reviews) next week. One more week of reviews then the test! That's the plan at least…
Hopefully a month from now I'll have 70-461 under my belt!
Cheers all!
Subscribe to:
Posts (Atom)

