1

My question is, is it necessary for a relation/table in database to have a candidate key and hence a primary key? Is it possible to have a relation where a row cannot be uniquely identified by any combination of attributes?

If no, why? And if yes, then how does a DBMS make operations like search, delete etc, efficient?

nvogel
  • 24,981
  • 1
  • 44
  • 82
Aditya Naidu
  • 697
  • 2
  • 7
  • 18
  • Gordon edited the original question and added the SQL tag. I think that's unjustified and has given rise to some possibly misleading answers (including my answer below). Aditya didn't specify that his question had anything to do with SQL. I have removed the SQL tag. – nvogel Sep 21 '15 at 14:55

3 Answers3

2

Relations always have distinct tuples which means that in a Relational DBMS a table always has at least one candidate key.

SQL is a different case. SQL tables are "tuple bags", not relations. SQL tables can have duplicate rows, which is one of SQL's biggest flaws. Despite the fact that SQL supports duplicate rows the language is ill-suited to cope with them. In the presence of duplicate rows the SQL standard UPDATE and DELETE for instance have no guaranteed way to reference individual rows without resorting to some complex cursor-based operations.

Consequent problems of duplicate rows are certain inefficiencies and complexities of SQL DBMSs and a lack of orthogonality in their features. SQL DBMS engines have to use internal structures and support special features as a prerequisite in order to deal with duplicate rows. Some DBMS vendors try to get around the difficulties by disabling certain features for tables that don't have keys.

nvogel
  • 24,981
  • 1
  • 44
  • 82
0

A database does not require a primary key. A table is just an unordered set of rows. Without any indexes, the only mechanism for accessing rows in a table is a full table scan (or a full partition scan, if the table is partitioned). Such operations are only efficient for very small numbers of rows.

Tables are more useful when you can refer to particular rows. Often, the best primary keys are auto incremented/identity primary keys. These are maintained by the database. In practice, all tables in a well-designed database are going to have primary keys. Here are three reasons:

  • Rows can be referred to by other tables.
  • Individual rows can be updated and deleted.
  • Individual rows can be selected efficiently and unambiguously.

Note: you can have indexes on a table without primary keys. And combinations of one or more columns can be made unique, even if the combination is not a primary key. The primary key itself is an index, so the inverse is not true. And all rows in a table have "row addresses" which are unique. Whether or not these are available for queries depends on the database engine.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

Yes, this is possible.

Just note, that some identifier does exists behind the scenes (Example from SQL Server):

When a table is stored as a heap, individual rows are identified by reference to a row identifier (RID) consisting of the file number, data page number, and slot on the page

How operations will be performed?

A table scan will be needed for almost any operation:

If a table is a heap and does not have any nonclustered indexes, then the entire table must be examined (a table scan) to find any row

Yosi Dahari
  • 6,794
  • 5
  • 24
  • 44