0% found this document useful (0 votes)
2 views

DBMS Unit 1(Notes)

The document provides an overview of Database Management Systems (DBMS), focusing on the Relational Model and SQL concepts. It covers key terminologies, constraints, and types of keys such as primary, foreign, and candidate keys, along with their functionalities and relationships in a relational database. Additionally, it explains the significance of referential integrity and how to manage constraints during data operations.

Uploaded by

tvganesann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DBMS Unit 1(Notes)

The document provides an overview of Database Management Systems (DBMS), focusing on the Relational Model and SQL concepts. It covers key terminologies, constraints, and types of keys such as primary, foreign, and candidate keys, along with their functionalities and relationships in a relational database. Additionally, it explains the significance of referential integrity and how to manage constraints during data operations.

Uploaded by

tvganesann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Database Management System

Database Management System

SQL Concepts
Unit-1

RELATIONAL MODEL AND RELATIONAL ALGEBRA:


Relational Model Concepts; Relational Model Constraints and Relational Database Schemas; Update Operations, Transactions
and Dealing with Constraint Violations.

SQL:
SQL Data Definition and Data Types; Specifying Constraints in SQL; Schema Change Statements in SQL; Basic Queries in
SQL; More Complex SQL Queries, Insert, Delete and Update Statements in SQL; Specifying Constraints as Assertions and
Triggers; Views in SQL; Additional Features of SQL.

Relational Model was proposed by E.F. Codd to model data in the form of relations or tables. After designing the
conceptual model of Database using ER diagram, we need to convert the conceptual model in the relational model
which can be implemented using any RDMBS languages like Oracle SQL, MySQL etc
Relational Model
Relational Model represents how data is stored in Relational Databases. A relational database
stores data in the form of relations (tables). Consider a relation STUDENT with attributes
ROLL_NO, NAME, ADDRESS, PHONE and AGE shown in Table 1.

STUDENT
ROLL_NO NAME ADDRESS PHONE AGE
1 RAM DELHI 9455123451 18
2 RAMESH GURGAON 9652431543 18
3 SUJIT ROHTAK 9156253131 20
4 SURESH DELHI 18

IMPORTANT TERMINOLOGIES
1. Attribute: Attributes are the properties that define a relation. e.g.; ROLL_NO, NAME
2. Relation Schema: A relation schema represents name of the relation with its attributes.
e.g.; STUDENT (ROLL_NO, NAME, ADDRESS, PHONE and AGE) is relation schema
for STUDENT. If a schema has more than 1 relation, it is called Relational Schema.

Page 1
Database Management System

3. Tuple: Each row in the relation is known as tuple. The above relation contains 4 tuples,
one of which is shown as:
1 RAM DELHI 9455123451 18

4. Relation Instance: The set of tuples of a relation at a particular instance of time is called
as relation instance. Table 1 shows the relation instance of STUDENT at a particular time.
It can change whenever there is insertion, deletion or updation in the database.

Page 2
Database Management System

5. Degree: The number of attributes in the relation is known as degree of the relation.
The STUDENT relation defined above has degree 5.
6. Cardinality: The number of tuples in a relation is known as cardinality.
The STUDENTrelation defined above has cardinality 4.
7. Column: Column represents the set of values for a particular attribute. The
column ROLL_NO is extracted from relation STUDENT.
ROLL_NO
1
2
3
4

8. NULL Values: The value which is not known or unavailable is called NULL value. It is
represented by blank space. e.g.; PHONE of STUDENT having ROLL_NO 4 is NULL.

Constraints in Relational Model


While designing Relational Model, we define some conditions which must hold for data present
in database are called Constraints. These constraints are checked before performing any
operation (insertion, deletion and updation) in database. If there is a violation in any of
constrains, operation will fail. Different types of constraints are:

1. Domain Constraints

2. Key constraints and null values

3. Referential integrity constraints

4. Entity integrity constraints

Domain Constraints: These are attribute level constraints. An attribute can only take values
which lie inside the domain range. e.g,; If a constrains AGE>0 is applied on STUDENT relation,
inserting negative value of AGE will result in failure.

Key Integrity: Every relation in the database should have atleast one set of attributes which

Page 3
Database Management System

defines a tuple uniquely. Those set of attributes is called key. e.g.; ROLL_NO in STUDENT is a
key. No two students can have same roll number. So a key has two properties:
 It should be unique for all tuples.
 It can’t have NULL values. Referential Integrity: When one attribute of a relation can only take
