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

Relational Data Model

The document discusses key concepts in relational databases including tables, rows, columns, entities, attributes, records, database management systems, relationships, keys, structured query language and data types. Specifically, it defines the components of a database, different types of relationships between tables, keys such as primary keys and foreign keys, and common data types used in databases.

Uploaded by

Priya Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Relational Data Model

The document discusses key concepts in relational databases including tables, rows, columns, entities, attributes, records, database management systems, relationships, keys, structured query language and data types. Specifically, it defines the components of a database, different types of relationships between tables, keys such as primary keys and foreign keys, and common data types used in databases.

Uploaded by

Priya Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 108

Class XII #Computer Science(083)

Database Concept 2

# Relational Data
Model
Database:
• The database refers to as collection of related data that are
organized in such a manner that it can be easily accessed
and update.
Components of a Database:
• With the reference to the above illustration the carious
components of the database are explained below:
• Table: a table refers to the structure inside a database
which is formed using rows and columns. It allows the user
to retrieve the data as per the requirement.
• Row: the horizontal lines of a table are called columns.
• Column: the vertical lines of a table are called columns.
Components of a Database:
• Entity: Every table has name in a database and is referred to as
an entity whose features can be defined.
Here, the table name is given as Admission, Thus, it represents
an entity.
• Attributes/Fields: Every column in a table is specified with a
name which is referred to as an attributes. The attributes define
the features of an entity.
• Here, Admission is an entity whose features or attributes are
SL.No. Adm.No., Name and Class.
• Record/ Tuple: each row in a table consist of a number of fields
which represent a record where, each record contains the data
for one person as specified by the intersecting fields.
Database management system(DBMS):
• DBMS is an application package which stores the data in
an orderly manner so that the information can be accessed
easily.
Need for a database management
system
• Some reasons are listed below:
• It helps in creating a structured database with data being
properly stored in the form of tables.
• It helps to add new data easily.
• It helps to modify the exiting data.
• The unwanted record can easily be deleted.
• We can retrieve the data as per our requirement.
Advantages of database managements
system
• The advantages of database management system are as
mentioned below:
• It minimizes the duplication of data due to integrating and
sharing of data files.
• It also results in a considerable saving of storage space.
• The user can rely on the results.
• It also allows the user to make different queries depending
upon the situation.
• Any change made can easily be updated in the database.
RDBMS (Relational Database
Management System):
• A relational database is a type of database which uses a
relational structure, allowing us to identify data from make
queries across multiple tables at once and provides a
visual representation of the data in the form of a table like
spreadsheet.
Relationship
• A relationship may be defined as the process of
establishing the link between common fields in two or
more tables.
Some features seen in the tables in a
RDBMS, are
• Each column contains the same kind of data whereas, different
columns have different data.
• A row in a table represents a relationship and the tables are referred
to as relations.
• All rows of a relation are distinct.
Types of relationship
• One to one relationship
• One to many relationships
• Many to many relationships
One-to-One Relationship
• One to one relationship, a table is sub-divided with some
fields which behave as a sub-set of the main table. It is
done to establish a relation between the two tables and
also to isolate a part of the table for security reasons. This
relationship can be represented as:
• Here the first table employee stores the details of all the
employees and the other table salary store the details of
their salaries. These tables have a one-to-one relationship
as they are very much relative. It means that the salary
details of each and every employee are different with
respect to their common fields.
One-to-Many Relationship:
• It is the most commo0n type of relationship used in a
structured data base. In this relationship, a record in table
1 can have many matching records in table 2, but a record
in table 2 has only one matching record in table1.

• In this relationship, the entities stream and student share a


