Practical Sql - Microsoft Sql Server T-SQL for Beginners

25
Practical Sql Microsoft Sql Server T-SQL for Beginners By Mark O'Donovan

description

A FREE SAMPLE OF THE BOOK 'Practical Sql' With Practical Sql you will stop being a novice when it comes to using the Microsoft Sql Server database and learn a useful skill. Practical Sql will provide a concise guide to sql server development with the main aim to prevent boredom. Nobody really likes reading IT books they just want to know how to do X, Y or Z. With Practical Sql you will learn: How to Install Microsoft Sql Server. How to Create Databases, Tables. How to manage the data in tables. Functions to changes field values. Conditional Statements. Creating checks on the data that goes into the table. Database Design rules. Using Triggers to create audit tables. How to use Transactions and Exceptions. Using and creating your own templates for faster development. and more... The examples within this book were created using Sql Server Express 2012.

Transcript of Practical Sql - Microsoft Sql Server T-SQL for Beginners

Page 1: Practical Sql - Microsoft Sql Server T-SQL for Beginners

Practical Sql

Microsoft Sql Server T-SQL

for Beginners

By Mark O'Donovan

Page 2: Practical Sql - Microsoft Sql Server T-SQL for Beginners

���������� ���������������

�����������������

�� !"� #�$%�&'�$ (

�� !"��#%&("�$%�&'�$ &

Page 3: Practical Sql - Microsoft Sql Server T-SQL for Beginners

���������

������������������������������������������������������������������������������������������������������������

������������������������������������������������������������������������������������������������������������

���������������� ����������������������������������������������������������������������������

�������������������������������������������������������������������������������������������������������������

�������������������������� �!����������������������������������������������������������"

������ ���������

���������������������������

����)*�+,���)���-����.����-/�01������/� ��23�������������������������������������������������

��������������������

�01�����������.�������������������������������������������������������������������������������������������������������������������%

�01������!�4������-��,�����������������������������������������������������������������������������������������������

�+�������5������������������������������������������������������������������������������������������������������������������������

����)���6������������������������������������������������������������������������������������������������������������������������'

7���1������+������������������������������������������������������������������������������������������������������������������(

�+�������1��-�����������������������������������������������������������������������������������������������������������������������%

�+�����������-�������)�����������������������������������������������������������������������������������������������������%

��������

������"���������� ������������������������������������������������������������������#�

���������� �����������

�����������������������

������"��,����������������������������������������������������������������������������������������������������������������������(

7��)�"��,����������������������������������������������������������������������������������������������������������������������$

8���.,������927�*��9�����������������������������������������������������������������������������������������������$�

Page 4: Practical Sql - Microsoft Sql Server T-SQL for Beginners

���-,�.�����������������������������������������������������������������������������������������������������������������������������$�

�����7���������������������������������������������������������������������������������������������������������������������������������$'

�����������������

���� ��!�����������

�!�2*9���)���������������������������������������������������������������������������������������������������������������������$(

�212�9���)���������������������������������������������������������������������������������������������������������������������'

:5��92���)��������������������������������������������������������������������������������������������������������������������'�

�21292���)���������������������������������������������������������������������������������������������������������������������''

��������

������#�$�������%!��������������������������������������������������������������������������&�

�"���#��$� �������

�������$���%�

����� �������������&��������

��'�����$�����������

��)������-��;�������������������������������������������������������������������������������������������������������������������&(

<���9������������������������������������������������������������������������������������������������������������������������������������(

�����;���"������������,.�������������������������������������������������������������������������������������������������(=

�,�����<������������������������������������������������������������������������������������������������������������������������������(&

��)6�����*�4�����������������������������������������������������������������������������������������������������������������������((

��������

������'����(�������(��������������������������������������������������������������������)#

�(�����������������������

�>2�?����������������������������������������������������������������������������������������������������������������������������%$

��������>2�?��!�9*��!9,��������������������������������������������������������������������������������������%$

5��)���?�������������������������������������������������������������������������������������������������������������������������������%&

Page 5: Practical Sql - Microsoft Sql Server T-SQL for Beginners

����&#��������%

������� �)���� ������

��*��+����

��,�����������%

�.����7,�.��������������������������������������������������������������������������������������������������������������������������&

9�6��"���,��-,�.���������������������������������������������������������������������������������������������������������������$

�,��"���)��7,�.���������������������������������������������������������������������������������������������������������%

%�� ��� &�����

�����$$�������

9������������6���������������������������������������������������������������������������������������������������������������$(

@�������-@9����������������������������������������������������������������������������������������������������������������������'�

-�(��������(���$����%

������./��#�������%�

����������������%-

��������

����!�����������������������������������������������������������������������������������������������������#

%!�*$��������������������������������������������������������������������������������������������������'

+���!�,�-��������������������������������������������������������������������������������������������

��.����-�+�������A�6��������������������������������������������������������������������������������������������������&(

����(��.�/,�-����-����$!����������������������������������������������������������)

����0��������������������������������������������������������������������������������������������������������1

Page 6: Practical Sql - Microsoft Sql Server T-SQL for Beginners

%

��# ��$!���

�����������������

!�����-����,6��.����)��6�������,.��������)��������-��)��6����)����

4���,4�������)������-��)���,����

(������&����

����,�����,�������,6����������)���������--������,��������-��)���������

