Join

zhaozj2021-02-17  54

Joins 101

Welcome to the T-SQL Classroom

A relational database is composed of entities (aka tables), relationships between the entities, and attributes that describe the entities. By creating related tables, you minimize data redundancy, and therefore, the disk space utilized. For example, suppose you have two entities , Employees and Departments. If you separate employee information into one table and departmental information into another, you minimize disk utilization by including in the Employees table only an identifier of the employee's department rather than duplicating the departmental information for every employee. Although this separation minimizes disk space, it creates a problem. When you need to display employee information along with the appropriate department, you need a way to bring the two tables together. The type of query that brings the two tables together is called a join. A join is One Important Way of Accessing Relational Databases; by Using a Join, You Can Unite Entities Into One ResultSet That Fully DE scribes the entities, relationships, and attributes that form your data's framework. Although joins are simple, many people have difficulty determining when and how to use them. So, I decided that joins were a good topic to start this column. To cover all aspects Of Joins, I Plan a Series of Articles To Cover Entity and Attribute Basics, Definitions of the Join Types, and Complex Join Concepts. I '

ll also examine how you can create indexes that improve join performance. Back to Basics A relational database consists of tables, with each table focusing on one entity. An entity is a classification given to a group of similar objects. Thus, an entity can be a person, place, or thing such as a customer or a product. Each entity has attributes that are typically specific only to that entity. An attribute-or, with respect to a table, a column-describes a non-decomposable characteristic of an entity. For example, in a products table, you might want to describe each product your company sells. Products generally have an ID (or other unique identifier), a name, a description, a price, and so on. Therefore, the Product entity table in your database would have simple attributes such as ProductID, ProductName, and Price. When you consider what entities to store, think about how the data will be used and searched. If an attribute, or a component of an attribute, needs to Be Reference in a wh ERE clause or in an ORDER BY clause, you probably want to define the component as a single column. For example, a Name column is acceptable, but the data could be more easily accessed if you broke it into FirstName, MiddleInitial, and LastName columns . The three-column approach best serves the following search conditions and ORDER BY clause: WHERE LastName = 'Smith' aND FirstName = 'Robert'ORDER BY LastName, FirstName, MiddleInitial However, you can take this approach too far For example, you. . could break AddressLine1 into StreetNumber, StreetName, and so forth you might need to break addresses down if you perform analysis based on demographics and need to search within an area's defined quadrants;

however, AddressLine1 is usually sufficient. When you describe an entity during table creation, you must also decide whether each attribute requires a defined value and, if so, what the domain of legal values ​​is that the attribute will support. If a definite value isn 't necessary, the column definition can allow NULL values. You implement these decisions by defining the attribute's data type and nullability (whether that column accepts NULL values). nullability is an important consideration, so do not leave the determination of nullability to the . current session settings or database settings Always describe nullability in the table definition after giving the subject serious thought After you define the entities and corresponding attributes in your database, you populate the table with data;. then you'll probably need to display that data In Reports. Entities Are A Database's Core Components; Attributes and Relationships Give An Entity Detail and Depth and Describe ITS Associations Wit h other entities. If a relational database has minimized redundancy by creating many entity tables, you use joins to create a picture of the data and its relationships. Let's review a sample database called TSQLTutorJoins (see "Obtaining the Code,"

page 3, for download instructions for the creation file) This database has four entities:... Customer, Product, Category, and Order To minimize the join results, each table has only a few attributes and only a few rows These limitations let you focus solely on the join results and not get lost in large resultsets and complex attributes. Also, I defined primary key and foreign key constraints on each table to enforce the relationships between the tables. This enforcement, declarative referential integrity (DRI), significantly helps maintain and enforce accuracy in the relationships between entities when the database processes additional INSERT, UPDATE, and DELETE statements. DRI is useful when you're first using a database because the DRI adds descriptive graphical information to the entity relationship diagram (ERD) you create THROUGH YOUR DATABASE-DIAGRAMMING TOOL. You can use a database-diagramming Tool To Generate a graphical representation of all your tables and their reasoningships . The DRI definitions expose the relationships. After running the TSQLTutorJoins.sql script to create the TSQLTutorJoins database, you can use Enterprise Manager to create a database diagram. Expand your database list, right-click the Diagrams option, then choose New Database Diagram. Add the Category, Customer, Product, and Order tables from the Available Tables list to the Tables to add to Diagram list Click Next to diagram the database;. your completed diagram should look like the diagram that Figure 1 shows In Figure 1, you. Can See That Customer IS Related To Order, Order Is Related To Product, And Category Is Related To Product. Let '

