DBMS Unit-2
DBMS Unit-2
Comparison Operators
A comparison (or relational) operator is a mathematical symbol which is used to compare two
values
Comparison operators are used in conditions that compares one expression with another.
Table: MATHS
SID SNAME SMARKS
101 Rahul 50
102 Ram 95
103 Harish 68
104 Santosh 79
105 Aarush 93
106 Aakash 76
Greater than (>) Operator: It returns the rows/tuples which have the value of the attribute
greater than the given value.
Query:
SELECT * FROM MATHS WHERE MARKS>60;
Less than (<) Operator: It returns the rows/tuples which have the value of the attribute
lesser than the given value.
Query:
SELECT * FROM MATHS WHERE MARKS<40;
Greater than or equal to (>=) Operator: It returns the rows/tuples which have the value
of the attribute greater or equal to the given value.
Query:
SELECT * FROM MATHS WHERE MARKS>=80;
Less than or equal to (<=) Operator: It returns the rows/tuples which have the value of
the attribute lesser or equal to the given value.
Query:
SELECT * FROM MATHS WHERE MARKS<=30;
Not equal to (<>) Operator: It returns the rows/tuples which have the value of the
attribute not equal to the given value.
Query:
SELECT * FROM MATHS WHERE MARKS<>70;
Integrity Constraints
o Integrity constraints are a set of rules. It is used to maintain the quality of information.
o Integrity constraints ensure that the data insertion, updating, and other processes have to
be performed in such a way that data integrity is not affected.
o Thus, integrity constraint is used to guard against accidental damage to the database.
Types of Integrity Constraint
1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of values for an
attribute.
o The data type of domain includes string, character, integer, time, date, currency, etc. The
value of the attribute must be available in the corresponding domain.
Example:
4. Key constraints
o Keys are the entity set that is used to identify an entity within its entity set uniquely.
o An entity set can have multiple keys, but out of which one key will be the primary key. A
primary key can contain a unique and null value in the relational table.
Example:
OUTER JOIN
o In the outer JOIN, all the content from both the tables is integrated together.
o Even though the records from both the tables are matched or not, the matching and non-
matching records from both the tables will be considered an output of the outer join in
SQL.
o There are three different types of outer join in SQL:
1. Left Outer Join
2. Right Outer Join
3. Full Outer Join
o 1. Left Outer Join: The left join operation returns all record from left table and
matching records from the right table. On a matching element not found in right table,
NULL is represented in that case.
o Syntax :
o SELECT column_name(s)
o FROM table1
o LEFT JOIN Table2
o ON Table1.Column_Name=table2.column_name;
o
o Syntax :
o SELECT column_name(s)
o FROM table1
o RIGHT JOIN table2
o ON table1.column_name = table2.column_name;
1. COUNT FUNCTION
o COUNT function is used to Count the number of rows in a database table. It can work on
both numeric and non-numeric data types.
o COUNT function uses the COUNT(*) that returns the count of all the rows in a specified
table. COUNT(*) considers duplicate and Null.
o
o
o Count(*): Returns total number of records .i.e 6.
Count(salary): Return number of Non Null values over the column salary. i.e 5.
Count(Distinct Salary): Return number of distinct Non Null values over the column
salary .i.e 4
2. SUM Function
Sum function is used to calculate the sum of all selected columns. It works on numeric fields
only.
sum(salary): Sum all Non Null values of Column salary i.e., 310
sum(Distinct salary): Sum of all distinct Non-Null values i.e., 250.
3. AVG function
The AVG function is used to calculate the average value of the numeric type. AVG function
returns the average of all non-Null values.
Avg():
4. MAX Function
MAX function is used to find the maximum value of a certain column. This function determines
the largest value of all selected values of a column
Max(salary): Maximum value in the salary i.e., 80.
5. MIN Function
MIN function is used to find the minimum value of a certain column. This function determines
the smallest value of all selected values of a column.
Min(salary): Minimum value in the salary column except NULL i.e., 40.
Operator Meaning
AND Operator
Query :
SELECT * FROM employee WHERE emp_city = 'Allahabad' AND emp_country = 'India';
IN Operator
Query:
SELECT * FROM employee WHERE emp_city IN ('Allahabad', 'Patna');
NOT Operator
Query:
SELECT * FROM employee WHERE emp_city NOT LIKE 'A%';
OR Operator
Query:
SELECT * FROM employee WHERE emp_city = 'Varanasi' OR emp_country = 'India';
LIKE Operator
Query:
SELECT * FROM employee WHERE emp_city LIKE 'P%';
BETWEEN Operator
Query:
SELECT * FROM employee WHERE emp_id BETWEEN 101 AND 104;
ALL Operator
Query:
SELECT * FROM employee WHERE emp_id = ALL
(SELECT emp_id FROM employee WHERE emp_city = 'Varanasi');
ANY Operator
Query:
SELECT * FROM employee WHERE emp_id = ANY
(SELECT emp_id FROM employee WHERE emp_city = 'Varanasi');
EXISTS Operator
Query:
SELECT emp_name FROM employee WHERE EXISTS
(SELECT emp_id FROM employee WHERE emp_city = 'Patna');
SOME Operator
Query:
SELECT * FROM employee WHERE emp_id < SOME
(SELECT emp_id FROM employee WHERE emp_city = 'Patna');
Syntax:
create trigger [trigger_name]
[before | after]
{insert | update | delete}
on [table_name]
[for each row]
[trigger_body]
Example:
Given Student Report Database, in which student marks assessment is recorded. In such
schema, create a trigger so that the total and percentage of specified marks is automatically
inserted whenever a record is insert.
Here, as trigger will invoke before record is inserted so, BEFORE Tag can be used.
Suppose the database Schema –
mysql> desc Student;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| sid | int(4) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | YES | | NULL | |
| subj1 | int(2) | YES | | NULL | |
| subj2 | int(2) | YES | | NULL | |
| subj3 | int(2) | YES | | NULL | |
| total | int(3) | YES | | NULL | |
| per | int(3) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
Above SQL statement will create a trigger in the student database in which whenever subjects
marks are entered, before inserting this data into the database, trigger will compute those two
values and insert with the entered values. i.e.
mysql> insert into Student values(0, "ABCDE", 20, 20, 20, 0, 0);
Query OK, 1 row affected (0.09 sec)
+-----+-------+-------+-------+-------+-------+------+
| sid | name | subj1 | subj2 | subj3 | total | per |
+-----+-------+-------+-------+-------+-------+------+
| 100 | ABCDE | 20 | 20 | 20 | 60 | 40 |
+-----+-------+-------+-------+-------+-------+------+
1 row in set (0.00 sec)
In this way trigger can be creates and executed in the databases.
Keys in SQL
o Keys play an important role in the relational database.
o It is used to uniquely identify any record or row of data from the table. It is also used to
establish and identify relationships between tables.
Types of keys:
1. Primary key
o It is the first key used to identify one and only one instance of an entity uniquely. An
entity can contain multiple keys, as we saw in the PERSON table. The key which is most
suitable from those lists becomes a primary key.
o In the EMPLOYEE table, ID can be the primary key since it is unique for each employee.
In the EMPLOYEE table, we can even select License_Number and Passport_Number as
primary keys since they are also unique.
o For each entity, the primary key selection is based on requirements and developers.
2. Candidate key
o A candidate key is an attribute or set of attributes that can uniquely identify a tuple.
o Except for the primary key, the remaining attributes are considered a candidate key. The
candidate keys are as strong as the primary key.
For example: In the EMPLOYEE table, id is best suited for the primary key. The rest of the
attributes, like SSN, Passport_Number, License_Number, etc., are considered a candidate key.
3. Super Key
Super key is an attribute set that can uniquely identify a tuple. A super key is a superset of a
candidate key.
5. Alternate key
There may be one or more attributes or a combination of attributes that uniquely identify each
tuple in a relation. These attributes or combinations of the attributes are called the candidate
keys. One key is chosen as the primary key from these candidate keys, and the remaining
candidate key, if it exists, is termed the alternate key. In other words, the total number of the
alternate keys is the total number of candidate keys minus the primary key. The alternate key
may or may not exist. If there is only one candidate key in a relation, it does not have an alternate
key.
For example, employee relation has two attributes, Employee_Id and PAN_No, that act as
candidate keys. In this relation, Employee_Id is chosen as the primary key, so the other candidate
key, PAN_No, acts as the Alternate key.
6. Composite key
Whenever a primary key consists of more than one attribute, it is known as a composite key.
This key is also known as Concatenated Key.
For example, in employee relations, we assume that an employee may be assigned multiple
roles, and an employee may work on multiple projects simultaneously. So the primary key will
be composed of all three attributes, namely Emp_ID, Emp_role, and Proj_ID in combination. So
these attributes act as a composite key since the primary key comprises more than one attribute.
We can create View using CREATE VIEW statement. A View can be created from a single
table or multiple tables.
Syntax:
StudentDetails
Examples:
What is MySQL?
MySQL is a relational database management system
MySQL is open-source
MySQL is free
MySQL is ideal for both small and large applications
MySQL is very fast, reliable, scalable, and easy to use
MySQL is cross-platform
MySQL is compliant with the ANSI SQL standard
MySQL was first released in 1995
MySQL is developed, distributed, and supported by Oracle Corporation
Nested query
A nested query is a query that has another query embedded within it. The
embedded query is called a sub query.
A sub query typically appears within the WHERE clause of a query. It can
sometimes appear in the FROM clause or HAVING clause.
1. SELECT column_name
2. FROM table_name
3. WHERE column_name expression operator
4. ( SELECT column_name from table_name WHERE ... );
Example
4 Alina 29 UK 6500.00
1. SELECT *
2. FROM EMPLOYEE
3. WHERE ID IN (SELECT ID
4. FROM EMPLOYEE
5. WHERE SALARY > 4500);
4 Alina 29 UK 6500.00
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
ALTER TABLE - ADD Column
Example
ALTER TABLE Customers
DROP COLUMN Email;
To change the data type of a column in a table, use the following syntax:
Example
NULL VALUE
The term NULL in SQL is used to specify that a data value does not exist in the
database. It is not the same as an empty string or a value of zero, and it signifies the
absence of a value or the unknown value of a data field.
Syntax
The basic syntax of NULL while creating a table.