6���4��.����.�������)�B���,�������,6�������������,)��������6����.���)���

���6������������-���������B��)���B������,����.�,���6����������)�������B4�����

�,.����������)����������,�-��)��������.�B�..����B���������.�,���

��-��)�������������6���������4���,4������B������3����������)������

9���#5��.�.���+�"��.����-�+�������9"�01-�� ��������

C������#��

Page 7: Practical Sql - Microsoft Sql Server T-SQL for Beginners

��0�+������ ���&#���(����

9���3�)���������6���,����+�������� ��23�����4��.������-4��������6����-���

��������-�+�������������6��-����4�����-��)��.����-�

��)����������3�)����-��,��4�����6���.��6���4�������-��)����4���������

�#

��#DD���.�.��"�+��.�)

�-��,����������6��)���4������������)�����������)�����-�E���.�.��"�+��.�)-������

��,����.���.������#DD���.�.��"�+��.�)D����.����3�

9�������,��6������6��)4���,����������3��.�������������������-�+�������6,4�

.���������� F�,�����������3�)����4����,���������,����������-��.����-�+�

������6,��)�-,�.����)�����--�������3����������,����������,.�����-��)�

-,�.����

Page 8: Practical Sql - Microsoft Sql Server T-SQL for Beginners

-

1������+�

�6���-�������4�-����--������.����.�������4�������6���#

������������������������������ ������

9�����.���������������-��)��4���������������������+��������.��������+��6��

������-��)���6���.+,������������4�������6���

�!�#������

9���.����������,.����6���B��4�,����6������4���������4�������

�!�#������

>���4�.������4����-���.����-�+��������3���������������������-����-4����

�!�#������

��.��+����������6��������������.�����4��������,���6���-�,��-����-4������

��4����������+�������-����-����)��

�����������������$�����(�����#&����

��.���������,.����6���.��-�+��������)����)�����4�.�������6����B�6������

-����������-,���)�������)��������-��)��6���.�������������,�����

�!�#�����

9���.�����4������4��,��4�.������,�-����+����������6�������������,����������

�-�+����������6�����

�!�#������

��.���,����.�������,����6������.�����4����3�������������3�)�����-.�������+�

�������6����������,�����

�!�#������

����-����.������-�����.���4�������,.���6���.�+����)���������B+,���B

,���������������-��)��+��6���

Page 9: Practical Sql - Microsoft Sql Server T-SQL for Beginners

����������2 ����� ���������

��.������������+��������)����)��������.����4����-���,�������B.������

.������������)������;�������6����-����������

�!�#������

9���.�����.�������4����-��).��.,�����������,���-����,.�����4��,)�����

�,)6������-������.�,����,)6���-��4�����6���

�!�#������

!�34�.���������,�-,�.����������4,��)����,��������-������������,�4����

�!�#������

�������������)�������4,����,����--�������,������������������,��-�������-

�������3���������7���3�)�����,)���4�����,�����--�������,��������������

�)��-�������)����4���4�����+,���4���3�.,���

�!�#������

A��������������+����6����)����-��������������,�������,��4���6���)���

�������6���9���.�����.������������,�4�����,.��.�)6�������������)���

�6����

Page 10: Practical Sql - Microsoft Sql Server T-SQL for Beginners

��������������3� �� ��!��3������

!�4����6���.��-�+��������)������6���.��������.���$.��������6������,.���

)����������)���.���+,���,.���,���������5��.��,���B7,�.�������C��4�B9�������B

���6����������,������),.�)����

�!�#�����

9���+����������.������,������.��.��������6�������������6���������B���

.�����4������4��,4����.���������,�.���������

�!�#������

9�)���������,��-,��+�������-��,���A�4������4��,��4�,��9�)�����������,�

��,��+��������)���������,�)����.����.�������.����������4�.������,��4�

�+��������)������

�!�#������

A�����,�����������)���.�)���3+,�������,.�������)���������.��,�������

