Vous êtes sur la page 1sur 5

JOINS

By using joins, we can retrieve data from two or more tables based on logical relationships
between the tables. Joins indicate how Microsoft® SQL Server™ 2000 should use data from
one table to select the rows in another table.

A join condition defines the way two tables are related in a query by:

• Specifying the column from each table to be used for the join. A typical join condition
specifies a foreign key from one table and its associated key in the other table.

• Specifying a logical operator (=, <>, and so on) to be used in comparing values from
the columns.

Joins can be specified in either the FROM or WHERE clauses. The join conditions combine
with the WHERE and HAVING search conditions to control the rows that are selected from
the base tables referenced in the FROM clause.

When we join tables, the type of join that we create affects the rows that appear in the result
set. We can create the following types of joins:

S.No Type of Join Description


1 Inner Join A join that displays only the rows that have a match in both joined
tables. (This is the default type of join in the Query
Designer.) For example, we can join the titles and
publishers tables to create a result set that shows the
publisher name for each title. In an inner join, titles for
which we do not have publisher information are not
included in the result set, nor are publishers with no
titles. The resulting SQL for such a join might look like
this

SELECT title, pub_name

FROM titles INNER JOIN

publishers ON titles.pub_id = publishers.pub_id

Note: Columns containing NULL do not match any values when


you are creating an inner join and are therefore excluded
from the result set. Null values do not match other null
values
2 Outer Join A join that includes rows even if they do not have related rows in
the joined table. We can create three variations of an
outer join as in 2a, 2b and 2c to specify the unmatched
rows to be included.
2a Left Outer Join All rows from the first-named table (the "left" table, which appears
leftmost in the JOIN clause) are included. Unmatched
rows in the right table do not appear. For example, the
following SQL statement illustrates a left outer join
between the titles and publishers tables to include all
titles, even those we do not have publisher information
for

SELECT titles.title_id,
titles.title,
publishers.pub_name
FROM titles LEFT OUTER JOIN publishers
ON titles.pub_id

= publishers.pub_id
2b Right Outer Join All rows in the second-named table (the "right" table, which
appears rightmost in the JOIN clause) are included.
Unmatched rows in the left table are not included. For
example, a right outer join between the titles and
publishers tables will include all publishers, even those
who have no titles in the titles table. The resulting SQL
might look like this:

SELECT titles.title_id,
titles.title,
publishers.pub_name
FROM titles RIGHT OUTER JOIN publishers
ON titles.pub_id
= publishers.pub_id

2c Full Outer Join All rows in all joined tables are included, whether they are matched
or not. For example, a full outer join between titles and
publishers shows all titles and all publishers, even those
that have no match in the other table

SELECT titles.title_id,
titles.title,
publishers.pub_name
FROM titles FULL OUTER JOIN publishers
ON titles.pub_id

= publishers.pub_id
3 Cross Join A join whose result set includes one row for each possible pairing
of rows from the two tables. For example, authors
CROSS JOIN publishers yields a result set with one row
for each possible author/publisher combination. The
resulting SQL might look like this:

SELECT *
FROM authors CROSS JOIN publishers

CONSTRAINTS

Constraints allow us to define the way Microsoft® SQL Server™ 2000 automatically enforces the
integrity of a database. Constraints define rules regarding the values allowed in columns and are
the standard mechanism for enforcing integrity. Using constraints is preferred to using triggers,
rules, and defaults. The query optimizer also uses constraint definitions to build high-performance
query execution plans.

CLASSES OF CONSTRAINTS

SQL Server 2000 supports five classes of constraints

S.No Type of Constraint Description


1 Primary Key PRIMARY KEY constraints identify the column or set of columns
whose values uniquely identify a row in a table.

No two rows in a table can have the same primary key value. We
cannot enter a NULL for any column in a primary key.
NULL is a special value in databases that represents an
unknown value, which is distinct from a blank or 0 value.
Using a small, integer column as a primary key is
recommended. Each table should have a primary key

A table may have more than one combination of columns that could
uniquely identify the rows in a table; each combination is
a candidate key. The database administrator picks one
of the candidate keys to be the primary key. For
example, in the part_sample table both part_nmbr and
part_name could be candidate keys, but only
part_nmbr is chosen as a primary key.

CREATE TABLE part_sample


(part_nmbr int PRIMARY KEY,
part_name char(30),
part_weight decimal(6,2),

part_color char(15) )
2 Foreign Key FOREIGN KEY constraints identify the relationships between
tables.

A foreign key in one table points to a candidate key in another


table. Foreign keys prevent actions that would leave
rows with foreign key values when there are no
candidate keys with that value. In the following sample,
the order_part table establishes a foreign key
referencing the part_sample table defined earlier.
Usually, order_part would also have a foreign key
against an order table, but this is a simple example.

CREATE TABLE order_part


(order_nmbr int,
part_nmbr int
FOREIGN KEY REFERENCES
part_sample(part_nmbr)
ON DELETE NO ACTION,
qty_ordered int)

GO

We cannot insert a row with a foreign key value (except NULL) if


there is no candidate key with that value. The ON
DELETE clause controls what actions are taken if you
attempt to delete a row to which existing foreign keys
point. The ON DELETE clause has two options

• NO ACTION specifies that the deletion fails with an


error.

• CASCADE specifies that all the rows with foreign


keys pointing to the deleted row are also deleted

The ON UPDATE clause defines the actions that are taken if you
attempt to update a candidate key value to which
existing foreign keys point. It also supports the NO
ACTION and CASCADE options.
3 Unique Key UNIQUE constraints enforce the uniqueness of the values in a set
of columns.

No two rows in the table are allowed to have the same not null
values for the columns in a UNIQUE constraint. Primary
keys also enforce uniqueness, but primary keys do not
allow null values. A UNIQUE constraint is preferred over
a unique index.
4 Not Null NOT NULL specifies that the column does not accept NULL values
5 Check CHECK constraints enforce domain integrity by limiting the values
that can be placed in a column.

A CHECK constraint specifies a Boolean (evaluates to TRUE or


FALSE) search condition that is applied to all values
entered for the column; all values that do not evaluate to
TRUE are rejected. We can specify multiple CHECK
constraints for each column. This sample shows the
creation of a named constraint, chk_id, which further
enforces the domain of the primary key by ensuring that
only numbers within a specified range are entered for the
key.

CREATE TABLE cust_sample

cust_id int PRIMARY KEY,

cust_name char(50),

cust_address char(50),

cust_credit_limit money,

CONSTRAINT chk_id CHECK (cust_id BETWEEN 0 and


10000 ))

Vous aimerez peut-être aussi