0% found this document useful (0 votes)
11 views6 pages

Introduction to Structured Query Language

Structured Query Language (SQL) is the standard language for interacting with relational databases, allowing users to efficiently manage data through simple commands. SQL operates through a series of steps including input, parsing, optimization, execution, and output, utilizing components like databases, tables, and transactions. Key commands include Data Definition Language (DDL) for defining database structures and Data Manipulation Language (DML) for updating data, with examples provided for querying a STUDENT table.

Uploaded by

abulbayan2005
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

Introduction to Structured Query Language

Structured Query Language (SQL) is the standard language for interacting with relational databases, allowing users to efficiently manage data through simple commands. SQL operates through a series of steps including input, parsing, optimization, execution, and output, utilizing components like databases, tables, and transactions. Key commands include Data Definition Language (DDL) for defining database structures and Data Manipulation Language (DML) for updating data, with examples provided for querying a STUDENT table.

Uploaded by

abulbayan2005
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Structured Query Language

Structured Query Language (SQL) is the standard language used to interact with
relational databases.
• It allows users to store, retrieve, update and manage data efficiently
through simple commands.
• It is known for its user-friendly syntax and powerful capabilities, SQL is
widely used across industries.

How Does SQL Work?


We interact with databases using SQL queries. DBMS tools like MySQL and SQL
Server have their own SQL engine and an interface where users can write and execute
SQL queries.
Below are the detailed steps involved in the SQL query execution.
1. Input: The user submits a query (e.g., SELECT, INSERT, UPDATE, DELETE)
via an application or interface.
2. Parsing: The query processor breaks the query into parts (tokens) and
checks for syntax and schema correctness.
3. Optimization: The optimizer finds the most efficient way to run the query
using indexes, statistics and available resources.
4. Execution: The execution engine runs the query using the chosen plan,
accessing or modifying the database as needed.
5. Output: Results are returned to the user, either data (for SELECT) or a
success message (for other operations).

Key Components of a SQL System


• Databases : A database is a structured collection of data. It organizes data
into tables, which are like spreadsheets with rows (records) and columns
(fields) .
• Tables: Each table enforces rules and relationships among its columns for
data integrity.
• Indexes: Indexes speed up queries by allowing the database to quickly
locate data without scanning the entire table.
• Views: A view is a virtual table basically a saved SELECT statement you can
query like a table.
• Stored Procedures: These are pre-written SQL scripts stored inside the
database. They can receive inputs, run complex logic and return results
boosting performance, reusability and security.
• Transactions: A transaction groups multiple SQL operations into a single
unit. It ensures all changes are applied successfully or none are, preserving
data integrity (ACID properties)
• Security and Permissions: SQL includes tools to restrict access, letting
DBAs assign who can do what whether it's accessing tables, executing
procedures, or changing structures.
• Joins: Joins combine data from multiple tables based on relationships
essential for querying across related datasets.

1. Data Definition Language


These commands are used to define the structure of database objects
by creating, altering and dropping the database objects. Based on the needs of the
business, database engineers create and modify database objects using DDL.
The CREATE command, for instance, is used by the database engineer to create
database objects like tables, views and indexes.

Command Description

Creates a new table, a view on a table, or some other object in the


CREATE database.

ALTER Modifies an existing database object, such as a table

Deletes an entire table, a view of a table, or other objects in the


DROP database

TRUNCATE Removes all records from a table but keeps the table structure intact.
2. Data Manipulation Language
A relational database can be updated with new data using data manipulation language
(DML) statements. The INSERT command, for instance, is used by an application to add
a new record to the database.

Command Description

INSERT Creates a record.

UPDATE Modifies records.

DELETE Deletes records.

Table 1 shows the relational database with only one relation called STUDENT which
stores ROLL_NO, NAME, ADDRESS, PHONE, and AGE of students.

STUDENT Table

ROLL_NO NAME ADDRESS PHONE AGE

1 RAM DELHI 9455123451 18

2 RAMESH GURGAON 9652431543 18

3 SUJIT ROHTAK 9156253131 20

4 SURESH DELHI 9156768971 18