one-to-many relationship, i.e. a student can have only one
stream, but a stream can be given to one or more students.
Many-to-many relationship:
• In this relationship, multiple units of entity 1 can be related
to multiple units of entity 2. This relationship can be
represented as:
The commonly used terminologies in
relational data model:
• ATTRIBUTE: Characteristic or parameters for which data are to
be stored in a relation. Simply stated, the columns of a relation
are the attributes which are also referred as fields.
For example, GUID, GName, GPhone and GAddress are
attributes of relation GUARDIAN.
• TUPLE: Each row of data in a relation (table) is called a tuple.
• DOMAIN: It is a set of values from which an attribute can take a
value in each row. Usually, a data type is used to specify
domain for an attribute.
For example, in STUDENT relation, the attribute RollNumber
takes integer values and hence its domain is a set of integer
values. Similarly, the set of character strings constitutes the
domain of the attribute SName.
The commonly used terminologies in
relational data model:
• DEGREE: The number of attributes in a relation is called
the Degree of the relation.
For example, relation GUARDIAN with four attributes is a
relation of degree 4.
• CARDINALITY: The number of tuples in a relation is called
the Cardinality of the relation.
For example, the cardinality of relation GUARDIAN is 5 as
there are 5 tuples in the table.
Three Important Properties of a
Relation
• In relational data model, following three properties are observed
with respect to a relation which makes a relation different from
a data file or a simple table.
• Property 1: imposes following rules on an attribute of the
relation.
• Each attribute in a relation has a unique name.
• Sequence of attributes in a relation is immaterial(unimportant).
• Property 2: governs following rules on a tuple of a relation.
• Each tuple in a relation is distinct.
For example, data values in no two tuples of relation
ATTENDANCE can be identical for all the attributes. Thus, each
tuple of a relation must be uniquely identified by its contents.
Three Important Properties of a
Relation
• Sequence of tuples in a relation is immaterial. The tuples are not
considered to be ordered, even though they appear to be in tabular form.
• Property 3: imposes following rules on the state of a relation.
• All data values in an attribute must be from the same domain (same data
type).
• Each data value associated with an attribute must be atomic (cannot be
further divisible into meaningful subparts). For example, GPhone of relation
GUARDIAN has ten digit numbers which is indivisible.
• No attribute can have many data values in one tuple. For example,
Guardian cannot specify multiple contact numbers under GPhone attribute.
• A special value “NULL” is used to represent values that are unknown or
non-applicable to certain attributes. For example, if a guardian does not
share his or her contact number with the school authorities, then GPhone
is set to NULL (data unknown).
Keys in a Relational Database:
• Primary key: the primary
key is an attribute which
uniquely identifies each
and every record of a table
in relational database.
Candidate key:
• when more than one
attributes are available
in the relation which
also possess the
unique identification
property. These keys
are known as the
Candidate keys.
Alternate key:
• if one key of the
candidate keys can
identify each row of the
table uniquely. In this
situation, one of them is
identified as primary key
and the rest of them are
known as the Alternate
keys.
Composite Primary Key:
• If no single attribute in a
relation is able to
uniquely distinguish the
tuples, then more than
one attributes are taken
together as primary key.
Such primary key
consisting of more than
one attribute is called
Composite Primary key.
Foreign key:
• The foreign key is a field in the parent table
which is referred to as the primary key in
another table (child table).
• Example:
• The table in which the foreign key is
defined, is referred to as the Child table or
Detail Table
• the table defining the Primary key is known
as the Parent Table or Master Table.
• It is important to know that the primary key
of the master table acts as a foreign key for
the detail table.
SQL:
• Structured Query
Language (SQL) is a
database management
system which is used to
create, maintain and
retrieve the data from the
relational databases like
MySQL, Oracle, SQL
server, PostGre, etc.
important points to be kept in mind
while using SQL:
• SQL is case insensitive.
For example, the column names ‘salary’ and
‘SALARY’ are the same for SQL.
• Always end SQL statements with a semicolon (;).
• To enter multiline SQL statements, we don’t write “;”
after the first line. We press the Enter key to
continue on the next line. The prompt mysql>
then changes to “->”, indicating that statement
is continued to the next line. After the last
line, put “;” and press enter.
Data type of Attribute
• Data type of an attribute indicates the type of data
value that an attribute can have. It also decides the
operations that can be performed on the data of that
attribute. For example, arithmetic operations can be
performed on numeric data but not on character data.
• MySQL uses the following data types , which are
categorized as under:
• Numeric data type
• String data type
• Date and Time data type
Numeric data type
You can specify a
Storage Minimum Maximum Minimum Value Maximum Value
Type width of up to
(Bytes) Value Signed Value Signed Unsigned Unsigned
digits.
TINYINT 1 -128 127 255 4
0
SMALLINT 2 -32768 32767 65535 5
0
MEDIUMINT 3 -8388608 8388607 16777215 9
0
INT 4 -2147483648 2147483647 4294967295 11
0
BIGINT 8 -263 263-1 264-1 20
0
FLOAT(M,D) 24

