introduction to sql
SQL (Structured Query Language) is the standard language for managing and manipulating relational databases. It allows you to define database structures, manipulate data, query data, control access, and manage transactions effectively.
This article explains the core components of SQL, including detailed examples for each SQL command, helping you understand how to work with databases using this powerful language.
SQL is a language used to communicate with relational databases. It enables you to perform operations such as creating, altering, and querying database objects (tables, views, indexes), as well as manipulating data and controlling access. SQL commands are divided into five major categories:
DDL commands are used to define and manage database structures, such as creating, altering, or deleting databases and tables. These commands form the foundation of a database system and are essential for setting up its structure.
The CREATE DATABASE
command is used to create a new database.
Syntax:
CREATE DATABASE database_name;
Example: To create a database called “company”:
CREATE DATABASE company;
To verify the creation of the database, use:
SHOW DATABASES;
The DROP DATABASE
command is used to delete an existing database.
Syntax:
DROP DATABASE database_name;
Example: To drop the “company” database:
DROP DATABASE company;
Verify the database has been deleted with:
SHOW DATABASES;
Before creating tables, you must select a database to work with using the USE
command.
Syntax to create a table:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);
Example: To create a “customers” table with columns for customer details:
CREATE TABLE customers (
CustomerId INT,
first_name VARCHAR(20),
last_name VARCHAR(20),
country VARCHAR(20)
);
The ALTER
command is used to modify the structure of an existing table, such as adding, removing, or changing columns.
Syntax:
ALTER TABLE table_name CHANGE old_column_name new_column_name datatype;
Example: To rename the “Second_name” column to “last_name” in the “customers” table:
ALTER TABLE customers CHANGE Second_name last_name VARCHAR(20);
Syntax:
ALTER TABLE table_name MODIFY column_name datatype constraint;
Example: To change the data type of the “first_name” column to VARCHAR(25)
and make it NOT NULL
:
ALTER TABLE customers MODIFY first_name VARCHAR(25) NOT NULL;
Syntax:
ALTER TABLE table_name ADD COLUMN column_name datatype;
Example: To add a “Salary” column to the “customers” table:
ALTER TABLE customers ADD COLUMN Salary INT;
Syntax:
ALTER TABLE table_name DROP COLUMN column_name;
Example: To drop the “Salary” column from the “customers” table:
ALTER TABLE customers DROP COLUMN Salary;
The DROP TABLE
command is used to delete an existing table from the database.
Syntax:
DROP TABLE table_name;
Example: To delete the “customers” table:
DROP TABLE customers;
The RENAME TABLE
command allows you to change the name of an existing table.
Syntax:
RENAME TABLE old_table_name TO new_table_name;
Example: To rename the “customers” table to “customer_info”:
RENAME TABLE customers TO customer_info;
DML commands are used to manipulate data stored in the database. These commands allow you to insert, update, delete, or select data in the database.
The INSERT INTO
command is used to add new records into a table.
Syntax:
INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);
Example: To add a new customer to the “customers” table:
INSERT INTO customers (CustomerId, first_name, last_name, country)
VALUES (1, 'John', 'Doe', 'USA');
The UPDATE
command is used to modify existing records in a table.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
Example: To update the last name of the customer with CustomerId = 1
:
UPDATE customers
SET last_name = 'Smith'
WHERE CustomerId = 1;
The DELETE
command is used to remove records from a table.
Syntax:
DELETE FROM table_name
WHERE condition;
Example: To delete a customer with CustomerId = 1
:
DELETE FROM customers
WHERE CustomerId = 1;
Warning: If you omit the WHERE
clause, all records in the table will be deleted.
The SELECT
command is used to retrieve data from one or more tables. It is one of the most commonly used SQL commands.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example: To retrieve all customer names from the “customers” table:
SELECT first_name, last_name
FROM customers;
To retrieve customer details for customers from the USA:
SELECT * FROM customers
WHERE country = 'USA';
You can also sort the results using ORDER BY
:
SELECT * FROM customers
ORDER BY last_name ASC;
DQL is primarily used for querying and retrieving data from the database. The main DQL command is SELECT
, which allows you to extract data based on specific criteria.
As discussed earlier, the SELECT
command allows you to retrieve records from one or more tables and filter them using various conditions. You can also perform joins, group data, and apply functions like COUNT, AVG, MAX, etc., to summarize the data.
DCL commands are used to control access to data in the database. They help manage permissions and roles for users.
The GRANT
command is used to provide specific privileges to a user or role on a database object.
Syntax:
GRANT privilege ON object TO user;
Example: To grant a user the ability to select data from the “customers” table:
GRANT SELECT ON customers TO user_name;
The REVOKE
command is used to remove previously granted privileges from a user.
Syntax:
REVOKE privilege ON object FROM user;
Example: To revoke the select privilege on the “customers” table from the user:
REVOKE SELECT ON customers FROM user_name;
TCL commands are used to manage transactions, ensuring that operations are completed successfully or undone in case of errors.
The COMMIT
command saves all changes made during the current transaction.
Syntax:
COMMIT;
Example:
COMMIT;
The ROLLBACK
command is used to undo the changes made during the current transaction.
Syntax:
ROLLBACK;
Example:
ROLLBACK;
START TRANSACTION;
Or:
BEGIN;
SAVEPOINT savepoint_name;
Example:
SAVEPOINT Save1;
ROLLBACK TO savepoint_name;
Example:
ROLLBACK TO Save1;
Once a savepoint is released, it can no longer be rolled back.
RELEASE SAVEPOINT savepoint_name;
Example:
RELEASE SAVEPOINT Save1;
For more clarification and explanation, you can go to this website.
SQL is a powerful language for managing and manipulating data in relational databases. This guide covers the key SQL commands and provides examples of their usage. Understanding these SQL commands—DDL, DML, DQL, DCL, and TCL—is essential for any database management or application development task. Master these commands to handle data efficiently, query information, and manage transactions securely.
If you’re looking to expand your programming skills beyond SQL, check out our comprehensive guide on Python Basics for Beginners. It covers everything you need to start coding in Python.
Are you looking for an exciting remote opportunity in customer support? Aircall is hiring now…
Are you looking for an opportunity to grow your career in data analysis? Deloitte is…
Are you looking to advance your career with a top-tier financial institution? American Express is…
Are you looking for a rewarding work-from-home career opportunity? Apply now for Remote Virtual Assistant…
If you are looking for an exciting career opportunity, Accenture New Job Opening for Associate…
A new job opening for the job seekers where HireVeda Is Hiring Now For Associate…