Wednesday, January 27, 2010

FOREIGN KEY


Share/Save/Bookmark
A foreign key (FK) is a column or combination of columns that is used to establish and enforce a link between the data in two tables. You can create a foreign key by defining a FOREIGN KEY constraint when you create or modify a table.
In a foreign key reference, a link is created between two tables when the column or columns that hold the primary key value for one table are referenced by the column or columns in another table. This column becomes a foreign key in the second table.
For example, the Sales.SalesOrderHeader table in the AdventureWorks database has a link to the Sales.SalesPerson table because there is a logical relationship between sales orders and salespeople. The SalesPersonID column in the SalesOrderHeader table matches the primary key column of the SalesPerson table. The SalesPersonID column in the SalesOrderHeader table is the foreign key to the SalesPerson table.
SalesOrderHeader.SalesPersonID is a foreign key.
A FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY constraint in another table; it can also be defined to reference the columns of a UNIQUE constraint in another table. A FOREIGN KEY constraint can contain null values; however, if any column of a composite FOREIGN KEY constraint contains null values, verification of all values that make up the FOREIGN KEY constraint is skipped. To make sure that all values of a composite FOREIGN KEY constraint are verified, specify NOT NULL on all the participating columns.

from : technet.microsoft.com

CHECK CONSTRAINT


Share/Save/Bookmark
A check constraint (also known as table check constraint) is a condition that defines valid data when adding or updating an entry in a table of a relational database. A check constraint is applied to each row in the table. The constraint must be a predicate. It can refer to a single or multiple columns of the table. The result of the predicate can be either TRUE, FALSE, or UNKNOWN, depending on the presence of NULLs. If the predicate evaluates to UNKNOWN, then the constraint is not violated and the row can be inserted or updated in the table. This is contrary to predicates in WHERE clauses in SELECT or UPDATE statements.
For example, in a table containing products, one could add a check constraint such that the price of a product and quantity of a product is a non-negative value:
PRICE >= 0
QUANTITY >= 0
If these constraints were not in place, it would be possible to have a negative price (-$30) or quantity (-3 items).
Check constraints are used to ensure the validity of data in a database and to provide data integrity. If they are used at the database level, applications that use the database will not be able to add invalid data or modify valid data so the data becomes invalid, even if the application itself accepts invalid data.

Definition

Each check constraint has to be defined in the CREATE TABLE or ALTER TABLE statement using the syntax:
CREATE TABLE table_name (
    ...,
    CONSTRAINT constraint_name CHECK ( predicate ),
    ...
 )
ALTER TABLE table_name
    ADD CONSTRAINT constraint_name CHECK ( predicate )
If the check constraint refers to a single column only, it is possible to specify the constraint as part of the column definition.
CREATE TABLE table_name (
    ...
    column_name type CHECK ( predicate ),
    ...
 )

NOT NULL Constraint

A NOT NULL constraint is functionally equivalent to the following check constraint with an IS NOT NULL predicate:
CHECK (column IS NOT NULL)
Some relational database management systems are able to optimize performance when the NOT NULL constraint syntax is used as opposed to the CHECK constraint syntax given above.

 Common Restrictions

Most database management systems restrict check constraints to a single row, with access to constants and deterministic functions, but not to data in other tables, or to data invisible to the current transaction because of transaction isolation.
Such constraints are not truly table check constraints but rather row check constraints. Because these constraints are generally only verified when a row is directly updated (for performance reasons,) and often implemented as implied INSERT or UPDATE triggers, integrity constraints could be violated by indirect action were it not for these limitations. Future, otherwise-valid modifications to these records would then be prevented by the CHECK constraint. Some examples of dangerous constraints include:
  • CHECK ((select count(*) from invoices where invoices.customerId = customerId) < 1000)
  • CHECK (dateInserted = CURRENT_DATE)
  • CHECK (countItems = RAND())
User-defined triggers can be used to work around these restrictions. Although similar in implementation, it is semantically clear that triggers will only be fired when the table is directly modified, and that it is the designer's responsibility to handle indirect, important changes in other tables; constraints on the other hand are intended to be "true at all times" regardless of the user's actions or the designer's lack of foresight.

from : wikipedia.org

UNIQUE CONTRAINTS