DOUBLE(M,D) 53

DECIMAL(M,D) 65
String data type
Minimum
Type Value Maximum Value

CHAR(M) 1 255
VARCHAR(M) 1 255
TINYTEXT or TINYBLOB 1 255
BLOB or TEXT 1 65535
MEDIUMTEXT 1 16777215
LONGTEXT 1 4294967295
ENUM Example : ENUM(‘P’,’Q’,’R’)
Date and Time data type
Data Type “Zero” Value SYNTAX RANGE
YYYY-MM-DD 1000-01-01 TO
DATE '0000-00-00' 9999-12-31
TIME '00:00:00' HH:MM:SS
YYYY-MM-DD HH:MM:SS 1000-01-01
00:00:00 TO
9999-12-31
DATETIME '0000-00-00 00:00:00' 23:59:59
TIMESTAMP '0000-00-00 00:00:00' YYYYMMDDHHMMSS
YEAR 0000 YYY 1901 TO 2155
Constraints:
Constraints are the certain types of restrictions on the
data values that an attribute can have. Table 9.2 lists
some of the commonly used constraints in SQL. They are
used to ensure correctness of data. However, it is not
mandatory to define constraints for each attribute of a
table.
Constraints Table:
COMMENTS IN MySQL
• Comments are non-executable statements. It enables the
user to understand the programming logic.
• Thus comments are used for the following purpose:
• To make the code more readable.
• To ensure the correct execution of code while testing
• MySQL server supports two different comment styles.
• These are-
• Using # or -- symbol
• Using /*……….*/
SQL COMMANDS
• SQL commands are mainly classified into four categories.
They are:
• DDL : Data Definition Language
• DQL: Data Query Language
• DML: Data Manipulation Language
• DCL: Data Control Language
SQL COMMANDS
It contains the most commonly used set of SQL commands to
define the database schema . It is used to create and modify
the structure of the objects in a database . Some of the DDL
commands along with their purposes are explained as under .
Create : to create the database or its objects ( like table , index ,
function , views , etc. )
Alter : to make any change in the structure of the database
Drop : to delete objects from the database
Rename : to rename an existing object in the database
Truncate : to remove all records from a table , including all the
spaces allocated for the records
Comment : to add comments to the database
The purpose of the DQL command is to get some
schema relation based on the queries passed on
to the databases . One of the most commonly
used command is mentioned below :
•Select : to retrieve from the a database
The SQL commands that deal with the manipulation of data
present in the database belong to Data Manipulation Language
. Some of the commonly used commands along with their
purposes are given below :
Insert : to insert new data into an existing table
• Update : to update existing data within a table
• Delete : to delete records from a database table
• Merge : to make changes in one table based on the values
matched from another table . It combines insert , update and
delete operations .
They are used for performing queries on the data within
schema objects .
It includes commands such as GRANT and REVOKE
which mainly deals with the rights , permissions and
other controls of the database system .
For example :
Grant : gives user's access privilege to a database .
Revoke : withdraws user's access privileges given by
using the GRANT command . •
a NULL value means an unknown value . Generally , we use the NULL value to
indicate that the data is missing , unknown or not applicable . A NULL value in
the field of be blank . It is important to know that a NULL value in a table is a
value that appears different than a zero value or a field that contains spaces .
When you create a table , you can specify whether a column accepts Null
values or not by using the NOT NULL constraint .