values from other attribute of same relation or any other relation, it is called referential integrity. Let us
suppose we have 2 relations
STUDENT
ROLL_NO NAME ADDRESS PHONE AGE BRANCH_CODE
1 RAM DELHI 9455123451 18 CS
2 RAMESH GURGAON 9652431543 18 CS
3 SUJIT ROHTAK 9156253131 20 ECE
4 SURESH DELHI 18 IT

BRANCH
BRANCH_CODE BRANCH_NAME
CS COMPUTER SCIENCE
IT INFORMATION TECHNOLOGY
ECE ELECTRONICS AND COMMUNICATION ENGINEERING
CV CIVIL ENGINEERING

BRANCH_CODE of STUDENT can only take the values which are present in
BRANCH_CODE of BRANCH which is called referential integrity constraint. The relation
which is referencing to other relation is called REFERENCING RELATION (STUDENT in this
case) and the relation to which other relations refer is called REFERENCED RELATION
(BRANCH in this case).

Referential Integrity constraint

A referential integrity constraint is also known as foreign key constraint. A foreign key is a key whose values are
derived from the Primary key of another table.

The table from which the values are derived is known as Master or Referenced Table and the Table in which values
are inserted accordingly is known as Child or Referencing Table, In other words, we can say that the table
containing the foreign key is called the child table, and the table containing the Primary key/candidate key is

Page 4
Database Management System

called the referenced or parent table. When we talk about the database relational model, the candidate key can be
defined as a set of attribute which can have zero or more attributes.

The syntax of the Master Table or Referenced table is:

1. CREATE TABLE Student (Roll int PRIMARY KEY, Name varchar(25) , Course varchar(10) );

Here column Roll is acting as Primary Key, which will help in deriving the value of foreign key in the child table.

27.4M
532
History of Java

The syntax of Child Table or Referencing table is

CREATE TABLE Subject (Roll int references Student, SubCode int, SubName varchar(10) );

In the above table, column Roll is acting as Foreign Key, whose values are derived using the Roll value of Primary
key from Master table.

Foreign Key Constraint OR Referential Integrity constraint.

Page 5
Database Management System

There are two referential integrity constraint:

Insert Constraint: Value cannot be inserted in CHILD Table if the value is not lying in MASTER Table

Delete Constraint: Value cannot be deleted from MASTER Table if the value is lying in CHILD Table

Suppose you wanted to insert Roll = 05 with other values of columns in SUBJECT Table, then you will immediately
see an error "Foreign key Constraint Violated" i.e. on running an insertion command as:

Insert into SUBJECT values(5, 786, OS); will not be entertained by SQL due to
Insertion Constraint ( As you cannot insert value in a child table if the value is not lying in the master table,
since Roll = 5 is not present in the master table, hence it will not be allowed to enter Roll = 5 in child table )

Similarly, if you want to delete Roll = 4 from STUDENT Table, then you will immediately see an error "Foreign
key Constraint Violated" i.e. on running a deletion command as:

Delete from STUDENT where Roll = 4; will not be entertained by SQL due to
Deletion Constraint. ( As you cannot delete the value from the master table if the value is lying in the child
table, since Roll = 5 is present in the child table, hence it will not be allowed to delete Roll = 5 from the master table,
lets if somehow we managed to delete Roll = 5, then Roll = 5 will be available in child table which will ultimately
violate insertion constraint. )

ON DELETE CASCADE.

As per deletion constraint: Value cannot be deleted from the MASTER Table if the value is lying in CHILD Table.
The next question comes can we delete the value from the master table if the value is lying in the child table without
violating the deletion constraint? i.e. The moment we delete the value from the master table the value corresponding
to it should also get deleted from the child table.

The answer to the above question is YES, we can delete the value from the master table if the value is lying in the
child table without violating the deletion constraint, we have to do slight modification while creating the child table,
i.e. by adding on delete cascade.

TABLE SYNTAX

CREATE TABLE Subject (Roll int references Student on delete cascade, SubCode int, SubName varchar(10) );

In the above syntax, just after references keyword( used for creating foreign key), we have added on delete cascade,
by adding such now, we can delete the value from the master table if the value is lying in the child table without
violating deletion constraint. Now if you wanted to delete Roll = 5 from the master table even though Roll = 5 is
lying in the child table, it is possible because the moment you give the command to delete Roll = 5 from the master
table, the row having Roll = 5 from child table will also get deleted.

