Showing posts with label Data Types. Show all posts
Showing posts with label Data Types. Show all posts

MySQL Data Types and Properties

MySQL Data Types and Properties


Have a look at the following line of code from the post Storing
and Retrieving Data from MySQL Database.


CREATE TABLE phno(

name varchar( 50 ) ,

phnum varchar( 20 )

)


As I told you we have to declare the column data types while creating tables
depending on the values we want to store. MySQL has wide variety of data types,
out of which I’m stating some of the important ones below:




  1. CHAR: For storing character and string data, faster than VARCHAR.




  2. VARCHAR: For storing character and string data. Slower but efficient in
    storing variable length string as only the needed memory is used no matter
    how much we allocate.




  3. INT: For storing regular integers. Same as INTEGER, other related data
    types are TINYINT, SMALLINT, MEDIUMINT, BIGINT.




  4. FLOAT: For storing floating point values, it is 4 bytes long. There is
    also a DOUBLE which is 8 bytes long.




  5. DATE: For storing YYYY-MM-DD date values.




Besides data types we can also assign column fields some properties to change
the way they store data. These are some of the useful ones:




  1. NOT NULL: This specifies that a particular field cannot be empty. When
    inserting data you must provide data for this field or else MySQL generates
    error. It is used to make sure value for certain column is always provided.




  2. AUTO_INCREMENT: This property can be used with integer field to automatically
    make MySQL insert serial numbers to each row inserted.




  3. PRIMARY KEY: primary key is very important property of a table. It helps
    MySQL speed up query processing. Usually, serial number column field is
    defined as a primary key but it can be any field which is unique for each
    row.




The following SQL code illustrates how the above properties are used:


CREATE TABLE temp(

id integer AUTO_INCREMENT PRIMARY KEY ,

name varchar( 20 ) NOT NULL

)


It is a good idea to always have an extra column for each table. This column
should be defined as both AUTO_INCREMENT and PRIMARY KEY.


Previous Articles:


Variables, Type Casting and Constants in PHP

Let’s start by looking at the fundamental difference between variables
in PHP and in C++:




  1. Variables in PHP need not be declared before using




  2. Variables can hold any type of values due to the fact that variable are
    not declared of any type, you can store any value in any variable no matter
    which type of value it is currently holding.




So, in the previous post (Conditional
Statements if...else in PHP
) when we needed a variable to
hold the integer (hour of the day) we just wrote


$t=(int) date(“G”);


and suppose we now want to have a string to be stored in $t variable, we’d
just have to write


$t=”PHP”;


You see, in the first statement it was an integer variable but now it’s
a string. Therefore we can conclude that the type of a variable is determined
by the value currently assigned to it.


PHP has the following types of data:



  • Integer

  • Float

  • String

  • Boolean

  • Array

  • Object


Since PHP takes care of data types internally so you don’t have to put
much of your brain to it.


Type casting


PHP being a loosely typed language gives us a way to force different data types
using type casting. Type casting, as you know is a powerful feature that C++
gives us and fortunately PHP too, and that too works in the same way.


Again form previous post’s (Conditional
Statements if...else in PHP
) example


$t=(int)date(“G”);


As date function returns a string, we’ve to convert it to an integer
to make calculations. For this we are forcing the variable to store the returned
value as an integer.


Declaring constants


Constants in PHP are declared in the following form:


define(“PI”,3.14);


It declares a constant PI with the value 3.14. the constant in UPPER CASE is
just a convention (as in C++) which makes it easier to distinguish from other
variables.


A few points to note




  • Variables can be of any length having letters, numbers and underscore




  • Variables and constants cannot begins with a number




  • Variables are case-sensitive. $t and $T are different.




  • Variables in PHP always start with a ‘$’ sign whereas constants
    DON”T.




Related Articles:


Operation on Bits and Bitwise Operators

OK guys, this is my first post in the New Year 2008, I thought of posting it
earlier but at last I didn’t. It’s already been so long since I
posted so let’s keep everything aside and talk just about what we have
for today. ;-)


I was sitting the other day thinking about what to write for a post here. Suddenly
I realized that we have discussed operations
on matrices
, arrays,
and what not but we haven’t had the chance to talk anything about the
most fundamental thing a computer understands. Yeah, Operation on Bits.


Bits can have only two values either ON (1) or OFF (0). In this article, we’ll
be discussing about the different operations which can be performed on bits.
One thing to note here is, we don’t perform these operation on single
bits but rather on a group of bits (byte(s)). So even though Bitwise operators
operate on bits its almost always a part of a group (byte, which makes up each
data type), it means we can do bitwise operations on any data type.


BTW, the operators that perform operation on bits are called Bitwise Operator
and such operations are known as Bitwise Operations


The six bitwise operators are listed below:




