An example is illustrated below :

.
Steps
1. Create database.
2. Use database
3. Create table
4. Show tables structure ( describe) and no. of
tables in databse.
5. Enter data into table
6. Display data of table.
To create a database, we
use the CREATE DATABASE
statement as shown in the
following syntax: CREATE
DATABASE databasename;
To create a database called
StudentAttendance, we
will type following
command at mysql
prompt.
In order to use the StudentAttendance database,
the following SQL statement is required.
Initially, the created database is empty. It can be
checked by using the show tables statement that
lists names of all the tables within a database.
CREATE TABLE tablename(
attributename1 datatype constraint,
attributename2 datatype constraint,
:
attributenameN datatype constraint);
Syntax: DESCRIBE tablename;
SHOW TABLES statement to see the tables in the
StudentAttendance database.
After creating a table, we may realize that we
need to add/remove an attribute or to modify
the datatype of an existing attribute or to add
constraint in attribute.
Syntax
ALTER TABLE table_name ADD attribute CONSTRAINT data_type;
# TO ADD CONSTRAINT
ALTER TABLE student ADD GUID UNIQUE;
# TO INSERT NEW attribute
ALTER TABLE student ADD class INT(2);
#Add foreign key to a relation
ALTER TABLE table_name ADD FOREIGN KEY(attribute name)
REFERENCES referenced_table_name (attribute name);
ALTER TABLE STUDENT ADD FOREIGN KEY(GUID) REFERENCES
GUARDIAN(GUID);
#Add constraint UNIQUE to an existing attribute
Syntax:
ALTER TABLE table_name ADD UNIQUE (attribute name);
Example :
mysql> ALTER TABLE guardian ADD UNIQUE(GPhone);
Modify datatype of an attribute
• Syntax:
• ALTER TABLE table_name MODIFY attribute DATATYPE;
Example
• mysql> ALTER TABLE GUARDIAN MODIFY GAddress
VARCHAR(40);
Modify constraint of an attribute

•Syntax:
• ALTER TABLE table_name MODIFY attribute
DATATYPE NOT NULL;
•Example
•mysql> ALTER TABLE STUDENT MODIFY SName
VARCHAR(20) NOT NULL;
Add default value to an attribute
• ALTER TABLE table_name MODIFY attribute DATATYPE
DEFAULT default_value;
• Example
Example : To set default value of SDateofBirth of STUDENT to
15th May 2000, write the following statement:
• mysql> ALTER TABLE STUDENT MODIFY SDateofBirth DATE
DEFAULT ‘2000-05- 15’;
Remove an attribute
• Using ALTER, we can remove attributes from a table, as
shown in the following
• syntax: ALTER TABLE table_name DROP attribute;
• Example
• mysql> ALTER TABLE GUARDIAN DROP income;
Remove primary key from the table
• Syntax: ALTER TABLE table_name DROP PRIMARY KEY;
• Example : To remove primary key of table GUARDIAN
• mysql> ALTER TABLE GUARDIAN DROP PRIMARY KEY;
DROP Statement
• Sometimes a table in a database or the database itself
needs to be removed. We can use a DROP statement
to remove a database or a table permanently from the
system.
• However, one should be very cautious while using this
statement as it cannot be undone.
• Syntax to drop a table:
• DROP TABLE table_name;
• Syntax to drop a database:
• DROP DATABASE database_name;
• Note: Using the DROP statement to remove a database
will ultimately remove all the tables within it.
INSERTION of Records
• INSERT INTO statement is used to insert new records in
a table.
• Its syntax is:
INSERT INTO tablename VALUES(value 1, value 2,....);
• If we want to insert values only for some of the
attributes in a table (supposing other attributes having
NULL or any other default value), then we shall specify
the attribute names in which the values are to be
inserted using the following syntax of INSERT INTO
statement.
Example