���.��6�������������6�������3�.,��4�����������������)������������

A�4���.�����.������������.��,��������������)���������������.��,����

�!�#�������

C��4�����4��,�����.�)���3�212�9���)�������,����������,����)�������.

���)����:����C��4.��������,.��������������������.�,����-.������)����

��������������+�����,��������������

�!�#������

!�34�4���.������--���������--,�.������.��6�.����������4���.�)�����

��������.��,����

�!�#�����%�

������)�����������-���6�������������.�����,��-,�4�����,�����������.�����

,�����-����.���),��������6�����

A�4������4��,��4�.�������)�����������)�4������,����6����

Page 11: Practical Sql - Microsoft Sql Server T-SQL for Beginners

�!�#�������

9�����������4��,��3�.,���)��+�4������.����������������6������6���A�

4���.������4�.���������,�����������,����)-�������,��,�������,.����,��������

���������,����-��).�������6����

�!�#�����-�

A�����,������������+�-��������)�,���-������������-�����6���4���6�

��.���������)����)������A�4���.������-,���)�����,����-���6���������.�����

���)��-��)��

�!�#�������

����+������������,.��6��3��.��������.����������6�����6��)��)����..,�4����

��,�.������,�������+�23.����������4��������������-����.�,����-�.�������

4�����,��3��.��������..,���

�!�#�������

7������4�.����9�����.�����9�����.���������@�����������@�����+�4�����G�,)���

������,)6���-.��������6���4������,���������.��,��6,4�������.�������6�

��������������������6������������3�.,�������-�����)���B�����.����4���

����4�������

Page 12: Practical Sql - Microsoft Sql Server T-SQL for Beginners

���.�����������"�����,.���

������������������������������ �������

������ �������

23�����������4�����!��������������56 �����������

9���,������-���6��������.���,����+����6����������)����������.�������,4���

������+��������)������.����-�+�������,��������������-�+�-��)��.����-.�����9"

�01H9"�01�����-��9�����.�01I�

A������������������������4�����-����������6�������4���)����)�,��

������

A����5��.�.���������-6�������@5��.�.���+�@�����.�����,4���6��,����-��)��

6���.��)��������.���.���+,���

9������,.����������.��.���4����-�.,�6������,��-,��3�)�����9����)��������

��,4���6�)������������)�)6��4���������6�,����.���.����4�����-��6�.����

�3�)��������������-��+,�����

)�������������

@5��.�.���+�@4���.�������+��������)������,���,���������.����-�+�������

���6�������)�

�+��������������)��������.��)����95��-��������������6,��������������������

��6���.��������)��������.���.���+,��.��6��.+,�����������

������4�����+��������)��������������4���6��-,���;,�� �@���������������

9�������)��������.��������������+����������6��������)����4������-���6�����

6�.�)����.�))����+,���)���

Page 13: Practical Sql - Microsoft Sql Server T-SQL for Beginners

���.�����������"�+�����������������

��������������������������

9�������������-�+�������23�������������-��4���B���,����.�,��������6��)����-����

G�,.����4�������.,������������-�+�������23�����-��)��-����4�����.����

��#DD444�)�.����-�.�)D��"�6D��4�����D����������3J��K�% =�

����.��,�����,������������6��4��������3�)���������6���4��������������

��.����-/�01������/� ��23������������,�����4������6�6������4�����������������

�-�+�������23�����4�.���������,�������

� ���&�4������&�����0���5������0�6���7�������6�����./#������

��##���� �1#������$�� ���&�

A����4�&BA����4�������� (*�BA����4�������� (�����.�5�.��BA����4�C���

�����.�5�.���

��8����� ���&��

��)�,��4��������.�)���6���8>L��-�������.�����H�8>L��-������

��.�))������I

%�8����� ���&��

��$8>L��-�������.�����

����),)�-'��� �-*��H�8 ��)�������.�))������I

���8 �-������6�������������.�

Page 14: Practical Sql - Microsoft Sql Server T-SQL for Beginners

���.�����������"�+�����������������

�� ���.���.��.�6�3���4���������������-�012M5*��CN3=$N2!:��3�-����,�

����,���������������6�H3($I��=$6�H3=$I�������B�����������4��,���-��

���6���4��#2!:O3=$O�012M5*A9N3=$N2!:��3�

�� ��.�����4��������.�)�������,6��.��.�����-��������������������

��7����������������������������

�� ���.�������������)��,�����������-���������-���.��������������.

@!�4�01����������"��������������������-��,�������3���������������@�

Page 15: Practical Sql - Microsoft Sql Server T-SQL for Beginners