Page 6
Database Management System

The above two tables STUDENT and SUBJECT having four values each are shown, now suppose you are looking to
delete Roll = 4 from STUDENT( Master ) Table by writing a SQL command: delete from STUDENT
where Roll = 4;

The moment SQL execute the above command the row having Roll = 4 from SUBJECT( Child ) Table will also get
deleted, The resultant STUDENT and SUBJECT table will look like:

Page 7
Database Management System

From the above two tables STUDENT and SUBJECT, you can see that in both the table Roll = 4 gets deleted at one
go without violating deletion constraint.

KEYS

DBMS has following seven types of Keys each have their different functionality:

 Super Key
 Primary Key
 Candidate Key
 Alternate Key
 Foreign Key
 Compound Key
 Composite Key
 Surrogate Key

Page 8
Database Management System

Super key

A super key is a group of single or multiple keys which identifies rows in a table. A Super key
may have additional attributes that are not needed for unique identification.

Example:

EmpSSN EmpNum EmpName


9812345098 AB05 Shown
9876512345 AB06 Roslyn
199937890 AB07 James

In the above-given example, EmpSSN and EmpNum name are superkeys.

Primary Key

A column or group of columns in a table which helps us to uniquely identifies every row in that
table is called a primary key. This DBMS can't be a duplicate. The same value can't appear more
than once in the table.

Rules for defining Primary key:

 Two rows can't have the same primary key value


 It must for every row to have a primary key value.
 The primary key field cannot be null.
 The value in a primary key column can never be modified or updated if any foreign key
refers to that primary key.

Example:

In the following example, <code>StudID</code> is a Primary Key.

StudID RollNo FirstName LastName Email


1 11 Tom Price [email protected]

Page 9
Database Management System

2 12 Nick Weight [email protected]


3 13 Dana Natan [email protected]

Alternate key

All the keys which are not primary key are called an alternate key. It is a candidate key which is
currently not the primary key. However, A table may have single or multiple choices for the
primary key.

Example: In this table.

StudID, Roll No, Email are qualified to become a primary key. But since StudID is the primary
key, Roll No, Email becomes the alternative key.

StudID RollNo FirstName LastName Email


1 11 Tom Price [email protected]
2 12 Nick Weight [email protected]
3 13 Dana Natan [email protected]

Candidate Key

A super key with no repeated attribute is called candidate key.

The Primary key should be selected from the candidate keys. Every table must have at least a
single candidate key.

Properties of Candidate key:

 It must contain unique values


 Candidate key may have multiple attributes
 Must not contain null values

Page 10
Database Management System

 It should contain minimum fields to ensure uniqueness


 Uniquely identify each record in a table

Example: In the given table Stud ID, Roll No, and email are candidate keys which help us to
uniquely identify the student record in the table.

StudID RollNo FirstName LastName Email


1 11 Tom Price [email protected]
2 12 Nick Weight [email protected]
3 13 Dana Natan [email protected]

Foreign key

A foreign key is a column which is added to create a relationship with another table. Foreign
keys help us to maintain data integrity and also allows navigation between two different
instances of an entity. Every relationship in the model needs to be supported by a foreign key.

DeptCode DeptName Teacher ID Fname Lname

001 Science B002 David Warner


002 English B017 Sara Joseph

Page 11
Database Management System

005 Computer B009 Mike Brunton

Example:

In this example, we have two table, teach and department in a school. However, there is no way
to see which search work in which department.

In this table, adding the foreign key in Deptcode to the Teacher name, we can create a
relationship between the two tables.

Teacher ID DeptCode Fname Lname

B002 002 David Warner

B017 002 Sara Joseph

B009 001 Mike Brunton

This concept is also known as Referential Integrity.

Compound key

Compound key has many fields which allow you to uniquely recognize a specific record. It is
possible that each column may be not unique by itself within the database. However, when
combined with the other column or columns the combination of composite keys become unique.

Example:

OrderNo PorductID Product Name Quantity

B005 JAP102459 Mouse 5

B005 DKT321573 USB 10

B005 OMG446789 LCD Monitor 20

B004 DKT321573 USB 15

Page 12
Database Management System

B002 OMG446789 Laser Printer 3

In this example, OrderNo and ProductID can't be a primary key as it does not uniquely identify a
record. However, a compound key of Order ID and Product ID could be used as it uniquely
identified each record.