&


AND


|


OR


^


XOR


>>


Right shift


<<


Left shift


~


One’s complement


For this post we’ll only be discussing &(AND) and | (OR) operators
leaving the rest for future posts ;-)


Bitwise AND (&) Operator: First thing, it’s nothing to do with the
Logical (&&) operator, both are different.


Now, if you know something about Logic Gates then you might already know about
this. For the rest of us, it does an AND mask on bits.


So, suppose if we have two separate bytes having binary values as 00100000
and 00100001 then doing AND operation would give us the following result.











First Byte:

Second Byte:

00100000

00100001

Result:

00100000

The truth table for this would be:













First Bit
Second Bit
& (AND)

1


1


0


0


1


0


1


0



1


0


0


0



As the Logic AND Gate does, it takes two bits (from the two separate bytes)
and if both of them are ON (1) then only it gives ON (1) in all other cases
it gives OFF (0). So starting from the right there is 0&1->0, 0&0->0,…,
1&1->1 and so on.


Bitwise OR (|) Operator: Here again, both OR (||) and Bitwise OR (|) are different.


The following example is sufficient for you all to understand its operation.











First Byte:

Second Byte:

00100000

00100001

Result:

00100001

Truth table













First Bit
Second Bit
& (OR)

1


1


0


0

1


0


1


0

1


1


1


0


There won’t be any example program here because to fully understand these
operators we need to express data as bits (binary form) and see how the operations
change them. Since decimal to binary conversion programs require some bitwise
operations that we’ve yet to discuss so I think it’ll be pointless
to have such programs now!


P.S. An integer in 32-Bit (Windows) environment is 4 bytes long. Short int
is half of that

8 bits make up one byte.


Related Articles:


Introduction to Dynamic Memory Allocation in C++

Suppose you are making a C++ program to store a particular number (specified
by the user at runtime) of phone numbers. What will you do? You cannot declare
array of 10 or 20 elements because you don’t know how many numbers the
user will enter. He may want to enter 1, 10 or even 100 phone numbers.


So what will you do?


Actually, you can do two things to achieve this, that are listed below:




  1. You can create a very large int(eger) array (say, of 500 elements) , in
    this case whether the user wants to store 1 or 500 phone numbers, the memory
    used will always be the same (large!).




  2. You can make use of C++’s dynamic memory allocation to allocate only
    the needed amount of memory.




The first choice is a very bad way of programming, nevertheless it works fine,
you should never employ such an inefficient method of programming.


So, you are left with only one choice to use the Dynamic Memory Allocation
of C++ programming language, that I will be discussing in this article.


As is clear from the above example, dynamic memory allocation is needed when
you don’t know the program’s memory needs beforehand.


Allocating Memory


Dynamic memory is allocated by the new keyword. Memory for
one variable is allocated as below:


ptr=new DataType (initializer);

Here,




  • ptr is a valid pointer of type DataType.




  • DataType is any valid c++ data type.




  • Initializer (optional) if given, the newly allocated variable is initialized
    to that value.




Ex.


  //Example Program in C++
#include<iostream.h>

void main(void)
{
int *ptr;
ptr=new int(10);


cout<<*ptr;

delete ptr;
}

This is will allocate memory for an int(eger) having initial value 10, pointed
by the ptr pointer.


Memory space for arrays is allocated as shown below:


ptr=new DataType [x];

Here,




  • ptr and DataType have the same meaning as above.




  • x is the number of elements and C is a constant.




Ex.


  //Example Program in C++
#include<iostream.h>

void main(void)
{
int *ptr, size;

cin>>size;
ptr=new int[size];

//arrays are freed-up like this
delete []ptr;
}

Freeing-Up Allocated Memory


Unlike static variables, c++ will not free-up the memory allocated through
dynamic allocation. Its your duty to free them up. delete keyword
is used to free-up the allocated memory.


delete ptr;

Arrays are deleted in a slightly different manner as shown below:


delete []ptr;

It’s easy to forget to free the allocated memory because C++ compiler
won’t inform you that you are doing this. It’s your job and should
always be done.


Now let me show you an example of dynamic memory allocation in action:


  //Example Program in C++
#include<iostream.h>

void main(void)
{
int *ptr, size;

cout<<"How many PH. Numbers do you wish to enter:";
cin>>size;
ptr=new int[size];//allocate memory

//input ph. numbers
for (int i=0;i<size;i++)
{
cout<<"Enter PH. NO."<<i+1<<" :";
cin>>ptr[i];
}

//output ph. numbers
cout<<"\n\n\n PH. NOs. are\n";
for (i=0;i<size;i++)
cout<<"\nPH. NO."<<i+1<<" :"<<ptr[i];

cout<<endl;

delete []ptr;//free-up memory
}

