0% found this document useful (0 votes)
30 views21 pages

SQL Webinar Final

This document provides an overview of an upcoming webinar on SQL and database concepts. It includes sections that define SQL, different data types, what a database and DBMS are, examples of DBMS tools with a focus on Microsoft SQL, cheat sheets on SQL statements and functions, how to create and execute stored procedures in SQL, guidelines for SQL coding standards and formatting, and tips for SQL query optimization.

Uploaded by

Yaswanth Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views21 pages

SQL Webinar Final

This document provides an overview of an upcoming webinar on SQL and database concepts. It includes sections that define SQL, different data types, what a database and DBMS are, examples of DBMS tools with a focus on Microsoft SQL, cheat sheets on SQL statements and functions, how to create and execute stored procedures in SQL, guidelines for SQL coding standards and formatting, and tips for SQL query optimization.

Uploaded by

Yaswanth Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

SQL – Zero to Hero

Webinar
March 2 | 2023

© 2023 Infinite Confidential


What is SQL?

2
What is Data?

3
Types of Data

Structured Data Semi-Structured Data Unstructured Data

All data which can be Data that does not reside in a Not organized in a predefined
stored in relational database but that manner or does not have a
database SQL in a table has some organizational predefined data model, thus it
with rows and columns. properties that make it easier is not a good fit for a
to analyze. mainstream relational
Example : XML , JSON database.
Example : PDF , Media Files

4
What is a Database?
An organized collection of structured information, or data,
typically stored electronically in a computer system

5
What is a DBMS?
DBMS are tools or software that are used to manage the data

Below are examples of such tools. We will deep-dive into MS SQL in the following section

6
Cheat Sheets

© 2023 Infinite Confidential


8
9
10
11
12
13
14
How to create an SP?
-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName>
-- Add the parameters for the stored procedure here
<@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>,
<@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here


SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
END
GO
15
Executing the SP
• EXEC SP_Name

16
How to do Debugging

17
Coding Standards

General Rules Indexes Rules:


• Do not use spaces in the name of database objects • IX__ Examples: IX_Products_ProductID
• Do not use SQL keywords as the name of database objects In
cases where this is necessary, surround the object name with Primary Keys: Rules:
brackets, such as [Year] • PK_ Examples: PK_Products
• Do not prefix stored procedures with ‘sp_’2 Prefix table
names with the owner name3
Foreign Keys Rules:
• FK__ Example: FK_Products_Orderss
Table Rules
Defaults: Rules: DF__ Example: DF_Products_Quantity
• Pascal notation; end with an ‘s’
• Examples: Products, Customers Group related table names1
Column Rules:
Stored Procs Rules • If a column references another table’s column, name it ID
• sp_[_] Examples: spOrders_GetNewOrders, Example: The Customers table has an ID column The Orders
spProducts_UpdateProduct table should have a CustomerID column

Triggers Rules:
• TR__ Examples: TR_Orders_UpdateProducts
• Notes: The use of triggers is discouraged

18
Formatting
• Use upper case for all SQL keywords
o SELECT, INSERT, UPDATE, WHERE, AND, OR, LIKE, etc.
• Indent code to improve readability
• Comment code blocks that are not easily understandable o Use single-line comment markers(--)
o Reserve multi-line comments (/*.. ..*/) for blocking out sections of code
• Use single quote characters to delimit strings
o Nest single quotes to express a single quote or apostrophe within a string For example, SET @sExample = 'SQL''s Authority'
• Use parentheses to increase readability
o WHERE (color=’red’ AND (size = 1 OR size = 2))
• Use BEGIN..END blocks only when multiple statements are present within a conditional code segment.
• Use one blank line to separate code sections.
• Use spaces so that expressions read like sentences.
o fillfactor = 25, not fillfactor=25
• Format JOIN operations using indents o Also, use ANSI Joins instead of old style joins4
• Place SET statements before any executing code in the procedure.

19
Query Optimization
• Optimize queries using the tools provided by SQL Server5
• Do not use SELECT *
• Return multiple result sets from one stored procedure to avoid trips from the application server to SQL server
• Avoid unnecessary use of temporary tables o Use 'Derived tables' or CTE (Common Table Expressions) wherever possible, as they perform better6
• Avoid using <> as a comparison operator o Use ID IN(1,3,4,5) instead of ID <> 2
• Use SET NOCOUNT ON at the beginning of stored procedures7
• Do not use cursors or application loops to do inserts8 o Instead, use INSERT INTO
• Fully qualify tables and column names in JOINs
• Fully qualify all stored procedure and table references in stored procedures.
• Do not define default values for parameters. o If a default is needed, the front end will supply the value.
• Do not use the RECOMPILE option for stored procedures.
• Place all DECLARE statements before any other code in the procedure.
• Do not use column numbers in the ORDER BY clause.
• Do not use GOTO.
• Check the global variable @@ERROR immediately after executing a data manipulation statement (like INSERT/UPDATE/DELETE), so that you can rollback the transaction if an
error occurs
o Or use TRY/CATCH
• Do basic validations in the front-end itself during data entry
• Off-load tasks, like string manipulations, concatenations, row numbering, case conversions, type conversions etc., to the front-end applications if these operations are going to
consume more CPU cycles on the database server
• Always use a column list in your INSERT statements.
o This helps avoid problems when the table structure changes (like adding or dropping a column).

20
THANK YOU !

© 2023 Infinite Confidential

You might also like