Data Types in SQL Databases
Definition
Data types in SQL are attributes given to columns which define the value that these columns will hold. This helps with speeding up performance, improves storage efficiency, and ensures data integrity.
Common Data Types in SQL
Numeric Data Types (INT / INTEGER): Used for whole numbers.
Example: 1, 42, 100
String Data Types (VARCHAR / TEXT): Used for text and character data.
Example: 'John Doe', 'user@example.com'
Boolean Data Types (BOOLEAN): Used for true or false conditions.
Example: TRUE or FALSE
Date and Timestamp Data Types (DATE / TIMESTAMP): Used for recording calendar dates and exact time logs.
Example: '2026-07-28', '2026-07-28 11:00:00'
Primary Keys
A primary key is a column which uniquely identifies every single row in a table. Due to its nature, it cannot be null and it must be unique meaning no two rows can have the same primary key. A primary key can be a single column or a combination of multiple columns.
Foreign Keys
A foreign key is a column on one table that references the primary key in another table. This is used to establish a relationship between those tables and ensures accuracy of data.
Normally, the table being referenced is called the parent table, and the table referencing it is the child table. This helps to prevent data redundancy and to store related data in different tables.
Database Relationships
Database relationships are an association established between two or more relational database tables using keys. They define how records in different tables connect.
Types of Database Relationships:
One-to-Many (1:N): Here, one row in one table connects to multiple rows in another table.
Example: In the project I created, where one user can have multiple goals.
One-to-One (1:1): This is where one row in one table connects to only one row in another table. This is very common in most apps, where one user has only one profile.
Many-to-Many (N:M): Here, multiple rows from one table connect to multiple rows in another table.
Example: In a university where multiple students take multiple different courses.
Top comments (0)