Professional Documents
Culture Documents
Muhammad Talha
Muhammad Talha
CS & IT Department
Database Systems
MCS & MIT 2nd
Semester: 2nd Subject: Database Systems Total Marks: 10
What is Database?
File system
file system is a software program that manages and organizes the files in a garage medium inside a
computer.
DBMS is a software program for dealing with the database.
Redundant facts may be found in a report system.
It doesn’t offer backup and restoration of files if it is lost.
there is no green query processing in file system.
there's less data consistency in report gadget.
it's miles much less complicated compared to DBMS.
report systems provide much less safety in assessment to DBMS.
it's miles less luxurious than DBMS.
DBMS
DBMS is a software for managing with the database.
In DBMS there is no redundant information.
It gives backup and recovery of data even though it's lost.
green question processing is there in DBMS.
there is more data consistency because of the normalization process
It has more complexity than file system.
DBMS has greater protection mechanisms as compared to document system.
It has a relatively higher cost than file system.
What is the difference between DDL (Data Definition Language) & DML (Data
Manipulation Language)?
DDL DML
DDL stands for Data Definition DML stands Data Manipulation
Language language
DDL is used to create database schema, DML is used to insert, update or update
constraints, users, table record/data
DDL has no classification It is further divided into procedural
DML and non-procedural DML
Its commands are create, drop, rename Its commands are insert, update and
and alter delete
1. Primary Key
A primary key is a column of a table or a set of columns that helps to identify every record present in that
table uniquely. There can be only one primary Key in a table. Also, the primary Key cannot have the same
values repeating for any row. Every value of the primary key has to be different with no repetitions.
The PRIMARY KEY (PK) constraint put on a column or set of columns will not allow them to have any null
values or any duplicates. One table can have only one primary key constraint. Any value in the primary key
cannot be changed by any foreign keys (explained below) which refer to it.
2. Super Key
Super Key is the set of all the keys which help to identify rows in a table uniquely. This means that all those
columns of a table than capable of identifying the other columns of that table uniquely will all be considered
super keys.
Super Key is the superset of a candidate key (explained below). The Primary Key of a table is picked from
the super key set to be made the table’s identity attribute.
3 Candidate Key
Candidate keys are those attributes that uniquely identify rows of a table. The Primary Key of a table is
selected from one of the candidate keys. So, candidate keys have the same properties as the primary keys
explained above. There can be more than one candidate keys in a table.
4. Alternate Key
As stated above, a table can have multiple choices for a primary key; however, it can choose only one. So, all
the keys which did not become the primary Key are called alternate keys.
5. Foreign Key
Foreign Key is used to establish relationships between two tables. A foreign key will require each value in a
column or set of columns to match the Primary Key of the referential table. Foreign keys help to maintain
data and referential integrity.
6. Composite Key
Composite Key is a set of two or more attributes that help identify each tuple in a table uniquely. The
attributes in the set may not be unique when considered separately. However, when taken all together, they
will ensure uniqueness.
7. Unique Key
Unique Key is a column or set of columns that uniquely identify each record in a table. All values will have
to be unique in this Key. A unique Key differs from a primary key because it can have only one null value,
whereas a primary Key cannot have any null values.
Explain operators with examples?
SQL operators are reserved keywords used in the WHERE clause of a SQL statement to perform
arithmetic, logical and comparison operations. Operators act as conjunctions in SQL statements to fulfill
multiple conditions in a statement.
Since, there are different types of operators in SQL, let us understand the same in the next section of this
article on SQL operators.
Types of SQL Operators
Arithmetic Operators
These operators are used to perform operations such as addition, multiplication, subtraction etc.
Operato
Operation Description
r
+ Addition Add values on either side of the operator
– Subtraction Used to subtract the right-hand side value from the left-hand side value
Multiplicatio
* Multiples the values present on each side of the operator
n
/ Division Divides the left-hand side value by the right-hand side value
Divides the left-hand side value by the right-hand side value; and returns the
% Modulus
remainder
Example
SELECT 40 + 20;
SELECT 40 - 20;
SELECT 40 * 20;
SELECT 40 / 20;
SELECT 40 % 20;
Comparison Operators
These operators are used to perform operations such as equal to, greater than, less than etc.
Operato
Operation Description
r
Used to check if the values of both operands are equal or not. If they are equal,
= Equal to
then it returns TRUE.
> Greater than Returns TRUE if the value of left operand is greater than the right operand.
Checks whether the value of left operand is less than the right operand, if yes
< Less than
returns TRUE.
Greater than or Used to check if the left operand is greater than or equal to the right operand, and
>=
equal to returns TRUE, if the condition is true.
Less than or equal
<= Returns TRUE if the left operand is less than or equal to the right operand.
to
Used to check if values of operands are equal or not. If they are not equal then, it
<> or != Not equal to
returns TRUE.
Checks whether the left operand is not greater than the right operand, if yes then
!> Not greater than
returns TRUE.
!< Not less than Returns TRUE, if the left operand is not less than the right operand.
The logical operators are used to perform operations such as ALL, ANY, NOT, BETWEEN etc.
Operator Description
ALL Used to compare a specific value to all other values in a set
ANY Compares a specific value to any of the values present in a set.
IN Used to compare a specific value to the literal values mentioned.
BETWEEN Searches for values within the range mentioned.
AND Allows the user to mention multiple conditions in a WHERE clause.
OR Combines multiple conditions in a WHERE clause.
NOT A negate operators, used to reverse the output of the logical operator.
EXISTS Used to search for the row’s presence in the table.
LIKE Compares a pattern using wildcard operators.
Similar to the ANY operator, and is used compares a specific value to some of the values present
SOME
in a set.
Explain all types of Joins with examples?
A JOIN clause is used to combine rows from two or more tables, based on a related column between them.
SELF-JOIN: A self-join is a regular join, but the table is joined with itself.
(INNER) JOIN: Returns records that have matching values in both 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 in either left or right table
What are relations and also explain all types of relations with examples?
One-to-One Relationship
One-to-One Relationship
Such a relationship exists when each record of one table is related to only one record of the other table.
For example, if there are two entities ‘Person’ (Id, Name, Age, Address) and ‘Passport’ (Passport_id,
Passport_no). So, each person can have only one passport and each passport belongs to only one person.
Such a relationship is not very common. However, such a relationship is used for security purposes. In the
above example, we can easily store the passport id in the ‘Person’ table only. But, we make another table for
the ‘Passport’ because Passport number may be sensitive data and it should be hidden from certain users. So,
by making a separate table we provide extra security that only certain database users can see it.
One-to-Many or Many-to-One Relationship
Such a relationship exists when each record of one table can be related to one or more than one record of the
other table. This relationship is the most common relationship found. A one-to-many relationship can also be
said as a many-to-one relationship depending upon the way we view it.
For example, if there are two entity type ‘Customer’ and ‘Account’ then each ‘Customer’ can have more than
one ‘Account’ but each ‘Account’ is held by only one ‘Customer’. In this example, we can say that each
Customer is associated with many Accounts. So, it is a one-to-many relationship. But if we see it the other way
i.e. many Accounts is associated with one Customer then we can say that it is a many-to-one relationship.
Many-to-Many Relationship
Such a relationship exists when each record of the first table can be related to one or more than one record of
the second table and a single record of the second table can be related to one or more than one record of the
first table. A many-to-many relationship can be seen as a two one-to-many relationship which is linked by a
'linking table' or 'associate table'. The linking table links two tables by having fields which are the primary key
of the other two tables. We can understand this with the following example.
Example: If there are two entity type ‘Customer’ and ‘Product’ then each customer can buy more than one
product and a product can be bought by many different customers.
Now, to understand the concept of the linking table here, we can have the ‘Order’ entity as a linking table
which links the ‘Customer’ and ‘Product’ entity. We can break this many-to-many relationship in two one-to-
many relationships. First, each ‘Customer’ can have many ‘Order’ whereas each ‘Order’ is related to only one
‘Customer’. Second, each ‘Order’ is related only one Product wheres there can many orders for the same
Product.
In the above concept of linking can be understood with the help of taking into consideration all the attributes
of the entities 'Customer', 'Order' and 'Product'. We can see that the primary key of both 'Customer' and
'Product' entity are included in the linking table i.e 'Order' table. These key act as foreign keys while referring
to the respective table from the 'Order' table.
What is Normalization and also explain all types of normalization with examples?
Normalization is the process of data organization in the form of tables. The normalization process includes
dividing data into tables and making relationships among them.
there are different types of normalization but some of most commonly used normalization is
given below: -
• First Normal Form (1NF)
• Second Normal Form (2NF)
• Third Normal Form (3NF)
In the above table, we can clearly see that the Phone Number column has two values. Thus it violated the
1st NF. Now if we apply the 1st NF to the above table we get the below table as the result.
By this, we have achieved atomicity and also each and every column has unique values.
This table has a composite primary key Employee ID, Department ID. The non-key attribute is Office
Location. In this case, Office Location only depends on Department ID, which is only part of the primary
key. Therefore, this table does not satisfy the second Normal Form.
To bring this table to Second Normal Form, we need to break the table into two parts. Which will give us
the below tables:
As you can see, we have removed the partial functional dependency that we initially had. Now, in the
table, the column Office Location is fully dependent on the primary key of that table, which is
Department ID.
Third Normal Form (3NF)
The same rule applies as before i.e., the table has to be in 2NF before proceeding to 3NF. The other
condition is there should be no transitive dependency for non-prime attributes. That means non-prime
attributes (which doesn’t form a candidate key) should not be dependent on other non-prime attributes in
a given table. So a transitive dependency is a functional dependency in which X → Z (X determines Z)
indirectly, by virtue of X → Y and Y → Z (where it is not the case that Y → X)
Let’s understand this more clearly with the help of an example:
The same rule applies as before i.e., the table has to be in 2NF before proceeding to 3NF. The other
condition is there should be no transitive dependency for non-prime attributes. That means non-prime
attributes (which doesn’t form a candidate key) should not be dependent on other non-prime attributes in
a given table.
As you can see from the above tables all the non-key attributes are now fully functional dependent only
on the primary key. In the first table, columns Student Name, Subject ID and Address are only
dependent on Student ID. In the second table, Subject is only dependent on Subject ID.
Query:
CREATE VIEW DetailsView AS
SELECT NAME, ADDRESS
FROM StudentDetails
WHERE S_ID < 5;
To see the data in the View, we can query the view in the
same manner as we query a table.
Having clause
It returns row when result matched with the given condition only. It is used with the combination with
group clause. It restricts the data on the group rather than individual record
Example
SELECT Country, COUNT(Id) AS Count
FROM Supplier
GROUP BY Country
HAVING COUNT(Id) > 2
Note:
Complete this assignment in your own words and plagiarism will not be acceptable otherwise marks will be
deducted.