SQL Webinar Final
SQL Webinar Final
Webinar
March 2 | 2023
2
What is Data?
3
Types of 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
16
How to do Debugging
17
Coding Standards
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 !