SQL Notes
SQL Notes
Agenda:
Functional Testing
Background and need of Database Testing(With few
Examples)
Why we need database?
Why we need backend testing?
What is SQL and what it can do?
Popular DB’s Available in the market
Customers_Sign_up table:
- Validate Existence of the values in the database
table
- Validate Correctness of the values in the database
table
- Validate Completeness of the values in a database
table
Ex.2: Google.com: Whatever we search we get data from
DB(database)
- On UI 200 results
- Check same in DB and let’s say we get 250 results
Database Systems:
Database: Storage Location: Collection of related data.
- Structured Data- IRCTC, Facebook, Flipkart,
Amazon, MSEB Board, MPSC
- Unstructured Data- Random Data—Social Media
Post- Data can not be stored in db in the form of
Database.
Database Management Systems (DBMS):
- To perform any operations on the data we need the
database management systems
- SQL Server,MySQL,Oracle,DB2,MongoDB
Installation:
Windows 7: https://www.techbeamers.com/quick-steps-to-
install-mysql-on-windows/
Windows 10: https://youtu.be/OM4aZJW_Ojs
28/12/2021:
Agenda:
- Data Types in SQL
- Types of SQL Commands
Data Types in SQL:
- Numeric
- Character
- Date and time
Decimal(s,d):
S=Size
D=after decimal
Ex: 10.44
S=4
D=2
Decimal(4,2)
Ex. 2
10.4444
Decimal(6,4)
29/12/2021:
Agenda: Data Definition Language: Create, Alter, Drop,
Truncate
CREATE TABLE:
Definition: It is the SQL Statement used to create a new table
in a database.
Syntax to Create a new Table:
CREATE TABLE table_name(
Column1 datatype,
Column2 datatype,
Column3 datatype,
.
.
ColumnN datatype);
Ex:
CREATE TABLE Persons(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255));
Syntax for Creating Database:
CREATE DATABASE database_name;
Ex.
CREATE DATABASE Pramod;
Before:
After:
TRUNCATE:
- It is used to delete the data from the table, but the
structure remains same.
- The SQL TRUNCATE TABLE command is used to delete
complete data from the table.
- You can also use DROP TABLE command to delete the
complete data but it will remove the complete table
structure from the database and you need to re-create that
table again if you have to store some data.
- It performs the same function as the DELETE Statement
without a WHERE Clause.
Syntax:
TRUNCATE TABLE table_name;
Ex:
TRUNCATE TABLE VCTC;
Before:
After:
30/12/2021:
Agenda:
- DDL: CREATE,DROP,TRUNCATE,ALTER
- DML: Data Manipulation Language: INSERT
INTO,SELECT
ALTER:
- It is used to add, delete or modify columns in a table.
If you want to ADD a column in a SQL Table:
Syntax:
ALTER TABLE table_name
ADD Column_name datatype;
Ex:
ALTER TABLE PersonsP
ADD EmailID varchar(255);
Before:
After:
After:
Before:
After:
Ex:
INSERT INTO
PersonsP(PersonID,LastName,FirstName,Address,City)
VALUES(1 ,’Ambhore’, ’Pramod’,’Kharadi’,’Pune’);
2nd Way:
- If you are adding values for all the columns of a table, you
do not need to specify the column names in the SQL query
- However Make Sure that the order of the values is in the
same order as columns in a table.
INSERT INTO table_name
VALUES(Value1,Value2,Value3,…,ValueN);
EX:
INSERT INTO PersonsP
VALUES(2 ,’Patil’, ’Prakash’,’Katraj’,’Pune’);
Ex:
SELECT PersonID, Address,City
From PersonsP;
31/12/2021:
Agenda:
- SELECT DISTINCT
- TOP Clause
- Aggregate Functions:
COUNT(),AVG(),SUM(),MIN(),MAX()
DISTINCT:
- It is SQL Clause/Keyword used in conjunction with
SELECT statement.
- Returns only DISTINCT Values
- Inside a table column often contains duplicate values,
sometimes we want unique values
- Only returns distinct records or values(Unique/Different
Values)
Syntax:
SELECT DISTINCT Column_name
FROM table_name;
Ex:
SELECT DISTINCT SALARY
FROM CUSTOMERS;
Before:
After:
TOP Clause:
- TOP Clause is used to fetch TOP N or X Percent of
records from the database table
- It is used in Conjunction with SELECT Statement.
- In MySQL TOP Clause is not supported so we have
LIMIT Clause.
- The SELECT TOP clause is used to specify the number of
records to return.
- The SELECT TOP clause is useful on large tables with
thousands of records. Returning a large number of records
can impact performance.
-
Syntax:
SELECT TOP Number|Percent Column_name
FROM Table_name;
Ex:
SELECT TOP 3 * from CUSTOMERS;
In MYSQL:
SELECT * FROM CUSTOMERS
LIMIT 3;
Note: Not all database systems support the SELECT TOP clause. MySQL
supports the LIMIT clause to select a limited number of records, while Oracle
uses FETCH FIRST n ROWS ONLY and ROWNUM.
Aggregate Functions: COUNT(),AVG(),SUM(),MIN(),MAX()
Ex:
SELECT MAX(SALARY)
FROM CUSTOMERS;
Before:
After:
MIN():
- It returns minimum value of a numeric column
- Returns smallest value of a selected column
- Minimum value in a column EXCEPT NULL.
Syntax:
SELECT MIN(Column_name)
FROM table_name;
03/01/2022:
Agenda: Aggregate Functions- AVG(), Count(),WHERE Clause
AVG():
- Function returns the average value of a numeric column
- It will apply for NON-NULL Values
- AVG=Sum(column_name)/COUNT(Column_name)
Syntax:
SELECT AVG(Column_name)
FROM table_name;
Ex:
SELECT AVG(SALARY)
FROM CUSTOMERS;
COUNT():
- Returns total number of records
Syntax:
SELECT COUNT(*) from CUSTOMERS
Ex:
SELECT MIN(SALARY)
FROM CUSTOMERS;
SUM():
- Sum of all NON-NULL values of a column
- It returns total sum of numeric column
Syntax:
SELECT SUM(Column_name)
FROM table_name;
Ex:
SELECT SUM(SALARY)
FROM CUSTOMERS;
WHERE Clause:
- This clause is used to specify a condition while fetching
data from a database table.
- If the given condition is satisfied then only it returns
specific value
- Used to fetch specific records from database table
- The WHERE Clause is used in DELETE and UPDATE.
Syntax:
SELECT Column1, Column2
FROM table_name
WHERE Condition;
Ex:
SELECT * from Customers
WHERE AGE=32 AND SALARY=2000;
OR: If one condition is true then it will display a record
Syntax:
SELECT Column1,Column2
FROM table_name
WHERE [Condition1] OR [Condition2]…OR [ConditionN];
Ex:
SELECT * from Customers
WHERE AGE=32 OR SALARY=2000;
Ex:
SELECT * FROM CUSTOMERS
WHERE NOT Age=32;
IN Operator:
- It is used along with SELECT Statement
- It allows you to specify multiple values in a WHERE
Clause
- The IN Operator is shorthand for Multiple OR Conditions
Syntax:
SELECT Column_name
FROM table_name
WHERE Column_name IN(Value1,Value2,…,ValueN);
By USING OR:
WHERE [Condition1(Column_name=Value1)] OR
[Condition2(Column_name=Value2)]
OR[Condition3(column_name=Value3)]
NOT IN Operator:
Syntax:
SELECT Column_name
FROM table_name
WHERE Column_name NOT IN(Value1,Value2,…,ValueN);
Ex:
SELECT *
FROM VCTC
WHERE Mockresult NOT IN(6,7,8,9);
04/01/2022:
Agenda: BETWEEN, LIKE, What is NULL Value? , How to
Check for Null Values?
BETWEEN:
- Selects values within a range
- Values Can be numbers, text or dates
- Start Value and end values are inclusive
Syntax:
SELECT Column_name
FROM table_name
WHERE Column_name BETWEEN Value 1 AND Value 2;
Ex:
SELECT Mockresult
FROM VCTC
WHERE Mockresult BETWEEN 8 AND 9;
NOT BETWEEN:
Syntax:
SELECT Column_name
FROM table_name
WHERE Column_name NOT BETWEEN Value 1 AND Value 2;
Ex:
SELECT Mockresult
FROM VCTC
WHERE Mockresult NOT BETWEEN 8 AND 9;
LIKE:
- Used with WHERE Clause to search specified condition in
table
- It is used to search specified Pattern in a column
There are two wildcards often used in conjuction with LIKE
Operator:
- The Percentage Sign(%) represents zero,one or Multiple
Characters
- The Underscore Sign(_) represents one Single Character
Null Value:
- A field with No Value
- Blank Value
- Null value is different than zero
- A field which is kept blank during record creation
How to check for NULL Values?
IS NULL:
Syntax:
SELECT Column_name
FROM table_name
WHERE Column_name IS NULL;
Ex:
SELECT SALARY
FROM Customers
WHERE SALARY IS NULL;
IS NOT NULL:
Syntax:
SELECT Column_name
FROM table_name
WHERE Column_name IS NOT NULL;
Ex:
SELECT SALARY
FROM Customers
WHERE SALARY IS NOT NULL;
Ex:
Select * from employeeself
WHERE ManagerID IS NOT NULL;
05/01/2021:
Agenda: Data Manipulation Language (DML): (INSERT INTO,
SELECT, UPDATE, DELETE), UNION and UNION ALL,
GROUP BY Clause
UPDATE:
- It is used to modify existing records in a database table.
- Used to modify specific records based on WHERE Clause
Syntax:
UPDATE table_name
SET Column1=Value1,Column2=Value2,..,ColumnN=ValueN
WHERE Condition;
Ex:
UPDATE Customers
SET Name=’Prakash’, Address=’Chepok’
WHERE C_ID=1;
Syntax:
DELETE FROM PersonsNew
WHERE PersonID=4;
DELETE Without WHERE Clause:
UNION ALL:
- Keep all records including duplicates
UNION and UNION ALL have some basic requirements of the
data being combined:
- There must same number of columns retrieved in each
SELECT Statement
- The Columns retrieved must be of similar datatype
- There Columns retrieved must be in same order in each
SELECT Statement.
06/01/2022:
Agenda: GROUP BY Clause, Having Clause, Order By Clause,
GROUP BY Clause:
- The GROUP BY Clause is utilized in SQL with SELECT
Statement to organize similar data into groups
- It combines the multiple records in single or more columns
using some functions. Generally these functions are called
as aggregate functions such as
MIN(),MAX(),COUNT(),AVG(),SUM().
- It is used with SELECT statement to make group of rows
based on the values of a specific column.
- The WHERE Clause is used to retrieve rows based on a
certain condition but it can’t be applied to Grouped Result.
Syntax:
SELECT Column_name(s)
FROM Table_name
WHERE Condition
GROUP BY Column_name
ORDER BY Column_name;
Points to Remember:
- GROUP BY Clause is utilized with SELECT Statement
- GROUP BY Clause aggregates the results on the basis of selected
column: MIN(),MAX(),SUM(),AVG().COUNT()
- GROUP BY Clause returns only one result per group of data
- GROUP BY Clause always follows WHERE Clause
- GROUP BY Clause always Preceedes the ORDER BY Clause.
Ex:
SELECT Country, Count(CustomerID)
FROM Customers
GROUP BY Country;
HAVING Clause:
- Having Clause is utilized in SQL as a conditional Clause
with GROUP BY Clause
- This Clause returns rows where aggregate function results
are matched with given condition only
- It is added to SQL because WHERE Clause cannot be
combined with aggregate functions
- WHERE only deal with Non-aggregated data or individual
records
- Having Clause is utilized with GROUP BY Clause
Ex:
SELECT Country,Count(CustomerID)
FROM Customers
GROUP BY Country
HAVING Count(CustomerID)>5
ORDER BY Count(CustomerID) Desc;
Ex:
SELECT DeptID, AVG(SALARY)
FROM employee
GROUP BY DeptID
HAVING AVG(SALARY)>3000;
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;
ORDER BY Clause:
- Used to sort the results in ascending or descending
- By default sorting order is ascending if we do not specify
the keyword
- To sort the results in descending use keyword: DESC
- To sort in Ascending order use keyword: ASC
Syntax:
SELECT Column_name
FROM table_name
ORDER By Column_name ASC|DESC;
Ex 1:
SELECT * FROM Customers
ORDER BY Country;
Ex 2:
SELECT * FROM Customers
ORDER BY Country DESC;
Aliases:
- Aliases are used to give a table or a column in a table a
temporary name
- It is to make the table name or column name more
readable
- It only exists for the duration of that query
- It is created using AS keyword.
Syntax:
SELECT Column_name AS alias_name
FROM table_name;
Ex:
Select C_ID AS CustomerID,
NAME As CustomerName,
AGE As CustomerAge,
ADDRESS As CustomerAddress,
SALARY As CustomerSALARY
from customers;
07/01/2022:
Agenda: Constraint, Types of Constraints, JOIN, Types of JOINs
Constraint:
- Used to limit the type of data that go inside the database
table columns
- Used to limit type of value
- Rules enforced on the data columns of a table in a database
- This ensures the accuracy and reliability of the data in the
table.
Types of Constraints:
NOT NULL: Ensures that a column can’t have null value.
UNIQUE: Ensures that all values in a column are different.
Default: Provides default value for a column when nothing is
specified
Primary Key(Unique+ NOT NULL): Uniquely identifies each
record in a database(One table can have only one Primary Key)
Ex: Phone Number, Adhar Card Number, PAN Card Number,
Student Roll Number, License Number, Passport Number, Student
Registration Number.
Check: Ensures that all the values in a column satisfy specified
condition. Ex: CHECK(Age>18)
Foreign Key:
- Used to link two tables together
- Uniquely identifies each record in any of the given
database table
- A foreign key is field(or collection of filed) in one table
that refers to Primary key of another table(We can have
one or More key)
- It is attribute or set of attributes that references to Primary
key of same table or Another table.
- It maintains referential integrity
Students:
Roll Number(pk) Name Address
1 A Delhi
2 B Mumbai
3 C Chennai
Courses:
CourseID Course_Name Roll Number(fk)
C1 DBMS 1
C2 Networks 2
10
Syntax:
CREATE TABLE table_name(
Fname varchar(255) NOT NULL,
Lname varchar(255) NOT NULL,
AdharID int UNIQUE,
State varchar(255) Default ‘Maharashtra’,
Age int CHECK(Age>18),
Primary Key(AdharID));
Ex:
Table 1: Customers:
CREATE TABLE CUSTOMERS(
C_ID int NOT NULL,
NAME Varchar(20) NOT NULL,
AGE INT NOT NULL,
ADDRESS Char(25),
SALARY int,
Primary Key(C_ID));
Table 2: ORDERS
CREATE TABLE ORDERS(
OrderID int NOT NULL,
OrderDate DATE,
CustomerID int,
Amount int,
Primary Key(CustomerID),
FOREIGN KEY(CustomerID) REFERENCES
CUSTOMERS(C_ID));
JOIN:
- A JOIN Clause is used to combine rows from two or
more tables based on related column between them.
- Used to Join two or more tables
- Used by backend developers during integration testing to
join database tables of multiple modules.
Integration Testing:
Module 1=Tested Individually=Table1
Module 2=Tested Individually=Table2
Module 3=Tested Individually=Table3
10/01/2022:
Agenda: Types of Joins:
INNER JOIN: Returns records that have matching values in both the
tables.
LEFT (OUTER)JOIN: Returns all records from the left table and
the matched records from the right table
RIGHT (OUTER)JOIN: Returns all records from the Right table
and the matched records from the Left table
FULL (OUTER) JOIN: Returns all records when there is a match
either in Left or Right table.
SELF JOIN: Joining a table itself is called Self-JOIN.
INNER JOIN:
Syntax:
SELECT table1.Column1,table2.Column2
FROM table1
INNER JOIN table2
ON table1.CommonField(pk)=table2.CommonField(fk);
Ex:
SELECT Customers.C_ID, Customers.NAME, Orders.OrderDate,
Orders.Amount
FROM Customers
INNER JOIN Orders
ON Customers.C_ID=Orders.CustomerID;
LEFT JOIN:
Syntax:
SELECT table1.Column1,table2.Column2
FROM table1
LEFT JOIN table2
ON table1.CommonField(pk)=table2.CommonField(fk);
Ex:
SELECT Customers.C_ID, Customers.NAME, Orders.OrderDate,
Orders.Amount
FROM Customers
LEFT JOIN Orders
ON Customers.C_ID=Orders.CustomerID;
RIGHT JOIN:
Syntax:
SELECT table1.Column1,table2.Column2
FROM table1
RIGHT JOIN table2
ON table1.CommonField(pk)=table2.CommonField(fk);
Ex:
SELECT Customers.C_ID, Customers.NAME, Orders.OrderDate,
Orders.Amount
FROM Customers
RIGHT JOIN Orders
ON Customers.C_ID=Orders.CustomerID;
FULL JOIN:
Syntax:
SELECT table1.Column1,table2.Column2
FROM table1
FULL JOIN table2
ON table1.CommonField(pk)=table2.CommonField(fk);
Ex:
SELECT Customers.C_ID, Customers.NAME, Orders.OrderDate,
Orders.Amount
FROM Customers
FULL JOIN Orders
ON Customers.C_ID=Orders.CustomerID;
Select Cs.C_ID As Customer, Cs.NAME As CustomerName,
Od.OrderDate,Od.Amount As OrderAmount
FROM customers Cs
INNER JOIN orders Od
ON Cs.C_ID=Od.CustomerID;
11/01/2022:
Agenda: SELF JOIN, INDEX, VIEW, Stored Procedure, Nth
Highest Salary
SELF JOIN:
- A SELF JOIN is normal SQL JOIN tat joins one table to
itself
- Joining a table to itself can be useful when you want to
compare values in a column to other values in the same
column
- It can either be INNER JOIN or OUTER JOIN
- We have a table with Employee Data and each row
contains information about employee and his/her manager
- One of the columns in the Same table contains ID of a
Manager, Who is also an employee of the same company
and their managers are present in the same table
- We want to Display Employee Name & their Managers
Syntax/Example:
SELECT E1.Name AS EmployeeName,
E2.Name AS ManagerName
From EmployeeSelf E1
INNER JOIN EmployeeSelf E2
ON E1.ManagaerID=E2.EmployeeID;
INDEX:
- Indexes are used to retrieve data from the database more
quickly than otherwise.
- Users cannot see these indexes but they are used to speed
up the searches/queries.
Syntax:
CREATE INDEX index_name
ON table_name(Column1,Column2,…,ColumnN);
Ex:
CREATE INDEX VCTCMockResult on VCTC(Mockresult);
To See Indexes:
Show Index from table_name;
Show Index From VCTC;
To Remove Index:
DROP INDEX VCTCMockresult ON VCTC;
VIEW:
- A VIEW is a virtual table based on the result set of an
SQL Statement.
- A view Contains rows and columns just like real table
Syntax:
CREATE VIEW view_name
AS
SELECT Column1,Column2
FROM table_name
WHERE Condition;
Ex:
CREATE VIEW MockResultVCTC
AS
SELECT * FROM VCTC WHERE Mockresult>5;
12/01/2022:
Agenda: Stored Procedure, Nth highest Salary, Sub Query,How to
find duplicate records?, CASE Statement in SQL.
Stored Procedure:
- A stored procedure is a prepared SQL code that you can
save, So that code can be reused over and over again
- So if you have a query that you write over and over again,
save it as a procedure and then just call it to execute.
How to Create Stored Procedure?
Syntax in MySQL:
DELIMITER//
CREATE PROCEDURE Procedure_name
Begin
SQL Query
End
DELIMITER;
Call sp_getCustomers_list();
Syntax in SQL Server:
Second Highest:
Select Max(Salary) From employeenthsalary where Salary<(Select
Max(Salary) from Employees);
Nth Highest Salary(2nd and 3rd ):
For nth highest Salary in MySQL:
In this section, we are going to learn how we can select the nth highest record in a database
table with the help of various techniques.
We can get the maximum (highest) or minimum (lowest) record in the database table very
easily by using the MAX() or MIN() function. But suppose we want to get the nth highest
record from the table (for example, get the second-most expensive salary from the employee
table). In that case, there is no function available to find it quickly, which makes it
complicated.
By performing the following steps, we can select the nth highest record in a MySQL database
table:
1. The first step is to sort the desired column in ascending order to get the n highest records,
which is the last record in the resultant output. See the below query:
2. After that, we need to sort the resultant output in descending order and get the first record.
SELECT * FROM (
SELECT * FROM table_name
ORDER BY colm_name ASC LIMIT N) AS temp_table
ORDER BY colm_name DESC LIMIT 1;
The above query can also be rewritten by using the LIMIT clause that constrains the number
of rows in the resultant output as follows:
This query will return the first row after the n-1 rows that should be the nth highest record.
Example:
Let us understand how to get the nth highest record from the table with the help of an
example. First, we will create an Employee table using the below query:
Suppose we want to get the second highest salary of an employee (n = 2) in the Employee
table; we can use the below statement:
We can also get the nth highest record with the help of subquery, which depends upon the
main query and processed for every record returned by the main query. This technique is
rarely used because of its slow performance/execution speed.
See the below query that returns nth highest record using the subquery:
See the below query that returns the second highest salary from the employee table using the
sub query:
(ID int ,
FirstName varchar(50),
LastName varchar(50),
Gender varchar(50),
Salary int
);
(ID int ,
FirstName varchar(50),
LastName varchar(50),
Gender varchar(50),
Salary int
);
To find the highest salary it is straight forward. We can simply use the Max() function as
shown below.
To get the second highest salary use a sub query along with Max() function as shown
below.
Select Max(Salary) from Employees where Salary < (Select Max(Salary) from Employees);
Sub Query:
- A SELECT clause
- A FROM clause
- A WHERE clause
- You can use the comparison operators, such as >, <, or =. The
comparison operator can also be a multiple-row operator, such as IN,
ANY, SOME, or ALL.
- A subquery can be treated as an inner query, which is a SQL query placed
as a part of another query called as outer query.
- The inner query executes first before its parent query so that the results of
the inner query can be passed to the outer query.
Ex:
SELECT first_name, last_name
FROM employees
WHERE department_id
IN (SELECT department_id FROM departments WHERE
department_name='IT');
);
('Jean','King','jean.king@me.com'),
('Peter','Ferguson','peter.ferguson@google.com'),
('Janine ','Labrune','janine.labrune@aol.com'),
('Jonas ','Bergulfsen','jonas.bergulfsen@mac.com'),
('Janine ','Labrune','janine.labrune@aol.com'),
('Susan','Nelson','susan.nelson@comcast.net'),
('Zbyszek
','Piestrzeniewicz','zbyszek.piestrzeniewicz@att.net'),
('Roland','Keitel','roland.keitel@yahoo.com'),
('Julie','Murphy','julie.murphy@yahoo.com'),
('Kwai','Lee','kwai.lee@google.com'),
('Jean','King','jean.king@me.com'),
('Susan','Nelson','susan.nelson@comcast.net'),
('Roland','Keitel','roland.keitel@yahoo.com');
ORDER BY email;
SELECT
email,
COUNT(email)
FROM
contacts
GROUP BY email
SELECT
first_name, COUNT(first_name),
last_name, COUNT(last_name),
email, COUNT(email)
FROM
contacts
GROUP BY
first_name ,
last_name ,
CASE Statement:
The CASE statement goes through conditions and returns a value when the first condition is met
(like an if-then-else statement). So, once a condition is true, it will stop reading and return the
result. If no conditions are true, it returns the value in the ELSE clause.
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
Example:
UPDATE Students
SET Grade = (
CASE
END
);
The INSERT INTO SELECT statement copies data from one table and inserts it into another
table.
The INSERT INTO SELECT statement requires that the data types in source and target tables
matches.
What is DCL?
DCL (Data Control Language) includes commands like GRANT and
REVOKE, which are useful to give “rights & permissions.” Other permission
controls parameters of the database system.
DCL includes commands such as GRANT and REVOKE which mainly deal with
the rights, permissions, and other controls of the database system.
Grant
Revoke
Grant:
This command is use to give user access privileges to a database.
Syntax:
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;
For example:
GRANT SELECT ON Users TO'Tom'@'localhost;
Revoke:
It is useful to back permissions from the user.
This command withdraws the user’s access privileges given by using the
GRANT command.
Syntax:
REVOKE privilege_name ON object_name FROM {user_name |PUBLIC |role_name}
For example:
REVOKE SELECT, UPDATE ON student FROM BCA, MCA;
What is TCL?
Transaction control language or TCL commands deal with the transaction
within the database.
TCL commands can only use with DML commands like INSERT, DELETE and UPDATE
only.
These operations are automatically committed in the database that's why they
cannot be used while creating tables or dropping them.
Commit
This command is used to save all the transactions to the database.
Syntax:
Commit;
For example:
DELETE FROM Students
WHERE RollNo =25;
COMMIT;
Rollback
Rollback command allows you to undo transactions that have not already
been saved to the database.
Syntax:
ROLLBACK;
Example:
DELETE FROM Students
WHERE RollNo =25;
SAVEPOINT
This command helps you to sets a savepoint within a transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;
Example:
SAVEPOINT RollNo;
Types of Triggers
There are two types of triggers:
1. DDL Trigger
2. DML Trigger
What is Normalization?
Normalization is a database design technique that reduces data
redundancy and eliminates undesirable characteristics like Insertion,
Update and Deletion Anomalies. Normalization rules divides larger tables
into smaller tables and links them using relationships. The purpose of
Normalisation in SQL is to eliminate redundant (repetitive) data and ensure
data is stored logically.
In our database, we have two people with the same name Robert Phil, but
they live in different places.
Composite key in Database
Foreign Key references the primary key of another Table! It helps connect
your Tables
A foreign key can have a different name from its primary key
It ensures rows in one table have corresponding rows in another
Unlike the Primary key, they do not have to be unique. Most often
they aren’t
Foreign keys can be null even though primary keys can not
Why do you need a foreign key?
Suppose, a novice inserts a record in Table B such as