Chapter 1 Creating Relational Database
Chapter 1 Creating Relational Database
Creating Relational
Database
RELATIONAL DATABASE DESIGN
Concept of relational database design:
Edgar Codd introduced the Relational Model as
general model of data.
Definition: A relational database is collectively
combination of data structure, storage and
retrieval operations and integrity constraints.
RDBMS is the basis for SQL, and for all modern
database systems like MS SQL Server, IBM DB2,
Oracle, MySQL and Microsoft Access.
COMPONENTS OF RELATIONAL
DATABASE:
Data structure-
set of relations and set of domains that define the
way data can be represented.
Data integrity-
integrity rules that define the procedure to protect
the data.
Data manipulation-
the operations that can be performed on the data.
RELATIONAL DATABASE CHARACTERISTICS:
Data in the relational database must be represented
in tables, with values in columns within rows.
Data within a column must be accessible by specifying
the table name, the column name, and the value of the
primary key of the row.
The DBMS must support an active on-line catalogue.
The DBMS must support at least one language that
can be used independently and from within programs,
and supports data definition operations, data
manipulation, constraints, and transaction
management.
Views must be updatable by the system.
The DBMS must support insert, update, and delete
operations on sets.
RELATIONAL DATABASE CHARACTERISTICS:
The DBMS must support logical data independence.
The DBMS must support physical data independence.
MySQL,
MS Access,
Oracle,
Sybase,
Informix,
Union (U)
Intersection
Rename (ρ)
UNARY OPERATIONS
Syntax: A ∪ B
where A and B are relations.
EXAMPLE OF UNION
INTERSECTION OPERATION
Normally 3 types
Left outer Join( ):Returns all values of left table
Right outer join(⟖):Returns all values of right table
Full outer join(⟗):Returns all values of both table. If
no match found it returns NULL
RDBMS TERMINOLOGY:
RDBMS TERMINOLOGY:
Relation(Table): a relation is a collection of data
elements organized in terms of rows and columns.
Attribute(Field): A table consists of several
records(row), each record can be broken down into
several smaller parts of data known
as Attributes. Column are also called as attribute.
Domain: Every attribute has some predefined value
scope, known as Attribute Domain.
When an attribute is defined in a relation(table), it is
defined to hold only a certain type of values, which is
known as Attribute Domain.
Tuple (Record): A single entry in a table is called a
Tuple or Record or Row. A tuple in a table represents
a set of related data.
CARDINALITY:
cardinality refers to the relationship between two
tables. Relationship can be of four types
One to One
One to Many
Many to One
Many to Many
DIFFERENT TYPES OF MAPPING
CARDINALITY
Quaternary relationships:-
when four entities are associated to form a relation,
then it is known as a quaternary relationship.
INTRODUCTION TO SQL
SQL is Structured Query Language, which is a
computer language for storing, manipulating and
retrieving data stored in a relational database.
Characteristics of SQL
SQL is easy to learn.
SQL is used to access data from relational database
management systems.
SQL can execute queries against the database.
SQL is used to describe the data.
SQL is used to define the data in the database and
manipulate it when needed.
SQL is used to create and drop the database and table.
SQL is used to create a view, stored procedure, function in
a database.
SQL allows users to set permissions on tables, procedures,
and views.
ADVANTAGES OF SQL:
High speed: Using the SQL queries, the user can
quickly and efficiently retrieve a large amount of records
from a database.
No coding needed: In the standard SQL, it is very easy
to manage the database system. It doesn't require a
substantial amount of code to manage the database
system.
Well defined standards: Long established are used by
the SQL databases that are being used by ISO and ANSI.
Portability: SQL can be used in laptop, PCs, server and
even some mobile phones.
Interactive language: SQL is a domain language used
to communicate with the database. It is also used to
receive answers to the complex questions in seconds.
Multiple data view: Using the SQL language, the users
can make different views of the database structure.
DATATYPES IN SQL:
SQL Datatype is used to define the values that a
column can contain.
Every column is required to have a name and
data type in the database table.
DATATYPES IN SQL:
char:It has a maximum length of 8000 characters.
It contains Fixed-length non-unicode characters.
Varchar:It has a maximum length of 8000
characters. It contains variable-length non-unicode
characters.
text:It has a maximum length of 2,147,483,647
characters. It contains variable-length non-unicode
characters.
date:It is used to store the year, month, and days
value.
Time:It is used to store the hour, minute, and
second values.
DATATYPES IN SQL:
Int:It is used to specify an integer value.
Smallint:It is used to specify small integer
value.
Decimal:It specifies a numeric value that can
have a decimal number.
Numeric:It is used to specify a numeric value.
TRUNCATE command
TRUNCATE command removes all the records
from a table. But this command will not destroy
the table's structure. When we use TRUNCATE
command on a table its (auto-increment) primary
key is also initialized.
syntax:
TRUNCATE TABLE table_name;
Example :
TRUNCATE TABLE student;
The above query will delete all the records from the
table student.
D. DROP COMMAND
DROP command
DROP command completely removes a table from the
database. This command will also destroy the table
structure and the data stored in it.
syntax:
DROP TABLE table_name;
Example:
DROP TABLE student;
The above query will delete the Student table
completely.
It can also be used on Databases, to delete the
complete database.
For example, to drop a database,
DROP DATABASE Test;
The above query will drop the database with name
Test from the system
E. RENAME QUERY
RENAME query
RENAME command is used to set a new name
for any existing table.
syntax:
RENAME TABLE old_table_name to new_table_name;
Example:
RENAME TABLE student to students_info;
The above query will rename the table student to
students_info.
F. DESC QUERY:
DESC Query:
DESCRIBE or DESC(both are same) command
to describe the structure of a table.
Syntax:
DESCRIBE one;
OR
DESC one;
USE OF DATA CONSTRAINTS:
Relational constraints are a set of rules. It is used to
maintain the quality of information.
Integrity constraints ensure that the data insertion,
updating, and other processes have to be performed in
such a way that data integrity is not affected.
Thus, integrity constraint is used to guard against
accidental damage to the database.
Types of Relational integrity Constraints are as
follows
1. Domain constraints
2. Entity integrity constraints
3. Referential Integrity Constraints
4. Key Constraint
TYPES OF CONSTRAINTS:
Entity integrity constraints -it provides a way
of ensuring that changes made to the database by
authorized users do not result in a loss of data
consistency.
For example: Primary key, unique constraints
Referential Integrity Constraints – It
establishes parent child relationship between two
tables.
For example :Foreign key constraints
Domain Constraint - It is used to maintain
value according to user specification .
For example: Not null, check constraint.
FOLLOWING ARE SOME OF THE MOST
COMMONLY USED CONSTRAINTS AVAILABLE IN
SQL −
a)NOT NULL Constraint − Ensures that a column
cannot have a NULL value.
b)DEFAULT Constraint − Provides a default value
for a column when none is specified.
c)UNIQUE Constraint − Ensures that all the values
in a column are different.
d)PRIMARY Key − Uniquely identifies each
row/record in a database table.
e)FOREIGN Key − Uniquely identifies a row/record in
any another database table.
f)CHECK Constraint − The CHECK constraint
ensures that all values in a column satisfy certain
conditions.
g)INDEX − Used to create and retrieve data from the
database very quickly.
SQL PRIMARY KEY CONSTRAINT
Orders" table:
Uniquely
Used to serve as a
determines a row
Basic unique identifier for
which isn’t primary
each row in a table.
key.
Number of keys
Only one primary More than one
that can be defined
key unique key
in the table