Monday, 6 February 2017

Define the term cardinality in SQL

What is cardinality in SQL, define cardinality, what is the cardinality of a relational table, what is the cardinality of an attribute


Cardinality (in SQL)


In SQL (Structured Query Language), the term cardinality means the uniqueness of the data values that are stored in a table. It can be defined as follows;

  • Cardinality of a relation (table) – the number of records (tuples) in the given table.
  • Cardinality of an attribute (column) – the number of unique data values in the specified attribute.

See the example EMPLOYEES table with few records below;

EMPID
HIREDATE
SALARY
DEPT
JOBCODE
SEX
119012
01JUL1973
42340.58
CSR010
602
F
120591
05DEC1985
31000.55
SHP002
602
F
127845
16JAN1972
75320.34
ACC024
204
M
129540
01AUG1987
56123.34
SHP002
204
F
135673
15JUL1989
46322.58
ACC013
602
F
212916
15FEB1958
52345.58
CSR010
602
F
216382
15JUN1990
34004.65
SHP013
602
F
234967
19DEC1993
17000.00
CSR004
602
M
237642
01NOV1981
43200.34
SHP013
602
M
239185
07MAY1986
57920.66
ACC024
602
M


  • Cardinality of the table
    • EMPLOYEES table – 10 (10 records are there in EMPLOYEES table)
  • Cardinality of the attributes
    • EMPID attribute – 10 (only 10 employees listed in this table).
    • DEPT attribute – 6 (only 6 departments – in other words only 6 permitted values for DEPT attribute)
    • JOBCODE attribute – 2 (only 2 types of jobs – in other words only 2 permitted values for JOBCODE attribute)
    • SEX attribute – 2 (only 2 genders male and female – in other words only 2 permitted values for SEX attribute)
And so on.

Note: Cardinality of the PRIMARY KEY attribute is equal to the number of records in the table. For example, EMPID is the primary key and hence the unique values are equal to the number of records, ie. 10.









No comments:

Post a Comment

SQL exercises for beginners one

SQL Exercises for Beginners / Simple SQL Exercises with Answers / Solved SQL exercises / SQL solved exercises with simple conditions / So...