0% found this document useful (0 votes)
4 views8 pages

SQL Notes

Structured Query Language (SQL) is the standard language for managing relational databases, allowing users to create, read, update, and delete data. SQL commands are categorized into five types: DDL, DML, DQL, DCL, and TCL, each serving different purposes in database management. Key components of SQL systems include databases, tables, indexes, and security measures, with specific rules for writing queries to ensure accuracy and readability.

Uploaded by

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

SQL Notes

Structured Query Language (SQL) is the standard language for managing relational databases, allowing users to create, read, update, and delete data. SQL commands are categorized into five types: DDL, DML, DQL, DCL, and TCL, each serving different purposes in database management. Key components of SQL systems include databases, tables, indexes, and security measures, with specific rules for writing queries to ensure accuracy and readability.

Uploaded by

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

What are the SQL?

Structured Query Language (SQL) is the standard language used to interact with relational databases.

 Mainly used to manage data. Whether you want to create, delete, update or read data, SQL
provides commands to perform these operations.

 Widely supported across various database systems like MySQL, Oracle, PostgreSQL, SQL
Server and many others.

 Mainly works with Relational Databases (data is stored in the form of tables)

SQL Working
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.

 Input – The user sends a SQL query (like SELECT or INSERT).

 Parsing – The system checks if the query is written correctly.

 Optimization – It chooses the fastest way to run the query.

 Execution – The database runs the query.

 Output – The result or confirmation is sent back to the user.

Components of a SQL System


 Database: A structured collection of data stored in tables with rows and columns.

 Tables: Store data and apply rules to keep it accurate and consistent.

 Indexes: Help the database find data faster without scanning the whole table.

 Views: Virtual tables created from SELECT queries for easy access to data.

 Stored Procedures: Pre-saved SQL code that runs tasks and improves performance and
security.

 Transactions: Group of SQL actions that either all succeed or all fail to keep data safe.

 Security & Permissions: Control who can view, change, or manage database data.

 Joins: Combine data from different tables based on relationships.

Rules for Writing SQL Queries


To make SQL queries correct, readable, and executable in any database, we must follow certain rules.

1. Semicolon (;) – Statement Terminator

Every SQL statement should end with a semicolon (;).


It tells the database that the command is finished.

Example:

SELECT * FROM students;

2. Case-Insensitive Nature of SQL

SQL keywords are NOT case-sensitive.

These are all correct:

Ex.

SELECT * FROM students;


select * from students;
SeLeCt * FrOm students;
But best practice:
Write SQL keywords in UPPERCASE for readability.

3. Whitespace (Spaces and New Lines)

Spaces and line breaks are allowed.


They improve readability.

Bad (hard to read):

Ex.

SELECT name,age FROM students WHERE age>18;

Good (easy to read):

SELECT name, age


FROM students
WHERE age > 18;

Both give the same result.

4. Reserved Words Rule

Do NOT use SQL keywords as table or column names.

Examples of reserved words:

 SELECT

 INSERT

 WHERE

 TABLE

 ORDER

 GROUP

Wrong:

CREATE TABLE SELECT (


id INT
);

5. Data Constraints Rule


Constraints ensure correct and accurate data.

Common constraints:
Constraint Meaning

NOT NULL Cannot store empty value

UNIQUE No duplicate values


PRIMARY KEY Unique + Not Null
FOREIGN KEY Links two tables
CHECK Condition must be true
DEFAULT Default value

Full Example:

CREATE TABLE students (


id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
age INT CHECK (age >= 18)
);

✔ id must be unique
✔ name cannot be empty
✔ email cannot repeat
✔ age must be 18 or more

6. String Values Rule

Strings must be written inside single quotes (' ').

Example

-Correct:

SELECT * FROM students


WHERE name = 'Rahul';

-Wrong:

WHERE name = Rahul;

Numbers do NOT need quotes:

WHERE age = 20;

7. Naming Rules (Very Important for Exams)


When creating table names, column names, or database names:

Rule 1: Must Start with a Letter

Correct:

student_data
employee1

Wrong:

1student
_data

Rule 2: Maximum 30 Characters

Name should not exceed 30 characters (in many SQL systems).

Correct:

student_information

Too long:

student_information_management_system_data

Rule 3: Allowed Characters

You can use:

 Letters (A–Z, a–z)

 Numbers (0–9)

 Underscore (_)

Correct:

student_data
employee_2024

Wrong:

student-data (dash not allowed)


student data (space not allowed)
student@data (@ not allowed)

Complete Practical Example


-- Create student table with proper naming rules

CREATE TABLE student_details (


student_id INT PRIMARY KEY,
student_name VARCHAR(50) NOT NULL,
student_email VARCHAR(100) UNIQUE,
student_age INT CHECK (student_age >= 18)
);

-- Insert data using string rule


INSERT INTO student_details
VALUES (1, 'Rahul Sharma', 'rahul@[Link]', 20);

-- Select data
SELECT student_name, student_age
FROM student_details
WHERE student_age > 18;

SQL Commands
SQL (Structured Query Language) is the standard language used to manage and manipulate
relational databases.

SQL commands are divided into five main categories:

1. DDL – Data Definition Language

2. DML – Data Manipulation Language

3. DQL – Data Query Language

4. DCL – Data Control Language

5. TCL – Transaction Control Language

DDL – Data Definition Language


Used to define and modify database structure.

[Link]

Creates database objects (database, table, view, index).

Create Database

CREATE DATABASE company;

Create Table

CREATE TABLE employees (


id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
salary DECIMAL(10,2),
hire_date DATE
);
[Link]

Modifies existing table structure.

Add Column

ALTER TABLE employees


ADD email VARCHAR(100);

Modify Column

ALTER TABLE employees


MODIFY salary DECIMAL(12,2);

Drop Column

ALTER TABLE employees


DROP COLUMN email;

[Link]

Deletes database objects permanently.

DROP TABLE employees;


DROP DATABASE company;

[Link]

Removes all rows but keeps table structure.

TRUNCATE TABLE employees;

You might also like