Database 2
Database 2
Database 2
•Relational Model
Relational Model
• The relational model represents how data is stored
in Relational Databases. A relational database
consists of a collection of tables, each of which is
assigned a unique name.
• The relational model for database management is an
approach to logically represent and manage the data
stored in a database. In this model, the data is
organized into a collection of two-dimensional
inter-related tables, also known as relations. Each
relation is a collection of columns and rows, where
the column represents the attributes of an entity and
the rows (or tuples) represents the records.
Basic Structure
• Given sets D1, D2, …. Dn a relation r is a subset of
D1 x D2 x … x Dn
Thus a relation is a set of n-tuples (a1, a2, …, an) where
each ai Di
• Ex: if customer-name = {Jones, Smith, Curry, Lindsay}
customer-street = {Main, North, North, Park}
customer-city = {Harrison, Rye, Ray, Pittsfield}
Then r = { (Jones, Main, Harrison),
(Smith, North, Rye),
(Curry, North, Rye),
(Lindsay, Park, Pittsfield)}
is a relation over customer-name x customer-street x
customer-city
Attributes
(or columns)
customer-name customer-street customer-city
Num. of super keys are more Number of primary keys are less
than number of primary key than number of super keys.
Char will pad the spaces to VarChar will not pad the
the right side to fill the spaces to the right side to fill
length specified during the the length specified during
Declaration Declaration.
It is not required to
It is required to specify the
specify the size at the
size at the time of
time of declaration. It will
declaration
take 1 Byte default
1 2 4
2 2 3
3 2 3
4 3 4
B C
2 4
2 3
3 4
• By Default, projection removes duplicate data.
Union Operation (∪)
• Suppose there are two tuples R and S. The union
operation contains all the tuples that are either in
R or S or both in R & S.
• A union operation must hold the following
condition:
• R and S must have the attribute of the same
number.
• Duplicate tuples are eliminated automatically.
Example
Table A Table B
column 1 column 2 column 1 column 2
1 1 1 1
1 2 1 3
A ∪ B gives
Table A ∪ B
column 1 column 2
1 1
1 2
1 3
Intersection (∩)
• An intersection is defined as all tuples (rows) that
are present in both of two union-compatible (same
columns and same type) relation A and B.
• The following definition are also equivalent.
• all elements of A that also belong to B
• all elements of B that also belong to A
Example
Table A Table B
column 1 column 2 column 1 column 2
1 1 1 1
1 2 1 3
A ∪ B gives
Table A ∩ B
column 1 column 2
1 1
Rename Operation rho (ρ)
• The rename operation is used to rename the output
relation. It is denoted by rho (ρ).
• In relational algebra, a rename is a unary
operation written as ρ (a/b)R where:
• R is a relation, a and b are attribute names of R
Employee ρEmployeeName/Name(Employee)
Table A Table B
column 1 column 2 column 1 column 2
1 1 1 1
1 2 1 3
A ∪ B gives
Table A - B
column 1 column 2
1 2
Structured Query Language (SQL)
• SQL was the first commercial language introduced for E.F
Codd's Relational model of database.
• Structured Query Language is a standard Database
language which is used to create, maintain and retrieve the
relational database
• SQL is case insensitive. But it is a recommended practice
to use keywords (like SELECT, UPDATE, CREATE, etc)
in capital letters and use user defined things (liked table
name, column name, etc) in small letters.
• SQL is the programming language for relational databases
like MySQL, Oracle, Sybase, SQL Server, Postgre, etc.
Other non-relational databases (also called NoSQL)
databases like MongoDB, DynamoDB, etc do not use SQL
Data Definition Language(DDL)
• DDL or Data Definition Language actually
consists of the SQL commands that can be used to
define the database schema. It simply deals with
descriptions of the database schema and is used to
create and modify the structure of database objects
in the database. DDL is a set of SQL commands
used to create, modify, and delete database
structures but not data. These commands are
normally not used by a general user, who should
be accessing the database via an application.
• Following are the five DDL
commands in SQL:
• CREATE Command
• DROP Command
• ALTER Command
• TRUNCATE Command
• RENAME Command
• COMMENT Command
CREATE Command
• CREATE is a DDL command used to create
databases, tables, triggers and other database
objects.
• Syntax to Create a Database:
• CREATE Database Database_Name;
• Suppose, you want to create a Books database in
the SQL database. To do this, you have to write
the following DDL Command:
• Create Database Books;
• Example describes how to create a new table using
the CREATE DDL command.
• CREATE TABLE table_name (column_Name1 data_ty
pe (size of the column ) ,column_Name2 data_type(size
of the column), column_NameN data_type(sizeof the col
umn)) ;
• Suppose, you want to create a Student table with five
columns in the SQL database. DDL command:
• CREATE TABLE Student (Roll_No. Int ,First_Name V
archar(20),Last_Name Varchar(20),Age Int,Marks Int;