Relational Data Model
Relational Data Model
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.
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 .
.
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