Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Database Design

Similar presentations


Presentation on theme: "Relational Database Design"— Presentation transcript:

1 Relational Database Design
Structured Query Language (SQL)

2 A Database Structure

3 Database Building Blocks

4 Structure of a Relational Database

5 Training Database Schema

6 Course Outline Executing a Simple Query
Performing a Conditional Search Working with Functions Organizing Data Retrieving Data from Tables Presenting Query Results

7 Executing a Simple Query
Connect to the SQL Database Query a Database Save a Query Modify a Query Execute a Saved Query

8 SSMS Shortcuts

9 Same category of information
Tables Header row Row Same category of information Column Related information

10 Information retrieved
Databases Database Table 1 Table 2 Table 3 Table 4 Information searched Database objects Information retrieved

11 Databases (Cont.)

12 Databases (Cont.) Default databases Customized databases

13 Servers Higher performance No user Manage resource Provide services
Network Client 1 Client 2 Client 3 Server

14 Clients Client 1 Client 2 Client 3 Server Connected to the same
network as the server Displays result Requests the service of the server

15 Syntax Clauses in precise order Placeholders

16 SQL Statements Appropriate syntax Optional clause Essential clauses
Column names Essential clauses

17 Queries Table name Column names Condition Database Output

18 SQL Logical Query Processing Steps
Clause Order Logical Processing Steps SELECT FROM JOIN____ON____ WHERE GROUP BY HAVING ORDER BY FROM ON OUTER WHERE GROUP BY CUBE | ROLLUP HAVING SELECT DISTINCT ORDER BY TOP

19 The SELECT Statement FROM clause SELECT clause Table name Column names

20 The SELECT Statement (Cont.)
Returns all columns from titles table Titles

21 characters are allowed
Data Types Alphanumeric values Only numeric values are allowed Data types characters are allowed Numeric values

22 Datatypes

23 Comments Characters to enclose comment SQL statement as multiple lines
of comment Characters to enclose comment

24 Comments (Cont.) Explains the code

25 Comments (Cont.) Comment not meant to execute

26 Performing a Conditional Search
Search Using a Simple Condition Compare Column Values Search Using Multiple Conditions Search for a Range of Values and Null Values Retrieve Data Based on Patterns

27 Search criteria used to search selected values in a table
Conditions Value Search criteria used to search selected values in a table Column name

28 Conditions (Cont.) Calculation performed on numeric values
Using multiple search conditions Calculation performed on numeric values

29 The Process of a Conditional Search

30 Operators Operator Column name Value Condition Word

31 Operators (Cont.) Condition Value WHERE clause Column name Operator

32 Operator used in condition
The WHERE Clause Value Column name Operator used in condition

33 The WHERE Clause (Cont.)
Condition 1 Condition 2

34 Comparison Operators Comparison operator Column name Value Condition

35 Arithmetic Operators Arithmetic operator Date column Numeric value
Calculation performed on date column Numeric value Date column Arithmetic operator Calculation performed on numeric values

36 Compound Assignment Operators
Column Variable Column Value

37 Column Aliasing Calculation performed on column name
Alphanumeric character Column alias displayed as column name

38 AND operator returns TRUE when both the boolean conditions are true
Logical Operators AND operator returns TRUE when both the boolean conditions are true Boolean condition 2 Boolean condition 1

39 Logical Operators (Cont.)
AND operator is evaluated before OR operator Condition returns TRUE or FALSE

40 Logical Operators (Cont.)
Condition 1 Logical operator Condition 3 Condition 2

41 The AND , OR, and NOT Operators
Boolean condition 1 Boolean condition 2 OR operator returns TRUE when either of the boolean conditions is true

42 The AND , OR, and NOT Operators (Cont.)
NOT operator negates the result of the condition

43 The AND , OR, and NOT Operators (Cont.)
NOT operator used with AND operator

44 The BETWEEN .. AND Operator
Conditional statement End value Start value Range of values Specifies values outside of range

45 IN operator returns the rows that match the list of values
The In Operator IN operator returns the rows that match the list of values List of values within parentheses Column name

46 The NULL Value

47 Displays only null value Rows that contain null values
The IS NULL Clause Condition 1 Condition 2 Displays only null value Rows that contain null values