s examine these relationships more closely. The line that connects two tables tells an important story. Between the Customer and Order tables, the line with a key at one end and an infinity symbol at the other end denotes a one-to-many (1 : M) relationship (ie, for one Customer, you can have many Orders) The connecting line also says that for any given Order, you can have only one Customer However, for the purpose of describing tables and their relationships, you can.. say that Order and Customer have a 1:. M relationship In this diagram, you can also see that a Product can be on many Orders, but an Order can contain only one Product Again, this relationship between Order and Product is 1:. M . What's the relationship between Customer and Product? In this database, a Customer can purchase many Products, and many Customers can purchase the same Product. When the diagram shows the line connecting two tables intersecting at one table with keys touching the main tables (Customer And product) and infinity symbols touching the table in common (Order), the relationship between the related "key" tables is many-to-many (M: N). Therefore, Customer to Product is an M: N relationship (which database designers frequently describe Through An Intermediate Table, AS I '

ve done here). You can use T-SQL syntax-specifically, sp_help and sp_helpconstraint-to view the tables, their relationships, and their details. However, a database diagram is an easy way of seeing the big picture quickly. However you discover the relationships, you can use this information to determine the most common means of joining the tables-primary keys and foreign keys. Most joins connect the primary key of one table to the foreign key of another. What Can joins Do for you? The four tables in the TSQLTutorJoins database contain six customers, three product categories, seven products, and eight orders. By looking at the tables, you can see who the customers are, what categories of products the company sells, which products it sells, and when each order was taken. You can also see that not every customer has made a purchase and not every product has been sold. Furthermore, some customers have made more than one purchase, and customers can make over-the-counter orders without supply . Ing a CustomerID Let's start with some easy questions about the data: Which customers have made a purchase Which customers have not made a purchase You do not need to know how to write a join to answer these questions; you can use?? . a subquery instead For example, you can answer the question, Who made a purchase with the following query:? SELECT * FROM dbo.CustomerWHERE Customer.CustomerID IN (. SELECT [Order] .CustomerID FROM dbo [Order]) This query returns The Results Table 1 Shows.

