Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
 
SQL Constraints Constraints are the rules enforced on data columns on table  Constraints are used to limit the type of data that can go into a table. Constraints can be specified when a table is created (with the CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement).
What is Primary Key? A  primary key  is a table column that can be used to uniquely identify every row of the table.  Any column that has this property will do  -- these columns are called  candidate  keys. A table can have many candidate keys but only one primary key.  The primary key cannot be null.
Composite key A is a primary key consisting of more than one column. The combinations like (RecordNo,FirstName), (RecordNo,Lastname), (RecordNo,FirstName, Lastname), and (FirstName,LastName) are all candidate keys.
Foreign Key A  foreign key  is a column, or combination of columns, that contain values that are found in the primary key of some table (including, possibly, itself). A foreign key may be null, and almost always is not unique.
Foreign Key Referenced attribute(s) of second relation must be declared primary key for their relation Any value appearing in an attribute of a foreign key must appear in the corresponding attribute of the second relation
JOIN’s JOIN is a query clause that can be used with the SELECT, UPDATE, and DELETE data query statements to simultaneously affect rows from multiple tables. There are several distinct types of JOIN statements that return different data result sets. Joined tables must each include at least one field in both tables that contain comparable data.  For example, if you want to join a  Customer  table and a  Transaction  table, they both must contain a common element, such as  CustomerID  column, to serve as a key on which the data can be matched. Tables can be joined on multiple columns so long as the columns have the potential to supply matching information. Column names across tables don't have to be the same, although for readability this standard is generally preferred.
JOINS When you use like column names in multiple tables, you must use fully qualified column names.  This is a “ dot ” notation that combines the names of tables and columns.  For example, if we have two tables,  Customer  and  Transaction,  and they both contain the column  CustomerID , we use the dot notation, as in  Customer.CustomerID  and  Transaction.CustomerID,  to let the database know which column from which table we are referring.
Types of Joins Inner   join:  only keeps rows that satisfy the join condition Left outer join:  keeps all rows from left table; fills in nulls as needed Right outer join:  keeps all rows from right table; fills in nulls as needed Full outer join:  keeps all rows from both tables; fills in nulls as needed Cross join:  Cartesian product
The basic JOIN statement A basic JOIN statement has the following format: SELECT Customer.CustomerID, TransID, TransAmt  FROM Customer JOIN Transaction  ON Customer.CustomerID = Transaction.CustomerID; In practice, you'd never use the example above because the type of join is not specified. In this case, SQL Server assumes an INNER JOIN. You can get the equivalent to this query by using the statement: SELECT Customer.CustomerID, TransID, TransAmt FROM Customer, Transaction;
Inner Join In  relational databases , a  join operation  matches records in two tables. The two tables must be  joined  by at least one common  field . That is, the  join field  is a member of both tables. Typically, a join operation is part of a SELECT  query .  select * from A, B where A.x = B.y The column names (x and y in this example) are often, but not necessarily, the same.
INNER JOIN This join returns rows when there is at least one match in both the tables.
OUTER JOIN There are three different Outer Join methods. LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN
LEFT OUTER JOIN This join returns all the rows from the left table in conjunction with the matching rows from the right table. If there are no columns matching in the right table, it returns NULL values.
RIGHT OUTER JOIN This join returns all the rows from the right table in conjunction with the matching rows from the left table. If there are no columns matching in the left table, it returns NULL values.
FULL OUTER JOIN This join combines left outer join and right after join. It returns row from either table when the conditions are met and returns null value when there is no match.
CROSS JOIN This join is a Cartesian join that does not necessitate any condition to join. The resultset contains records that are multiplication of record number from both the tables.
Additional Notes related to JOIN The following are two classic examples to display where Outer Join is useful. You will notice several instances where developers write query as given below. SELECT t1.* FROM Table1 t1 WHERE t1.ID NOT IN (SELECT t2.ID FROM Table2 t2) The query demonstrated above can be easily replaced by Outer Join. Indeed, replacing it by Outer Join is the best practice. The query that gives same result as above is displayed here using Outer Join and WHERE clause in join. /* LEFT JOIN - WHERE NULL */ SELECT t1.*,t2.* FROM Table1 t1 LEFT JOIN Table2 t2 ON t1.ID = t2.ID WHERE t2.ID IS NULL
LEFT OUTER JOIN
Right Outer Join
NOT INNER JOIN Remember, the term Not Inner Join does not exist in database terminology. However, when full Outer Join is used along with WHERE condition, as explained in the above two examples, it will give you exclusive result to Inner Join. This join will give all the results that were not present in Inner Join.
Stored Procedure A  stored procedure  is a group of Transact-SQL statements compiled into a single execution plan. A stored procedure is nothing more than prepared SQL code that you save so you can reuse the code over again.  So if you think about a query that you write over and over again, instead of having to write that query each time you would save it as a stored procedure and then just call the stored procedure to execute the SQL code that you saved as part of the stored procedure. In addition to running the same SQL code over and over again you also have the ability to pass parameters to the stored procedure, so depending on what the need is the stored procedure can act accordingly based on the parameter values that were passed.
Writing Stored Procedures Tasks performed by the client application Tasks performed by the stored procedure, when invoked The CALL statement Explicit parameter to be defined :  IN : Passes a value to the stored procedure from the client application OUT : Stores a value that is passed to the client application when the stored procedure terminates. INOUT :  Passes a value to the stored procedure from the client application, and returns a value to the  Client  application when the stored procedure terminates
Stored Procedure Syntax CREATE  OR  REPLACE   PROCEDURE   <name>  (<arglist>)  AS < declarations > BEGIN <procedure statements> END
Sample SP Before you create a stored procedure you need to know what your end result is, whether you are selecting data, inserting data, etc..  EXAMPLE: CREATE PROCEDURE UPDATE_SALARY_1  (1) (IN EMPLOYEE_NUMBER CHAR(6),  (2) IN RATE INTEGER)  (2) LANGUAGE SQL  (3) BEGIN UPDATE EMPLOYEE  (4) SET SALARY = SALARY * (1.0 * RATE / 100.0 ) WHERE SSN = EMPLOYEE_NUMBER; END LANGUAGE value of SQL and the BEGIN...END block, which forms the procedure body, are particular to an SQL procedure 1)The stored procedure name is UPDATE_SALARY_1.  2)The two parameters have data types of CHAR(6) and INTEGER. Both are input parameters.  3)LANGUAGE SQL indicates that this is an SQL procedure, so a procedure body follows the other parameters. 4)The procedure body consists of a single SQL UPDATE statement, which updates rows in the employee table.
Some Valid SQL Procedure Body Statements CASE statement FOR statement GOTO statement IF statement ITERATE statement RETURN statement WHILE statement
CONDITIONAL STATEMENTS IF  <condition>  THEN <statement(s)> ELSE <statement(s)> END   IF ; Loops LOOP  …… EXIT   WHEN  <condition> …… END   LOOP ;
SQL - Views SQL  VIEWS  are data objects, and like SQL Tables, they can be queried, updated, and dropped. A SQL  VIEW  is a virtual table containing columns and rows except that the data contained inside a view is generated dynamically from SQL tables and does not physically exist inside the view itself.
SQL Create View CREATE VIEW virtualInventory AS SELECT * FROM inventory;  SELECT * FROM virtualInventory;
Even though a SQL  VIEW  is treated like a data object in SQL, no data is actually stored inside of the view itself. The view is essentially a dynamic  SELECT  query, and if any changes are made to the originating table(s), these changes will be reflected in the SQL  VIEW  automatically. UPDATE inventory SET price = '1.29' WHERE product = 'Pen';  SELECT * FROM virtualInventory WHERE product = 'Pen';  Above two queries will give the same output
SQL Drop View To delete the view, DROP VIEW virtualInventory;
Thank You !

