SQL ensure data integrity via primary keys, foreign keys, and unique constraints, which prevent duplicate or invalid entries.
Key Takeaways
SQL is used to store, retrieve, and manipulate data in relational databases. SQL helps manage data relationships using schemas in RDBMS.
SQL allows querying data effectively using commands like SELECT
, enabling data analysis and retrieval from complex datasets.
Primary SQL operations are divided into DDL (CREATE
, ALTER
, DROP
), DML (INSERT
, UPDATE
, DELETE
, SELECT
), and DCL (GRANT
, REVOKE
).
Structured Query Language (SQL) is a standard data-oriented language that is primarily used to store, retrieve, and manipulate data using simple code snippets, called queries, in a relational database management system (RDBMS).
An RDBMS organizes data into tables containing rows and columns, with relations between the different entities and variables. The database schema defines these relations, specifying not only the relation between various entities but also the organization of data associated with those entities in the database.
Using SQL, we can create and modify databases, manage data, and ensure database integrity with primary and foreign key constraints.
Get a hands-on practice with SQL operators with this project, Build a CRUD Application Using Golang and React.
SQL commands are grouped into two main categories—DDL, DML, and DCL:
Data Definition Language (DDL) commands affect the schema/structure of the database and do not usually interact with the data inside. These commands define and manage the structure of databases and tables. Commonly used DDL in SQL querying are:
CREATE
: Creates new databases and tables.
ALTER
: Modifies the structure of existing tables or databases.
DROP
: Deletes tables or entire databases.
Data Manipulation Language (DML) operations involve data retrieval and manipulation, allowing users to interact with and modify the data. These commands handle the manipulation of data stored within the tables. Commonly used DML in SQL querying are:
INSERT INTO
: Adds new data into tables.
UPDATE
: Modifies existing data.
DELETE
: Removes records from a table.
SELECT
: Retrieves data from one or more tables.
Data Control Language (DCL) commands control access to data in the database, defining permissions for users or roles. Commonly used DCL commands are:
GRANT
: Gives specific privileges to a user or role.
REVOKE
: Removes specific privileges from a user or role.
Practice building an application with SQL in an actual project by attempting this project, Create a Chat Application using Angular, Flask, and Socket.IO.
Now let's look at an example of how to use the DDL, DML, and DCL commands in a real-world example. We will create a small employee management system to demonstrate CREATE
, ALTER
, INSERT
, SELECT
, UPDATE
, DELETE
, GRANT
, and REVOKE
commands.
-- Step 1: Create a databaseCREATE DATABASE CompanyDB;-- Step 2: Use the created databaseUSE CompanyDB;-- Step 3: Create a table for employeesCREATE TABLE Employees (EmployeeID INT PRIMARY KEY,FirstName VARCHAR(100),LastName VARCHAR(100),Position VARCHAR(50),Salary DECIMAL(10, 2),Department VARCHAR(50));-- Step 4: Insert employee records into the Employees tableINSERT INTO Employees (EmployeeID, FirstName, LastName, Position, Salary, Department) VALUES(1, 'Alice', 'Johnson', 'Manager', 75000.00, 'HR'),(2, 'Bob', 'Smith', 'Developer', 85000.00, 'IT');-- Step 5: Retrieve all employee recordsSELECT * FROM Employees;-- Step 6: Update an employee's salary (for EmployeeID = 2)UPDATE EmployeesSET Salary = 95000.00WHERE EmployeeID = 2;-- Verify the updateSELECT * FROM Employees;-- Step 7: Delete a record (EmployeeID = 1)DELETE FROM EmployeesWHERE EmployeeID = 1;-- Verify the deletionSELECT * FROM Employees;-- Step 8: Create a user for database accessCREATE USER 'user1'@'localhost' IDENTIFIED BY 'password123';-- Step 9: Grant permissions to the userGRANT SELECT, INSERT ON CompanyDB.Employees TO 'user1'@'localhost';-- Step 10: Revoke a permission from the userREVOKE INSERT ON CompanyDB.Employees FROM 'user1'@'localhost';-- Step 11: Drop the Employees tableDROP TABLE Employees;-- Verify the table no longer existsSHOW TABLES;
Line 2: We create a new database named CompanyDB
using the CREATE
command.
Line 5: We switch to the CompanyDB
database to perform subsequent operations.
Lines 8-15: We create a table named Employees
with columns for employee details, including EmployeeID
, FirstName
, LastName
, Position
, Salary
, and Department
.
Lines 18-20: We insert two employee records into the Employees
table.
Line 23: We retrieve and display all records from the Employees
table to verify the insertion.
Lines 26-28: We update the salary of the employee with EmployeeID = 2
from 85000
to 95000
.
Lines 31: We display all records to confirm that the salary update was successful.
Lines 34-35: We delete the record of the employee with EmployeeID = 1
.
Line 38: We display all records to verify that the deletion was successful.
Line 41: We create a new user before assigning privileges.
Line 44: We give user1
permission to select and insert data into the Employees
table.
Line 47: We remove the INSERT
permission from user1
.
Line 50: We remove the Employees
table from the database.
Line 53: We display all tables in the current database to confirm that the Employees
table has been dropped.
Try out this project to get real-world SQL related expereince, Build a Python Airline Reservation System.
Haven’t found what you were looking for? Contact Us
Free Resources