Entity Integrity page 1

Entity integrity means that each object represented by a row in the table should be distinguishable from any other object. In other words, there should be such a set of attributes whose unique combination of values would allow telling one object from another. Such an attribute set unambiguously identifying the object is called a candidate key. It cant contain NULL marks because it wont let us identify the object (like, say, a book with an unknown title).

A table can have several candidate keys. For instance, a person can be identified by her/his passport number, insurance policy number, VATIN, drivers license number, etc.

To enforce entity integrity,  SQL(Structured Query Language) is a database computer language designed for the retrieval and management of data in relational database management systems (RDBMS), database schema creation and modification, and database object access control management.SQL lets you define the PRIMARY KEY and UNIQUE specifications. A table can only have one primary key, but several unique indexes. That is, you can define the PRIMARY KEY specification for one of the candidate keys, and UNIQUE for the rest of them.

What can serve as the primary key in our case? Since a maker can produce several models and thus turn up in the Product table data several times, the maker column isnt suitable for the role of the primary key. Similarly, the type attribute isnt unique either.

The only attribute having no duplicates is the model code, and this is our sole contender to be the primary key. There are no other candidate keys in the table. To proof that, we can scrutinize all other column combinations and demonstrate they dont ensure object identification. In particular, the value combination of the three columns in the example above is unique, yet fails to identify model 1232 nevertheless.

Lets create a primary key. SQL allows changing the structure of an existing table by means of the ALTER TABLE statement. However, to avoid too much information at one time, well just re-create the table, that is, delete it and create it anew with a primary key. Deleting a table isnt difficult (destroying is easy, building is hard, as the saying goes) you just need to carry out the DROP TABLE <table name> statement. So then,

  1. DROP TABLE Product;
  2. CREATE TABLE Product (
  3. maker varchar(10),
  4. model varchar(50) PRIMARY KEY,
  5. type varchar(50)
  6. );

We included the primary key specification into the column definition. However, we could achieve the same effect by using a separate constraint:

  1. CONSTRAINT < constraint name > PRIMARY KEY (< list of COLUMNS making up the PRIMARY KEY >)

Our code will then look like this (again, we delete the previously created table first):

  1. DROP TABLE Product;
  2. CREATE TABLE Product (
  3. maker varchar(10),
  4. model varchar(50),
  5. type varchar(50),
  6. CONSTRAINT product_PK PRIMARY KEY (model)
  7. );

Now, the DBMS itself will make sure the primary key doesnt have any duplicate values or NULL marks. If we try to add data using the INSERT statement from before, well get the following error message: :

Violation of PRIMARY KEY constraint 'product_PK'. Cannot insert duplicate key in object 'Product'. The duplicate key value is (1232).

If we correct the input error by providing a proper model code for the printer

  2. ('A', '1232', 'PC'),
  3. ('A', '3001', 'Printer'),
  4. (NULL, NULL, NULL);
well get another error caused by the uncertainty of the primary key value:

Cannot insert the value NULL into column 'model', table 'Product'; column does not allow nulls. INSERT fails.

The statement

  2. ('A', '1232', 'PC'),
  3. ('A', '3001', 'Printer'),
  4. (NULL, '2000', NULL);
will work fine.

Bookmark and Share
Pages 1 2
aggregate functions Airport ALL AND AS keyword ASCII AVG Battles Bezhaev Bismarck C.J.Date calculated columns Cartesian product CASE cast CHAR CHARINDEX Chebykin check constraint classes COALESCE common table expressions comparison predicates Computer firm CONSTRAINT CONVERT correlated subqueries COUNT CROSS APPLY CTE data type conversion data types database schema DATEADD DATEDIFF DATENAME DATEPART DATETIME date_time functions DDL DEFAULT DEFAULT VALUES DELETE DISTINCT DML duplicates edge equi-join EXCEPT exercise (-2) More tags
The book was updated
month ago
HAPPYLUXHOME . goldnice.ru
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.