Share/Save/Bookmark

What is a unique constraint?

A unique constraint is a single field or combination of fields that uniquely defines a record. Some of the fields can contain null values as long as the combination of values is unique.
Note:
In Oracle, a unique constraint can not contain more than 32 columns.
A unique constraint can be defined in either a CREATE TABLE statement or an ALTER TABLE statement.

What is the difference between a unique constraint and a primary key?

Primary Key
Unique Constraint
None of the fields that are part of the primary key can contain a null value.
Some of the fields that are part of the unique constraint can contain null values as long as the combination of values is unique.
Oracle does not permit you to create both a primary key and unique constraint with the same columns.

Using a CREATE TABLE statement

The syntax for creating a unique constraint using a CREATE TABLE statement is:
CREATE TABLE table_name
(column1 datatype null/not null,
column2 datatype null/not null,
...
CONSTRAINT constraint_name UNIQUE (column1, column2, . column_n)
);

For example:
CREATE TABLE supplier
(
supplier_id
numeric(10)
not null,

supplier_name
varchar2(50)
not null,

contact_name
varchar2(50),


CONSTRAINT supplier_unique UNIQUE (supplier_id)
);
In this example, we've created a unique constraint on the supplier table called supplier_unique. It consists of only one field - the supplier_id field.

We could also create a unique constraint with more than one field as in the example below:
CREATE TABLE supplier
(
supplier_id
numeric(10)
not null,

supplier_name
varchar2(50)
not null,

contact_name
varchar2(50),


CONSTRAINT supplier_unique UNIQUE (supplier_id, supplier_name)
);

Using an ALTER TABLE statement

The syntax for creating a unique constraint in an ALTER TABLE statement is:
ALTER TABLE table_name
add CONSTRAINT constraint_name UNIQUE (column1, column2, ... column_n);

For example:
ALTER TABLE supplier
add CONSTRAINT supplier_unique UNIQUE (supplier_id);
In this example, we've created a unique constraint on the existing supplier table called supplier_unique. It consists of the field called supplier_id.

We could also create a unique constraint with more than one field as in the example below:
ALTER TABLE supplier
add CONSTRAINT supplier_unique UNIQUE (supplier_id, supplier_name);

Drop a Unique Constraint

The syntax for dropping a unique constraint is:
ALTER TABLE table_name
drop CONSTRAINT constraint_name;

For example:
ALTER TABLE supplier
drop CONSTRAINT supplier_unique;
In this example, we're dropping a unique constraint on the supplier table called supplier_unique.

Disable a Unique Constraint

The syntax for disabling a unique constraint is:
ALTER TABLE table_name
disable CONSTRAINT constraint_name;

For example:
ALTER TABLE supplier
disable CONSTRAINT supplier_unique;
In this example, we're disabling a unique constraint on the supplier table called supplier_unique.

Enable a Unique Constraint

The syntax for enabling a unique constraint is:
ALTER TABLE table_name
enable CONSTRAINT constraint_name;

For example:
ALTER TABLE supplier
enable CONSTRAINT supplier_unique;
In this example, we're enabling a unique constraint on the supplier table called supplier_unique.


from : www.techonthenet.com

PRIMARY KEY


Share/Save/Bookmark

PRIMARY KEY Constraints

A table usually has a column or combination of columns whose values uniquely identify each row in the table. This column (or columns) is called the primary key of the table and enforces the entity integrity of the table. You can create a primary key by defining a PRIMARY KEY constraint when you create or alter a table.
A table can have only one PRIMARY KEY constraint, and a column that participates in the PRIMARY KEY constraint cannot accept null values. Because PRIMARY KEY constraints ensure unique data, they are often defined for identity column.
When you specify a PRIMARY KEY constraint for a table, Microsoft® SQL Server™ 2000 enforces data uniqueness by creating an unique index, for the primary key columns. This index also permits fast access to data when the primary key is used in queries.
If a PRIMARY KEY constraint is defined on more than one column, values may be duplicated within one column, but each combination of values from all the columns in the PRIMARY KEY constraint definition must be unique.
As shown in the following illustration, the au_id and title_id columns in the titleauthor table form a composite PRIMARY KEY constraint for the titleauthor table, which ensures that the combination of au_id and title_id is unique.

