Sponsored Links

Kamis, 12 Juli 2018

Sponsored Links

What is difference between Primary key and Unique key 8445277 ...
src: www.sitesbay.com

In database relational modeling and implementation, a unique key (also known as a candidate key) of a relation is a minimal superkey for that relation; that is, a set of attributes such that:

  1. the relation does not have two distinct tuples (i.e. rows or records in common database language) with the same values for these attributes (which means that the set of attributes is a superkey)
  2. there is no proper subset of these attributes for which (1) holds (which means that the set is minimal).

When a column or set of columns is defined as unique to the database management system, the system verifies that each set of values is unique before assigning the constraint. After the columns are defined as unique, an error will occur if an insertion is attempted with values that already exist. Some systems will not allow key values to be updated, all systems will not allow duplicates. This ensures that uniqueness is maintained in both the primary table and any relations that are later bound to it.


Video Unique key



Summary

Keys provide the means for database users and application software to identify, access and update information in a database table. There may be several keys in any given table. For example, two distinct keys in a table of employees might be employee number and login name. The enforcement of a key constraint (i.e. a uniqueness constraint) in a table is also a data integrity feature of the database. The DBMS prevents updates that would cause duplicate key values and thereby ensures that tables always comply with the desired rules for uniqueness. Proper selection of keys when designing a database is therefore an important aspect of database integrity.

A relational database table may have one or more available keys (formally called candidate keys). One of those keys per table may be designated the primary key; other keys are called alternate keys.

Any key may consist of one or more attributes. For example, a Social Security Number might be a single attribute key for an employee; a combination of flight number and date might be a key consisting of two attributes for a scheduled flight.

There are several types of keys used in database modeling and implementations.

At the most basic definition, "a key is a unique identifier", so unique key is a pleonasm. Keys that are within their originating entity are unique within that entity. Keys that migrate to another entity may or may not be unique, depending on the design and how they are used in the other table. Foreign keys may be the primary key in another table; for example a PersonID may become the EmployeeID in the Employee table. In this case, the EmployeeID is both a foreign key and the unique primary key, meaning that the tables have a 1:1 relationship. In the case where the person entity contained the biological father ID, the father ID would not be expected to be unique because a father may have more than one child.

Here is an example of a primary key becoming a foreign key on a related table. ID migrates from the Author table to the Book table.

Here ID serves as the primary key in the table 'Author', but also as AuthorID serves as a Foreign Key in the table 'Book'. The Foreign Key serves as the link, and therefore the connection, between the two related tables in this sample database.

In a relational database, a candidate key uniquely identifies each row of data values in a database table. A candidate key comprises a single column or a set of columns in a single database table. No two distinct rows or data records in a database table can have the same data value (or combination of data values) in those candidate key columns since NULL values are not used. Depending on its design, a database table may have many candidate keys but at most one candidate key may be distinguished as the primary key.

A key constraint applies to the set of tuples in a table at any given point in time. A key is not necessarily a unique identifier across the population of all possible instances of tuples that could be stored in a table but it does imply a data integrity rule that duplicates should not be allowed in the database table. Some possible examples of unique keys are Social Security Numbers, ISBNs, vehicle registration numbers or user login names.

Unique keys as well as primary keys may be logically referenced by foreign keys, but most RDBMS only allow a foreign key constraint against a primary key.


Maps Unique key



Defining unique keys in SQL

The definition of other unique keys is syntactically very similar to primary keys.

Likewise, unique keys can be defined as part of the CREATE TABLE SQL statement.

Differences from primary key constraints

Differences between Primary Key constraint and Unique constraint:

Primary Key constraint
1. A primary key cannot allow null (a primary key cannot be defined on columns that allow nulls).
2. Each table cannot have more than one primary key.
3. On some RDBMS a primary key generates a clustered index by default.

Unique constraint
1. A unique constraint can be defined on columns that allow nulls.
2. Each table can have multiple unique keys.
3. On some RDBMS a unique key generates a nonclustered index by default.

Note that unlike the PRIMARY KEY constraint a UNIQUE constraint does not imply NOT NULL for the columns participating in the constraint. NOT NULL must be specified to make the column(s) a key. It is possible to put UNIQUE constraints on nullable columns but the SQL standard states that the constraint does not guarantee uniqueness of nullable columns (uniqueness is not enforced for rows where any of the columns contains a null).

According to the SQL standard a unique constraint does not enforce uniqueness in the presence of nulls and can therefore contain several rows with identical combinations of nulls and non-null values -- however not all RDBMS implement this feature according to the SQL standard.


Difference between Unique key Constraint and Primary Key ...
src: i.ytimg.com


See also

  • Globally Unique Identifier
  • Persistent Object Identifier

Special Unique Key Rack Ideas For You #7272
src: ceveta.com


References


Creating a Unique Constraint in Microsoft SQL Server Management ...
src: i.ytimg.com


External links

  • Relation Database terms of reference, Keys: An overview of the different types of keys in an RDBMS

Source of the article : Wikipedia

Comments
0 Comments