48 The Is Null Clause (Cont.)
Search condition Checks whether the values are not null Logical operator

49 Wildcards Wildcard with a character Column name

50 Wildcards (Cont.) Wildcard for single character unlimited
number of characters

51 Pattern Matching Pattern Wildcard used to search characters Output

52 Working with Functions
Perform Date Calculations Calculate Data Using Aggregate Functions Manipulate String Values

53 Functions Parameters Function name Column name Function without

54 Date Functions Date function Date column Date parameter Numeric values
Date and time values

55 Date and Time Data Types
Datetimeoffset value Datetimeoffset data type Date data type Time data type Date value Time value Datetime2 value Datetime2 data type

56 Aggregate Functions Function name Reference to values
Summary values Single row output

57 Keywords Displayed in color Word reserved for retrieving data
entering table name

58 Keyword used outside its predetermined context
Keywords (Cont.) Keyword used outside its predetermined context

59 The DISTINCT Keyword No duplicate values Column names DISTINCT keyword

60 arithmetic calculation String enclosed in single quotes
Strings Case is ignored during compilation Cannot be used in arithmetic calculation String enclosed in single quotes Special characters

61 String Functions String function String input Numeric values

62 Case Conversion Functions
Converts lower case characters to upper case Converts upper case characters to lower case Column name Output of the UPPER function Output of the LOWER function

63 Leading and Trailing Spaces
Maximum number of characters that can be stored in a column Leading space Trailing space Characters stored in the column

64 The Trim Function Removes blank spaces before a column value
after a column value String values No space in front of values No space after values

65 Character Extraction

66 The SUBSTRING Function
Extracted substrings Integer that specifies number of characters to be extracted Column name Integer specifies where the substring begins String value

67 Trailing spaces present
Concatenation Concatenated strings Same data type Trailing spaces present in the name column Expression 1 Expression 2 String concatenation operator

68 Organizing Data Sort Data Rank Data Group Data Filter Grouped Data
Summarize Grouped Data Use PIVOT and UNPIVOT Operators

69 Arranged in ascending order of slprice
Sorting Lowest value Highest value Arranged in ascending order of slprice

70 Arranged in descending order of slprice
Sorting (Cont.) Highest value Lowest value Arranged in descending order of slprice

71 The ORDER BY Clause Entered at the end of the SELECT statement
Values in the descending order Values in the ascending order Sorts the output in ascending order Sorts the output in descending order Column name Entered at the end of the SELECT statement Column name

72 The Ranking Functions Ranking functions Column alias name
Lists the values of quantity in the descending order Ranking values with gaps in between them Partitions the rows of repid column Ranking functions Column alias name

73 The RANK Function Ranking values with gaps in between them
Lists the values of quantity in the descending order Column alias name Partitions the rows of repid column

74 The DENSE_RANK Function
Consecutive rank values Lists the values of quantity in the descending order Column alias name Partitions the rows of repid column

75 The ROW_NUMBER Function
Sequential numbering of each row Lists the values of quantity in the descending order Column alias name Partitions the rows of repid column

76 The NTILE Function Ranks after grouping and partition Lists the values of quantity in the descending order Column alias name Partitions the rows of repid column

77 Groups Input Table Records of each group Collection of records
are listed here Collection of records based on one or more columns Input Table Output Table

78 The GROUP BY Clause Column alias cannot be used in the GROUP BY clause Aggregate function returns summary values for groups Grouped based on two columns

79 The GROUP BY Clause (Cont.)
Entire list included in the GROUP BY clause Non-aggregate list in the SELECT clause

80 CUBE and ROLLUP Subclauses
Column values Hierarchy of grouping sets

81 CUBE and ROLLUP Subclauses (Cont.)
Column values Hierarchy of grouping sets

82 Column values in grouping sets

83 The HAVING Clause Aggregate function
Search condition applied for the groups Aggregate function HAVING clause used with the GROUP BY clause

84 The HAVING Clause (Cont.)
SELECT Statement without the GROUP BY clause HAVING clause behaves like the WHERE clause

85 The CUBE and ROLLUP Operators
Operator entered at the end of the GROUP BY clause Summary column values displayed as null Summary rows for each group Output of the GROUP BY clause Group based on two columns

