Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL When to use Which Data Type

Tags:

types

sql

Hi I was wondering when I should use the different data types. As in in my table, how can I decide which to use: nvarchar, nchar, varchar, varbinary, etc.

Examples:

What would I use for a ... column:

Phone number,
Address,
First Name, Last Name,
Email,
ID number,
etc. 

Thanks for any help!

like image 468
Oliver S Avatar asked Feb 27 '09 16:02

Oliver S


People also ask

Which datatype is used for in SQL?

SQL data types can be broadly divided into following categories. Numeric data types such as int, tinyint, bigint, float, real, etc. Date and Time data types such as Date, Time, Datetime, etc. Character and String data types such as char, varchar, text, etc.

Why do we use data types in SQL?

Data types are used to represent the nature of the data that can be stored in the database table. For example, in a particular column of a table, if we want to store a string type of data then we will have to declare a string data type of this column.

How do I select data type in SQL?

You can get the MySQL table columns data type with the help of “information_schema. columns”. SELECT DATA_TYPE from INFORMATION_SCHEMA. COLUMNS where table_schema = 'yourDatabaseName' and table_name = 'yourTableName'.


1 Answers

As a general rule, I would not define anything as a "number" field if I wasn't going to be doing arithmetic on it, even if the data itself was numeric.

Your "phone" field is one example. I'd define that as a varchar.

like image 93
user71950 Avatar answered Oct 20 '22 01:10

user71950