Composite key

A key which has multiple attributes to uniquely identify rows in a table is called a composite
key. The difference between compound and the composite key is that any part of the compound
key can be a foreign key, but the composite key may or maybe not a part of the foreign key.

Difference between Primary key & foreign key

Page 13
Database Management System

Primary Key Foreign Key

Helps you to uniquely identify a record It is a field in the table that is the primary key
in the table. of another table.

Primary Key never accept null values. A foreign key may accept multiple null values.

Primary key is a clustered index and A foreign key cannot automatically create an
data in the DBMS table are physically index, clustered or non-clustered. However,
organized in the sequence of the you can manually create an index on the
clustered index. foreign key.
You can have the single Primary key in You can have multiple foreign keys in a table.
a table.

Relational Database schema

A database is a collection of interrelated data files or structures. It is designed to meet the


various information needs of the organization. Also, it is integrated and shared. Thus, a
relational database schema is an arrangement of relation states in such a manner that every
relational database state fulfills the integrity constraints set on a relational database schema.

As we know that a relational database schema is an arrangement of integrity constraints.

Page 14
Database Management System

Page 15
Update Operations, Transactions, and Dealing with Constraint Violations
Database Management System

There are three basic operations that can change the states of relations in the data-base: Insert,
Delete, and Update (or Modify). They insert new data, delete old data, or modify existing
data records. Insert is used to insert one or more new tuples in a relation, Delete is used to
delete tuples, and Update (or Modify) is used to change the values of some attributes in
existing tuples. Whenever these operations are applied, the integrity constraints specified on
the relational database schema should not be violated.
1. The Insert Operation
The Insert operation provides a list of attribute values for a new tuple t that is to be inserted
into a relation R.
Insert can violate any of the four types of constraints
 Domain constraints can be violated if an attribute value is given that does not appear
in the corresponding domain or is not of the appropriate data type.
 Key constraints can be violated if a key value in the new tuple t already exists in
another tuple in the relation r(R).
 Entity integrity can be violated if any part of the primary key of the new
tuple t is NULL.
 Referential integrity can be violated if the value of any foreign key in t refers to a
tuple that does not exist in the referenced relation
Operation:

Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, NULL, ‘1960-04-05’, ‘6357 Windy Lane, Katy, TX’, F,
28000, NULL, 4> into EMPLOYEE.

Result: This insertion violates the entity integrity constraint (NULL for the primary key Ssn),
so it is rejected.

Operation:

Insert <‘Alicia’, ‘J’, ‘Zelaya’, ‘999887777’, ‘1960-04-05’, ‘6357 Windy Lane, Katy, TX’, F,
28000, ‘987654321’, 4> into EMPLOYEE.

Result: This insertion violates the key constraint because another tuple with the same Ssn
value already exists in the EMPLOYEE relation, and so it is rejected.

2. The Delete Operation


The Delete operation can violate only referential integrity. This occurs if the tuple being
deleted is referenced by foreign keys from other tuples in the database. To specify deletion, a
condition on the attributes of the relation selects the tuple (or tuples) to be deleted. Here are
some examples.
Operation:
Delete the WORKS_ON tuple with Essn = ‘999887777’ and Pno = 10.
Result: This deletion is acceptable and deletes exactly one tuple. Page 16
Database Management System

Operation:
Delete the EMPLOYEE tuple with Ssn = ‘999887777’.
Result: This deletion is not acceptable, because there are tuples in WORKS_ON that refer to
this tuple. Hence, if the tuple in EMPLOYEE is Deleted, referential integrity violations will
result.
3. The Update Operation

The Update (or Modify) operation is used to change the values of one or more attributes in a
tuple (or tuples) of some relation R. It is necessary to specify a condition on the attributes of
the relation to select the tuple (or tuples) to be modified. Here are some examples.

Operation:

Update the salary of the EMPLOYEE tuple with Ssn = ‘999887777’ to 28000.
Result: Acceptable.
Operation:

Update the Dno of the EMPLOYEE tuple with Ssn = ‘999887777’ to 1.


Result: Acceptable.

Operation:

Update the Dno of the EMPLOYEE tuple with Ssn = ‘999887777’ to 7.


Result: Unacceptable, because it violates referential integrity.

Operation:

Update the Ssn of the EMPLOYEE tuple with Ssn = ‘999887777’ to ‘987654321’.

