Database
Database
1 Database is a collection of related information that is organized in such a way that supports for easy
access, modify and maintain data.
2 The database is managed by some special software packages known as Database Management Systems
(DBMS)
DBMSs are specially designed applications to create connection between user and program, and to store
data in an organized manner.
The purpose of DBMSs software is to allow the user to create, modify and administration of database.
3 Examples of database management systems are: Ms-Access, MySQL, PostgreSQL, SQLite, Microsoft SQL
Server, Oracle, SAP, dBASE, FoxPro, etc.
4 Relational data model
In the relational data model, database is represented as collection of related tables.
Each table is termed as relation
Tables are formed by using rows and columns.
A row (horizontal subset) of a table represents a tuple or record,
A column (vertical subset) of a table represents an attribute.
5 Relation
In database, a relation means a 'table', in which data are organized in the form of rows and columns.
Therefore in database, relations are equivalent to tables.
6 A domain is the original sets of atomic values used to model data.
In data base management and database, a domain refers to all the possible unique values of a particular
column.
For example:
i) The domain of gender column has a set of two possible values i.e, Male or Female.
ii) The domain of marital status has a set of four possible values i.e, Married, Unmarried, Widows
and Divorced.
Therefore, a domain is a set of acceptable values of a particular column, which is based on various
properties and data types
7 Tuple
Horizontal subset/information in a table is called tuple. The tuple is also known as a 'record', row which
gives particular information of the relation (table).
For example: i) In customer table, one row gives information about one customer only.
ii) In student table, one row gives information about one student only.
8 The degree is the number of attributes (columns) in a table.
9 Cardinality is number of rows (tuples) in a table.
1 Keys are an important part of a relational database and a vital part of the structure of a table. They help
0 enforce integrity and help identify the relationship between tables.
There are three main types of keys - candidate keys, primary keys, foreign keys and alternate keys.
1 Primary Key: A column or set of columns that uniquely identifies a row within a table is called primary
1 key.
1 Candidate Key: Candidate keys are set of fields (columns with unique values) in the relation that are
2 eligible to act as a primary key.
1 Alternate Key: Out of the candidate keys, after selecting a key as primary key, the remaining keys are
3 called alternate key.
1 Foreign Key: A foreign key is a field (or collection of fields) in one table that uniquely identifies a row of
4 another table. In other words, a foreign key is a column or a combination of columns that is used to
establish a link between two tables.
Explain the use of ‘Foreign Key’ in a Relational Database Management System. Give example to support your answer.
Ans: A foreign key is used to set or represent a relationship between two relations ( or tables) in a database. Its value is derived from
the primary key attribute of another relation.
For example: In the tables TRAINER and COURSE given below,
TID is primary key in TRAINER table but foreign key in COURSE table.
TRAINER
TID TNAME CITY HIREDATE SALARY
101 Ritu Nagpur 1998-10-15 56700
102 Navin Goa 1994-12-24 80000
103 Murugan Chandigarh 2001-12-21 82000
104 Jyothi Guwahati 2002-12-25 68000
105 Chanu Mumbai 1996-01-12 95000
106 Arbaaz Delhi 2001-12-12 69000
COURSE
1|Page
Database Concepts and SQL
2|Page
Database Concepts and SQL
2 CHAR: CHAR should be used for storing fixed length character strings.
3 String values will be space/blank padded (The adding of meaningless data [usually blanks] to a unit
of data to bring it up to some fixed size) before they are stored on the disk.
If this type is used to store variable length strings, it will waste a lot of disk space (always allocate
fixed memory) .
If we declare data type as CHAR, then it will occupy space for NULL values.
Format: CHAR(n)
Fixed-length character string having maximum length n.
2 VARCHAR: Varchar is a variable character stringFormat: VARCHAR (n)
4 Variable-length character string having maximum length n.
Format: VARCHAR2 (n)
Example:
CHAR(10) has fixed length, right padded with spaces.
VARCHAR(10) has fixed length, right padded with NULL
Name char (10): Suppose if we store Name is as "Ravi", then first four places of the ten characters
are filled with Ravi and the remaining 6 spaces are also allocated to Name. Thus, the size of name is
always ten.
Name varchar (10): Suppose if we store Name is as "Ravi", then first four places of the ten
characters are filled with Ravi and the remaining 6 spaces are filled with NULL.
.
2 Numeric data types are mainly used to store number with or without fraction part.
5 The numeric data types are:
2 1. NUMBER
6 2. DECIMAL
3. NUMERIC
4. INT
5. FLOAT
NUMBER: The Number data type stores fixed and floating-point numbers. The Number data type is
used to store integers (negative, positive, floating) of up to 38 digits of precision.
The following numbers can be stored in a Number data type column:
Positive numbers
Negative numbers
Zero
Format:
NUMBER (p, s)
Where;
– 'p' is the precision or the total number of significant decimal digits, where the most significant digit
is
the left-most nonzero digit and the least significant digit is the right-most known digit.
– 's' is the scale or the number of digits from the decimal point to the least significant digit.
DECIMAL and NUMERIC: Decimal and numeric data types have fixed precision and scale.
Format:
DECIMAL[ (p[ , s] )] and NUMERIC[ (p[ , s] )]
Square brackets ([ ]) are option.
3|Page
Database Concepts and SQL
where;
– 'p' is the precision or the total number of significant decimal digits, where the most significant digit
is
the left-most nonzero digit and the least significant digit is the right-most known digit.
– 's' is the scale or the number of digits from the decimal point to the least significant digit.
INT/INTEGER: The int data type is the integer data type in SQL. This used to store integer number
(without any fraction part).
FLOAT: This data type is used to store number with fraction part(real numbers).
2 Q1 In a table in MYSQL database, an attribute A of datatype varchar(20)has the value “Keshav”.
7 The attribute B of datatype char(20) has value “Meenakshi”. How many characters are occupied by
attribute A and attribute B?
a. 20,6 b. 6,20 c. 9,6 d. 6,9
ans b 6,20
Q2In MYSQL database, if a table, Alpha has degree 5 and cardinality 3, and another table, Beta has
degree 3 and cardinality 5, what will be the degree and cardinality of the Cartesian product of Alpha
and Beta?
a. 5,3 b. 8,15 c. 3,5 d. 15,8
ans b 8,15
2 DATE
8 Date is used to store valid date values
The date format is: YYYY-MM-DD
Cartesian product
SQL joins are used to relate information in different tables.
It combines fields from two or more tables by comparing values of common columns (join
condition).
A join condition is a part of the SQL query that retrieves rows from two or more tables.
If join condition is ommitted or if it is invalid, then join operation will result in a Cartesian
product.
Cartesian product is a binary operation and is denoted by (x) Cartesian product returns a number
of rows equal to number of rows in the first table multiply by number of rows in the second
table. At the same time, number of columns equal to number of columns in the first table added
by number of columns in the second table.
4|Page
Database Concepts and SQL
5|Page