In: Computer Science
For this week let’s discuss the concepts of primary keys and table indexes.
Give some examples of data types and fields that you might use for certain scenarios and also what types are not good to use.
# To discuss these concepts we suppose primay key as a mapping concept and index value as array concept.
# So from here you may start thinking about what is array.
# Array-> Is a collection of similar data type , which is used for storage purpose of similar data .
Suppose you have U.S Dollars of different prices from 0-10. (similar data type)
you wanted to count them how many u have for a particular coin.
for that purpose you can use array
Amount counting
$ 0 -> 0
$ 1 -> 2
$ 2 -> 3
$ 3 -> 4
$ 4 -> 7
$ 5 -> 8
$ 6 -> 9
$ 7 -> 4
$ 8 -> 6
$ 9 -> 8
$ 10 -> 2
ARRAY
0 | 2 | 3 | 4 | 7 | 8 | 9 | 4 | 6 | 8 | 2 |
Index 0 1 2 3 4 5 6 7 8 9 10
Here index work -> coin number.(similar data type)
c++/c++14
#include<iostream>
using namespace std;
int main()
{
int a[11]; // this way you can intilise an array.
for (int i=0;i<11;++i) // loop for iteration
{
cin>>a[i];
}
}
# MAPPING-> suppose we have two array's and after combining both of them we get mapping
KEY POINTS:
1. Primary key -> Is unique , it can't be similar.
2. value of key -> It can be similar or different
Suppose Goku , Ash , Friza and Naruto are friends all of them have different U.S doller ammount
Name Amount
Goku -> $ 450
Ash -> $ 1200
Friza -> $ 900
Naruto -> $ 450
Method->1
we can use two array to store both values
Array-1 store Names
Goku | Ash | Friza | Naruto |
Index 0 1 2 3
Array-2 store Amount they have
450 | 1200 | 900 | 450 |
Index 0 1 2 3
Method->2
use a single map
primary key | value of key |
---|---|
Ash | 1200 |
Friza | 900 |
Goku | 450 |
Naruto | 450 |
Note->mapping has the property to sort accordingly to it's primay key.
use->you can write names and they will come in sorted (Dictionary manner).
program in mapping
Note-> map header file in mendotary
#include<iostream>
#inlcude<map>
using namespace std;
int main()
{
map<string,int> a; // this way you can declare map.
for (int i=0;i<4;++i) // loop for iteration
{
string s;
int amount ;
cout<<"\n Enter name: ";
cin>>s;
cout<<"Enter amount they have: ";
cin>>amount;
}
}
Disanvantage of using map -> It use more memory as compare to array, that's why use map only in case of storing related data in case of non-relative data use array.