���.�����������"�+�����������������

��7����������������#�

7����������&��

$� 2���,��������.������)��-����-4������,��6�����������

'� 9�.���6�3@��..������.������)�@���.��.���3�

���,����,���4������������4�����.���@����-��,��,��������

��.����-@������

,�����������������

=� ?�������-�,����������.��.���3��

Page 16: Practical Sql - Microsoft Sql Server T-SQL for Beginners

%��.�����������"�+�����������������

������������0�$��������

&� ������3�)���4�.��������������01� ���

����������0�$��������

(� ?�������-�,����������.��.���3�

Page 17: Practical Sql - Microsoft Sql Server T-SQL for Beginners

���.�����������"�+�����������������

(��������.�$�������0�$��������8�����������0�$��������

%� ���.������������3������������������4���-�����

� � 9��.,����,������,���������6���@���.�-��01��)���������@����

(����(�����������

��� 9����6���4�����-�,���.�����-�����6���B6�.�,�������-�����9������,����

�����6�.������,�������,����������)������.�����,����������

��� :��������.�O7�12�9*2��"9����6�4�������+,������.������-����������������

.�����4�#�����$�

��� ���.���3�6�����������������

Page 18: Practical Sql - Microsoft Sql Server T-SQL for Beginners

-��.�����������"�+�����������������

�$� ��.��������������-�+����������.�)����������,��4������4�-��������6���

����������,�����������������-.�����-��,���B�����4�6���4#

�'� 9����4�������6����������������������-�������,.��-����,)�����-��)����

��������������..,�����,�����������������

�=� ���.������

�&� �������@�01���������������������@4����4����������,�.�)�,���

Page 19: Practical Sql - Microsoft Sql Server T-SQL for Beginners

���.�����������"�+�������9�,�

������������������

�-���4�����������������+��������������)��4�4��������6���-�,��-���+�������

�������)������-�

9���.�����4�����,����,�����+����������6�����)��������6,����)H����.������-

��,���.�)��������4��+�������I���-�)����������,����-4����)��-��-��,���B

�����.�����6���..��-��,������-�+���������

9��)����+��������������,�4���6�,����-����.��-��,���������������)��������

.�������@�+�������������)���,���@-��)��4�����4���6���-����������5��

��7�����������������

�����.�����6���.����������)����,�����6�.����,���-��,�.�)�,���

9���+��������,��,���������.��B��,.�����4���������.�����������4���-���3�)����-��,

���#

���5�����)�"�01������� ��"���-��,�����9����"�01���������-��,������������

���.������+������������.������������,4�������������.��������,�����

�����������9��7���:�� �

9��)��������.�-���+��������G�,4�����6��6����..����+��������-��������.�����

�,������

�����������2$����9��7���:��

9��������.�����4���,����-��)�����,�������.�,�������.���,�����-;�6�-���3�)����

6�.�,���,��+����������6�������3�.,��+�������.,����)��

�����������3��+�����

9���+�������6��4�������������-��)����������+,����4���.����.������+����������

������4��.����),�����������������-�+�������������4��������-������6���+��������

4���������������+��������

9�-����4���3�)���������6�����,���,��������������������.��

Page 20: Practical Sql - Microsoft Sql Server T-SQL for Beginners

����.�����������"�+�������9�,�

7��)���+�������.��-��,�����)��������,.��.��-��,����.��-���������.��B-���3�)���

�-������,�)��.����������..�,�����,�����,��������.��

9����-�,��..�,�-�����+������������.���!9�����.�O���01P�01� ��

9���..�,�����,�����,������.�����)�����4������+��������������..�����

-�������)-���3�)���4��������6�.�,���������������

Page 21: Practical Sql - Microsoft Sql Server T-SQL for Beginners

���.�����������"�+�������9�,�

��7��������;��+��<����0�$��������

A�������+�������!�4������-��,�������,.��.��-��,��4��.�����.���.���..�����,�

�+��������

9������.������;,���--����4�����4��.���4��.�)�����������������-��)��,��+�

�����������������

9�������.���4��.�4�4���.������3�����������)���B!�)��5�������9�5D�5�

�!��� �5�&�� "��-�,����6���

9�������.������4�,�����,�����������)�.�)�,���.����.����+��������

;�&� �)�#��"��-�,�����6���

!�)�����������-��,��������4�����.������������)�������,��9�5D�5�.����.

����+�������

��-�,���)������#OO�O����O���01P�01� ��O�+�O+,���

9���.����.�������+�������,������)�������#

�� *���.��.�����@!�)��5����@����.���������.5���������

�� �������������#

OO�O����O)���

�� 8���+������������.��B����.��.������01�������������.������

