Activant Solutions Inc. SQL 2005: Basic Data Manipulation SQL Server 2005 suite Course 4 of 4
This class is designed for Beginner/Intermediate SQL Server 2005 System Administrators
Objectives System Stored Procedures System Functions SELECT INSERT UPDATE JOINS CONSTRAINTS
This course will NOT cover SQL Server 2005 Installation SQL Server Management SQL Server Maintenance
Using System Stored Procedures System stored procedure overview Common system stored procedures Using system stored procedures to retrieve information
System Stored Procedure Compiled set of Transact-SQL statements used for data retrieval or manipulation Name of the procedure usually begins with sp Run them from any database using the EXEC statement in a query window Useful for retrieving information about objects in the database
Using System Stored Procedures to Retrieve Information Command EXEC sp_help oe_line EXEC sp_help EXEC sp_helptext myproc Results stored procedure information about the table called oe_line list of all the objects in the current database code that is in a stored procedure called myproc
Understanding System Functions System function overview System functions vs. system stored procedures
System Function Compiled procedures provided by SQL Server to perform complex mathematical calculations, or to manipulate data Rowset Functions Aggregate Functions Scalar Functions
Aggregate Functions SUM AVG MIN MAX COUNT Sums a column of values and returns the result Averages a set of values and returns the results Returns the minimum value from a list of values Returns the maximum value from a list of values Return the count of values in a list
Aggregate Functions To return the average value of the extended price column in the oe_line table SELECT AVG(extended_price) FROM oe_line
Aggregate Functions To return the average value of the extended price column in the oe_line table Select AVG(extended_price) from oe_line To return the number of invoices in the inv_hdr table which have an invoice_amount above $1000.00 SELECT count(invoice_no) FROM invoice_hdr WHERE total_amount > 1000
Scalar Functions Getdate() Substring Cast Returns the current system date Used to take only a certain part of a longer string Used to convert data from one data type to another SELECT SUBSTRING(customer_name, 1, 5) FROM customer
SELECT Statement Overview Choosing columns Choosing rows Sorting results Grouping Results Implementing Joins
Basic Syntax SELECT column_list Column_list is a comma-separated list of the columns you want in your result set FROM table Indicates the table where the columns in the select list or the where clause reside WHERE search criteria Where clause specifies the criteria for the rows you want to see
Choosing Columns SELECT * FROM oe_hdr SELECT order_no, ship2_name FROM oe_hdr SELECT order_no, 'My Literal', ship2_name FROM oe_hdr SELECT order_no AS 'Order Number', ship2_name AS 'Ship To Name FROM oe_hdr
Choosing Rows Comparison Operators Ranges Lists String Matches Unknown Values Combinations Negations =, >, <, >=, <=, <>,!=,!<,!> BETWEEN, NOT BETWEEN IN, NOT IN LIKE, NOT LIKE IS NULL, IS NOT NULL AND, OR NOT
Choosing Rows SELECT * FROM oe_hdr WHERE order_no = 10000 SELECT * FROM oe_hdr WHERE ship2_add1 = 1234 Any Street Put a single quote mark around all literal constants for the columns of char, varchar, text, datetime, and smalldatetime data types.
Choosing Rows SELECT * FROM oe_hdr WHERE order_no in (100000, 100010, 100020) SELECT * FROM oe_hdr WHERE ship2_add1 LIKE '1234 Market%'
Wildcards % Any string of zero or more characters _ Any single character [] Any single character within a range/set [^] Single character not with a range or set
Wildcards SELECT * FROM oe_hdr WHERE ship2_add1 LIKE %Ave% SELECT * FROM oe_hdr WHERE order_no IN (100000, 100005, 100006) OR ship2_add1 IS NULL
Sorting Results ORDER BY clause sorts the results of your query in a specified order SELECT * FROM oe_hdr WHERE order_no > 150000 ORDER BY ship2_name SELECT * FROM oe_hdr WHERE order_no > 150000 ORDER BY ship2_name DESC
Grouping Results Commonly used in conjunction with aggregate functions to produce summary data SELECT order_no, AVG(extended_price) FROM oe_line GROUP BY order_no SELECT order_no, SUM(qty_ordered) FROM oe_line GROUP BY order_no
HAVING SELECT order_no, AVG(extended_price) FROM oe_line GROUP BY order_no HAVING AVG(extended_price) >= 70
Implementing Joins In a relational database environment, the information you need often resides in more than one table In order to retrieve this data, you must join to tables
Joins INNER JOIN CROSS JOIN OUTER JOIN Includes only rows that satisfy the join condition Includes every combination of all of the rows between the two tables Includes the rows that satisfy the join condition, plus the remaining rows in one of the tables
Joins SELECT column_list FROM table JOIN table ON table.column = table.column WHERE criteria
Allowed Join Operators = Equal to > Greater than < Less than >= Greater or equal to <= Less or equal to <> Not equal to
Inner Joins Connect two tables to create a third table that includes only the rows satisfying the join condition
INNER JOIN Example SELECT oe_hdr.customer_id, oe_line.qty_invoiced, oe_line.commission_cost FROM oe_hdr INNER JOIN oe_line ON oe_hdr.order_no = oe_line.order_no WHERE oe_hdr.customer_id = 100000
Cross Joins Result set containing all the combinations of the rows between tables in the join 5 rows, 9 cols = 45 rows in result set Cross Join Example SELECT customer_id, qty_invoiced FROM oe_hdr CROSS JOIN oe_line
Outer Joins - ANSI Left Outer Join Right Outer Join Full Outer Join Includes all rows from the table on the left, while only including related records from the table on the right Includes all rows from the table on the right, while only including related records from the table on the left Includes all non-matching rows for both the left side table and the right side table
Outer Joins SELECT inv_mast.item_id, sum(oe_line.qty_ordered) FROM inv_mast LEFT OUTER JOIN oe_line ON inv_mast.inv_mast_uid = oe_line.inv_mast_uid GROUP BY inv_mast.item_id
INSERT Statement Overview Basic syntax Inserting rows with SELECT
INSERT Statement Data modification statement that inserts rows into a table while still observing constraints on the columns of the table Basic Syntax INSERT INTO table_name (column_list) VALUES (value_list)
INSERT statement INSERT INTO oe_hdr (order_id, customer_id, ship_to_address) VALUES ( 100050, 107543, 1234 Any Street)
Inserting with SELECT Insert more than one row at a time into a table SELECT * INTO oe_line_copy FROM oe_line
Inserting with SELECT Insert more than one row at a time into a table SELECT * INTO oe_line_copy FROM oe_line INSERT INTO oe_line_copy SELECT * FROM oe_line
UPDATE Statement Overview Basic Syntax Updating data based on other tables
UPDATE Statement Updates data that already exists in tables Observes column and table constraints that reside on the table
UPDATE Statement Updates data that already exists in tables Observes column and table constraints that reside on the table UPDATE table_name SET Specifies the columns and the new values WHERE Specifies the exact rows to update
UPDATE syntax UPDATE table_name SET column_name = expression, column_name2 = expression WHERE where_clause
UPDATE statement UPDATE inv_mast SET price1 = price1 + (price1 *.10) UPDATE inv_mast SET description = Discontinued Item WHERE inactive = Y
SQL Information P21 RMB on field Choose Help SQL Information
SQL Information P21 The Database name for Order Number is oe_hdr.order_no
Data Dictionary
P21 Type of Views Basic views Based on one table, with extra information Table name is oe_hdr View name is p21_view_oe_hdr Master views Based on multiple tables linked together Take care of links between tables Master views name format: p21_<name>_view
Database Constraints Primary Key Foreign Key Alternate Key (Unique) Check Default
Primary Key Only 1 Primary Key constraint per table Values must be unique Null values are not allowed Create a Unique Index on that column P21 naming pk_<table name>
Foreign Key Must reference Primary Key or Unique constraint from another table Provides referential integrity Does not create indexes P21 naming fk_<main table>_<referencing table> fk_oe_hdr_oe_line
Alternate (Unique) Key Allow only 1 NULL value Multiple Unique constraints on a table Defined on 1 or more columns Creates index P21 naming ak_<table name>
Check Constraints Used with Insert and Update SQL Can reference other columns in the same table Example: ALTER TABLE employees ADD CONSTRAINT ck_birthdate CHECK (birthdate > 01-01-1900 AND birthdate < getdate())
Default Constraints Apply only to Insert SQL Only 1 per column P21 naming df_<table name_column_name>
Summary Defined system stored procedures and functions Reviewed SELECT/INSERT/UPDATE syntax and examples Defined JOINS INNER/OUTER most commonly used Defined Constraints on a database
For More Information SQL Server Books Online Help Files Visit Activant on the web: http://distribution.activant.com/ View Educational Services course offerings Browse Documentation Search the Solutions database Submit a Support case E-mail Professional Services Sales pss@activant.com