SQL - Data Types
SQL recognizes 4 general types of data. As the database designer you will be selecting which type of data that can be placed in each table column. Before we look at each type of table column we will elaborate on specific data types and how they are handled in SQL.
- Character Strings - ('Words or numbers')
- Numbers - (3, 3.423, -17)
- Booleans - (True / False)
- Nulls - (empty fields)
SQL - NULL Values
A null value may be the most foreign to new programmers. Stating that a value has a null value indicates that nothing exists in that table field. When the table is created you may either allow a table to have a null value or may disallow null values for each table column.
SQL Code:
CREATE TABLE weekly_payroll
(employee_id VARCHAR(10) PRIMARY KEY,
total_hours INT NULL,
hourly_rate MONEY NOT NULL,);
SQL - Numeric Data
Dates, time stamps, integers, and money are all numeric data types. The advantage of working with numbers is that SQL has built in functions such as the AVG() or SUM() functions that will return the average or the sum of a numeric column.
Numbers:
| rate_of_pay |
| 27 |
| 26.66 |
| 28.40 |
SQL - Boolean Data
Boolean values are either yes/no (true/false) types of data. Others use a 1/0 (1 for yes 0 for no) approach. Either something is or something is not.
Boolean Values:
SQL - Character Strings
Character strings are sentences, symbols, or a combination of both. Math functions can not be performed with character strings.
Character Strings:
| employee_id |
| TS_0036 |
| TS_0078 |
| CL_1099 |
Tips
- Dates and times should always be set to "NOT NULL" since time always exists.
- Character Strings are sometimes referred to as varchar(s).
|