86 The CUBE and ROLLUP Operators (Cont.)
Operator entered at the end of the GROUP BY clause Group based on two columns Output of the GROUP BY clause Summary column values displayed as null Summary rows for each group Summary value for all groups 86

87 The PIVOT and UNPIVOT Operators
Pivoted columns Alias name for the pivot table Aggregate functions Non pivoted columns Column value converted into header Summary values

88 Retrieving Data from Tables
Combine Results of Two Queries Compare Results of Two Queries Retrieve Data by Joining Tables Check for Unmatched Records Retrieve Information from a Single Table Using Joins

89 The UNION Operator Number of columns in each statement is identical
SELECT statement 2 SELECT statement 1 Keyword entered between two SELECT statements Number of columns in each statement is identical Data type of both columns is compatible Operator used to display duplicate rows

90 Column common to both tables
Joins Column common to both tables Table 2 Table 1 Output

91

92 Keyword entered between two table names
The Cross Join slspers titles Output containing every row of slspers table paired with every row of titles table Keyword entered between two table names Tables to be joined

93 The Inner Join Tables to be joined
Keyword entered between two table names Condition containing the comparison operator Tables to be joined Records that have matching values

94 The Outer Join Column used to join tables
Keyword entered between two table names ON clause containing the condition Column used to join tables Matching rows from both tables Rows from one table Output after joining two tables

95 Table alias names used instead of original names
The Table Alias Name Full name of the tables Table alias names used instead of original names Table alias names

96 The Self-Join Table alias used in condition
Keyword entered between table names Table alias used in condition ON clause containing the comparison operator Two table alias names assigned to a single table Potential Customers Relating data in a table to itself

97 Presenting Query Results
Save the Query Result Generate an XML Report

98 Reports Retrieved from titles table sales table

99 The FOR Clause Column name Table name Returns results in a
Column value Table name Returns results in a nested XML tree Returns results as XML type Returns columns as sub elements

100 Course Outline Using Subqueries to Perform Advanced Querying
Manipulating Table Data Manipulating the Table Structure Working with Views Indexing Data Managing Transactions

101 Using Subqueries to Perform Advanced Querying
Search Based on Unknown Values Compare a Value with Unknown Values Search Based on the Existence of Records Generate Output Using Correlated Subqueries Filter Grouped Data Within Subqueries Perform Multiple-Level Subqueries

102 Outer Queries Depends on the values returned by this query Outer query

103 returned to outer query
Subqueries The unknown value is returned to outer query Subquery Outer query Opening parenthesis Can return a computed value Closing parenthesis Sales

104 Search value specified
The IN Operator Search value specified before the IN operator Can be column name or constant value Search for list of values returned Used in search condition Customers Sales

105 Modified Comparison Operators
Function of comparison operator changed by using ANY Value specified before operator Minimum value in this list compared Logical operator Always used with comparison operator Sales Slspers Output of the outer query

106 The EXISTS Operator Used in condition Values not specified
Only checks for existence of records Asterisk instead of column name Condition tested Used in condition of outer query Titles Sales Qty > 200 EXISTS=TRUE

107 Correlated Subqueries
Always a constant value Depends on input from outer query Located in the WHERE clause Outer query Correlated subquery Executed simultaneously for each record targeted for display

108 Correlated Subqueries (Cont.)
Customers Slspers Sales Output of the outer query

109 Process of Correlating Queries

110 to all rows of an outer table
The APPLY Operator The APPLY operator The WHERE condition Enables you to apply a table expression to all rows of an outer table

111 Nested Subqueries Inner query level 1 Nested subquery
Output of the outer query Nested subquery Inner query level 1 Inner query level 2 Outer query depends on values returned by inner queries Number of closing parentheses matches number of inner queries Titles Customers Sales

112 Manipulating Table Data
Insert Data Modify and Delete Data

113 into the target columns
The INSERT Statement Target table Target columns Values to be inserted into the target columns

114 Values to be inserted in corresponding columns
The OUTPUT Clause Values to be inserted in corresponding columns Used with the INSERT statement The OUTPUT keyword

115 Table Value Constructors
Used with the INSERT INTO statement Values to be inserted in corresponding columns Graphic has to be inserted.