When you work with joins, PRIMARY KEY constraints relate one table to another. For example, to determine which authors have written which books, you can use a three-way join between the authors table, the titles table, and the titleauthor table. Because titleauthor contains columns for both the au_id and title_id columns, the titles table can be accessed by the relationship between titleauthor and titles.
from : msdn.microsoft.com

CONSTRAINT


Share/Save/Bookmark
In the object hierarchy under any table, you will find Constraints. Constraints can be added by selecting ADD TABLE CONSTRAINT from the Constraints context menu. Once a constraint has been created, it can also be commented on and dropped. This section describes how to create, comment on, and drop a constraint with RHDB Administrator.
Constraints are not actual entities themselves (except for check constraints). Constraints are mostly a collection of indexes and triggers that restrict certain actions on a table. There are four types of constraints:
Primary Key Constraints
Unique Constraints
Check Constraints
Foreign Key (FK) Constraints.
The following sections describe each of these constraints in brief and provide an example of how to create them. Note that all of these examples are based on the table invoices

Primary Key Constraints

A primary key is a type of index that will most likely be used as the primary index when a query is made on the table (assuming of course, that one of the conditional fields in the query pertains to the column on which the index exists).

Unique Constraints

Unique constraints may be placed on multiple columns. They constrain the UPDATE/INSERTS on the table so that the values being updated or inserted do not match any other row in the table for the corresponding values.

Check Constraints

A check constraint prevents updates/inserts on the table by placing a check condition on the selected column. The UPDATE/INSERT is allowed only if the check condition qualifies.

Foreign Key (FK) Constraints

A foreign key constraint allows certain fields in one table to refer to fields in another table. This type of constraint is useful if you have two tables, one of which has partial information, details on which can be sought from another table with a matching entry. A foreign key constraint in this case will prevent the deletion of an entry from the table with detailed information if there is an entry in the table with partial information that matches it.

from : www.redhat.com

RELATIONAL DATABASE


Share/Save/Bookmark
A relational database matches data by using common characteristics found within the data set. The resulting groups of data are organized and are much easier for people to understand.
For example, a data set containing all the real-estate transactions in a town can be grouped by the year the transaction occurred; or it can be grouped by the sale price of the transaction; or it can be grouped by the buyer's last name; and so on.
Such a grouping uses the relational model (a technical term for this is schema). Hence, such a database is called a "relational database."
The software used to do this grouping is called a relational database management system. The term "relational database" often refers to this type of software.
Relational databases are currently the predominant choice in storing financial records, manufacturing and logistical information, personnel data and much more.

 Contents
Strictly, a relational database is a collection of relations (frequently called tables). Other items are frequently considered part of the database, as they help to organize and structure the data, in addition to forcing the database to conform to a set of requirements.

Terminology

The term relational database was originally defined and coined by Edgar Codd at IBM Almaden Research Center in 1970.[1]


Relational database terminology.
Relational database theory uses a set of mathematical terms, which are roughly equivalent to SQL database terminology. The table below summarizes some of the most important relational database terms and their SQL database equivalents.

Relational term
SQL equivalent
relation, base relvar  
table
derived relvar
view, query result, result set
tuple
row
attribute
column

Relations or Tables

A relation is defined as a set of tuples that have the same attributes. A tuple usually represents an object and information about that object. Objects are typically physical objects or concepts. A relation is usually described as a table, which is organized into rows and columns All the data referenced by an attribute are in the same domain and conform to the same constraints.
The relational model specifies that the tuples of a relation have no specific order and that the tuples, in turn, impose no order on the attributes. Applications access data by specifying queries, which use operations such as select to identify tuples, project to identify attributes, and join to combine relations. Relations can be modified using the insert, delete, and update operators. New tuples can supply explicit values or be derived from a query. Similarly, queries identify tuples for updating or deleting. It is necessary for each tuple of a relation to be uniquely identifiable by some combination (one or more) of its attribute values. This combination is referred to as the primary key.


from : wikipedia

QUERY


Share/Save/Bookmark
Query languages are computer language used to make queries into databases and information system
Broadly, query languages can be classified according to whether they are database Query languages or information retrieval query language Examples include: