DBMS UNIT-2
DBMS UNIT-2
SYLLABUS
1. Relational Model:
2. Introduction to relational model,
3. concepts of domain,
4. attribute, tuple, relation,
5. importance of null values,
6. constraints (Domain, Key constraints, integrity
constraints) and their importance BASIC SQL:
7. Simple Database schema,
8. data types,
9. table definitions (create, alter),
10. different DML operations (insert, delete,
update),
11. basic SQL querying (select and project) using
where clause,
12. arithmetic & logical operations, SQL
functions(Date and Time, Numeric, String conversion)
web-D Page 1
SEM :- 2-2 (R20) DBMS UNIT-2
1. Tables (Relations):
o The relational model uses a collection of tables to represent both data and the
relationships among that data.
o Each table has multiple columns, and each column has a unique name.
o Tables are also known as relations.
2. Record-Based Model:
o The relational model is an example of a record-based model.
o In record-based models, the database is structured into fixed-format records of
several types.
o Each table contains records of a particular type, and each record type defines a fixed
number of fields (attributes).
3. Terminologies:
o Attribute: Attributes are the properties that define an entity. For example, in a
STUDENT relation, attributes could be ROLL_NO, NAME, ADDRESS, PHONE, and AGE.
o Relation Schema: A relation schema defines the structure of the relation and
represents the name of the relation along with its attributes. For instance, the
relation schema for STUDENT could be STUDENT(ROLL_NO, NAME, ADDRESS,
PHONE, and AGE).
o Tuple: Each row in the relation is known as a tuple. The STUDENT relation contains
multiple tuples, each representing a student record.
o Relation Instance: The set of tuples of a relation at a particular instance of time is
called a relation instance. It can change due to insertions, deletions, or updates in
the database.
web-D Page 2
SEM :- 2-2 (R20) DBMS UNIT-2
o Degree: The number of attributes in the relation is known as the degree. For
example, the STUDENT relation defined above has a degree of 5.
o Cardinality: The number of tuples in a relation is known as cardinality. The STUDENT
relation defined above has a cardinality of 4.
o Column: A column represents the set of values for a particular attribute. For
instance, the column ROLL_NO is extracted from the STUDENT relation.
o NULL Values: A NULL value represents data that is not known or unavailable. It is
represented by a blank space. For example, the PHONE attribute for the student
with ROLL_NO 4 is NULL.
o Relation Key: These keys are used to identify rows uniquely or help in identifying
tables.
Remember, the relational model is widely used, and most current database systems are
based on it. It provides a structured way to organize and manage data efficiently. 🌟🌟
STUDENT Table:
StudentID Name DateOfBirth ContactNumber Email
101 Alice Lee 1998-03-15 123-456-7890 [email protected]
102 Bob Smith 1999-05-20 987-654-3210 [email protected]
COURSE Table:
CourseID CourseName Instructor Credits
201 Math 101 Prof. Johnson 3
202 History 201 Prof. Adams 4
web-D Page 3
SEM :- 2-2 (R20) DBMS UNIT-2
ENROLLMENT Table:
EnrollmentID StudentID CourseID EnrollmentDate
301 101 201 2023-01-15
302 102 202 2023-01-20
BOOK Table:
BookID Title Genre Price
401 “Harry Potter” Fantasy 19.99
402 “Lord of the Rings” Fantasy 24.99
AUTHOR Table:
AuthorID AuthorName
501 J.K. Rowling
502 J.R.R. Tolkien
ORDER Table:
OrderID CustomerID BookID OrderDate
601 701 401 2023-02-10
602 702 402 2023-02-12
These tables represent the relationships between students, courses, books, authors, and
customer orders.
web-D Page 4
SEM :- 2-2 (R20) DBMS UNIT-2
Domain:
A domain represents the set of allowable values for an attribute (column) in a relation
(table).
For example:
o The domain of the “Age” attribute could be integers between 0 and 120.
o The domain of the “Gender” attribute could be {Male, Female, Other}.
Attribute:
web-D Page 5
SEM :- 2-2 (R20) DBMS UNIT-2
Tuple:
Relation:
Examples:
1. Domain Example:
o Attribute: “Age”
o Domain: Integers between 0 and 120
2. Attribute Example:
o Relation: “Student”
o Attributes: “StudentID,” “Name,” “GPA”
3. Tuple Example:
o Relation: “Employee”
o Tuple: EmployeeID = 101, Name = “Alice,” Salary = $60,000
4. Relation Example:
o Relation: “Customers”
o Attributes: “CustomerID,” “Name,” “Email”
o Sample Tuple: CustomerID = 201, Name = “Bob,” Email = “[email protected]”
Remember, understanding these concepts is essential for effective database design and
management. 🌟🌟
-------------------------------------------------------------------------------------------------------------------------------
web-D Page 6
SEM :- 2-2 (R20) DBMS UNIT-2
Remember, understanding null values is crucial for managing data integrity and
semantics within a database. 🌟🌟
------------------------------------------------------------------------------------------------------------
The relational model enforces data integrity and consistency through constraints. These
act like rules that govern the type and organization of data stored within the database.
Domain Constraints: As discussed earlier, domain constraints define the valid set of
values that an attribute (column) can hold. This ensures data consistency by restricting
the type of information stored (e.g., numbers, text) and preventing invalid entries.
web-D Page 7
SEM :- 2-2 (R20) DBMS UNIT-2
Key Constraints: These constraints uniquely identify rows within a table and play a
crucial role in maintaining data integrity. There are two main types:
o Primary Key: A table can have only one primary key, which is a minimal set of
attributes that uniquely identifies each row. This eliminates duplicate records and
o Candidate Key: Any set of attributes that can uniquely identify rows in a table is
considered a candidate key. A table can have multiple candidate keys, but only one is
related tables. They enforce a relationship by referencing the primary key of one table
(parent) in another table (child). A foreign key in the child table must hold a value that
exists in the primary key of the parent table. This prevents orphaned records (entries in
the child table that don't correspond to any valid record in the parent table).
Importance of Constraints:
Constraints are essential for maintaining a clean and reliable database. They:
Optimize Data Retrieval: Unique identifiers like primary keys facilitate efficient data
web-D Page 8
SEM :- 2-2 (R20) DBMS UNIT-2
Databases
SQL (Structured Query Language) is the standard language for interacting with
Create, modify, and delete tables: You can define the structure of your tables with
Insert and update data: SQL provides commands to populate your tables with data
Retrieve data: The powerful SELECT statement allows you to fetch specific data from
Manage database objects: You can use SQL to control user access, create backups,
1. Artists
Name Varchar(255) No
Genre Varchar(50) No
web-D Page 9
SEM :- 2-2 (R20) DBMS UNIT-2
2. Albums
Title Varchar(255) No
Constraints in Action:
The Artist_ID in the Artists table would be a primary key, ensuring each artist has a
unique identifier.
The Artist_ID in the Albums table would be a foreign key referencing the Artists table's
primary key. This enforces referential integrity, guaranteeing that albums are linked to
existing artists.
web-D Page 10
SEM :- 2-2 (R20) DBMS UNIT-2
o You can also create a new table based on an existing one using CREATE
TABLE AS SELECT.
2. Altering Tables:
3. DML Operations:
Address, City)
'Smith';
------------------------------------------------------------------------------------------------------------
web-D Page 11
SEM :- 2-2 (R20) DBMS UNIT-2
1. SELECT Statement:
o The SELECT statement retrieves data from one or more tables.
o Syntax:
o SELECT column1, column2, ...
o FROM table_name;
o Example:
o SELECT FirstName, LastName
o FROM Persons;
2. Projecting Columns:
o Projecting means selecting specific columns to display in the result set.
o Example:
o SELECT FirstName, LastName, City
o FROM Customers;
1. Arithmetic Operators:
o +, -, *, /, % (addition, subtraction, multiplication, division, modulo)
o Example:
o SELECT Quantity * Price AS TotalCost
o FROM OrderDetails;
web-D Page 12
SEM :- 2-2 (R20) DBMS UNIT-2
2. Logical Operators:
o AND, OR, NOT (combine conditions)
o Example:
o SELECT ProductName
o FROM Products
o WHERE CategoryID = 1 AND Price > 50;
SQL Functions
o Example:
o SELECT OrderID, OrderDate
o FROM Orders
o WHERE YEAR(OrderDate) = 2023;
2. Numeric Functions:
o SUM(), AVG(), COUNT(), MAX(), MIN()
o Example:
o SELECT CategoryName, COUNT(ProductID) AS ProductCount
o FROM Categories
o GROUP BY CategoryName;
3. String Conversion Functions:
o CONCAT(), SUBSTRING(), UPPER(), LOWER()
o Example:
o SELECT CONCAT(FirstName, ' ', LastName) AS FullName
o FROM Employees;
web-D Page 13