Database Development
Trương Quý Quỳnh
References
• UDEMY: SQL Database MasterClass: Go From Pupil To Master!
• Database Systems - A Practical Approach to Design, Implementation, and Management by Thomas Connolly
Outline
• Sub-Queries• Joins• User-defined functions• Stored procedures• Views• SQL profiler, Optimizing
Sub-Queries
• A Subquery is a Select within a Select or a From or a Where.
• Example:Select Id,(Select Name from Person where
Id=Class.PersonID), PersonID from Class.Select ID, PersonID from (Select * from Class) C.Select ID,PersonID from Class where PersonID in
(Select ID from Person where Name=“QuynhTQ”)
Sub-Queries
• Advantages– To quickly get more data from another Table.– To Optimize a SQL Statement.
• Disadvantages– Performance issues.
Joins
• Inner join– When use inner join, you are telling the DB that I
have 2 tables and those tables have relationship where value1=value2.
– When use inner join, I will not show value from either table or row where this statement is not true.
• Outer join– Usually used “left outer join”.
User-defined functions
• Scalar function– Returns only 1 value.–Can not join to scalar function– If you put it in select, it will execute 1 time for
every row.• Table function– Returns a table variable– Can join to table function
Stored procedures
• Store procedure is similar to UDFs but returns zero or more values.
Views
• A view is a way to save a query for later use.• A table holds all its data, but view is a smaller
portion. Like a window looking to the house at the table.
View
• Advantages.– Save queries.– Security.
• Disadvantages.– Performance.
Introduction to SQL profiler
Optimize SQL
• Remove Scalar UDFs (Replace with view wherever possible)
• Join properly• Move where to join clause if possible• Query object order• Add indexes• Remove cursors
Top Related