Successfully reported this slideshow.
 
SQL Constraints <ul><li>Constraints are the rules enforced on data columns on table  </li></ul><ul><li>Constraints are use...
What is Primary Key? <ul><li>A  primary key  is a table column that can be used to uniquely identify every row of the tabl...
Composite key <ul><li>A is a primary key consisting of more than one column. </li></ul><ul><li>The combinations like (Reco...
Foreign Key <ul><li>A  foreign key  is a column, or combination of columns, that contain values that are found in the prim...
Foreign Key <ul><ul><li>Referenced attribute(s) of second relation must be declared primary key for their relation </li></...
JOIN’s <ul><li>JOIN is a query clause that can be used with the SELECT, UPDATE, and DELETE data query statements to simult...
JOINS <ul><li>When you use like column names in multiple tables, you must use fully qualified column names.  </li></ul><ul...
Types of Joins <ul><li>Inner   join:  only keeps rows that satisfy the join condition </li></ul><ul><li>Left outer join:  ...
The basic JOIN statement <ul><li>A basic JOIN statement has the following format: </li></ul><ul><li>SELECT Customer.Custom...
Inner Join <ul><li>In  relational databases , a  join operation  matches records in two tables. The two tables must be  jo...
INNER JOIN <ul><li>This join returns rows when there is at least one match in both the tables. </li></ul>
OUTER JOIN <ul><li>There are three different Outer Join methods. </li></ul><ul><li>LEFT OUTER JOIN </li></ul><ul><li>RIGHT...
LEFT OUTER JOIN <ul><li>This join returns all the rows from the left table in conjunction with the matching rows from the ...
RIGHT OUTER JOIN <ul><li>This join returns all the rows from the right table in conjunction with the matching rows from th...
FULL OUTER JOIN <ul><li>This join combines left outer join and right after join. It returns row from either table when the...
CROSS JOIN <ul><li>This join is a Cartesian join that does not necessitate any condition to join. The resultset contains r...
Additional Notes related to JOIN <ul><li>The following are two classic examples to display where Outer Join is useful. You...
<ul><li>LEFT OUTER JOIN </li></ul>
Right Outer Join
NOT INNER JOIN <ul><li>Remember, the term Not Inner Join does not exist in database terminology. However, when full Outer ...
Stored Procedure <ul><li>A  stored procedure  is a group of Transact-SQL statements compiled into a single execution plan....
Writing Stored Procedures <ul><li>Tasks performed by the client application </li></ul><ul><li>Tasks performed by the store...
Stored Procedure Syntax <ul><li>CREATE  OR  REPLACE   PROCEDURE   <name>  (<arglist>)  AS < declarations > </li></ul><ul><...
Sample SP <ul><li>Before you create a stored procedure you need to know what your end result is, whether you are selecting...
Some Valid SQL Procedure Body Statements <ul><li>CASE statement </li></ul><ul><li>FOR statement </li></ul><ul><li>GOTO sta...
CONDITIONAL STATEMENTS <ul><ul><li>IF  <condition>  THEN </li></ul></ul><ul><ul><li><statement(s)> </li></ul></ul><ul><ul>...
SQL - Views <ul><li>SQL  VIEWS  are data objects, and like SQL Tables, they can be queried, updated, and dropped. A SQL  V...
SQL Create View <ul><li>CREATE VIEW virtualInventory AS SELECT * FROM inventory;  </li></ul><ul><li>SELECT * FROM virtualI...
<ul><li>Even though a SQL  VIEW  is treated like a data object in SQL, no data is actually stored inside of the view itsel...
SQL Drop View <ul><li>To delete the view, </li></ul><ul><li>DROP VIEW virtualInventory;  </li></ul>
<ul><li>Thank You ! </li></ul>
Upcoming SlideShare
Loading in …5
×

Ms sql server ii

1,381 views

Published on

Published in: Education, Technology
  • Be the first to comment

Ms sql server ii

  1. 2. SQL Constraints <ul><li>Constraints are the rules enforced on data columns on table </li></ul><ul><li>Constraints are used to limit the type of data that can go into a table. </li></ul><ul><li>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). </li></ul>
  2. 3. What is Primary Key? <ul><li>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. </li></ul><ul><li>A table can have many candidate keys but only one primary key. </li></ul><ul><li>The primary key cannot be null. </li></ul>
  3. 4. Composite key <ul><li>A is a primary key consisting of more than one column. </li></ul><ul><li>The combinations like (RecordNo,FirstName), (RecordNo,Lastname), (RecordNo,FirstName, Lastname), and (FirstName,LastName) are all candidate keys. </li></ul>
  4. 5. Foreign Key <ul><li>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). </li></ul><ul><li>A foreign key may be null, and almost always is not unique. </li></ul>
  5. 6. Foreign Key <ul><ul><li>Referenced attribute(s) of second relation must be declared primary key for their relation </li></ul></ul><ul><ul><li>Any value appearing in an attribute of a foreign key must appear in the corresponding attribute of the second relation </li></ul></ul>
  6. 7. JOIN’s <ul><li>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. </li></ul><ul><li>Joined tables must each include at least one field in both tables that contain comparable data. </li></ul><ul><li>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. </li></ul>
  7. 8. JOINS <ul><li>When you use like column names in multiple tables, you must use fully qualified column names. </li></ul><ul><li>This is a “ dot ” notation that combines the names of tables and columns. </li></ul><ul><li>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. </li></ul>
  8. 9. Types of Joins <ul><li>Inner join: only keeps rows that satisfy the join condition </li></ul><ul><li>Left outer join: keeps all rows from left table; fills in nulls as needed </li></ul><ul><li>Right outer join: keeps all rows from right table; fills in nulls as needed </li></ul><ul><li>Full outer join: keeps all rows from both tables; fills in nulls as needed </li></ul><ul><li>Cross join: Cartesian product </li></ul>
  9. 10. The basic JOIN statement <ul><li>A basic JOIN statement has the following format: </li></ul><ul><li>SELECT Customer.CustomerID, TransID, TransAmt FROM Customer JOIN Transaction </li></ul><ul><li>ON Customer.CustomerID = Transaction.CustomerID; </li></ul><ul><li>In practice, you'd never use the example above because the type of join is not specified. In this case, </li></ul><ul><li>SQL Server assumes an INNER JOIN. You can get the equivalent to this query by using the statement: </li></ul><ul><li>SELECT Customer.CustomerID, TransID, TransAmt FROM Customer, Transaction; </li></ul>
  10. 11. Inner Join <ul><li>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 . </li></ul><ul><li>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. </li></ul>
  11. 12. INNER JOIN <ul><li>This join returns rows when there is at least one match in both the tables. </li></ul>
  12. 13. OUTER JOIN <ul><li>There are three different Outer Join methods. </li></ul><ul><li>LEFT OUTER JOIN </li></ul><ul><li>RIGHT OUTER JOIN </li></ul><ul><li>FULL OUTER JOIN </li></ul>
  13. 14. LEFT OUTER JOIN <ul><li>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. </li></ul>
  14. 15. RIGHT OUTER JOIN <ul><li>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. </li></ul>
  15. 16. FULL OUTER JOIN <ul><li>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. </li></ul>
  16. 17. CROSS JOIN <ul><li>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. </li></ul>
  17. 18. Additional Notes related to JOIN <ul><li>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. </li></ul><ul><li>SELECT t1.* FROM Table1 t1 WHERE t1.ID NOT IN (SELECT t2.ID FROM Table2 t2) </li></ul><ul><li>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. </li></ul><ul><li>/* LEFT JOIN - WHERE NULL */ </li></ul><ul><li>SELECT t1.*,t2.* FROM Table1 t1 LEFT JOIN Table2 t2 ON t1.ID = t2.ID WHERE t2.ID IS NULL </li></ul>
  18. 19. <ul><li>LEFT OUTER JOIN </li></ul>
  19. 20. Right Outer Join
  20. 21. NOT INNER JOIN <ul><li>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. </li></ul>
  21. 22. Stored Procedure <ul><li>A stored procedure is a group of Transact-SQL statements compiled into a single execution plan. </li></ul><ul><li>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. </li></ul><ul><li>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. </li></ul>
  22. 23. Writing Stored Procedures <ul><li>Tasks performed by the client application </li></ul><ul><li>Tasks performed by the stored procedure, when invoked </li></ul><ul><li>The CALL statement </li></ul><ul><li>Explicit parameter to be defined : </li></ul><ul><ul><li>IN : Passes a value to the stored procedure from the client application </li></ul></ul><ul><ul><li>OUT : Stores a value that is passed to the client application when the stored procedure terminates. </li></ul></ul><ul><ul><li>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 </li></ul></ul>
  23. 24. Stored Procedure Syntax <ul><li>CREATE OR REPLACE PROCEDURE <name> (<arglist>) AS < declarations > </li></ul><ul><ul><ul><li>BEGIN </li></ul></ul></ul><ul><ul><ul><li><procedure statements> </li></ul></ul></ul><ul><ul><ul><li>END </li></ul></ul></ul>
  24. 25. Sample SP <ul><li>Before you create a stored procedure you need to know what your end result is, whether you are selecting data, inserting data, etc..  </li></ul><ul><li>EXAMPLE: </li></ul><ul><ul><ul><li>CREATE PROCEDURE UPDATE_SALARY_1 (1) </li></ul></ul></ul><ul><li>(IN EMPLOYEE_NUMBER CHAR(6), (2) </li></ul><ul><li>IN RATE INTEGER) (2) </li></ul><ul><li>LANGUAGE SQL (3) </li></ul><ul><li>BEGIN </li></ul><ul><li>UPDATE EMPLOYEE (4) </li></ul><ul><li>SET SALARY = SALARY * (1.0 * RATE / 100.0 ) </li></ul><ul><li>WHERE SSN = EMPLOYEE_NUMBER; </li></ul><ul><li>END </li></ul><ul><li>LANGUAGE value of SQL and the BEGIN...END block, which forms the procedure body, are particular to an SQL procedure </li></ul><ul><li>1)The stored procedure name is UPDATE_SALARY_1. </li></ul><ul><li>2)The two parameters have data types of CHAR(6) and INTEGER. Both are input parameters. </li></ul><ul><li>3)LANGUAGE SQL indicates that this is an SQL procedure, so a procedure body follows the other parameters. </li></ul><ul><li>4)The procedure body consists of a single SQL UPDATE statement, which updates rows in the employee table. </li></ul>
  25. 26. Some Valid SQL Procedure Body Statements <ul><li>CASE statement </li></ul><ul><li>FOR statement </li></ul><ul><li>GOTO statement </li></ul><ul><li>IF statement </li></ul><ul><li>ITERATE statement </li></ul><ul><li>RETURN statement </li></ul><ul><li>WHILE statement </li></ul>
  26. 27. CONDITIONAL STATEMENTS <ul><ul><li>IF <condition> THEN </li></ul></ul><ul><ul><li><statement(s)> </li></ul></ul><ul><ul><li>ELSE </li></ul></ul><ul><ul><li><statement(s)> </li></ul></ul><ul><ul><li>END IF ; </li></ul></ul><ul><li>Loops </li></ul><ul><ul><li>LOOP </li></ul></ul><ul><ul><li>…… </li></ul></ul><ul><ul><li>EXIT WHEN <condition> </li></ul></ul><ul><ul><li>…… </li></ul></ul><ul><ul><li>END LOOP ; </li></ul></ul>
  27. 28. SQL - Views <ul><li>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. </li></ul>
  28. 29. SQL Create View <ul><li>CREATE VIEW virtualInventory AS SELECT * FROM inventory; </li></ul><ul><li>SELECT * FROM virtualInventory; </li></ul>
  29. 30. <ul><li>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. </li></ul><ul><li>UPDATE inventory SET price = '1.29' WHERE product = 'Pen'; </li></ul><ul><li>SELECT * FROM virtualInventory WHERE product = 'Pen'; </li></ul><ul><li>Above two queries will give the same output </li></ul>
  30. 31. SQL Drop View <ul><li>To delete the view, </li></ul><ul><li>DROP VIEW virtualInventory; </li></ul>
  31. 32. <ul><li>Thank You ! </li></ul>

×