$� ���������������.�����."���6���2������

�������-����������)�����������#

OO�O����O)���

Page 22: Practical Sql - Microsoft Sql Server T-SQL for Beginners

����.�����������"�+�������9�,�

9��������-��,������)�����������������)�������,��9�5D�5.��.����.

���,����6����

��)=�)"��-�,�����6���

9�����������.������,4��������.��-��,�������4��,��+��������6��..����-��)

����)�.���������,���4����

)�����������

.����� "������G������6��������.���

>��#�2����H)������.����I"9�������4�-����.����.�������+�������.��.�"���������

����-�,�� H� ��.����I�

7������2��"�5���������,���-����������������H��-�,������I�

�)�2 �����������

:����.��-��,������������������,��������������

7���3�)����-��,������,��+��������6��..�����6�������)�.����B��,4�,�������

�����������������)����,���4������.���������6���B-���3�)���#

9����-�,����-���+����������$��B�+��������3�����,�������)�.����6���-�,�

��)�( ��&���)����"�����6�����,�����.�����

A���,��������)�.��������������+�������,���.������4����+��������������

��)�)���"�-��,4�����������������@.�������,.���������������9�55��

-�����!�.���������+,���������-����4���3�)���������6����

Page 23: Practical Sql - Microsoft Sql Server T-SQL for Beginners

����.�����������"�+�������9�,�

�����������)��#�������

9������������,�����������-���+���������.��6���,�����������

��������6���������,�����+��������������)��4�4���.�����-�4�-��,��-,����������

��,)��4����.����������

9����4�����������-����,��+���������������������.��.������+��������.������

�6;�.�3�����������4�#

������-���������-��@������5��������@���������������������,4����������

������������������������-����4��������#

"��������

9���������������������������,�����"��������������,.�������������-���+����������

����������-��4����4�������������)�

G�,.�������������������������,.��+�.�))�����,.�����-����4���#

�212�9

��!C2*9H�����)�B�2*C2*5*�52*9GH@��������)�@IIB

�2*C2*5*�52*9GH@5���,.C������@I��5���,.C������B

�2*C2*5*�52*9GH@5���,.1����@I��5���,.1����B

Page 24: Practical Sql - Microsoft Sql Server T-SQL for Beginners

����.�����������"�+�������9�,�

�2*C2*5*�52*9GH@2�����@I��2�����B

�2*C2*5*�52*9GH@2�����2�����@I��2�����2�����B

�2*C2*5*�52*9GH@��������@I����������Q

9�����,�-��)���+,������,��������)�������-����4����.�������#

5�&�� �

9����������4����+�������)�)���������B�,.�����)���),)���)�3�),)�)�,��-

)�)�������4���6�,���6����+��������

������� �

9�����������4���,���4������+��,����.����������4���6�,�������������+�

�������A����4��,����.����)���������-�,�������

������������

9��@����4��)��.����.��������������@����)���)�������������������B

����.������-��,�����������.�����,���������������������������..�����,��+�

���������6����

(��������������$��

>�����4������,������-�,���.�����-�������H���+����������6���-����IB1��H�+�

����������I���6�.�,��#

���

�#O5�����)7����O��.����-�01������O���01����01� ��O���01O��9�O

1��

�#O5�����)7����O��.����-�01������O���01����01� ��O���01O��9�O

�.�,�

�#O5�����)7����O��.����-�01������O���01����01� ��O���01O �.�,�

Page 25: Practical Sql - Microsoft Sql Server T-SQL for Beginners

����.�����������"�+�������9�,�

2 ����� ��

9�������)�������������������9��)���)������������)�)�����#

��-�,�����,���")����,�����.����.-����,�.�,������6��4����-����--����.��

6�4���2������H:�I��� �����2�������9����-�,�����,����������,���4���.��������4

�������

)��&��������

9�����)��������������4���.�))���������,�������.���3�.,������������

7����4�-��,����.�����������,,����.����.����+�������B�������.��

@2--�.���@�6���6��)�-��������,4�����������-���)�������4��.����6���.�����

����-���������������.����4����.����.������+�������B-���3�)���#

� �*2�92�!G��9� ��2

��������������.��.������+����������6����

�-��,����.�������@!9�:9>�*�9GO�G�92�@��,4��������������-�--�.������)�������

��)��������.������,�������

A�������4��������.������.����.���,��+����������6���������������������

�����.����������6������������������)�����������.,������+,����

� ���&� ���������

A�����,-����������+��������������$���6�����������.������

9����4����$����)���6�����3���������6����-����������6;�.�3������B������

�3������@����)���6����@-������