116 The UPDATE Statement The UPDATE keyword Columns to be updated
New values Condition The SET keyword

117 The DELETE Statement The DELETE keyword Condition

118 The TRUNCATE TABLE Statement
The TRUNCATE keyword Targeted table

119 The MERGE Statement Used with the MERGE INTO statement
The DELETE keyword The INSERT keyword The UPDATE keyword Graphic has to be inserted.

120 CAST Graphic has to be inserted.

121 CASE Graphic has to be inserted.

122 Manipulating the Table Structure
Create a Table Create a Table with Constraints Add and Drop Table Columns Add and Drop Constraints Modify the Column Definition Back Up Tables Delete a Table

123 The CREATE TABLE Statement
Table name Restricts storage of null value Size enclosed within parentheses Datatype and size specified Column names and definitions

124 Table Design Columns Rows Column header

125 Data Integrity All values are valid and can be used further qty<225
Column contains valid data Linked columns have matching values Each record is unique

126 Constraints Qty>0 Column-level constraint Defines rules to
test column value Domain integrity implemented

127 Primary Keys Primary key Uniquely identifies each column
Cannot store null or duplicate values

128 PRIMARY KEY Constraints
Constraint name Prevents record duplication Creates primary key on this column Table-level constraint Column name chosen as primary key

129 UNIQUE Constraints Can accept null values Column-level
Implements uniqueness in values stored Can accept null values

130 Foreign Keys Foreign key Can store Primary key of duplicate values
Stores matching values Links records Primary key of referenced table Referenced table

131 FOREIGN KEY Constraints
Defined when creating a table Referenced table name Column-level constraint Referenced column Cannot be modified once created without being deleted Foreign key name Creates the foreign key Table-level constraint

132 column-level constraint
DEFAULT Constraints Specified when creating a table Specified only as a column-level constraint Specifies a default value for a column Value must correspond to the datatype of the column

133 value based on condition
CHECK Constraints Validates column value based on condition Executed in the order specified Multiple expressions linked Column-level CHECK constraint Table-level Logical expression

134 The ALTER TABLE Statement
Modifies table structure Adds a column Modifies column definitions Adds a constraint Drops a column Drops a constraint

135 Sparse Columns Table name Sparse attribute for the ‘deptno’ column
Column definition Code to be inserted here .

136 The SELECT INTO Statement
New table Creates a backup of a table’s structure and data

137 The DROP TABLE Statement
Table to be dropped Table referencing this table must be dropped first Column definitions and constraints also dropped

138 Working with Views Create a View Manipulate Data in Views
Create Aliases Modify and Drop Views

139 Views Source table Retrieves and displays records from
Does not store records Retrieves and displays records from a large source table Virtual table

140 The CREATE VIEW Statement
keyword Name of the view to be created Optional column names can be specified

141 Schema Binding Written after the CREATE VIEW statement
The SCHEMABINDING keyword

142 The TOP Keyword Number of records The TOP keyword to be retrieved
Condition

143 Aliases Table alias Column name Column alias The AS keyword Table name

144 The ALTER VIEW Statement
Target view name Column names Table name

145 The DROP VIEW Statement
Name of the view

146 Indexing Data Create Indices Drop Indices

147 Indices Can be created on Helps in speedy data retrieval
Columns frequently used for search to be indexed Cannot delete the column without deleting the index Helps in speedy data retrieval Can be created on multiple columns Query uses pointer to identify and retrieve record Pointers assigned to each value of indexed column

148 Constraint vs. Index

149 The CREATE INDEX Statement
Creates an index Name of the index Multiple columns can be indexed Columns targeted for indexing Name of the targeted table Creates a unique non-clustered index

150 The DROP INDEX Statement
Deletes an index Targeted table Not created by a clustered primary key Index name

151 Managing Transactions
Create Transactions Commit Transactions

152 Transactions Name of the transaction The BEGIN TRAN statement
The SAVE TRAN Savepoint name Transaction Statements that belong to the transaction and executed as a single unit

153 The ROLLBACK TRAN Statement
Cancels changes done by the transaction up until the save point

154 The COMMIT TRAN Statement
Saves changes done in a transaction


Download ppt "Relational Database Design"

Similar presentations


Ads by Google