Case 1: If we want to retrieve attributes ROLL_NO and NAME of all students, the
query will be:
SELECT ROLL_NO, NAME FROM STUDENT;
ROLL_NO NAME

1 RAM

2 RAMESH

3 SUJIT

4 SURESH

Case 2: If we want to retrieve ROLL_NO and NAME of the students whose ROLL_NO
is greater than 2, the query will be:

SELECT ROLL_NO, NAME FROM STUDENT


WHERE ROLL_NO>2;

ROLL_NO NAME

3 SUJIT

4 SURESH

CASE 3: If we want to retrieve all attributes of students, we can write * in place of


writing all attributes as:

SELECT * FROM STUDENT


WHERE ROLL_NO>2;

ROLL_NO NAME ADDRESS PHONE AGE

3 SUJIT ROHTAK 9156253131 20

4 SURESH DELHI 9156768971 18


CASE 4: If we want to represent the relation in ascending order by AGE, we can use
ORDER BY clause as:

SELECT * FROM STUDENT ORDER BY AGE;

ROLL_NO NAME ADDRESS PHONE AGE

1 RAM DELHI 9455123451 18

2 RAMESH GURGAON 9652431543 18

4 SURESH DELHI 9156768971 18

3 SUJIT ROHTAK 9156253131 20

Common questions

Powered by AI

The TRUNCATE command in SQL is preferred over DELETE for removing all records from a table without logging individual row deletions, offering faster performance due to less logging overhead. Unlike DELETE, which can trigger row-specific triggers, TRUNCATE doesn't check constraints or trigger deletions, improving speed but necessitating caution to maintain data integrity .

The DROP command entirely removes database objects like tables, losing all data and structural definitions, while TRUNCATE deletes all table records but retains the structure for future use. Both affect the database state by altering content, but DROP also removes metadata, making TRUNCATE a preferable choice for resetting data while maintaining schema .

Views in a SQL system act as virtual tables representing saved SELECT statements, allowing users to encapsulate complex queries, promote data security by restricting access to the base tables, and simplify query interactions by providing a consistent interface. They enhance performance by allowing pre-computed data to be reused .

ACID properties—Atomicity, Consistency, Isolation, Durability—ensure reliable transactions in SQL. Atomicity guarantees all parts of a transaction are completed; Consistency ensures data validity before and after the transaction; Isolation prevents transaction interference; Durability ensures data persistence post-transaction. Together, they maintain data integrity and prevent corruption .

Indexes improve query performance by significantly reducing data retrieval time through efficient data location, eliminating the need for full table scans. However, the trade-offs include increased storage requirements and maintenance overhead, as indexes must be updated whenever data is modified, potentially impacting write performance .

The query optimization process enhances the efficiency of SQL queries by determining the most efficient way to execute a given query. It considers factors such as available indexes, query statistics, and system resources to find the optimal execution plan, reducing overhead and improving performance .

Stored procedures in SQL enhance performance by allowing precompiled and reusable SQL scripts, minimizing repeated parsing and optimizing execution plans. They improve security by abstracting database logic and controlling access through encapsulation, allowing users to execute procedures without revealing sensitive underlying structures or data .

Joins in SQL enable complex queries by combining data from multiple tables based on related keys, allowing for integration and retrieval of comprehensive datasets with precision. Through INNER, OUTER, LEFT, RIGHT, and CROSS JOINS, they support diverse data propagation needs, creating extensive relational views necessary for in-depth analysis and reporting .

Using SELECT with WHERE conditions filters data efficiently by narrowing down rows of interest, while ORDER BY sorts the results for better data presentation or logic. For example, the command SELECT * FROM STUDENT WHERE ROLL_NO>2; retrieves only specific student entries, and SELECT * FROM STUDENT ORDER BY AGE; organizes students by their ages, optimizing both focus and readability of outputs .

Database security and permissions are critical for protecting sensitive data from unauthorized access and alterations. SQL manages this through roles and privileges, allowing Database Administrators (DBAs) to define who can access, modify, or execute specific database objects, ensuring compliance with access policies and data integrity .

You might also like