To insert the fourth record of Table where GPhone is not given, we need to
insert values in the other three fields (GPhone was set to NULL by default at
the time of table creation).
In this case, we have to specify the names of attributes in which we want to
insert values. The values must be given in the same order in which
attributes are written in INSERT statement.
• Let us now insert the third record of Table 9.7 where GUID
is NULL. Recall that GUID is foreign key of this table and
thus can take NULL value. Hence, we can put NULL value
for GUID and insert the record by using the following
statement
SELECT Statement
The SQL statement SELECT is used to retrieve data from
the tables in a database and the output is also displayed
in tabular form.
Example
QUERYING using Database OFFICE
• Organisations maintain databases to store data in
the form of tables. Let us consider the database
OFFICE of an organisation that has many related
tables like EMPLOYEE, DEPARTMENT and so on.
Every EMPLOYEE in the database is assigned to a
DEPARTMENT and his/her Department number
(DeptId) is stored as a foreign key in the table
EMPLOYEE. Let us consider the relation ‘EMPLOYEE’
as shown in Table 9.8 and apply the SELECT
statement to retrieve data:
(A) Retrieve selected columns
• The following query selects employee numbers of all
the employees
The following query selects the employee
number and employee name of all the
employees, we write:
(B) Renaming of columns
• In case we want to rename any column while
displaying the output, it can be done by using the alias
'AS'.
Select names of all employees along with their annual
income (calculated as Salary*12). While displaying the
query result, rename the column EName as Name
Select names of all employees along with their annual
income (calculated as Salary*12). While displaying the
query result, rename the column EName as Name
(C) Distinct Clause
(C) Distinct Clause
• By default, SQL shows all the data retrieved through
query as output. However, there can be duplicate
values. The SELECT statement when combined with
DISTINCT clause, returns records without repetition
(distinct records).
• For example, while retrieving a department number
from employee relation, there can be duplicate values
as many employees are assigned to the same
department. To select unique department number for
all the employees, we use DISTINCT as shown below:
WHERE Clause
• The WHERE clause is used to retrieve data that meet
some specified conditions.
• In the OFFICE database, more than one employee can
have the same salary. Following query gives distinct
salaries of the employees working in the department
number D01:
• In the above example, = operator is used in the WHERE
clause. Other relational operators (<=, >, >=, !=) can be
used to specify such conditions. The logical operators
AND, OR, and NOT are used to combine multiple
conditions.
Q. Display all the details of those employees of
D04 department who earn more than 5000.
(E) Membership operator IN
• The IN operator compares a value with a set of values
and returns true if the value belongs to that set. The
above query can be rewritten using IN operator as
shown below:
(F) ORDER BY Clause
• ORDER BY clause is used to display data in an ordered
form with respect to a specified column.
• By default, ORDER BY displays records in ascending
order of the specified column’s values.
• To display the records in descending order, the DESC
(means descending) keyword needs to be written with
that column.
Example :
Example 9.9 The following query selects details of all
the employees in ascending order of their salaries.
(G) Handling NULL Values
• SQL supports a special value called NULL to represent a
missing or unknown value.
• For example, the Gphone column in the GUARDIAN table
can have missing value for certain records. Hence, NULL is
used to represent such unknown values. It is important to
note that NULL is different from 0 (zero).
• Also, any arithmetic operation performed with NULL value
gives NULL.
• For example: 5 + NULL = NULL because NULL is unknown
hence the result is also unknown.
• In order to check for NULL value in a column, we use IS
NULL operator.
(H) Substring pattern matching
• Many a times we come across situations where we do not
want to query by matching exact text or value. Rather, we
are interested to find matching of only a few characters or
values in column values.
• For example, to find out names starting with “T” or to find
out pin codes starting with ‘60’. This is called substring
pattern matching.
• We cannot match such patterns using = operator as we are
not looking for an exact match.
• SQL provides a LIKE operator that can be used with the
WHERE clause to search for a specified pattern in a column
(H) Substring pattern matching
• The LIKE operator makes use of the following two wild
card characters:
• • % (per cent)- used to represent zero, one, or multiple
characters
• • _ (underscore)- used to represent exactly a single
character

You might also like