SQL Notes
SQL Notes
What is SQL ?
SQL or Structured Query Language is a fundamental skill for anyone who wants to
interact with databases. This standard Query Language all users to create,
manage, and retrieve data from relational databases.
Introduction to Databases
A database is an electronically stored, systematic collection of data that can
include words, numbers, images, videos, and other types of files. Databases are
managed using specialized software called a Database Management System (DBMS),
which allows users to store, retrieve, and manipulate data efficiently. Databases are the
backbone of modern applications, supporting businesses, organizations, and systems across
industries.
Components of a Database
Data:
Data is the core component of any database, representing the actual information
stored. It can include numbers, text, images, videos, or documents, depending
on the database’s purpose.
Schema:
The schema is the blueprint or structure of the database. It defines how data is
organized and includes details like tables, columns, data types, and relationships
between entities
DBMS:
The DBMS is the software layer that enables interaction with the database. It
manages the storage, retrieval, and manipulation of data while ensuring security
and data integrity.
Queries :
Queries are commands used to interact with the database, allowing users to
retrieve, manipulate, or update data. For relational databases, SQL (Structured
Query Language) is commonly used. For instance, a query like SELECT * FROM
Customers WHERE Country = 'USA'; retrieves all customers from the USA.
Users:
Users are individuals or applications that interact with the database. They can have
different levels of access based on their roles, such as administrators, developers, or
end-users. For example, a database administrator might have full control, including the
ability to create or delete tables, while a regular user might only have permission to view
specific data.
Types of DataBase
1. Centralized Database
It is the type of database that stores data at a centralized database system. It
comforts the users to access the stored data from different locations through
several applications.
2. Distributed Databases
A distributed database is made up of two or more files that are spread across
multiple locations. The database could be dispersed across many networks,
housed in one physical place, or kept on several computers.
Examples: Google Spanner, Apache Cassandra.
Use Cases: Global-scale applications, content delivery networks (CDNs).
3. Relational Databases
A relational database’s contents are arranged as a collection of tables with rows
and columns. Accessing structured data is made most flexible and efficient by
relational database technology.
4. NoSQL Databases
NoSQL databases handle unstructured and semi-structured data. They are
highly scalable and flexible, making them ideal for real-time applications
5. Cloud databases:
A collection of organized or unorganized data that is housed on a private, public,
or hybrid cloud computing platform is known as a cloud database. Cloud database
models come in two flavors: traditional and database as a service (DBaaS) . With
DBaaS, a service provider handles maintenance and administrative duties.
6. Object-Oriented Databases
The type of database that uses the object-based data model approach for storing
data in the database system is called Object-Oriented Databases.
7. Hierarchical Databases
It is the type of database that stores data in the form of parent-children
relationship nodes. Here, it organizes data in a tree-like structure. Data get stored
in the form of records that are connected via links. Each child record in the tree
will contain only one parent. On the other hand, each parent record can
have multiple child records .
We use the SHOW DATABASES command and it will return a list of databases
that exist in our system. Now we will look for the name of the database
(GeeksForGeeks) that we have just created.
Syntax
SHOW DATABASES;
Once your database is created, we can switch to that database to begin adding
tables, inserting data, and performing queries.
To do this, use the USE command.
Syntax
USE database_name
2. DROP:
If you ever need to remove a database, the DROP DATABASE command can be
used to delete the database and all its contents:
DROP DATABASE GeeksForGeeks;
Purpose: To remove a database from the DBMS permanently.
Outcome: All data, schema, and database objects are erased.
To avoid any error while running the DROP DATABASE command use the IF
EXISTS clause, which will delete the database only if it exists in the system.
Syntax
DROP DATABASE IF EXISTS Database_Name;
1. DROP Table:
To delete an entire table, including its data and structure:
Syntax:
DROP TABLE table_name;
2. DROP database:
To delete an entire database and all of its associated tables:
Syntax:
DROP DATABASE database_name;
3. ALTER :
To rename a database in SQL, you’ll need to use the ALTER
DATABASE command. The syntax varies slightly between different SQL platforms,
but the core functionality remains the same. The ALTER DATABASE statement
allows you to modify the properties of a database, including its name.
In this example, we will use the ALTER command with MODIFY NAME
clause to rename the database.
ALTER DATABASE Test MODIFY NAME = Example
RENAME :
4.TRUNCATE :
After running the above query Student_details table will be truncated, i.e,
the data will be deleted but the structure will remain in the memory for
further operations.
DML ( DATA MANIPULATION LANGUAGE )
Insert :
Insert data into a table
Syntax
INSERT INTO employees (first_name, last_name, department)
VALUES ('Jane', 'Smith', 'HR');
UPDATE:
Update existing data within a table
Syntax
UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
DELETE:
Delete records from a database table
Syntax
DELETE FROM table_name WHERE condition;