Good-Bye!


Related Articles:


C++ Data Types in Detail

Data types are means to identify the type of data and associated operations
of handling it. C++ provides a predefined set of data types for handling the
data it uses. When variables are declared of a particular data type then the
variable becomes the place where the data is stored and data types is the type
of value(data) stored by that variable.

Data can be of may types such as character, integer, real etc. since the data
to be dealt with are of may types, a programming language must provide different
data types.

In C++ data types are of two types:-



  1. Fundamental Data Types: As the name suggests these are
    the atomic or the fundamental data types in C++. Earlier there was five of
    these (int, char, float, double and void) but later two new data types namely
    bool and wchar_t have been added. Int stores integer data or whole numbers
    such as 10, -340 etc., char stores any of the ASCII characters, float is used
    to store numbers having fractional part such as 10.097, double stores the
    same data as float but with higher range and precision, bool can only store
    true and false values.
        //Program to illustrate various fundamental data type
    #include<iostream.h>
    void main(void)
    {
    int age;
    float salary;
    char code;
    cout<<"Enter age, salary and code:";
    cin>>age;
    cin>>salary;
    cin>>code;
    cout<<endl;//goto next line
    cout<<"DETAILS"<<endl;//short for cout<<"DETAILS";cout<<endl;

    cout<<"Age:"<<age<<endl;
    cout<<"Salary:"<<salary<<endl; cout<<"Code:"<<code<<endl;



    }



  1. Derived Data Types: These are the data types which are
    derived from the fundamental data types. It is further divide into two categories
    i)Built-In and ii)User-defined, which are discussed below as seperate topics.

Built-In Derived Data Type

  1. Arrays: Arrays refer to a list of finite number of same
    data types. The data in the array can be accessed by an index number ranging
    from 0 to n(where n is the number of data element it can store). Ex- if arr[3]
    is an array of int(egers) then the different values in the array can be accessed
    as shown below.

    arr[0], arr[1],arr[2]

    when we declare an array such as the one sown above then by arr[3] we mean
    that we want three elements in the array and hence while accessing arr[2]
    is the last element.


        //Program to illustrate arrays
    #include<iostream.h>
    void main(void)
    {
    int arr[3];//it will store 3 integer elements
    cout<<"enter 3 numbers:";
    cin>>arr[0]>>arr[1]>>arr[2];//this statement is same as using three cin's

    cout<<endl;//goto next line
    cout<<arr[0]<<arr[1]<<arr[2];

    }


  2. Pointer: A pointer is a variable that holds the memory
    address of other variable. It is also of different data types, ex- char pointer
    can store address of only char variables, int pointer can store address of
    int variables and so on.
  3. Reference: A reference in the simplest sense is an alias
    or alternate name for a previously defined variable.

         
    //Program to illustrate References
    #include<iostream.h>
    void main(void)
    {
    int var;
    int &refvar=var;//here a reference variable to var is declared remember var was previously declared

    var=10;//var is given the value 10
    cout<<var<<endl;
    refvar=100;//reference variable of var is changed

    cout<<var;//but var also gets changed

    }


User-Defined Derived Data Types


  1. Class: A class is a collection of variables and function
    under one reference name. it is the way of separating and storing similar
    data together. Member functions are often the means of accessing, modifying
    and operating the data members (i.e. variables). It is one of the most important
    features of C++ since OOP is usually implemented through the use of classes.

  2. Structure: In C++ structure and class same except for
    some very minor differences.

  3. Union: A union is a memory location shared by two or more
    different variables, generally of different data types. Giving more details
    here would only confuse you; I’ll leave it for future articles.

  4. Enumerations: It can be used to assign names to integer
    constants.

         //Program to illustrate Enumerators
#include<iostream.h>
void main(void)
{
enum type{POOR,GOOD,EXCELLENT};//this is the syntax of enumerator

int var;
var=POOR;//this makes programs more understandable
cout<<var<<endl;
var=GOOD;
cout<<var<<endl;
var=EXCELLENT;
cout<<var;

}

Data Types Modifiers


  • signed

  • unsigned

  • short

  • long




Int, char, float, double data types can be preceded with these modifiers to
alter the meaning of the base type to fit various situations properly.

Every data type has a limit of the larges and smallest value that it can store
known as the range. An integer (usually 4 bytes long) can store any value ranging
from -2147483648 to 2147483647. Data Type modifiers usually alter the upper
and lower limit of a data type.

Unsigned modifier makes a variable only to store positive values. Ex- if a data
type has a range from –a to a then unsigned variable of that type has
a range from 0 to 2a. Preceding any data type with signed is optional because
every data type is signed by default. Short integers are 2 bytes long and long
integers are 8 bytes long.


Related Articles:


Check out this stream