Page 17
Database Management System

Result: Unacceptable, because it violates primary key constraint by repeating a value that
already exists as a primary key in another tuple; it violates refer-ential integrity constraints
because there are other relations that refer to the existing value of Ssn.
SQL statements:

SELECT command (Retrieval command)

Select statements are used to retrieve data from SQL tables. The Select statement illustrated
below retrieves all of the columns and rows from the named table.

Syntax:

Select *
from tablename;

Explanation:

 A Select statement is a SQL statement that begins with the word "select."
 Select statements are used to retrieve data from SQL tables.
 An asterisk after the word "select" means retrieve all fields (columns).
 The name of the table from which you are retrieving data is specified in the From
clause.
 Use a semicolon to signify the end of a SQL statement.
 The SQL 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.
 Syntax
 The basic syntax of the SELECT statement is as follows −
 SELECT column1, column2, columnN FROM table_name;
 Here, column1, column2... are the fields of a table whose values you want to
fetch. If you want to fetch all the fields available in the field, then you can use
the following syntax.
 SELECT * FROM table_name;
 Example
 Consider the CUSTOMERS table having the following records −
 +----+----------+-----+-----------+----------+
 | ID | NAME | AGE | ADDRESS | SALARY |
 +----+----------+-----+-----------+----------+
 | 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
 | 2 | Khilan | 25 | Delhi | 1500.00 |
 | 3 | kaushik | 23 | Kota | 2000.00 |
 | 4 | Chaitali | 25 | Mumbai | 6500.00 |
 | 5 | Hardik | 27 | Bhopal | 8500.00 |
 | 6 | Komal | 22 | MP | 4500.00 |
 | 7 | Muffy | 24 | Indore | 10000.00 |
Page 18
 +----+----------+-----+-----------+----------+
Database Management System

 The following code is an example, which would fetch the ID, Name and
Salary fields of the customers available in CUSTOMERS table.
 SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS;

 This would produce the following result −


 +----+----------+----------+
 | ID | NAME | SALARY |
 +----+----------+----------+
 | 1 | Ramesh | 2000.00 |
 | 2 | Khilan | 1500.00 |
 | 3 | kaushik | 2000.00 |
 | 4 | Chaitali | 6500.00 |
 | 5 | Hardik | 8500.00 |
 | 6 | Komal | 4500.00 |
 | 7 | Muffy | 10000.00 |
 +----+----------+----------+
 If you want to fetch all the fields of the CUSTOMERS table, then you should
use the following query.
 SQL> SELECT * FROM CUSTOMERS;

 This would produce the result as shown below.


 +----+----------+-----+-----------+----------+
 | ID | NAME | AGE | ADDRESS | SALARY |
 +----+----------+-----+-----------+----------+
 | 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
 | 2 | Khilan | 25 | Delhi | 1500.00 |
 | 3 | kaushik | 23 | Kota | 2000.00 |
 | 4 | Chaitali | 25 | Mumbai | 6500.00 |
 | 5 | Hardik | 27 | Bhopal | 8500.00 |
 | 6 | Komal | 22 | MP | 4500.00 |
 | 7 | Muffy | 24 | Indore | 10000.00 |
 +----+----------+-----+-----------+----------+

Practice the following commands with example:

 Insert, delete and update statements


 Drop command
 Alter command
 Select – From-Where structure
 Aliasing
 Group By clause
 Order By Clause

 Distinct
 Pattern Matching
 Nested Queries:
 Aggregate functions: Page 19
Database Management System

SQL Data Types:


Data types are used to represent the nature of the data that can be stored in the
database table.

Data types mainly classified into three categories for every database.

o String Data types


o Numeric Data types
o Date and time Data types
o Binary Data types
o Boolean data types

o Numeric data types: These are used to store numeric values.


Examples include INT, BIGINT, DECIMAL, and FLOAT.

o Character data types: These are used to store character strings.


Examples include CHAR, VARCHAR, and TEXT.

o Date and time data types: These are used to store date and time
values. Examples include DATE, TIME, and TIMESTAMP.

o Binary data types: These are used to store binary data, such as
images or audio files. Examples include BLOB and BYTEA.

o Boolean data type: This data type is used to store logical values.
The only possible values are TRUE and FALSE.

Schema Change Statements in SQL: create, alter and drop commands are used for changing the
schema of the SQL

Page 20

You might also like