Placement
Placement
DBMS: DBMS (Database Management System) is software used to create, manage, and
organize databases.
DBMS
RDBMS
----------------------------------------------------------------------------
------------------------------------------------------------------------------
DBMS stores data as file. RDBMS
stores data in tabular form.
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
SQL
NOSQL
----------------------------------------------------------------------------
----------------------------------------------------------------------------
Vertically Scalable
Horizontally scalable
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
Vertical Scaling:
When new resources are added to the existing system to meet the expectation, it is
known as vertical scaling.
Vertical scaling involves adding more power (CPU, RAM, storage, etc.) to an
existing machine.
Consider a rack of servers and resources that comprises the existing system. (as
shown in the figure). Now when the existing system fails to meet the expected
needs, and the expected needs can be met by just adding resources, this is
considered vertical scaling. Vertical scaling is based on the idea of adding more
power(CPU, RAM) to existing systems, basically adding more resources.
Vertical scaling is not only easy but also cheaper than Horizontal Scaling. It also
requires less time to be fixed.
Horizontal Scaling:
When new server racks are added to the existing system to meet the higher
expectation, it is known as horizontal scaling.
Consider a rack of servers and resources that comprises the existing system. (as
shown in the figure). Now when the existing system fails to meet the expected
needs, and the expected needs cannot be met by just adding resources, we need to
add completely new servers. This is considered horizontal scaling. Horizontal
scaling is based on the idea of adding more machines to our pool of resources.
Horizontal scaling is difficult and also costlier than Vertical Scaling. It also
requires more time to be fixed.
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
SQL DATATYPES:
VARCHAR: It's a variable-length character data type. It stores only the characters
you provide plus the overhead required to store the length of the string. For
example, if you define a column as VARCHAR(10) and store the string "Hello", it
will only take up 5 characters of storage, plus some additional bytes to store the
length of the string.
In a CHAR(10) column: "Hello" would occupy 10 bytes because CHAR columns allocate a
fixed amount of space, padding shorter strings with spaces to fill the allocated
space. So, "Hello" would be stored as "Hello " (with six trailing spaces).
In a VARCHAR(10) column: "Hello" would occupy 6 bytes (5 bytes for the characters
"Hello" plus 1 byte to store the length of the string).
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
1. DQL (Data Query Language) : Used to retrieve data from databases. (SELECT)
2. DDL (Data Definition Language) : Used to define the structure of the database
schema and create, alter, and delete database objects like tables, indexes, etc.
(CREATE, ALTER, RENAME, DROP, TRUNCATE) (CARDT)
3. DML (Data Manipulation Language): Used to manipulate the data within the
database objects of the database. (DELETE, UPDATE, INSERT) (DUI)
4. DCL (Data Control Language): Used to grant & revoke permissions. (GRANT, REVOKE)
5. TCL (Transaction Control Language): Used to manage transactions. (COMMIT, START
TRANSACTIONS, SAVEPOINT, ROLLBACK) (CSSR)
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
SYNTAX:
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
EXAMPLE:
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
SYNTAX:
EXAMPLE:
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
4. The DROP command is part of the Data Definition Language (DDL) in SQL
(Structured Query Language) and is used to eliminate existing database objects.
Once a database object is dropped, it is permanently removed from the database,
along with any associated data or metadata.
SYNTAX:
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
5. INSERT is a SQL (Structured Query Language) statement used to add new records or
rows into a database table.
SYNTAX:
EXAMPLE:
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
SYNTAX:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
EXAMPLE:
UPDATE employees
SET age = 31, salary = 50000
WHERE employee_id = 1001;
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
SYNTAX:
GRANT privileges
ON object
TO user_or_role;
EXAMPLE:
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
SYNTAX:
REVOKE privileges
ON object
FROM user_or_role;
EXAMPLE:
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
9. COMMIT is a command used to permanently save the changes made during a
transaction. It is part of the ACID (Atomicity, Consistency, Isolation, Durability)
properties of database transactions, where "D" stands for Durability.
When you execute a COMMIT statement, all the changes made within the current
transaction are made permanent and visible to other users or transactions. Once
committed, the changes cannot be rolled back or undone.
SYNTAX:
COMMIT;
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
10. ROLLBACK is a command used to undo the changes made during a transaction that
has not yet been committed. It allows you to revert the database back to its state
before the transaction began, effectively canceling or discarding any modifications
made within that transaction.
SYNTAX:
ROLLBACK;
EXAMPLE:
BEGIN TRANSACTION;
UPDATE accounts
SET balance = balance - 100
WHERE account_id = 123;
ROLLBACK;
In this example, if an error occurred after the UPDATE statement but before the
INSERT statement, executing the ROLLBACK statement would undo the changes made by
the UPDATE operation, restoring the balance of the accounts table for account_id =
123 to its original value.
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
SYNTAX:
START TRANSACTION;
EXAMPLE:
START TRANSACTION;
UPDATE accounts
SET balance = balance - 100
WHERE account_id = 123;
COMMIT;
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
12. A savepoint is a point within a transaction where you can mark a specific point
of progress. It allows you to set a named marker within a transaction so that you
can later roll back to that point if needed. Savepoints provide a way to implement
nested transactions or to perform partial rollbacks within a larger transaction.
SYNTAX:
SAVEPOINT savepoint_name;
EXAMPLE:
START TRANSACTION;
1.DELETE:
- `DELETE` is a DML (Data Manipulation Language) command used to remove specific
rows from a table based on a condition.
- It allows you to specify a condition using a `WHERE` clause to delete only
certain rows that meet the condition. If you omit the `WHERE` clause, it will
delete all rows from the table.
- `DELETE` generates an entry for each deleted row in the transaction log,
making it possible to roll back the changes using a transaction rollback if
necessary.
- It is slower compared to `TRUNCATE`, especially for large tables, because it
processes each row individually.
SYNTAX:
DELETE FROM table_name WHERE condition;
2.TRUNCATE:
- `TRUNCATE` is a DDL (Data Definition Language) command used to remove all rows
from a table without logging individual row deletions.
- Unlike `DELETE`, it doesn't require specifying a condition; it removes all
rows from the table.
- `TRUNCATE` resets any associated identity columns or sequences to their
initial values.
- It's faster and more efficient than `DELETE`, especially for large tables,
because it deallocates data pages rather than removing individual rows.
- `TRUNCATE` doesn't generate entries in the transaction log for each deleted
row, making it non-rollbackable.
SYNTAX:
TRUNCATE TABLE table_name;
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
KEYS:
1.Primary Key (PK):
- A primary key is a column or a set of columns in a table that uniquely
identifies each row. It must contain unique values and cannot contain NULL values.
There can only be one primary key in a table.
- Example: In a table of employees, an "Employee ID" column might serve as the
primary key.
2.Super Key:
- A super key is a set of one or more columns that uniquely identifies each row
in a table. It can contain more columns than necessary to uniquely identify rows.
- Example: In a table of employees, a combination of "Employee ID" and "Social
Security Number" might form a super key.
4.Alternate Key:
- An alternate key is a candidate key that is not selected as the primary key.
In other words, it is a unique column or set of columns that could serve as the
primary key but is not chosen as such.
- Example: In a table of students, both "Student ID" and "Social Security
Number" might be candidate keys, but only one is chosen as the primary key, while
the other becomes an alternate key.
5.Candidate Key:
- A candidate key is a column or a set of columns in a table that can uniquely
identify each row. It means that each candidate key could potentially serve as the
primary key.
- Example: In a table of employees, both "Employee ID" and "Email Address" might
be candidate keys, as each uniquely identifies employees.
6.Composite Key:
- A composite key is a primary key composed of multiple columns. Together, these
columns uniquely identify each row in the table.
- Example: In a table of sales, a composite key might consist of "Order ID" and
"Product ID" columns, ensuring that each combination of order and product is
unique.
7.Unique Key:
- A unique key is a column or a set of columns in a table that ensures the
values stored in the column(s) are unique across the table. Unlike the primary key,
it can contain NULL values (except in SQL Server).
- Example: In a table of email subscribers, the "Email Address" column might
have a unique key constraint to ensure that no two subscribers have the same email
address.
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
ACID Properties:
1. Atomicity –
- This property ensures that either the transaction occurs completely or it does
not occur at all.
- In other words, it ensures that no transaction occurs partially.
2. Consistency –
- This property ensures that integrity constraints are maintained.
- In other words, it ensures that the database remains consistent before and
after the transaction.
3. Isolation –
- This property ensures that multiple transactions can occur simultaneously
without causing any inconsistency.
- The resultant state of the system after executing all the transactions is the
same as the state that would be achieved if the transactions were executed serially
one after the other.
4. Durability –
- This property ensures that all the changes made by a transaction after its
successful execution are written successfully to the disk.
- It also ensures that these changes exist permanently and are never lost even
if there occurs a failure of any kind.
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
DATA QUERIES:
1. WHERE:
The WHERE clause is used to filter records
SYNTAX:
SELECT column1, column2, ... FROM table_name WHERE condition;
EXAMPLE:
SELECT * FROM Customers WHERE Country='Mexico';
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
2. OPERATORS USED:
= : Equal
> : Greater than
< : Less than
>= : Greater than or equal
<= : Less than or equal
<> or != : Not equal.
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
SYNTAX:
SELECT column1, column2, ... FROM table_name WHERE condition1 AND condition2 AND
condition3 ...;
EXAMPLE:
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
4. DISTINCT:
SYNTAX:
SELECT DISTINCT column1, column2 FROM table_name;
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
5. LIKE:
The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.
There are two wildcards often used in conjunction with the LIKE operator:
- The percent sign (%) represents zero, one, or multiple characters
- The underscore sign (_) represents one, single character
EXAMPLE:
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
6. IN:
EXAMPLE:
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
7. BETWEEN:
Filters results within a specified range in the WHERE clause.
EXAMPLE:
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
8. IS NULL:
EXAMPLE:
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
9. AS:
EXAMPLE:
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
The ORDER BY clause allows you to sort the result set of a query based on one or
more columns.
EXAMPLE:
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
The GROUP BY clause follows the SELECT statement and is used to group rows based on
specified columns.
EXAMPLE:
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
JOINS:
1) Inner Join:
An inner join combines data from two or more tables based on a specified condition,
known as the join condition.
The result of an inner join includes only the rows where the join condition is met
in all participating tables.
It essentially filters out non-matching rows and returns only the rows that have
matching values in both tables.
SYNTAX:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
EXAMPLE:
SELECT Customers.CustomerName, Orders.Product
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
2) Outer Join
Outer joins combine data from two or more tables based on a specified condition,
just like inner joins.
However, unlike inner joins, outer joins also include rows that do not have
matching values in both tables.
Outer joins are particularly useful when you want to include data from one table
even if there is no corresponding match in the other table.
EXAMPLE:
SELECT Customers.CustomerName, Orders.Product
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
EXAMPLE:
SELECT Customers.CustomerName, Orders.Product
FROM Customers
RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
3) Cross Join
SYNTAX:
SELECT columns
FROM table1
CROSS JOIN table2;
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
4) Self Join
SYNTAX:
SELECT columns
FROM table1 AS alias1
JOIN table1 AS alias2 ON alias1.column = alias2.column;
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
CONSTRAINTS IN SQL:
1. NOT NULL:
--- ----
Applied only on the column level
By default, any column can have a NULL values
It restricts the columns in a table from having NULL values
Example:
CREATE TABLE Student
(
Student_ID INT NOT NULL,
Roll_No INT NOT NULL,
Name VARCHAR (100)
);
2. UNIQUE:
------
This applies on both table and column level
It ensures that the column has only unique values
Example:
CREATE TABLE Student
(
Student_ID INT UNIQUE,
Roll_No INT UNIQUE,
Name VARCHAR (100)
);
3. DEFAULT:
-------
This applies on both table and column level
It provides a default value for a column if no value is assigned
Example:
CREATE TABLE Product
(
Product_ID VARCHAR (50) NOT NULL,
Product_Name VARCHAR (50) UNIQUE,
Country of Origin VARCHAR (70) DEFAULT ‘India’
);
4. CHECK:
-----
This applies on both table and column level
It is used to restrict the value of a column between a range
It is similar to data validation in Excel
Example:
CREATE TABLE Student
(
Name VARCHAR(100) NOT NULL,
Age INT(2) CHECK (Age >= 21)
);
5. PRIMARY KEY:
------- ---
This applies on both table and column level
The primary Key constraint is a combination of both UNIQUE and NOT NULL
constraints.
It helps to retrieve query results from the table.
Only one PRIMARY KEY can be created per table
Example:
CREATE TABLE Employee
(
Employee ID INT PRIMARY KEY,
Employee Name VARCHAR (55),
Department VARCHAR (55) NOT NULL
);
6. FOREIGN KEY:
------- ---
This applies on both table and column level
It is used to relate two or more tables and prevents the operation that destroys
the link between the tables.
A foreign key can be a primary key if the table is connected by a one-to-one
relationship, not a one-to-many relationship.
Multiple foreign keys can be created per table
Example:
CREATE TABLE Order
(
Order ID INT PRIMARY KEY,
Product ID INT REFERENCES Products (ID)
);
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------
TO CREATE A NEW TABLE FROM AN OLDER TABLE THAT IS TO CREATE A DUPLICATE TABLE:
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------