Introduction to SQL and Databases
A database is a system used to store, organize, and retrieve data. A relational database stores data in tables, which can be linked to each other using primary keys and foreign keys. In SQL, you can interact with a database by creating tables, inserting data, querying information, and modifying structures.
This tutorial will walk you through SQL basics, database creation, understanding data types, joins, stored procedures, and more, with visual examples.
1. Creating a Database and Defining Data Types
Creating a Database
Before we start creating tables and inserting data, we need to create a database.
You can check if the database was created by listing all available databases:
Once the database is created, switch to it:
Defining Data Types
In SQL, each column in a table must have a data type, which specifies the kind of data it can store. Here are some common data types:
- INT: Stores integers (whole numbers).
- VARCHAR(size): Stores variable-length strings (text).
- TEXT: Stores long text.
- DATE: Stores date values in 'YYYY-MM-DD' format.
- FLOAT: Stores floating-point numbers.
- BOOLEAN: Stores TRUE or FALSE values.
2. Creating Tables with Data Types
Let's start by creating a table to store employee information, defining appropriate data types for each column.
Visualizing the Table Structure:
employee_id | name | age | salary | hire_date | active |
---|---|---|---|---|---|
1 | John Doe | 28 | 55000.00 | 2020-03-10 | TRUE |
2 | Jane Smith | 35 | 65000.00 | 2018-06-20 | FALSE |
3 | Emily Davis | 40 | 75000.00 | 2015-09-15 | TRUE |
3. Inserting Data into Tables
After creating a table, you can insert data into it using the INSERT INTO
statement.
4. SQL Queries and Joins
Now that we have data in our employees
table, let's perform some SQL queries and explore joins.
4.1 SELECT Statement
To retrieve data from a table, you can use the SELECT
statement:
This will return all columns and rows from the employees
table.
4.2 INNER JOIN
We will now create a second table, departments
, and show how to use INNER JOIN to combine data from multiple tables.
Now let's use an INNER JOIN
to combine the employee data with the department data.
Visualizing the JOIN Result:
name | age | department_name |
---|---|---|
John Doe | 28 | Human Resources |
Jane Smith | 35 | Engineering |
Emily Davis | 40 | Human Resources |
5. Advanced SQL Commands
5.1 Using SQL Functions
- COUNT(): Returns the number of rows.
- SUM(): Adds the values in a numeric column.
- AVG(): Finds the average value of a column.
- DISTINCT: Retrieves unique values from a column.
5.2 ALTER TABLE
To modify the structure of an existing table (e.g., adding a new column):
6. SQL Constraints
Constraints are used to enforce rules on the data in a table.
- NOT NULL: Ensures a column cannot have NULL values.
- UNIQUE: Ensures all values in a column are unique.
- CHECK: Ensures that the values in a column meet a specified condition.
7. Stored Procedures
A stored procedure is a precompiled set of SQL statements that can be executed as a unit. Stored procedures help to avoid repeated code and improve performance.
Example: Creating a stored procedure that returns the details of an employee by employee_id
:
To execute the stored procedure:
8. Indexing
An index improves the speed of data retrieval in a table. Let's create an index on the name
column of the employees
table:
9. SQL Data Types Summary
Below is a summary of common SQL data types:
Data Type | Description |
---|---|
INT | Integer numbers (whole numbers) |
VARCHAR(n) | Variable-length string with a maximum length of n |
TEXT | Long text |
DATE | Date in 'YYYY-MM-DD' format |
FLOAT | Floating-point number (decimal) |
BOOLEAN | TRUE or FALSE value |
10. Conclusion
This SQL tutorial covered key topics such as creating databases and tables, understanding data types, writing basic and advanced SQL queries, using joins, stored procedures, and managing constraints and indexes. These concepts are foundational to working with relational databases, enabling you to create, retrieve, and manage data efficiently.
By mastering SQL commands and concepts, you can effectively handle complex database operations and queries.
0 Comments