Académique Documents
Professionnel Documents
Culture Documents
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:
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
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.
part_color char(15) )
2 Foreign Key FOREIGN KEY constraints identify the relationships between
tables.
GO
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.
cust_name char(50),
cust_address char(50),
cust_credit_limit money,