SQL Data Types & Basic Syntax

We introduced SQL and its importance in managing relational databases. Today, we’ll dive deeper into the foundational concepts of SQL, starting with data types and basic syntax. Understanding data types is crucial for creating efficient and well-structured databases, while mastering basic SQL syntax will allow you to start interacting with your data.


Different Data Types in SQL

In SQL, every column in a table is assigned a specific data type that defines the kind of data it can store. Choosing the right data type is essential for optimizing storage and ensuring data integrity. Here are some of the most common SQL data types:

1. Numeric Data Types

  • INT: Stores whole numbers (integers). Example: 42, -15.

  • DECIMAL(p, s): Stores exact numeric values with a fixed number of digits before and after the decimal point. Example: DECIMAL(5, 2) can store values like 123.45.

  • FLOAT: Stores approximate numeric values with floating-point precision. Example: 3.14, -0.001.

2. Character/String Data Types

  • VARCHAR(n): Stores variable-length strings with a maximum length of n. Example: VARCHAR(50) can store up to 50 characters.

  • CHAR(n): Stores fixed-length strings. If the data is shorter than n, it is padded with spaces. Example: CHAR(10) always uses 10 characters of storage.

  • TEXT: Stores large text data, such as paragraphs or documents.

3. Date and Time Data Types

  • DATE: Stores a date in the format YYYY-MM-DD. Example: 2023-10-25.

  • TIME: Stores a time in the format HH:MM:SS. Example: 14:30:00.

  • DATETIME: Stores both date and time in the format YYYY-MM-DD HH:MM:SS. Example: 2023-10-25 14:30:00.

  • TIMESTAMP: Similar to DATETIME, but often used for recording the exact moment an event occurs.

4. Boolean Data Type

  • BOOLEAN: Stores TRUE or FALSE values. Some databases use TINYINT(1) or BIT to represent Boolean values.

5. Other Data Types

  • BLOB: Stores binary data, such as images or files.

  • ENUM: Stores a value from a predefined list of options. Example: ENUM('Red', 'Green', 'Blue').


Writing Basic SQL Statements

Now that you’re familiar with SQL data types, let’s explore how to write basic SQL statements. These statements allow you to interact with your database by querying, inserting, updating, and deleting data.

1. Creating a Table

To store data, you first need to create a table. The CREATE TABLE statement defines the table’s structure, including its columns and data types.

sql

Copy

CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    FirstName VARCHAR(50),
    LastName VARCHAR(50),
    BirthDate DATE,
    Salary DECIMAL(10, 2)
);
  • EmployeeID is the primary key, which uniquely identifies each row.

  • FirstName and LastName are variable-length strings.

  • BirthDate stores a date.

  • Salary stores a decimal value with up to 10 digits, 2 of which are after the decimal point.


2. Inserting Data

The INSERT INTO statement adds new rows of data to a table.

sql

Copy

INSERT INTO Employees (EmployeeID, FirstName, LastName, BirthDate, Salary)
VALUES (1, 'John', 'Doe', '1990-05-15', 55000.00);

This adds a new employee with the specified details to the Employees table.


3. Querying Data

The SELECT statement retrieves data from one or more tables.

sql

Copy

SELECT FirstName, LastName, Salary
FROM Employees
WHERE Salary > 50000;

This query retrieves the first name, last name, and salary of employees who earn more than $50,000.


4. Updating Data

The UPDATE statement modifies existing data in a table.

sql

Copy

UPDATE Employees
SET Salary = 60000.00
WHERE EmployeeID = 1;

This updates the salary of the employee with EmployeeID = 1 to $60,000.


5. Deleting Data

The DELETE statement removes rows from a table.

sql

Copy

DELETE FROM Employees
WHERE EmployeeID = 1;

This deletes the employee with EmployeeID = 1 from the Employees table.


6. Filtering and Sorting Data

You can filter data using the WHERE clause and sort it using the ORDER BY clause.

sql

Copy

SELECT FirstName, LastName, BirthDate
FROM Employees
WHERE BirthDate > '1985-01-01'
ORDER BY LastName ASC;

This retrieves employees born after 1985 and sorts the results by last name in ascending order.


Summary

On Day 2, we explored SQL data types and basic syntax. Here’s a quick recap:

  • Data Types: Define the kind of data a column can store (e.g., INT, VARCHAR, DATE).

  • Basic SQL Statements: Allow you to create tables (CREATE TABLE), insert data (INSERT INTO), query data (SELECT), update data (UPDATE), and delete data (DELETE).

With these fundamentals, you’re ready to start building and interacting with your own databases. On Day 3, we’ll dive deeper into more advanced SQL concepts, such as joins, aggregations, and constraints. Stay tuned!