TABLE 1: Results of Query to Find Which Customers Purchased ProductsCustomerIDFirstNameMiddleInitialLastName1ArthurACurry2BruceBWayne5EddieRNashton6DianaWPrince (4 row (s) affected You can answer the question, Who has not made a purchase with the following query:? SELECT * FROM dbo.CustomerWHERE Customer.CustomerID NOT IN ( SELECT [Order] .CustomerID FROM dbo. [Order]) which returns no rows. But you know that two customers, CustomerID 3 (Selina) and CustomerID 4 (Oswald), have not made any purchases. Why does not the subquery answer the question correctly? The reason goes back to nullability. Because your database allows NULL values ​​for CustomerID, the set has undefined rows-rows in which no certain value is known. in this subquery, you're asking for the customers who are NOT IN a specific list, but that list includes unknown values. Therefore, you can not know with certainty that any rows are NOT IN the undefined set. If you use the default behavior of Query Analyzer-ANSI_NULLS behavior ON-the subquery will not work here unless you add a search condition to it. I recommend working within the bounds of the Query Analyzer session-setting defaults because some changes to those settings can have a negative impact on other operations. (For more information about session settings and their impact, see SQL Server Books Online-BOL-for "Set Options that Affect Results.") I describe the behavior I found with the defaults in Query Analyzer. Note that you could create an entry in Customer for the "over- The-counter "Customer To Solve this Problem, But you won"

t always have control over the database. Understanding how NULL values ​​work in all situations is crucial if you plan to support NULL values. The following query (with the search condition added) SELECT * FROM dbo.CustomerWHERE Customer.CustomerID NOT IN (SELECT [ Order] .customerid from dbo. [Order] where [Order] .customerid is not null) Returns the expected results, Which Table 2 Shows.

TABLE 2: Results of Query to Find Which Customers Purchased No ProductsCustomerIDFirstNameMiddleInitialLastName3SelinaCKyle4OswaldPCobblepot (2 row (s) affected) Now you have the answer to the original questions of who has and has not bought a product, but what if you need details How many? orders did Bruce place, what did he order, and when did he place the orders? With subqueries, you can see an order's existence but not its details. joins help you to see all the details. Let's look at the various types of joins. An inner join is the intersection between two tables. For inner and outer joins, Venn diagrams show, in a simple way, the relationships between the joined tables and the rows that the join query should return. For example, let's review the Customer and Order tables. You can graphically represent the question, Which customers have made a purchase? by showing Customer as the left circle and Order as the right circle, as Figure 2 shows. In the circles, CustomerIDs represent those customers who have placed an order. Note that two rows in the Customer circle (CustomerIDs 3 and 4) and two rows in the Order circle (representing over-the-counter sales) are outside the intersection. To perform a join between these tables, You First Need to Define The Criteria To Base The Intersection On. If You Want To Display The Orders for a Given Customer, The Customer's Customerid Must Equal To ORDER '

. S CustomerID This correspondence acts as the join definition Typically, joins are based on primary keytoforeign key relationships, so looking for these relationships first can help you write your joins You can write a join in either of two ways:.. You can use the current ANSI standard syntax (ANSI-92), which SQL Server 2000 supports, in which you place the join conditions in the FROM clause. Or you can use the older ANSI standard, in which you place the join conditions in the WHERE clause. you 'll encounter both styles, so let's look at the semantics of both Especially in dealing with outer joins, the two styles differ in the resultset produced as well as in syntax Both syntaxes start with the SELECT list:.. What information do you need about ? each order and the customer who placed the order for this example, let's ask for the customer's FirstName and the order's ProductID: SELECT Customer.FirstName, [Order] .ProductID - NOTE: Order must be in square brackets - because it's also A Keyword . To truly understand joins, I recommend that you anticipate the results. Ask yourself, How many rows should the query return? If you have six customers and eight orders, the maximum number of rows that an inner join between these tables could return is eight -the number of rows in the larger of the two tables. anticipating the number of rows helps you know when you've made a mistake. Because this estimation works only for small tables, I created the TSQLTutorJoins database with a limited number of rows. If Every Row in The Order Table Has A Match, You'll See Each Order Along with The Customerid for The Customer Who Placed The Order. Figure 2 Shows That Four Customerids-1, 2, 5, And 6-Have Placed Orders;

however, it does not show how many orders each one placed. So you know that your query should return between four and eight rows. With two rows in the Order table showing NULL for the CustomerID, you know that an inner join of these tables will return exactly six rows. Estimating the results is an important first step toward fully understanding the join relationships between tables. If you understand the resultset first, each join type will come into focus. Next, let's put together the JOIN clause. Using the newer ANSI standard-ANSI-92-you begin with the keyword FROM and add the name of one of the tables, fully qualifying the owner: FROM dbo.Customer Next, add the join type (INNER JOIN) and the remaining table name: INNER JOIN dbo. [Order] Note that the table order in the query is unimportant. you could also reverse the table order to write this query. Then, you need an ON clause that defines the relationship and contains the conditions on which the join is based. In this case, the customer r's CustomerID must match the order's CustomerID: ON Customer.CustomerID = [Order] .CustomerID Using the recommended ANSI syntax, the complete JOIN statement isSELECT Customer.Firstname AS CustomerFirstName, [Order] .ProductIDFROM dbo.Customer INNER JOIN dbo [Order]. ON Customer .customerid =

[Order] .CustomerID The old ANSI-standard syntax can be useful in correlated subqueries and other T-SQL syntax, although that syntax contains less detail about the type of join in the FROM clause. In addition, with the older syntax, you place the join conditions in the WHERE clause instead of the FROM clause. So, using the old ANSI standard syntax, the complete JOIN statement isSELECT Customer.Firstname AS CustomerFirstName, [Order] .ProductIDFROM dbo.Customer, dbo. [Order] WHERE Customer. Customerid =

转载请注明原文地址:https://www.9cbs.com/read-31134.html

New Post(0)