DBMS-Unit2
DBMS-Unit2
Keys in DBMS
KEYS in DBMS is an attribute or set of attributes which helps you to identify a row(tuple) in a
relation(table). They allow you to find the relation between two tables. Keys help you uniquely
identify a row in a table by a combination of one or more columns in that table.
• A superkey is a group of single or multiple keys which identifies rows in a table. Super
Key is a superset of Candidate key.
• All of the following sets of super key are able to uniquely identify a row of the employee
table.
• {Emp_SSN}
• {Emp_Number}
• {Emp_SSN, Emp_Number}
• {Emp_SSN, Emp_Name}
• {Emp_SSN, Emp_Number, Emp_Name}
• {Emp_Number, Emp_Name}
Candidate Key:
CANDIDATE KEY in SQL is a set of attributes that uniquely identify tuples in a table.
Candidate Key is a super key with no repeated attributes. The Primary key should be selected
from the candidate keys. Every table must have at least a single candidate key. A table can have
multiple candidate keys but only a single primary key.
A primary key is selected from the set of candidate keys. That means we can either have Emp_Id
or Emp_Number as primary key. The decision is made by DBA.
Primary Key:
PRIMARY KEY in DBMS is a column or group of columns in a table that uniquely identify
every row in that table. The Primary Key can’t be a duplicate meaning the same value can’t
appear more than once in the table. A table cannot have more than one primary key.
Foreign key:
FOREIGN KEY is a column that creates a relationship between two tables. The purpose of
Foreign keys is to maintain data integrity and allow navigation between two different instances
of an entity. It acts as a cross-reference between two tables as it references the primary key of
another table.
Composite Key:
Sometimes, a table might not have a single column/attribute that uniquely identifies all the
records of a table. To uniquely identify rows of a table, a combination of two or more
columns/attributes can be used. It still can give duplicate values in rare cases. So, we need to
find the optimal set of attributes that can uniquely identify rows in a table.
It acts as a primary key if there is no primary key in a table
Two or more attributes are used together to make a composite key.
Different combinations of attributes may give different accuracy in terms of identifying
the rows uniquely.
Alternate Key:
The candidate key other than the primary key is called an alternate key.
All the keys which are not primary keys are called alternate keys.
It is a secondary key.
It contains two or more fields to identify two or more records.
These values are repeated.
Surrogate key:
SURROGATE KEYS is An artificial key which aims to uniquely identify each record is called a
surrogate key. This kind of partial key in dbms is unique because it is created when you don’t
have any natural primary key.
1. Domain Constraints
1. Every domain must contain atomic values(smallest indivisible units) which means
composite and multi-valued attributes are not allowed.
2. We perform a datatype check here, which means when we assign a data type to a column
we limit the values that it can contain. Eg. If we assign the datatype of attribute age as int,
we can’t give it values other than int datatype.
Example:
EID Name Phone
123456789
01 Bikash Dutta
234456678
Explanation: In the above relation, Name is a composite attribute and Phone is a multi-values
attribute, so it is violating domain constraint.
2. Entity Integrity Constraint
Entity Integrity Constraint is used to ensure that the primary key cannot be null. A primary key is
used to identify individual records in a table and if the primary key has a null value, then we
can't identify those records. There can be null values anywhere in the table except the primary
key column.
Example:
EID Name Phone
01 Bikash 9000900099
02 Paul 600000009
Explanation: In the above relation, EID is made the primary key, and the primary key can’t
take NULL values but in the third tuple, the primary key is null, so it is violating Entity
Integrity constraints.
In the Referential integrity constraints, if a foreign key in Table 1 refers to the Primary Key of
Table 2, then every value of the Foreign Key in Table 1 must be null or be available in Table 2.
4. Key constraints:
Keys are the set of entities that are used to identify an entity within its entity set uniquely. There
could be multiple keys in a single entity set, but out of these multiple keys, only one key will be
the primary key. A primary key can only contain unique and not null values in the relational
database table.
NOT NULL:
NOT NULL constraint makes sure that a column does not hold NULL value. When we
don’t provide value for a particular column while inserting a record into a table, it takes
NULL value by default. By specifying NULL constraint, we can be sure that a particular
column(s) cannot have NULL values.
Example:
CREATE TABLE STUDENT(
ID INT NOT NULL,
NAME VARCHAR (35) NOT NULL,
AGE INT NOT NULL,
PRIMARY KEY (ID)
);
UNIQUE:
UNIQUE Constraint enforces a column or set of columns to have unique values. If a column has
a unique constraint, it means that particular column cannot have duplicate values in a table.
CHECK:
This constraint is used for specifying range of values for a particular column of a table.
When this constraint is being set on a column, it ensures that the specified column must
have the value falling in the specified range.
DEFAULT:
The DEFAULT constraint provides a default value to a column when there is no value
provided while inserting a record into a table.
PRIMARY KEY:
Primary key uniquely identifies each record in a table. It must have unique values and cannot
contain nulls. The primary key has automatically UNIQUE and NOT NULL constraints applied
to it. It is usually used to index the table or uniquely identify each tuple in the table.
We can say that:
PRIMARY KEY CONSTRAINT = UNIQUE CONSTRAINT + NOTNULL CONSTRAINT
FOREIGN KEY Constraint:
Foreign keys are the columns of a table that points to the primary key of another table. They act
as a cross-reference between tables. So FOREIGN KEY constraint prevents operations that can
destroy the link between the tables. Foreign Keys are used in situations where we do not want
the deletion of data from one table to hamper the data in the other table.
One important thing to note here is that - the Foreign Key of one table points to or references
the Primary Key of the second table. So, when we delete data from the main table that points to
data in the other table, we will be shown the error - Record in child table exists.
Querying Relational Data in DBMS
A relational database query is a question about the data, and the answer consists of a new relation
containing the result. For example, we might want to find all students AGE less than 18 or all
students enrolled in perticular course.
The SELECT statement is used to fetch the data from a database table which returns this data in
the form of a result table. These result tables are called result-sets.
If you want to select all the fields available in the table, use the following syntax:
Syntax :
SELECT * FROM table_name;
The symbol ´*´ means that we retain all fields of selected tuples in the result.
We can retrieve rows corresponding to students who are younger than 18 withthe following SQL
query:
SELECT * FROM Students WHERE age < 18;
The condition age < 18 in the WHERE clause specifies that we want to select only tuples in
which the age field has a value less than 18.
In addition to selecting a subset of tuples, a query can extract a subset of the fields of each
selected tuple. we can compute the student_id and First_name of students who are younger than
18 with the following query:
SELECT ID,FirstName FROM Students WHERE age < 18;
SQL Aliases
Aliases are the temporary names given to tables or columns. An alias is created with
the AS keyword.
Alias Column Syntax :
Example:
Example:
• Inserting a row in the view takes the same syntax as we use to insert a row in a simple
table.
Syntax:
• INSERT INTO ViewName(column1, column2,..) VALUES(value1, value2,..);
We can use the CREATE OR REPLACE VIEW statement to add or remove fields from a
view.
Syntax:
For example, if we want to update the view MarksView and add the field AGE to this View
from StudentMarks Table, we can do this as:
The WITH CHECK OPTION clause in SQL is a very useful clause for views. It is applicable
to an updatable view. If the view is not updatable, then there is no meaning of including this
clause in the CREATE VIEW statement.
The WITH CHECK OPTION clause is used to prevent the insertion of rows in the view
where the condition in the WHERE clause in CREATE VIEW statement is not satisfied.
If we have used the WITH CHECK OPTION clause in the CREATE VIEW statement, and
if the UPDATE or INSERT clause does not satisfy the conditions then they will return an
error.
Example: In the below example we are creating a View SampleView from StudentDetails
Table with WITH CHECK OPTION clause.
CREATE VIEW SampleView AS SELECT S_ID, NAME FROM StudentDetails WHERE NAME IS NOT
NULL WITH CHECK OPTION;
In this View if we now try to insert a new row with null value in the NAME column then it
will give an error because the view is created with the condition for NAME column as NOT
NULL.
Select (σ):
• It is used to retrieve tuples(rows) from the table where the given condition is satisfied.
• It is a unary operator means it requires only one operand.
• Where σ is used to represent SELECTION,R is used to represent RELATION
Example:
Query Used :
σ Name and Age>21 (Student_Details)
Ex 2:
Query:
σ Customer_City="Agra" (CUSTOMER)
Project (∏):
Example:
Query Used :
πMarks(Student_Details)
Example 2:
Query:
∏ Customer_Name, Customer_City (CUSTOMER)
Output
Union (∪):
Union operation is done by Union Operator which is represented by "union"(∪).
It is the same as the union operator from set theory, i.e., it selects all tuples from both relations
but with the exception that for the union of two relations/tables both relations must have the
same set of Attributes.
• It is a binary operator as it requires two operands.
• Also, two things need to keep in mind while applying union operation are :
Both the relations compulsory to have same number of attributes.
Both the relations compulsory to have same domain for attributes.
Consider the two tables with relations X1(Name, Age) and X2(Name, Age).
• If we wish to apply the union operation, then it can be done by
Example:
Query:
Example:
Query:
RXS
Intersection Operator (∩):
Intersection operator is denoted by ∩ symbol and it is used to select common rows (tuples) from
two tables (relations).
Syntax of Intersection Operator (∩)
table_name1 ∩ table_name2
Query:
Inner Join:
This is a simple JOIN in which the result is based on matched data as per the equality condition
specified in the SQL query.
Inner Join Syntax is,
Example;
Natural Join is a type of Inner join which is based on column having same name and same
datatype present in both the tables to be joined.
The syntax for Natural Join is,
Example:
Output:
OUTER JOIN
Outer Join is based on both matched and unmatched data. Outer Joins subdivide further into,
1. Left Outer Join
2. Right Outer Join
3. Full Outer Join
LEFT Outer Join
The left outer join returns a resultset table with the matched data from the two tables and then
the remaining rows of the left table and null from the right table's columns.
Syntax:
Query:
SELECT * FROM song LEFT JOIN artist ON song.artist_id=artist.artist_id
The right outer join returns a resultset table with the matched data from the two tables being
joined, then the remaining rows of the right table and null for the remaining left table's columns.
Syntax for Right Outer Join is,
Output:
The full outer join returns a resultset table with the matched data of two table then remaining
rows of both left table and then the right table.
Syntax:
Query:
SELECT * FROM song FULL JOIN artist ON song.artist_id=artist.artist_id
Output:
Relational Calculus
Procedural Language - Those Languages which clearly define how to get the required results
from the Database are called Procedural Language.
Relational algebra is a Procedural Language.
Declarative Language - Those Language that only cares about What to get from the database
without getting into how to get the results are called Declarative Language.
Relational Calculus is a Declarative Language.
Relational calculus is a non-procedural query language, and instead of algebra, it uses
mathematical predicate calculus.
Relational Calculus in database management system (DBMS) is all about "What you want ?".
Relational calculus does not tell us how to get the results from the Database, but it just cares
about what we want.
The relational calculus tells what to do but never explains how to do.
Relational Calculus is of Two Types:
• Tuple Relational Calculus (TRC)
• Domain Relational Calculus (DRC)
Tuple Relational Calculus (TRC):
• Tuple Relational Calculus (TRC) is a non-procedural query language used in relational
database management systems (RDBMS) to retrieve data from tables.
• TRC is a declarative language, meaning that it specifies what data is required from
the database, rather than how to retrieve it.
• Tuple Relational Calculus in DBMS uses a tuple variable (t) that goes to each row of the
table and checks if the predicate is true or false for the given row.
• Depending on the given predicate condition, it returns the row or part of the row.
• Table: Student
Last_Name
---------
Singh
• Query to display all the details of students where Last name is ‘Singh’
First_Name Age
---------- ----
Ajeet 30
Chaitanya 31
Carl 28