More Related Content

Ms sql server ii

  • 1.  
  • 2. SQL Constraints Constraints are the rules enforced on data columns on table Constraints are used to limit the type of data that can go into a table. Constraints can be specified when a table is created (with the CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement).
  • 3. What is Primary Key? A primary key is a table column that can be used to uniquely identify every row of the table. Any column that has this property will do -- these columns are called candidate keys. A table can have many candidate keys but only one primary key. The primary key cannot be null.
  • 4. Composite key A is a primary key consisting of more than one column. The combinations like (RecordNo,FirstName), (RecordNo,Lastname), (RecordNo,FirstName, Lastname), and (FirstName,LastName) are all candidate keys.
  • 5. Foreign Key A foreign key is a column, or combination of columns, that contain values that are found in the primary key of some table (including, possibly, itself). A foreign key may be null, and almost always is not unique.
  • 6. Foreign Key Referenced attribute(s) of second relation must be declared primary key for their relation Any value appearing in an attribute of a foreign key must appear in the corresponding attribute of the second relation
  • 7. JOIN’s JOIN is a query clause that can be used with the SELECT, UPDATE, and DELETE data query statements to simultaneously affect rows from multiple tables. There are several distinct types of JOIN statements that return different data result sets. Joined tables must each include at least one field in both tables that contain comparable data. For example, if you want to join a Customer table and a Transaction table, they both must contain a common element, such as CustomerID column, to serve as a key on which the data can be matched. Tables can be joined on multiple columns so long as the columns have the potential to supply matching information. Column names across tables don't have to be the same, although for readability this standard is generally preferred.
  • 8. JOINS When you use like column names in multiple tables, you must use fully qualified column names. This is a “ dot ” notation that combines the names of tables and columns. For example, if we have two tables, Customer and Transaction, and they both contain the column CustomerID , we use the dot notation, as in Customer.CustomerID and Transaction.CustomerID, to let the database know which column from which table we are referring.
  • 9. Types of Joins Inner join: only keeps rows that satisfy the join condition Left outer join: keeps all rows from left table; fills in nulls as needed Right outer join: keeps all rows from right table; fills in nulls as needed Full outer join: keeps all rows from both tables; fills in nulls as needed Cross join: Cartesian product
  • 10. The basic JOIN statement A basic JOIN statement has the following format: SELECT Customer.CustomerID, TransID, TransAmt FROM Customer JOIN Transaction ON Customer.CustomerID = Transaction.CustomerID; In practice, you'd never use the example above because the type of join is not specified. In this case, SQL Server assumes an INNER JOIN. You can get the equivalent to this query by using the statement: SELECT Customer.CustomerID, TransID, TransAmt FROM Customer, Transaction;
  • 11. Inner Join In relational databases , a join operation matches records in two tables. The two tables must be joined by at least one common field . That is, the join field is a member of both tables. Typically, a join operation is part of a SELECT query . select * from A, B where A.x = B.y The column names (x and y in this example) are often, but not necessarily, the same.
  • 12. INNER JOIN This join returns rows when there is at least one match in both the tables.
  • 13. OUTER JOIN There are three different Outer Join methods. LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN
  • 14. LEFT OUTER JOIN This join returns all the rows from the left table in conjunction with the matching rows from the right table. If there are no columns matching in the right table, it returns NULL values.
  • 15. RIGHT OUTER JOIN This join returns all the rows from the right table in conjunction with the matching rows from the left table. If there are no columns matching in the left table, it returns NULL values.
  • 16. FULL OUTER JOIN This join combines left outer join and right after join. It returns row from either table when the conditions are met and returns null value when there is no match.
  • 17. CROSS JOIN This join is a Cartesian join that does not necessitate any condition to join. The resultset contains records that are multiplication of record number from both the tables.
  • 18. Additional Notes related to JOIN The following are two classic examples to display where Outer Join is useful. You will notice several instances where developers write query as given below. SELECT t1.* FROM Table1 t1 WHERE t1.ID NOT IN (SELECT t2.ID FROM Table2 t2) The query demonstrated above can be easily replaced by Outer Join. Indeed, replacing it by Outer Join is the best practice. The query that gives same result as above is displayed here using Outer Join and WHERE clause in join. /* LEFT JOIN - WHERE NULL */ SELECT t1.*,t2.* FROM Table1 t1 LEFT JOIN Table2 t2 ON t1.ID = t2.ID WHERE t2.ID IS NULL
  • 21. NOT INNER JOIN Remember, the term Not Inner Join does not exist in database terminology. However, when full Outer Join is used along with WHERE condition, as explained in the above two examples, it will give you exclusive result to Inner Join. This join will give all the results that were not present in Inner Join.
  • 22. Stored Procedure A stored procedure is a group of Transact-SQL statements compiled into a single execution plan. A stored procedure is nothing more than prepared SQL code that you save so you can reuse the code over again.  So if you think about a query that you write over and over again, instead of having to write that query each time you would save it as a stored procedure and then just call the stored procedure to execute the SQL code that you saved as part of the stored procedure. In addition to running the same SQL code over and over again you also have the ability to pass parameters to the stored procedure, so depending on what the need is the stored procedure can act accordingly based on the parameter values that were passed.
  • 23. Writing Stored Procedures Tasks performed by the client application Tasks performed by the stored procedure, when invoked The CALL statement Explicit parameter to be defined : IN : Passes a value to the stored procedure from the client application OUT : Stores a value that is passed to the client application when the stored procedure terminates. INOUT : Passes a value to the stored procedure from the client application, and returns a value to the Client application when the stored procedure terminates
  • 24. Stored Procedure Syntax CREATE OR REPLACE PROCEDURE <name> (<arglist>) AS < declarations > BEGIN <procedure statements> END
  • 25. Sample SP Before you create a stored procedure you need to know what your end result is, whether you are selecting data, inserting data, etc..  EXAMPLE: CREATE PROCEDURE UPDATE_SALARY_1 (1) (IN EMPLOYEE_NUMBER CHAR(6), (2) IN RATE INTEGER) (2) LANGUAGE SQL (3) BEGIN UPDATE EMPLOYEE (4) SET SALARY = SALARY * (1.0 * RATE / 100.0 ) WHERE SSN = EMPLOYEE_NUMBER; END LANGUAGE value of SQL and the BEGIN...END block, which forms the procedure body, are particular to an SQL procedure 1)The stored procedure name is UPDATE_SALARY_1. 2)The two parameters have data types of CHAR(6) and INTEGER. Both are input parameters. 3)LANGUAGE SQL indicates that this is an SQL procedure, so a procedure body follows the other parameters. 4)The procedure body consists of a single SQL UPDATE statement, which updates rows in the employee table.
  • 26. Some Valid SQL Procedure Body Statements CASE statement FOR statement GOTO statement IF statement ITERATE statement RETURN statement WHILE statement
  • 27. CONDITIONAL STATEMENTS IF <condition> THEN <statement(s)> ELSE <statement(s)> END IF ; Loops LOOP …… EXIT WHEN <condition> …… END LOOP ;
  • 28. SQL - Views SQL VIEWS are data objects, and like SQL Tables, they can be queried, updated, and dropped. A SQL VIEW is a virtual table containing columns and rows except that the data contained inside a view is generated dynamically from SQL tables and does not physically exist inside the view itself.
  • 29. SQL Create View CREATE VIEW virtualInventory AS SELECT * FROM inventory; SELECT * FROM virtualInventory;
  • 30. Even though a SQL VIEW is treated like a data object in SQL, no data is actually stored inside of the view itself. The view is essentially a dynamic SELECT query, and if any changes are made to the originating table(s), these changes will be reflected in the SQL VIEW automatically. UPDATE inventory SET price = '1.29' WHERE product = 'Pen'; SELECT * FROM virtualInventory WHERE product = 'Pen'; Above two queries will give the same output
  • 31. SQL Drop View To delete the view, DROP VIEW virtualInventory;

Editor's Notes

  1. The DataReader was developed to get at data and in read-only forward-only fashion and provide the data as fast as possible to enable the developers as fast as possible. With the help of DataSet, it takes a snapshot of the data from database and storing this data offline for as long as necessary. When the end user using this copy of data he can persist the data and any changes to the data right back to data store.