DB Lab Manual
DB Lab Manual
DB Lab Manual
SEng 2041
LAB MANUAL
1. SQL Commands
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
o DDL changes the structure of the table like creating a table, deleting a
table, altering a table, etc.
o All the command of DDL are auto-committed that means it permanently
save all the changes in the database.
o CREATE
o ALTER
o DROP
o TRUNCATE
Syntax:
Example:
b. DROP: It is used to delete both the structure and record stored in the table.
Syntax
DROP TABLE table_name;
Example
c. ALTER: It is used to alter the structure of the database. This change could
be either to modify the characteristics of an existing attribute or probably to
add a new attribute.
Syntax:
EXAMPLE
d. TRUNCATE: It is used to delete all the rows from the table and free the
space containing the table.
Syntax:
Example:
o DML commands are used to modify the database. It is responsible for all
form of changes in the database.
o The command of DML is not auto-committed that means it can't
permanently save all the changes in the database. They can be rollback.
Here are some commands that come under DML:
o INSERT
o UPDATE
o DELETE
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into
the row of a table.
Syntax:
Or
For example:
Syntax:
For example:
Syntax:
For example:
DELETE FROM staffsub WHERE Author="Sonoo";
DCL commands are used to grant and take back authority from any database
user.
o Grant
o Revoke
Example
Example
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.
o COMMIT
o ROLLBACK
o SAVEPOINT
a. Commit: Commit command is used to save all the transactions to the
database.
Syntax:
COMMIT;
Example:
Transaction T1
COMMIT;
Syntax:
ROLLBACK;
Example:
Transaction T1
Syntax:
SAVEPOINT SAVEPOINT_NAME;
o SELECT
Syntax:
For example:
2. INTEGRITY CONSTRAINTS:
There are 5 important Integrity Constraints
1. Not NULL
2. Primary Key
3. Unique
4. Foreign Key
5. Check Constraints
Not NULL:
Unique Constraints:
Person table:
1 Kumar Ravi 30
2 Giri Raj 31
3 Peter David 25
Order Table:
1 77895 3
2 44678 3
3 22456 2
4 24562 1
Here PersonID column in the order table points to the PersonID in the
Person Table.
PersonID column in the “order” table is foreign key.
Foreign key constraint is used to prevent actions that would destroy
links between tables.
Foreign key constraint also prevents invalid data from being inserted into
the foreign key column.
SQL Server:
Check Constraints:
The INNER JOIN keyword selects records that have matching values in both
tables.
INNER JOIN Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Demo Database
10308 2 7 1996-09-18 3
10309 37 3 1996-09-19 1
10310 77 8 1996-09-20 2
The following SQL statement selects all orders with customer information:
Example
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
The LEFT JOIN keyword returns all records from the left table (table1), and the
matched records from the right table (table2). The result is NULL from the right
side, if there is no match.
Demo Database
10308 2 7 1996-09-18
10309 37 3 1996-09-19
10310 77 8 1996-09-20
The following SQL statement will select all customers, and any orders they
might have:
Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT OUTER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
Example
SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM Orders
RIGHT OUTER JOIN Employees ON Orders.EmployeeID =
Employees.EmployeeID
ORDER BY Orders.OrderID;
The FULL OUTER JOIN keyword return all records when there is a match in
either left (table1) or right (table2) table records.
Note: FULL OUTER JOIN can potentially return very large result-sets!
Tip: FULL OUTER JOIN and FULL JOIN are the same.
The following SQL statement selects all customers, and all orders:
4. Views
Views in SQL are kind of virtual tables. A view also has rows and columns as
they are in a real table in the database. We can create a view by selecting fields
from one or more tables present in the database. A View can either have all the
rows of a table or specific rows based on certain condition.
In this article we will learn about creating , deleting and updating Views.
Sample Tables:
StudentDetails
StudentMarks
CREATING VIEWS
We can create View using CREATE VIEW statement. A View can be created
from a single table or multiple tables.
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condition;
Complex View (Creating View from multiple tables) : In this example we will
create a View named MarksView from two tables StudentDetails and
StudentMarks. To create a View from multiple tables we can simply include
multiple tables in the SELECT statement. Query:
CREATE VIEW MarksView AS
SELECT StudentDetails.NAME, StudentDetails.ADDRESS,
StudentMarks.MARKS
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;
To display data of View MarksView:
SELECT * FROM MarksView;
Output:
DELETING VIEWS
We have learned about creating a View, but what if a created View is not
needed any more? Obviously we will want to delete it. SQL allows us to delete
an existing View. We can delete or drop a View using the DROP statement.
Syntax:
DROP VIEW view_name;
5. Set Operations:
SET operators are special type of operators which are used to combine the
result of two queries.
1. UNION
2. UNION ALL
3. INTERSECT
4. MINUS
There are certain rules which must be followed to perform operations using
SET operators in SQL. Rules are as follows:
Let us see each of the SET operators in more detail with the help of examples.
Consider we have the following tables with the given data.
Table 1: t_employees
Table 2: t2_employees
Table 3: t_students
Table 4: t2_students
1. UNION:
o UNION will be used to combine the result of two select statements.
o Duplicate rows will be eliminated from the results obtained after performing the
UNION operation.
Example 1:
Write a query to perform union between the table t_employees and the table
t2_employees.
Query:
Here, in a single query, we have written two SELECT queries. The first SELECT
query will fetch the records from the t_employees table and perform a UNION
operation with the records fetched by the second SELECT query from the
t2_employees table.
Since we have performed union operation between both the tables, so only the
records from the first and second table are displayed except for the duplicate
records.
Example 2:
Write a query to perform union between the table t_students and the table
t2_students.
Query:
Here, in a single query, we have written two SELECT queries. The first SELECT
query will fetch the records from the t_students table and perform a UNION
operation with the records fetched by the second SELECT query from the
t2_students table.
Since we have performed union operation between both the tables, so only the
records from the first and second table are displayed except for the duplicate
records.
2. UNION ALL
o This operator combines all the records from both the queries.
o Duplicate rows will be not be eliminated from the results obtained after
performing the UNION ALL operation.
Example 1:
Write a query to perform union all operation between the table t_employees and
the table t2_employees.
Query:
Here, in a single query, we have written two SELECT queries. The first SELECT
query will fetch the records from the t_employees table and perform UNION ALL
operation with the records fetched by the second SELECT query from the
t2_employees table.
Since we have performed union all operation between both the tables, so all the
records from the first and second table are displayed, including the duplicate
records.
Example 2:
Write a query to perform union all operation between the table t_students and
the table t2_students.
Query:
Here, in a single query, we have written two SELECT queries. The first SELECT
query will fetch the records from the t_students table and perform UNION ALL
operation with the records fetched by the second SELECT query from the
t2_students table.
Since we have performed union all operation between both the tables, so all the
records from the first and second table are displayed, including the duplicate
records.
3. INTERSECT:
o It is used to combine two SELECT statements, but it only returns the records
which are common from both SELECT statements.
Example 1:
Write a query to perform intersect operation between the table t_employees and
the table t2_employees.
Query:
Here, in a single query, we have written two SELECT queries. The first SELECT
query will fetch the records from the t_employees table and perform
INTERSECT operation with the records fetched by the second SELECT query
from the t2_employees table.
Since we have performed intersect operation between both the tables, so only
the common records from both the tables are displayed.
Example 2:
Write a query to perform intersect operation between the table t_students and
the table t2_students.
Query:
Here, in a single query, we have written two SELECT queries. The first SELECT
query will fetch the records from the t_students table and perform an
INTERSECT operation with the records fetched by the second SELECT query
from the t2_students table.
Since we have performed intersect operation between both the tables, so only
the common records from both the tables are displayed.
4. MINUS
o It displays the rows which are present in the first query but absent in the
second query with no duplicates.
Example 1:
Write a query to perform a minus operation between the table t_employees and
the table t2_employees.
Query:
Here, in a single query, we have written two SELECT queries. The first SELECT
query will fetch the records from the t_employees table and perform MINUS
operation with the records fetched by the second SELECT query from the
t2_employees table.
Since we have performed Minus operation between both the tables, so only the
unmatched records from both the tables are displayed.
Example 2:
Write a query to perform a minus operation between the table t_students and
the table t2_students.
Query:
Here, in a single query, we have written two SELECT queries. The first SELECT
query will fetch the records from the t_employees table and perform a MINUS
operation with the records fetched by the second SELECT query from the
t2_employees table.
Since we have performed a minus operation between both the tables, so only
the Unmatched records from both the tables are displayed.
5. Arithmetic Operators:
Addition (+) :
It is used to perform addition operation on the data items, items include
either single column or multiple columns.
Implementation:
SELECT employee_id, employee_name, salary, salary + 100 AS "salary +
100" FROM addition;
Output:
2 rr 55000 55100
Here we have done addition of 100 to each Employee’s salary i.e, addition
operation on single column.
Let’s perform addition of 2 columns:
SELECT employee_id, employee_name, salary, salary + employee_id AS
"salary + employee_id" FROM addition;
Output:
2 rr 55000 55002
3 jpm 52000 52003
Here we have done addition of 2 columns with each other i.e, each employee’s
employee_id is added with its salary.
Subtraction (-) :
It is use to perform subtraction operation on the data items, items include
either single column or multiple columns.
Implementation:
SELECT employee_id, employee_name, salary, salary – 100 AS "salary - 100"
FROM subtraction;
Output:
Here we have done subtraction of 2 columns with each other i.e, each
employee’s employee_id is subtracted from its salary.
Multiplication (*) :
It is use to perform multiplication of data items.
Implementation:
SELECT employee_id, employee_name, salary, salary * 100 AS "salary * 100"
FROM addition;
Output:
Here we have done multiplication of 2 columns with each other i.e, each
employee’s employee_id is multiplied with its salary.